|
1 | 1 | import { |
2 | | - ChainIdToNetwork, |
3 | | - HTTPRequestStructureSchema, |
4 | | - PaymentRequirementsSchema, |
5 | | - x402ResponseSchema, |
6 | | -} from 'x402/types'; |
| 2 | + PaymentRequiredV1Schema, |
| 3 | + PaymentRequirementsV1Schema, |
| 4 | +} from '@x402/core/schemas'; |
7 | 5 | import { z as z3 } from 'zod3'; |
8 | 6 |
|
| 7 | +import { ChainIdToNetwork } from '../chain-mapping'; |
9 | 8 | import { FieldDefSchema } from '../shared'; |
10 | 9 |
|
| 10 | +// Inlined from the v1 `x402` SDK. v2 dropped the typed HTTP request structure |
| 11 | +// in favor of an opaque `inputSchema: Record<string, unknown>`, so there's no |
| 12 | +// equivalent in `@x402/*` to import. |
| 13 | +const HTTPRequestStructureSchema = z3.object({ |
| 14 | + type: z3.literal('http'), |
| 15 | + method: z3.enum(['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD']), |
| 16 | + queryParams: z3.record(z3.string(), z3.string()).optional(), |
| 17 | + bodyType: z3 |
| 18 | + .enum(['json', 'form-data', 'multipart-form-data', 'text', 'binary']) |
| 19 | + .optional(), |
| 20 | + bodyFields: z3.record(z3.string(), z3.any()).optional(), |
| 21 | + headerFields: z3.record(z3.string(), z3.any()).optional(), |
| 22 | +}); |
| 23 | + |
11 | 24 | export const outputSchemaV1 = z3.object({ |
12 | 25 | input: HTTPRequestStructureSchema.omit({ |
13 | 26 | queryParams: true, |
@@ -45,21 +58,19 @@ const networkSchemaV1 = z3.union([ |
45 | 58 | .transform(v => ChainIdToNetwork[Number(v.split(':')[1])]), |
46 | 59 | ]); |
47 | 60 |
|
48 | | -export const paymentRequirementsSchemaV1 = PaymentRequirementsSchema.extend({ |
| 61 | +export const paymentRequirementsSchemaV1 = PaymentRequirementsV1Schema.extend({ |
49 | 62 | network: networkSchemaV1, |
50 | 63 | outputSchema: outputSchemaV1.optional(), |
51 | 64 | }); |
52 | 65 |
|
53 | | -export const x402ResponseSchemaV1 = x402ResponseSchema |
54 | | - .omit({ |
55 | | - error: true, |
56 | | - accepts: true, |
57 | | - }) |
58 | | - .extend({ |
59 | | - x402Version: z3.literal(1).default(1), |
60 | | - error: z3.string().nullish(), |
61 | | - accepts: z3.array(paymentRequirementsSchemaV1).optional(), |
62 | | - }); |
| 66 | +export const x402ResponseSchemaV1 = PaymentRequiredV1Schema.omit({ |
| 67 | + error: true, |
| 68 | + accepts: true, |
| 69 | +}).extend({ |
| 70 | + x402Version: z3.literal(1).default(1), |
| 71 | + error: z3.string().nullish(), |
| 72 | + accepts: z3.array(paymentRequirementsSchemaV1).optional(), |
| 73 | +}); |
63 | 74 |
|
64 | 75 | export type X402ResponseV1 = z3.infer<typeof x402ResponseSchemaV1>; |
65 | 76 | export type OutputSchemaV1 = z3.infer<typeof outputSchemaV1>; |
0 commit comments