@bankofai/x402-core 1.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +294 -0
- package/dist/cjs/client/index.d.ts +149 -0
- package/dist/cjs/client/index.js +909 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +206 -0
- package/dist/cjs/facilitator/index.js +431 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/http/index.d.ts +50 -0
- package/dist/cjs/http/index.js +1452 -0
- package/dist/cjs/http/index.js.map +1 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +31 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/schemas/index.d.ts +891 -0
- package/dist/cjs/schemas/index.js +216 -0
- package/dist/cjs/schemas/index.js.map +1 -0
- package/dist/cjs/server/index.d.ts +79 -0
- package/dist/cjs/server/index.js +2568 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/index.js +97 -0
- package/dist/cjs/types/index.js.map +1 -0
- package/dist/cjs/types/v1/index.d.ts +1 -0
- package/dist/cjs/types/v1/index.js +19 -0
- package/dist/cjs/types/v1/index.js.map +1 -0
- package/dist/cjs/utils/index.d.ts +77 -0
- package/dist/cjs/utils/index.js +179 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/x402Client-BgegfQgE.d.ts +1807 -0
- package/dist/cjs/x402Client-TQHctrG7.d.ts +1807 -0
- package/dist/esm/chunk-ABS7D6VX.mjs +145 -0
- package/dist/esm/chunk-ABS7D6VX.mjs.map +1 -0
- package/dist/esm/chunk-AGOUMC4P.mjs +68 -0
- package/dist/esm/chunk-AGOUMC4P.mjs.map +1 -0
- package/dist/esm/chunk-BJTO5JO5.mjs +11 -0
- package/dist/esm/chunk-BJTO5JO5.mjs.map +1 -0
- package/dist/esm/chunk-FPXAE3OS.mjs +161 -0
- package/dist/esm/chunk-FPXAE3OS.mjs.map +1 -0
- package/dist/esm/chunk-IL77TMJL.mjs +1275 -0
- package/dist/esm/chunk-IL77TMJL.mjs.map +1 -0
- package/dist/esm/chunk-VE37GDG2.mjs +7 -0
- package/dist/esm/chunk-VE37GDG2.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +149 -0
- package/dist/esm/client/index.mjs +504 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +206 -0
- package/dist/esm/facilitator/index.mjs +399 -0
- package/dist/esm/facilitator/index.mjs.map +1 -0
- package/dist/esm/http/index.d.mts +50 -0
- package/dist/esm/http/index.mjs +35 -0
- package/dist/esm/http/index.mjs.map +1 -0
- package/dist/esm/index.d.mts +3 -0
- package/dist/esm/index.mjs +8 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/schemas/index.d.mts +891 -0
- package/dist/esm/schemas/index.mjs +70 -0
- package/dist/esm/schemas/index.mjs.map +1 -0
- package/dist/esm/server/index.d.mts +79 -0
- package/dist/esm/server/index.mjs +1318 -0
- package/dist/esm/server/index.mjs.map +1 -0
- package/dist/esm/types/index.d.mts +1 -0
- package/dist/esm/types/index.mjs +14 -0
- package/dist/esm/types/index.mjs.map +1 -0
- package/dist/esm/types/v1/index.d.mts +1 -0
- package/dist/esm/types/v1/index.mjs +1 -0
- package/dist/esm/types/v1/index.mjs.map +1 -0
- package/dist/esm/utils/index.d.mts +77 -0
- package/dist/esm/utils/index.mjs +28 -0
- package/dist/esm/utils/index.mjs.map +1 -0
- package/dist/esm/x402Client-BgegfQgE.d.mts +1807 -0
- package/dist/esm/x402Client-TQHctrG7.d.mts +1807 -0
- package/package.json +142 -0
package/README.md
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# `@bankofai/x402-core` • [](https://www.npmjs.com/package/@bankofai/x402-core)
|
|
2
|
+
|
|
3
|
+
Core implementation of the x402 payment protocol for TypeScript/JavaScript applications. Provides transport-agnostic client, server and facilitator components.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm install @bankofai/x402-core
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
|
|
13
|
+
### Client Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { x402Client } from '@bankofai/x402-core/client';
|
|
17
|
+
import { x402HTTPClient } from '@bankofai/x402-core/http';
|
|
18
|
+
import { ExactEvmScheme } from '@bankofai/x402-evm/exact/client';
|
|
19
|
+
|
|
20
|
+
// Create core client and register payment schemes
|
|
21
|
+
const coreClient = new x402Client()
|
|
22
|
+
.register('eip155:*', new ExactEvmScheme(evmSigner));
|
|
23
|
+
|
|
24
|
+
// Wrap with HTTP client for header encoding/decoding
|
|
25
|
+
const client = new x402HTTPClient(coreClient);
|
|
26
|
+
|
|
27
|
+
// Make a request
|
|
28
|
+
const response = await fetch('https://api.example.com/protected');
|
|
29
|
+
|
|
30
|
+
if (response.status === 402) {
|
|
31
|
+
// Extract payment requirements from response
|
|
32
|
+
const paymentRequired = client.getPaymentRequiredResponse(
|
|
33
|
+
(name) => response.headers.get(name),
|
|
34
|
+
await response.json()
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Create and send payment
|
|
38
|
+
const paymentPayload = await client.createPaymentPayload(paymentRequired);
|
|
39
|
+
|
|
40
|
+
const paidResponse = await fetch('https://api.example.com/protected', {
|
|
41
|
+
headers: client.encodePaymentSignatureHeader(paymentPayload),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// Get settlement confirmation
|
|
45
|
+
const settlement = client.getPaymentSettleResponse(
|
|
46
|
+
(name) => paidResponse.headers.get(name)
|
|
47
|
+
);
|
|
48
|
+
console.log('Transaction:', settlement.transaction);
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Server Usage
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { x402ResourceServer, HTTPFacilitatorClient } from '@bankofai/x402-core/server';
|
|
56
|
+
import { x402HTTPResourceServer } from '@bankofai/x402-core/http';
|
|
57
|
+
import { ExactEvmScheme } from '@bankofai/x402-evm/exact/server';
|
|
58
|
+
|
|
59
|
+
// Connect to facilitator
|
|
60
|
+
const facilitatorClient = new HTTPFacilitatorClient({
|
|
61
|
+
url: 'https://x402.org/facilitator',
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// Create resource server with payment schemes
|
|
65
|
+
const resourceServer = new x402ResourceServer(facilitatorClient)
|
|
66
|
+
.register('eip155:*', new ExactEvmScheme());
|
|
67
|
+
|
|
68
|
+
// Initialize (fetches supported kinds from facilitator)
|
|
69
|
+
await resourceServer.initialize();
|
|
70
|
+
|
|
71
|
+
// Configure routes with payment requirements
|
|
72
|
+
const routes = {
|
|
73
|
+
'GET /api/data': {
|
|
74
|
+
accepts: {
|
|
75
|
+
scheme: 'exact',
|
|
76
|
+
network: 'eip155:8453',
|
|
77
|
+
payTo: '0xYourAddress',
|
|
78
|
+
price: '$0.01',
|
|
79
|
+
},
|
|
80
|
+
description: 'Premium data access',
|
|
81
|
+
mimeType: 'application/json',
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Create HTTP server wrapper
|
|
86
|
+
const httpServer = new x402HTTPResourceServer(resourceServer, routes);
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Facilitator Usage
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
import { x402Facilitator } from '@bankofai/x402-core/facilitator';
|
|
93
|
+
import { registerExactEvmScheme } from '@bankofai/x402-evm/exact/facilitator';
|
|
94
|
+
|
|
95
|
+
const facilitator = new x402Facilitator();
|
|
96
|
+
|
|
97
|
+
// Register scheme implementations using helper
|
|
98
|
+
registerExactEvmScheme(facilitator, {
|
|
99
|
+
signer: evmSigner,
|
|
100
|
+
networks: 'eip155:84532',
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
// Verify payment
|
|
104
|
+
const verifyResult = await facilitator.verify(paymentPayload, paymentRequirements);
|
|
105
|
+
|
|
106
|
+
if (verifyResult.isValid) {
|
|
107
|
+
// Settle payment
|
|
108
|
+
const settleResult = await facilitator.settle(paymentPayload, paymentRequirements);
|
|
109
|
+
console.log('Transaction:', settleResult.transaction);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Route Configuration
|
|
114
|
+
|
|
115
|
+
Routes use the `accepts` field to define payment options:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
const routes = {
|
|
119
|
+
// Single payment option
|
|
120
|
+
'GET /api/data': {
|
|
121
|
+
accepts: {
|
|
122
|
+
scheme: 'exact',
|
|
123
|
+
network: 'eip155:8453',
|
|
124
|
+
payTo: '0xAddress',
|
|
125
|
+
price: '$0.01',
|
|
126
|
+
},
|
|
127
|
+
description: 'Data endpoint',
|
|
128
|
+
mimeType: 'application/json',
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
// Multiple payment options (EVM + SVM)
|
|
132
|
+
'POST /api/*': {
|
|
133
|
+
accepts: [
|
|
134
|
+
{
|
|
135
|
+
scheme: 'exact',
|
|
136
|
+
network: 'eip155:8453',
|
|
137
|
+
payTo: evmAddress,
|
|
138
|
+
price: '$0.05',
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
scheme: 'exact',
|
|
142
|
+
network: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
143
|
+
payTo: svmAddress,
|
|
144
|
+
price: '$0.05',
|
|
145
|
+
},
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Client Configuration
|
|
152
|
+
|
|
153
|
+
Use `fromConfig()` for declarative setup:
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
const client = x402Client.fromConfig({
|
|
157
|
+
schemes: [
|
|
158
|
+
{ network: 'eip155:8453', client: new ExactEvmScheme(evmSigner) },
|
|
159
|
+
{ network: 'solana:mainnet', client: new ExactSvmScheme(svmSigner) },
|
|
160
|
+
],
|
|
161
|
+
policies: [
|
|
162
|
+
// Filter by max price
|
|
163
|
+
(version, reqs) => reqs.filter(r => BigInt(r.amount) < BigInt('1000000')),
|
|
164
|
+
],
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Lifecycle Hooks
|
|
169
|
+
|
|
170
|
+
### Client Hooks
|
|
171
|
+
|
|
172
|
+
```typescript
|
|
173
|
+
client
|
|
174
|
+
.onBeforePaymentCreation(async (ctx) => {
|
|
175
|
+
console.log('Creating payment for:', ctx.selectedRequirements.network);
|
|
176
|
+
// Return { abort: true, reason: '...' } to cancel
|
|
177
|
+
})
|
|
178
|
+
.onAfterPaymentCreation(async (ctx) => {
|
|
179
|
+
console.log('Payment created:', ctx.paymentPayload);
|
|
180
|
+
})
|
|
181
|
+
.onPaymentCreationFailure(async (ctx) => {
|
|
182
|
+
console.error('Payment failed:', ctx.error);
|
|
183
|
+
// Return { recovered: true, payload: ... } to recover
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Server Hooks
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
resourceServer
|
|
191
|
+
.onBeforeVerify(async (ctx) => { /* ... */ })
|
|
192
|
+
.onAfterVerify(async (ctx) => { /* ... */ })
|
|
193
|
+
.onBeforeSettle(async (ctx) => { /* ... */ })
|
|
194
|
+
.onAfterSettle(async (ctx) => { /* ... */ });
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
### Facilitator Hooks
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
facilitator
|
|
201
|
+
.onBeforeVerify(async (ctx) => { console.log('Before verify', ctx); })
|
|
202
|
+
.onAfterVerify(async (ctx) => { console.log('After verify', ctx); })
|
|
203
|
+
.onVerifyFailure(async (ctx) => { console.log('Verify failure', ctx); })
|
|
204
|
+
.onBeforeSettle(async (ctx) => { console.log('Before settle', ctx); })
|
|
205
|
+
.onAfterSettle(async (ctx) => { console.log('After settle', ctx); })
|
|
206
|
+
.onSettleFailure(async (ctx) => { console.log('Settle failure', ctx); });
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
## HTTP Headers
|
|
210
|
+
|
|
211
|
+
### v2 Protocol (Current)
|
|
212
|
+
|
|
213
|
+
| Header | Description |
|
|
214
|
+
|--------|-------------|
|
|
215
|
+
| `PAYMENT-SIGNATURE` | Base64-encoded payment payload |
|
|
216
|
+
| `PAYMENT-REQUIRED` | Base64-encoded payment requirements |
|
|
217
|
+
| `PAYMENT-RESPONSE` | Base64-encoded settlement response |
|
|
218
|
+
|
|
219
|
+
### v1 Protocol (Legacy)
|
|
220
|
+
|
|
221
|
+
| Header | Description |
|
|
222
|
+
|--------|-------------|
|
|
223
|
+
| `X-PAYMENT` | Base64-encoded payment payload |
|
|
224
|
+
| `X-PAYMENT-RESPONSE` | Base64-encoded settlement response |
|
|
225
|
+
|
|
226
|
+
## Network Pattern Matching
|
|
227
|
+
|
|
228
|
+
Register handlers for network families using wildcards:
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
// All EVM networks
|
|
232
|
+
server.register('eip155:*', new ExactEvmScheme());
|
|
233
|
+
|
|
234
|
+
// Specific network takes precedence
|
|
235
|
+
server.register('eip155:8453', new ExactEvmScheme());
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
## Types
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
type Network = `${string}:${string}`; // e.g., "eip155:8453"
|
|
242
|
+
|
|
243
|
+
type PaymentRequirements = {
|
|
244
|
+
scheme: string;
|
|
245
|
+
network: Network;
|
|
246
|
+
asset: string;
|
|
247
|
+
amount: string;
|
|
248
|
+
payTo: string;
|
|
249
|
+
maxTimeoutSeconds: number;
|
|
250
|
+
extra: Record<string, unknown>;
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
type PaymentPayload = {
|
|
254
|
+
x402Version: number;
|
|
255
|
+
resource: ResourceInfo;
|
|
256
|
+
accepted: PaymentRequirements;
|
|
257
|
+
payload: Record<string, unknown>;
|
|
258
|
+
extensions?: Record<string, unknown>;
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
type PaymentRequired = {
|
|
262
|
+
x402Version: number;
|
|
263
|
+
error?: string;
|
|
264
|
+
resource: ResourceInfo;
|
|
265
|
+
accepts: PaymentRequirements[];
|
|
266
|
+
extensions?: Record<string, unknown>;
|
|
267
|
+
};
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Framework Integration
|
|
271
|
+
|
|
272
|
+
For framework-specific middleware, use:
|
|
273
|
+
|
|
274
|
+
- `@bankofai/x402-express` - Express.js middleware
|
|
275
|
+
- `@bankofai/x402-hono` - Hono middleware
|
|
276
|
+
- `@bankofai/x402-next` - Next.js integration
|
|
277
|
+
- `@bankofai/x402-axios` - Axios interceptor
|
|
278
|
+
- `@bankofai/x402-fetch` - Fetch wrapper
|
|
279
|
+
|
|
280
|
+
## Implementation Packages
|
|
281
|
+
|
|
282
|
+
For blockchain-specific implementations:
|
|
283
|
+
|
|
284
|
+
- `@bankofai/x402-evm` - Ethereum and EVM-compatible chains
|
|
285
|
+
- `@bankofai/x402-svm` - Solana blockchain
|
|
286
|
+
- `@bankofai/x402-avm` - Algorand blockchain
|
|
287
|
+
|
|
288
|
+
## Examples
|
|
289
|
+
|
|
290
|
+
See the [examples directory](https://github.com/x402-foundation/x402/tree/main/examples/typescript) for complete examples.
|
|
291
|
+
|
|
292
|
+
## Contributing
|
|
293
|
+
|
|
294
|
+
Contributions welcome! See [Contributing Guide](https://github.com/x402-foundation/x402/blob/main/CONTRIBUTING.md).
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { c as PaymentRequired, ae as x402Client, P as PaymentPayload, S as SettleResponse } from '../x402Client-TQHctrG7.js';
|
|
2
|
+
export { aj as AfterPaymentCreationHook, ai as BeforePaymentCreationHook, aq as ClientExtension, ao as ClientExtensionHooks, ap as ClientTransportExtensionHooks, ak as OnPaymentCreationFailureHook, am as OnPaymentResponseHook, ag as PaymentCreatedContext, af as PaymentCreationContext, ah as PaymentCreationFailureContext, ar as PaymentPolicy, al as PaymentResponseContext, as as SchemeRegistration, an as SelectPaymentRequirements, at as x402ClientConfig } from '../x402Client-TQHctrG7.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Context provided to onPaymentRequired hooks.
|
|
6
|
+
*/
|
|
7
|
+
interface PaymentRequiredContext {
|
|
8
|
+
paymentRequired: PaymentRequired;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Hook called when a 402 response is received, before payment processing.
|
|
12
|
+
* Return headers to try before payment, or void to proceed directly to payment.
|
|
13
|
+
*/
|
|
14
|
+
type PaymentRequiredHook = (context: PaymentRequiredContext) => Promise<{
|
|
15
|
+
headers: Record<string, string>;
|
|
16
|
+
} | void>;
|
|
17
|
+
interface HTTPClientExtensionHooks {
|
|
18
|
+
onPaymentRequired?: (declaration: unknown, context: PaymentRequiredContext) => Promise<{
|
|
19
|
+
headers: Record<string, string>;
|
|
20
|
+
} | void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* HTTP-specific client for handling x402 payment protocol over HTTP.
|
|
24
|
+
*
|
|
25
|
+
* Wraps a x402Client to provide HTTP-specific encoding/decoding functionality
|
|
26
|
+
* for payment headers and responses while maintaining the builder pattern.
|
|
27
|
+
*/
|
|
28
|
+
declare class x402HTTPClient {
|
|
29
|
+
private readonly client;
|
|
30
|
+
private paymentRequiredHooks;
|
|
31
|
+
/**
|
|
32
|
+
* Creates a new x402HTTPClient instance.
|
|
33
|
+
*
|
|
34
|
+
* @param client - The underlying x402Client for payment logic
|
|
35
|
+
*/
|
|
36
|
+
constructor(client: x402Client);
|
|
37
|
+
/**
|
|
38
|
+
* Register a hook to handle 402 responses before payment.
|
|
39
|
+
* Hooks run in order; first to return headers wins.
|
|
40
|
+
*
|
|
41
|
+
* @param hook - The hook function to register
|
|
42
|
+
* @returns This instance for chaining
|
|
43
|
+
*/
|
|
44
|
+
onPaymentRequired(hook: PaymentRequiredHook): this;
|
|
45
|
+
/**
|
|
46
|
+
* Run hooks and return headers if any hook provides them.
|
|
47
|
+
*
|
|
48
|
+
* @param paymentRequired - The payment required response from the server
|
|
49
|
+
* @returns Headers to use for retry, or null to proceed to payment
|
|
50
|
+
*/
|
|
51
|
+
handlePaymentRequired(paymentRequired: PaymentRequired): Promise<Record<string, string> | null>;
|
|
52
|
+
/**
|
|
53
|
+
* Encodes a payment payload into appropriate HTTP headers based on version.
|
|
54
|
+
*
|
|
55
|
+
* @param paymentPayload - The payment payload to encode
|
|
56
|
+
* @returns HTTP headers containing the encoded payment signature
|
|
57
|
+
*/
|
|
58
|
+
encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string>;
|
|
59
|
+
/**
|
|
60
|
+
* Extracts payment required information from HTTP response.
|
|
61
|
+
*
|
|
62
|
+
* @param getHeader - Function to retrieve header value by name (case-insensitive)
|
|
63
|
+
* @param body - Optional response body for v1 compatibility
|
|
64
|
+
* @returns The payment required object
|
|
65
|
+
*/
|
|
66
|
+
getPaymentRequiredResponse(getHeader: (name: string) => string | null | undefined, body?: unknown): PaymentRequired;
|
|
67
|
+
/**
|
|
68
|
+
* Extracts payment settlement response from HTTP headers.
|
|
69
|
+
*
|
|
70
|
+
* @param getHeader - Function to retrieve header value by name (case-insensitive)
|
|
71
|
+
* @returns The settlement response object
|
|
72
|
+
*/
|
|
73
|
+
getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse;
|
|
74
|
+
/**
|
|
75
|
+
* Creates a payment payload for the given payment requirements.
|
|
76
|
+
* Delegates to the underlying x402Client.
|
|
77
|
+
*
|
|
78
|
+
* @param paymentRequired - The payment required response from the server
|
|
79
|
+
* @returns Promise resolving to the payment payload
|
|
80
|
+
*/
|
|
81
|
+
createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload>;
|
|
82
|
+
/**
|
|
83
|
+
* Parses response headers into protocol types, fires payment response hooks (v2 only),
|
|
84
|
+
* and returns whether a hook signaled recovery.
|
|
85
|
+
*
|
|
86
|
+
* Called by transport wrappers (fetch, axios) after the paid request completes.
|
|
87
|
+
*
|
|
88
|
+
* @param paymentPayload - The payload that was sent with the request
|
|
89
|
+
* @param getHeader - Function to retrieve a response header by name
|
|
90
|
+
* @param status - The HTTP status code of the response
|
|
91
|
+
* @returns Whether a hook recovered and the parsed settle response (if any)
|
|
92
|
+
*/
|
|
93
|
+
processPaymentResult(paymentPayload: PaymentPayload, getHeader: (name: string) => string | null | undefined, status: number): Promise<{
|
|
94
|
+
recovered: boolean;
|
|
95
|
+
settleResponse?: SettleResponse;
|
|
96
|
+
}>;
|
|
97
|
+
/**
|
|
98
|
+
* Parses HTTP status, headers, and body into an `HTTPResourceResponse`.
|
|
99
|
+
*
|
|
100
|
+
* Decodes the x402 payment header into `header`: the `PAYMENT-RESPONSE`
|
|
101
|
+
* settlement if present, otherwise the `PAYMENT-REQUIRED` declaration on
|
|
102
|
+
* 402 responses (whose `error` field carries the server's failure reason).
|
|
103
|
+
*
|
|
104
|
+
* @param args - Normalized response inputs from any HTTP transport
|
|
105
|
+
* @param args.status - HTTP response status code
|
|
106
|
+
* @param args.getHeader - Callback to read response headers by name
|
|
107
|
+
* @param args.body - Response body payload
|
|
108
|
+
* @returns The parsed status, body, and decoded payment header
|
|
109
|
+
*/
|
|
110
|
+
parsePaymentResult(args: {
|
|
111
|
+
status: number;
|
|
112
|
+
getHeader: (name: string) => string | null | undefined;
|
|
113
|
+
body: unknown;
|
|
114
|
+
}): HTTPResourceResponse;
|
|
115
|
+
/**
|
|
116
|
+
* Parses a fetch Response into an `HTTPResourceResponse` for app-level convenience.
|
|
117
|
+
*
|
|
118
|
+
* @param response - The fetch Response to process
|
|
119
|
+
* @returns The parsed status, body, and decoded payment header
|
|
120
|
+
*/
|
|
121
|
+
processResponse(response: Response): Promise<HTTPResourceResponse>;
|
|
122
|
+
/**
|
|
123
|
+
* Manual HTTP hooks run before extension hooks scoped to the 402 response.
|
|
124
|
+
*
|
|
125
|
+
* @param paymentRequired - The payment required response from the server
|
|
126
|
+
* @returns Hooks in invocation order
|
|
127
|
+
*/
|
|
128
|
+
private getPaymentRequiredHooks;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Parsed result of an HTTP request to an x402 resource.
|
|
132
|
+
*/
|
|
133
|
+
type HTTPResourceResponse = {
|
|
134
|
+
/** HTTP status code. */
|
|
135
|
+
status: number;
|
|
136
|
+
/** x402 payment outcome. */
|
|
137
|
+
paymentStatus: HTTPPaymentStatus;
|
|
138
|
+
/** Parsed response body. */
|
|
139
|
+
body: unknown;
|
|
140
|
+
/**
|
|
141
|
+
* Decoded x402 payment header, if present:
|
|
142
|
+
* - SettleResponse (from PAYMENT-RESPONSE / X-PAYMENT-RESPONSE)
|
|
143
|
+
* - PaymentRequired (from PAYMENT-REQUIRED; its `error` carries the server reason)
|
|
144
|
+
*/
|
|
145
|
+
header?: SettleResponse | PaymentRequired;
|
|
146
|
+
};
|
|
147
|
+
type HTTPPaymentStatus = "settled" | "settle_failed" | "payment_required" | "none";
|
|
148
|
+
|
|
149
|
+
export { type HTTPClientExtensionHooks, type HTTPPaymentStatus, type HTTPResourceResponse, type PaymentRequiredContext, type PaymentRequiredHook, x402Client, x402HTTPClient };
|