import {z} from 'zod' export type GetApiBuyDiffArgs = {url: string, prevHash?: string | undefined, prevText?: string | undefined, format?: ('text' | 'markdown') | undefined}; export type GetApiBuyDiffOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBuyDiffResponse = z.object({ok: z.boolean(), url: z.string(), status: z.number().int().optional(), changed: z.boolean().optional(), fromHash: z.string().optional(), toHash: z.string().optional(), comparedBy: z.enum(["hash", "text"]).optional(), diff: z.object({added: z.number().int().optional(), removed: z.number().int().optional(), hunks: z.array(z.string()).optional(), truncated: z.boolean().optional()}).optional(), text: z.string().optional(), bytes: z.number().int().optional(), contentType: z.string().optional(), fetchedAt: z.string().optional(), error: z.string().optional(), attestation: z.object({keyId: z.string().optional(), alg: z.string().optional(), address: z.string().optional(), contentHash: z.string().optional(), nonce: z.string().optional(), signedAt: z.string().optional(), signature: z.string().optional(), verify: z.object({message: z.string().optional(), howTo: z.string().optional()}).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 }; /** * MERCURY Cited Diff — signed web-change proof (refetch + verifiable fromHash->toHash receipt) * * GET /buy/diff */ export const getApiBuyDiff = async (args: GetApiBuyDiffArgs, options?: GetApiBuyDiffOptions) => { 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.url != null) query.append("url", String(args.url)) if (args.prevHash != null) query.append("prevHash", String(args.prevHash)) if (args.prevText != null) query.append("prevText", String(args.prevText)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/buy/diff${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(getApiBuyDiffResponse, await res.json()) };