import {z} from 'zod' export type GetApiV01PublicCodepushUpdateCheckArgs = {deployment_key: string, app_version: string, package_hash?: string | undefined, label?: string | undefined, client_unique_id?: string | undefined, is_companion?: boolean | undefined}; export type GetApiV01PublicCodepushUpdateCheckOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const updateCheckResponse = z.object({appVersion: z.string().optional(), description: z.string().optional(), isDisabled: z.boolean().optional(), isMandatory: z.boolean().optional(), rollout: z.number().int().gte(1).lte(100).optional(), label: z.string().optional(), isAvailable: z.boolean(), downloadURL: z.string().optional(), packageSize: z.number().int().optional(), shouldRunBinaryVersion: z.boolean().optional(), updateAppVersion: z.boolean().optional(), target_binary_range: z.string().optional()}); export const getApiV01PublicCodepushUpdateCheckResponse = z.object({update_info: updateCheckResponse.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 }; /** * Check for updates (v0.1 API) * * GET /v0.1/public/codepush/update_check */ export const getApiV01PublicCodepushUpdateCheck = async (args: GetApiV01PublicCodepushUpdateCheckArgs, options: GetApiV01PublicCodepushUpdateCheckOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.deployment_key != null) query.append("deployment_key", String(args.deployment_key)) if (args.app_version != null) query.append("app_version", String(args.app_version)) if (args.package_hash != null) query.append("package_hash", String(args.package_hash)) if (args.label != null) query.append("label", String(args.label)) if (args.client_unique_id != null) query.append("client_unique_id", String(args.client_unique_id)) if (args.is_companion != null) query.append("is_companion", String(args.is_companion)) const url = `${baseUrl.replace(/\/+$/, '')}/v0.1/public/codepush/update_check${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(getApiV01PublicCodepushUpdateCheckResponse, await res.json()) };