import {z} from 'zod' export type GetApiBuyBatchArgs = {urls: string, format?: ('text' | 'markdown') | undefined}; export type GetApiBuyBatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBuyBatchResponse = z.object({ok: z.boolean(), url: z.string(), status: z.number().int().optional(), text: z.string().optional(), fetchedAt: z.string().optional(), data: z.object({merkleRoot: z.string().optional(), leafCount: z.number().int().optional(), requested: z.number().int().optional(), okCount: z.number().int().optional(), failCount: z.number().int().optional(), leafTag: z.string().optional(), nodeTag: z.string().optional(), pages: z.array(z.object({index: z.number().int().optional(), url: z.string().optional(), ok: z.boolean().optional(), status: z.number().int().optional(), title: z.string().optional(), text: z.string().optional(), contentHash: z.string().optional(), leafHash: z.string().optional(), proof: z.array(z.object({side: z.enum(["left", "right"]).optional(), hash: z.string().optional()})).optional(), bytes: z.number().int().optional(), contentType: z.string().optional(), redirects: z.array(z.string()).optional(), error: z.string().optional()})).optional(), dropped: z.array(z.string()).optional()}).optional(), error: 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 }; /** * MERCURY Cited Batch — signed multi-page snapshot (Merkle root over every page's contentHash + per-page proofs) * * GET /buy/batch */ export const getApiBuyBatch = async (args: GetApiBuyBatchArgs, options?: GetApiBuyBatchOptions) => { const {baseUrl = "https://network.mercury-hq.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.urls != null) query.append("urls", String(args.urls)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/buy/batch${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(getApiBuyBatchResponse, await res.json()) };