import {z} from 'zod' export type DynamicQrUpdateRequest = {destination?: string | undefined, label?: string | undefined, status?: ('active' | 'paused') | undefined, rotateToken?: boolean | undefined}; export type PatchApiApiDynamicQrSlugArgs = {'If-Match': string, 'X-Dynamic-QR-Version'?: string | undefined, body: DynamicQrUpdateRequest}; export type PatchApiApiDynamicQrSlugOptions = {baseUrl?: string | undefined, DynamicQrBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dynamicQrRecord = z.object({slug: z.string(), destination: z.string(), label: z.string(), status: z.enum(["active", "paused"]), version: z.number().int().gte(1), createdAt: z.string(), updatedAt: z.string()}); export const dynamicQrAnalyticsSummary = z.object({available: z.boolean(), approximate: z.literal(true), retentionDays: z.literal(90), dailyCountLimit: z.literal(100000), totalScans: z.number().int().gte(0).optional(), daily: z.array(z.object({date: z.string(), count: z.number().int().gte(1).lte(100000)})).optional(), lastCountedAt: z.string().optional()}); export const dynamicQrUpdateResponse = z.object({success: z.boolean(), record: dynamicQrRecord, redirectUrl: z.string(), analytics: dynamicQrAnalyticsSummary.optional(), managementToken: z.string().optional(), managementUrl: z.string().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 }; /** * Update a dynamic QR record * * PATCH /api/dynamic-qr/{slug} */ export const patchApiApiDynamicQrSlug = async (args: PatchApiApiDynamicQrSlugArgs, options?: PatchApiApiDynamicQrSlugOptions) => { const {baseUrl = "https://qrcodecrafter.com", fetch: fetchFn = fetch, DynamicQrBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['If-Match'] != null) headers.set("If-Match", String(args['If-Match'])) if (args['X-Dynamic-QR-Version'] != null) headers.set("X-Dynamic-QR-Version", String(args['X-Dynamic-QR-Version'])) if (DynamicQrBearer !== undefined) headers.set('authorization', `Bearer ${DynamicQrBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/dynamic-qr/${encodeURIComponent(String(args.slug))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(dynamicQrUpdateResponse, await res.json()) };