import {z} from 'zod' export type GetApiX402PaymentInstructionsArgs = {limit?: number | undefined, pageToken?: string | undefined, cid?: string | undefined, name?: string | undefined, id?: string | undefined}; export type GetApiX402PaymentInstructionsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentRequirement = z.object({asset: z.string(), pay_to: z.string(), network: z.enum(["base", "base-sepolia", "eip155:8453", "eip155:84532"]), description: z.string().max(255).optional(), amount: z.string()}); export const paymentInstruction = z.object({id: z.string().optional(), version: z.number().int().optional(), payment_requirements: z.array(paymentRequirement).optional(), name: z.string().optional(), description: z.string().optional(), created_at: z.string().optional()}); export const getApiX402PaymentInstructionsResponse = z.object({data: z.object({payment_instructions: z.array(paymentInstruction).optional(), next_page_token: z.string().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 }; /** * List Payment Instructions * * GET /x402/payment_instructions */ export const getApiX402PaymentInstructions = async (args: GetApiX402PaymentInstructionsArgs, options?: GetApiX402PaymentInstructionsOptions) => { const {baseUrl = "https://api.pinata.cloud/v3", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (args.cid != null) query.append("cid", String(args.cid)) if (args.name != null) query.append("name", String(args.name)) if (args.id != null) query.append("id", String(args.id)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/x402/payment_instructions${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(getApiX402PaymentInstructionsResponse, await res.json()) };