import {z} from 'zod' export type VersionEventType = 'VERSION_CREATED' | 'VERSION_ACTIVATED' | 'VERSION_FAILED' | 'VERSION_DRAINING' | 'VERSION_RETIRED' | 'VERSION_ROLLED_BACK_FROM' | 'VERSION_ROLLED_BACK_TO' | 'VERSION_REDEPLOYED' | 'TASKS_MIGRATED' | 'VERSION_CLEANUP_FAILED'; export type GetApiBranchesBranchIdEventsArgs = {branch_id: string, event_type?: (VersionEventType | null) | undefined, after_sequence_id?: (number | null) | undefined, before_sequence_id?: (number | null) | undefined, limit?: number | undefined, descending?: boolean | undefined}; export type GetApiBranchesBranchIdEventsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const versionStatus = z.enum(["DEPLOYING", "ACTIVE", "FAILED", "UNHEALTHY", "DRAINING", "RETIRED", "ROLLED_BACK"]); export const versionEventTrigger = z.enum(["deploy", "container_register", "rollback", "hot_swap", "helm_failure", "redeploy", "cleanup", "manual"]); export const versionEventContent = z.object({actor_email: z.string().nullable().optional(), container_logs: z.string().nullable().optional(), container_logs_container_name: z.string().nullable().optional(), container_logs_fetched_at: z.string().nullable().optional(), container_logs_pod_name: z.string().nullable().optional(), container_logs_source: z.string().nullable().optional(), container_logs_truncated: z.boolean().nullable().optional(), container_logs_unavailable_reason: z.string().nullable().optional(), error_message: z.string().nullable().optional(), failure_reason: z.string().nullable().optional(), from_version_id: z.string().nullable().optional(), git_hash: z.string().nullable().optional(), migrated_task_count: z.number().int().nullable().optional(), migrated_task_ids: z.array(z.string()).nullable().optional(), new_status: versionStatus.nullable().optional(), previous_status: versionStatus.nullable().optional(), target_version_id: z.string().nullable().optional(), triggered_by: versionEventTrigger.nullable().optional()}); export const versionEventType = z.enum(["VERSION_CREATED", "VERSION_ACTIVATED", "VERSION_FAILED", "VERSION_DRAINING", "VERSION_RETIRED", "VERSION_ROLLED_BACK_FROM", "VERSION_ROLLED_BACK_TO", "VERSION_REDEPLOYED", "TASKS_MIGRATED", "VERSION_CLEANUP_FAILED"]); export const versionEventResponse = z.object({actor_id: z.string().nullable().optional(), branch_id: z.string(), content: versionEventContent.nullable().optional(), created_at: z.string(), event_type: versionEventType, id: z.string(), sequence_id: z.number().int(), version_id: z.string()}); export const versionEventListResponse = z.object({events: z.array(versionEventResponse), has_more: z.boolean(), total: z.number().int()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * List Branch Events * * GET /branches/{branch_id}/events */ export const getApiBranchesBranchIdEvents = async (args: GetApiBranchesBranchIdEventsArgs, options: GetApiBranchesBranchIdEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.event_type != null) query.append("event_type", String(args.event_type)) if (args.after_sequence_id != null) query.append("after_sequence_id", String(args.after_sequence_id)) if (args.before_sequence_id != null) query.append("before_sequence_id", String(args.before_sequence_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.descending != null) query.append("descending", String(args.descending)) const url = `${baseUrl.replace(/\/+$/, '')}/branches/${encodeURIComponent(String(args.branch_id))}/events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(versionEventListResponse, await res.json()) };