import {z} from 'zod' export type GetApiApiValidateArgs = {url: string}; export type GetApiApiValidateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const validationChecks = z.object({fetchable: z.boolean(), is_xml: z.boolean(), svg_root: z.boolean(), namespace_correct: z.boolean(), version_1_2: z.boolean(), base_profile_tiny_ps: z.boolean(), has_viewbox: z.boolean(), viewbox_square: z.boolean(), has_title: z.boolean(), no_scripts: z.boolean(), no_external_refs: z.boolean(), no_image_elements: z.boolean(), no_embedded_bitmaps: z.boolean(), no_animations: z.boolean(), no_foreign_object: z.boolean(), file_size_ok: z.boolean()}); export const validationReport = z.object({valid: z.boolean(), url: z.string(), file_size_bytes: z.number().int().gte(0).optional(), file_size_kb: z.string().optional(), errors: z.array(z.string()), warnings: z.array(z.string()), checks: validationChecks, meta: z.record(z.string(), z.unknown()), spec: z.string(), tool: 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 }; /** * Validate a public SVG URL for BIMI readiness * * GET /api/validate */ export const getApiApiValidate = async (args: GetApiApiValidateArgs, options?: GetApiApiValidateOptions) => { const {baseUrl = "https://makebimi.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.url != null) query.append("url", String(args.url)) const url = `${baseUrl.replace(/\/+$/, '')}/api/validate${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(validationReport, await res.json()) };