import {z} from 'zod' export type GetApiBuyNotarizeArgs = {url?: string | undefined, content?: string | undefined}; export type GetApiBuyNotarizeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBuyNotarizeResponse = z.object({ok: z.boolean(), url: z.string(), status: z.number().int().optional(), text: z.string().optional(), data: z.object({algorithm: z.string().optional(), contentHash: z.string().optional(), byteLength: z.number().int().optional(), source: z.string().optional(), sourceKind: z.enum(["url", "inline"]).optional(), witnessedAt: z.string().optional(), statementVersion: z.string().optional(), verify: z.object({howTo: z.string().optional(), wellKnown: z.string().optional()}).optional()}).optional(), fetchedAt: z.string().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 Notarize * * GET /buy/notarize */ export const getApiBuyNotarize = async (args: GetApiBuyNotarizeArgs, options?: GetApiBuyNotarizeOptions) => { 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.content != null) query.append("content", String(args.content)) const url = `${baseUrl.replace(/\/+$/, '')}/buy/notarize${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(getApiBuyNotarizeResponse, await res.json()) };