import {z} from 'zod' export type GetApiMobileUploadSecretKeyImagesArgs = {secret_key: string, qr_visible?: boolean | undefined}; export type GetApiMobileUploadSecretKeyImagesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const legacyImageDocResponse = z.object({urls: z.array(z.string()), uploaded_at: z.string()}); export const legacyImagesResponse = z.object({status: z.literal("ok").optional(), images: z.array(legacyImageDocResponse)}); 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 }; /** * Poll Mobile Images * * GET /mobile-upload/{secret_key}/images */ export const getApiMobileUploadSecretKeyImages = async (args: GetApiMobileUploadSecretKeyImagesArgs, options: GetApiMobileUploadSecretKeyImagesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.qr_visible != null) query.append("qr_visible", String(args.qr_visible)) const url = `${baseUrl.replace(/\/+$/, '')}/mobile-upload/${encodeURIComponent(String(args.secret_key))}/images${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(legacyImagesResponse, await res.json()) };