43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
export const DEPLOYMENT_STATUSES = ["deploying", "ready", "failed"] as const;
|
|
|
|
export type DeploymentStatus = (typeof DEPLOYMENT_STATUSES)[number];
|
|
|
|
export interface DeploymentStatusEvent {
|
|
deploymentId: string;
|
|
status: DeploymentStatus;
|
|
updatedAt: string;
|
|
}
|
|
|
|
const DEPLOYMENT_ID_PATTERN = /^[a-f0-9]{40}$/u;
|
|
|
|
export function isDeploymentId(value: unknown): value is string {
|
|
return typeof value === "string" && DEPLOYMENT_ID_PATTERN.test(value);
|
|
}
|
|
|
|
export function isDeploymentStatus(value: unknown): value is DeploymentStatus {
|
|
return DEPLOYMENT_STATUSES.some((status) => status === value);
|
|
}
|
|
|
|
export function parseDeploymentStatusEvent(
|
|
value: string,
|
|
): DeploymentStatusEvent | null {
|
|
try {
|
|
const parsed = JSON.parse(value) as Record<string, unknown>;
|
|
if (
|
|
!isDeploymentId(parsed.deploymentId) ||
|
|
!isDeploymentStatus(parsed.status) ||
|
|
typeof parsed.updatedAt !== "string" ||
|
|
!Number.isFinite(Date.parse(parsed.updatedAt))
|
|
) {
|
|
return null;
|
|
}
|
|
return {
|
|
deploymentId: parsed.deploymentId,
|
|
status: parsed.status,
|
|
updatedAt: parsed.updatedAt,
|
|
};
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|