import {z} from 'zod' export type Job = {type?: string | undefined, id?: string | undefined, attributes?: ({approvalTeam?: string | undefined, comments?: string | undefined, createdBy?: string | undefined, createdDate?: string | undefined, output?: string | undefined, status?: ('pending' | 'waitingApproval' | 'approved' | 'queue' | 'running' | 'completed' | 'rejected' | 'cancelled' | 'failed') | undefined, tcl?: string | undefined, templateReference?: string | undefined, terraformPlan?: string | undefined, updatedBy?: string | undefined, updatedDate?: string | undefined}) | undefined, relationships?: ({organization?: ({id?: string | undefined, type?: 'organization' | undefined}) | undefined, step?: ({id?: string | undefined, type?: 'step' | undefined}) | undefined, workspace?: ({id?: string | undefined, type?: 'workspace' | undefined}) | undefined}) | undefined}; export type CreateApiStepStepIdJobArgs = {body: {data?: Job | undefined}}; export type CreateApiStepStepIdJobOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const job = z.object({type: z.string().optional(), id: z.string().optional(), attributes: z.object({approvalTeam: z.string().optional(), comments: z.string().optional(), createdBy: z.string().optional(), createdDate: z.string().optional(), output: z.string().optional(), status: z.enum(["pending", "waitingApproval", "approved", "queue", "running", "completed", "rejected", "cancelled", "failed"]).optional(), tcl: z.string().optional(), templateReference: z.string().optional(), terraformPlan: z.string().optional(), updatedBy: z.string().optional(), updatedDate: z.string().optional()}).optional(), relationships: z.object({organization: z.object({id: z.string().optional(), type: z.literal("organization").optional()}).optional(), step: z.object({id: z.string().optional(), type: z.literal("step").optional()}).optional(), workspace: z.object({id: z.string().optional(), type: z.literal("workspace").optional()}).optional()}).optional()}); export const createApiStepStepIdJobResponse = z.object({data: job.optional()}); 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 }; /** * Creates an item of type job and adds it to job * * POST /step/{stepId}/job */ export const createApiStepStepIdJob = async (args: CreateApiStepStepIdJobArgs, options: CreateApiStepStepIdJobOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") const url = `${baseUrl.replace(/\/+$/, '')}/step/${encodeURIComponent(String(args.stepId))}/job${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiStepStepIdJobResponse, await res.json()) };