( headerValue: string, )
| 34 | * Parse the X-Payment-Required header from a 402 response. |
| 35 | */ |
| 36 | export function parsePaymentRequirement( |
| 37 | headerValue: string, |
| 38 | ): PaymentRequirement { |
| 39 | try { |
| 40 | const parsed = JSON.parse(headerValue) as PaymentRequirement |
| 41 | if (!parsed.scheme || !parsed.network || !parsed.maxAmountRequired || !parsed.payTo) { |
| 42 | throw new Error('Missing required fields in payment requirement') |
| 43 | } |
| 44 | return parsed |
| 45 | } catch (error) { |
| 46 | throw new Error( |
| 47 | `Invalid x402 payment requirement header: ${error instanceof Error ? error.message : String(error)}`, |
| 48 | ) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Validate that a payment requirement is within configured limits. |
no test coverage detected