import {z} from 'zod' export type GenerateVerifiedQrRequest = {value: string, format?: ('svg' | 'png' | 'jpg' | 'webp') | undefined, size?: number | undefined, margin?: number | undefined, fgColor?: string | undefined, bgColor?: string | undefined, errorCorrectionLevel?: ('L' | 'M' | 'Q' | 'H') | undefined, logo?: (string | null) | undefined}; export type CreateApiNetlifyFunctionsGenerateVerifiedQrArgs = {body: GenerateVerifiedQrRequest}; export type CreateApiNetlifyFunctionsGenerateVerifiedQrOptions = {baseUrl?: string | undefined, AgentApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const generatedAssetDimensions = z.object({width: z.number(), height: z.number(), unit: z.enum(["px", "pt"])}); export const qrSafePayloadReview = z.object({kind: z.enum(["url", "email", "phone", "sms", "wifi", "vcard", "event", "location", "payment", "text"]), level: z.enum(["clear", "caution", "blocked"]), displayHost: z.string().nullable(), protocol: z.enum(["http:", "https:"]).nullable(), warnings: z.array(z.string())}); export const qrProductionChecks = z.object({quietZone: z.object({margin: z.number().int(), recommendedMinimum: z.literal(4), passed: z.boolean()}), contrast: z.object({ratio: z.number(), workflowThreshold: z.literal(4.5), foregroundDarker: z.boolean(), passed: z.boolean()}), errorCorrection: z.object({level: z.enum(["L", "M", "Q", "H"]), logoPresent: z.boolean(), passed: z.boolean()})}); export const generateVerifiedQrResponse = z.object({success: z.boolean(), data: z.string().optional(), dataUrl: z.string().optional(), downloadUrl: z.string().optional(), contentType: z.string().optional(), extension: z.string().optional(), suggestedFilename: z.string().optional(), byteLength: z.number().int().optional(), dimensions: generatedAssetDimensions.optional(), qrOptions: z.object({size: z.number().int().optional(), margin: z.number().int().optional(), errorCorrectionLevel: z.enum(["L", "M", "Q", "H"]).optional()}).optional(), assetSha256: z.string(), verification: z.object({version: z.literal("1.0"), status: z.enum(["verified", "verified_with_cautions"]), productionReady: z.boolean(), verifiedAt: z.string(), payload: qrSafePayloadReview, decode: z.object({decoder: z.literal("jsQR 1.4.0"), status: z.enum(["exact_match", "normalized_match", "not_detected", "mismatch"]), requestedPayloadSha256: z.string(), decodedPayloadSha256: z.string().nullable().optional(), payloadHashMatch: z.boolean()}), productionChecks: qrProductionChecks, cautions: z.array(z.string()), evidence: z.object({readabilityLab: z.string(), methodology: z.string(), release: z.string()}), limitation: z.string()})}); 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 and verify a QR code asset * * POST /.netlify/functions/generate-verified-qr */ export const createApiNetlifyFunctionsGenerateVerifiedQr = async (args: CreateApiNetlifyFunctionsGenerateVerifiedQrArgs, options?: CreateApiNetlifyFunctionsGenerateVerifiedQrOptions) => { 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-verified-qr${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(generateVerifiedQrResponse, await res.json()) };