import {z} from 'zod' export type GetApiUpdateCheckArgs = {deploymentKey: string, appVersion: string, packageHash?: string | undefined, label?: string | undefined, clientUniqueId?: string | undefined, isCompanion?: boolean | undefined}; export type GetApiUpdateCheckOptions = {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 getApiUpdateCheckResponse = z.object({updateInfo: 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 * * GET /updateCheck */ export const getApiUpdateCheck = async (args: GetApiUpdateCheckArgs, options: GetApiUpdateCheckOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.deploymentKey != null) query.append("deploymentKey", String(args.deploymentKey)) if (args.appVersion != null) query.append("appVersion", String(args.appVersion)) if (args.packageHash != null) query.append("packageHash", String(args.packageHash)) if (args.label != null) query.append("label", String(args.label)) if (args.clientUniqueId != null) query.append("clientUniqueId", String(args.clientUniqueId)) if (args.isCompanion != null) query.append("isCompanion", String(args.isCompanion)) const url = `${baseUrl.replace(/\/+$/, '')}/updateCheck${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(getApiUpdateCheckResponse, await res.json()) };