import {z} from 'zod' export type GenerateBulkQrRow = {value: string, filename?: string | undefined, name?: string | undefined, label?: string | undefined}; export type GenerateBulkQrRequest = {rows: string | Array, format?: ('svg' | 'png' | 'jpg' | 'webp' | 'pdf' | 'eps') | undefined, size?: number | undefined, margin?: number | undefined, fgColor?: string | undefined, bgColor?: string | undefined, errorCorrectionLevel?: ('L' | 'M' | 'Q' | 'H') | undefined}; export type CreateApiNetlifyFunctionsGenerateQrBulkArgs = {body: GenerateBulkQrRequest}; export type CreateApiNetlifyFunctionsGenerateQrBulkOptions = {baseUrl?: string | undefined, AgentApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const bulkQrManifestRow = z.object({filename: z.string().optional(), value: z.string().optional(), format: z.enum(["svg", "png", "jpg", "webp", "pdf", "eps"]).optional(), status: z.enum(["generated", "failed"]).optional(), bytes: z.number().int().nullable().optional(), message: z.string().optional()}); export const generateBulkQrResponse = z.object({success: z.boolean().optional(), data: z.string().optional(), dataUrl: z.string().optional(), contentType: z.string().optional(), extension: z.string().optional(), suggestedFilename: z.string().optional(), byteLength: z.number().int().optional(), fileCount: z.number().int().optional(), failedCount: z.number().int().optional(), manifest: z.array(bulkQrManifestRow).optional(), qrOptions: z.object({format: z.enum(["svg", "png", "jpg", "webp", "pdf", "eps"]).optional(), size: z.number().int().optional(), margin: z.number().int().optional(), fgColor: z.string().optional(), bgColor: z.string().optional(), errorCorrectionLevel: z.enum(["L", "M", "Q", "H"]).optional()}).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 }; /** * Generate a ZIP archive of static QR code files * * POST /.netlify/functions/generate-qr-bulk */ export const createApiNetlifyFunctionsGenerateQrBulk = async (args: CreateApiNetlifyFunctionsGenerateQrBulkArgs, options?: CreateApiNetlifyFunctionsGenerateQrBulkOptions) => { const {baseUrl = "https://qrcodecrafter.com", fetch: fetchFn = fetch, AgentApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (AgentApiKey !== undefined) headers.set("X-Agent-Api-Key", AgentApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/.netlify/functions/generate-qr-bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(generateBulkQrResponse, await res.json()) };