import {z} from 'zod' export type PaymentRequirement = {asset: string, pay_to: string, network: 'base' | 'base-sepolia' | 'eip155:8453' | 'eip155:84532', description?: string | undefined, amount: string}; export type PatchApiX402PaymentInstructionsIdArgs = {id: string, body: {name?: string | undefined, description?: string | undefined, payment_requirements?: Array | undefined}}; export type PatchApiX402PaymentInstructionsIdOptions = {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 patchApiX402PaymentInstructionsIdResponse = z.object({data: paymentInstruction.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 }; /** * Update Payment Instruction * * PATCH /x402/payment_instructions/{id} */ export const patchApiX402PaymentInstructionsId = async (args: PatchApiX402PaymentInstructionsIdArgs, options?: PatchApiX402PaymentInstructionsIdOptions) => { const {baseUrl = "https://api.pinata.cloud/v3", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/x402/payment_instructions/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(patchApiX402PaymentInstructionsIdResponse, await res.json()) };