import {z} from 'zod' export type GetApiApiV3TeamsTeamIdPreviewsArgs = {page_size?: number | undefined, page?: number | undefined, team_id: string}; export type GetApiApiV3TeamsTeamIdPreviewsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; 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 teamPreviewsSchemasAppPreviewSchema = z.object({id: z.string(), streaming_public_key: z.string(), app: applicationRef, build: previewBuild, artifact: previewArtifact, created_at: z.string(), expires_at: z.string(), deleted_at: z.string().nullable()}); export const appPreviewSchemaClassicPagination = z.object({data: z.array(teamPreviewsSchemasAppPreviewSchema), page_size: z.number().int(), current_page: z.number().int(), total_pages: 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 app previews for the team * * GET /api/v3/teams/{team_id}/previews */ export const getApiApiV3TeamsTeamIdPreviews = async (args: GetApiApiV3TeamsTeamIdPreviewsArgs, options?: GetApiApiV3TeamsTeamIdPreviewsOptions) => { const {baseUrl = "https://codemagic.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) headers.set("x-auth-token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/teams/${encodeURIComponent(String(args.team_id))}/previews${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(appPreviewSchemaClassicPagination, await res.json()) };