import {z} from 'zod' export type GetApiApiV3PreviewsPreviewIdArgs = {preview_id: string}; export type GetApiApiV3PreviewsPreviewIdOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const devicePlatform = z.enum(["android", "ios"]); export const applicationRef = z.object({id: z.string(), name: z.string(), icon_url: z.string().nullable()}); export const releaseNote = z.object({language: z.string(), text: z.string()}); export const previewBuild = z.object({id: z.string(), release_notes: z.array(releaseNote), labels: z.array(z.string())}); export const previewArtifact = z.object({name: z.string(), path: z.string(), version_code: z.string().nullable(), version_name: z.string().nullable()}); export const previewsSchemasAppPreviewSchema = z.object({id: z.string(), streaming_public_key: z.string(), platform: devicePlatform, app: applicationRef, build: previewBuild, artifact: previewArtifact, created_at: z.string(), expires_at: z.string(), deleted_at: z.string().nullable()}); export const appPreviewSchemaNoPagination = z.object({data: previewsSchemasAppPreviewSchema}); 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 }; /** * Get app preview information * * GET /api/v3/previews/{preview_id} */ export const getApiApiV3PreviewsPreviewId = async (args: GetApiApiV3PreviewsPreviewIdArgs, options?: GetApiApiV3PreviewsPreviewIdOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/previews/${encodeURIComponent(String(args.preview_id))}${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(appPreviewSchemaNoPagination, await res.json()) };