import {z} from 'zod' export type BuildInstanceType = 'mac_mini_m2' | 'mac_mini_m4' | 'mac_studio_m4_max' | 'linux_x2' | 'linux_x4' | 'windows_x2'; export type EnvironmentSchema = {variables?: ({[key: string]: string} | null) | undefined, groups?: (Array | null) | undefined, software_versions?: ({[key: string]: string} | null) | undefined}; export type StartBuildSchema = {workflow_id: string, branch?: (string | null) | undefined, tag?: (string | null) | undefined, ssh_access_enabled?: boolean | undefined, labels?: (Array | null) | undefined, instance_type?: (BuildInstanceType | null) | undefined, inputs?: ({[key: string]: boolean | number | number | string} | null) | undefined, environment?: (EnvironmentSchema | null) | undefined, priority?: number | undefined}; export type CreateApiApiV3AppsAppIdBuildsArgs = {app_id: string, body: StartBuildSchema}; export type CreateApiApiV3AppsAppIdBuildsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const startedBuildSchema = z.object({id: z.string()}); export const startedBuildSchemaNoPagination = z.object({data: startedBuildSchema}); 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 }; /** * Start a new build * * POST /api/v3/apps/{app_id}/builds */ export const createApiApiV3AppsAppIdBuilds = async (args: CreateApiApiV3AppsAppIdBuildsArgs, options?: CreateApiApiV3AppsAppIdBuildsOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/apps/${encodeURIComponent(String(args.app_id))}/builds${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(startedBuildSchemaNoPagination, await res.json()) };