@parallel-protocol/x402 0.7.1 → 0.8.1
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 +158 -58
- package/dist/{chunk-AENXD4OC.js → chunk-GXPUE3ES.js} +233 -77
- package/dist/{chunk-JPMK5YBU.cjs → chunk-KZZY5LS6.cjs} +13 -2
- package/dist/{chunk-AFRDHGDT.js → chunk-QAECRSWF.js} +13 -2
- package/dist/{chunk-POU7EH3E.cjs → chunk-SNW5Z5HB.cjs} +255 -97
- package/dist/express.cjs +6 -6
- package/dist/express.d.cts +1 -1
- package/dist/express.d.ts +1 -1
- package/dist/express.js +2 -2
- package/dist/fastify.cjs +20 -29
- package/dist/fastify.d.cts +1 -1
- package/dist/fastify.d.ts +1 -1
- package/dist/fastify.js +13 -22
- package/dist/hono.cjs +7 -7
- package/dist/hono.d.cts +1 -1
- package/dist/hono.d.ts +1 -1
- package/dist/hono.js +2 -2
- package/dist/index.cjs +27 -19
- package/dist/index.d.cts +56 -7
- package/dist/index.d.ts +56 -7
- package/dist/index.js +2 -2
- package/dist/llm.cjs +13 -13
- package/dist/llm.d.cts +1 -1
- package/dist/llm.d.ts +1 -1
- package/dist/llm.js +1 -1
- package/dist/next.cjs +28 -44
- package/dist/next.d.cts +1 -1
- package/dist/next.d.ts +1 -1
- package/dist/next.js +20 -36
- package/dist/{types-q_gu5wjz.d.cts → types-BkGQ2lPA.d.cts} +30 -1
- package/dist/{types-q_gu5wjz.d.ts → types-BkGQ2lPA.d.ts} +30 -1
- package/package.json +1 -1
package/dist/llm.d.cts
CHANGED
package/dist/llm.d.ts
CHANGED
package/dist/llm.js
CHANGED
package/dist/next.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
require('./chunk-
|
|
3
|
+
var chunkSNW5Z5HB_cjs = require('./chunk-SNW5Z5HB.cjs');
|
|
4
|
+
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
var server = require('next/server');
|
|
6
6
|
|
|
7
7
|
// src/next/adapter.ts
|
|
@@ -25,7 +25,7 @@ var NextAdapter = class {
|
|
|
25
25
|
// Read from a clone: a request body is a stream that can be consumed once,
|
|
26
26
|
// and the handler still needs the original.
|
|
27
27
|
async getBody() {
|
|
28
|
-
if (!
|
|
28
|
+
if (!chunkSNW5Z5HB_cjs.isJsonContentType(this.req.headers.get("content-type") ?? void 0))
|
|
29
29
|
return void 0;
|
|
30
30
|
try {
|
|
31
31
|
return await this.req.clone().json();
|
|
@@ -36,29 +36,27 @@ var NextAdapter = class {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
// src/next/index.ts
|
|
39
|
+
function fromResult(result) {
|
|
40
|
+
return new server.NextResponse(JSON.stringify(result.body), {
|
|
41
|
+
status: result.status,
|
|
42
|
+
headers: { "Content-Type": "application/json", ...result.headers }
|
|
43
|
+
});
|
|
44
|
+
}
|
|
39
45
|
function withPayment(config, handler) {
|
|
40
|
-
const gate =
|
|
46
|
+
const gate = chunkSNW5Z5HB_cjs.createPaymentGate(config);
|
|
41
47
|
return async (req) => {
|
|
42
48
|
const gateResult = await gate(new NextAdapter(req));
|
|
43
49
|
if (gateResult.type === "pass") {
|
|
44
50
|
return handler(req);
|
|
45
51
|
}
|
|
46
|
-
if (gateResult.type === "error")
|
|
47
|
-
return new server.NextResponse(JSON.stringify(gateResult.result.body), {
|
|
48
|
-
status: gateResult.result.status,
|
|
49
|
-
headers: {
|
|
50
|
-
...gateResult.result.headers,
|
|
51
|
-
"Content-Type": "application/json"
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
52
|
+
if (gateResult.type === "error") return fromResult(gateResult.result);
|
|
55
53
|
let response;
|
|
56
54
|
try {
|
|
57
|
-
const outcome = await
|
|
55
|
+
const outcome = await chunkSNW5Z5HB_cjs.raceTimeout(
|
|
58
56
|
handler(req),
|
|
59
57
|
gateResult.handlerTimeoutMs
|
|
60
58
|
);
|
|
61
|
-
if (outcome ===
|
|
59
|
+
if (outcome === chunkSNW5Z5HB_cjs.HANDLER_TIMEOUT) {
|
|
62
60
|
return new server.NextResponse(null, { status: 504 });
|
|
63
61
|
}
|
|
64
62
|
response = outcome;
|
|
@@ -67,10 +65,10 @@ function withPayment(config, handler) {
|
|
|
67
65
|
}
|
|
68
66
|
let raw;
|
|
69
67
|
if (gateResult.scheme === "upto") {
|
|
70
|
-
raw = response.headers.get(
|
|
68
|
+
raw = response.headers.get(chunkSNW5Z5HB_cjs.SETTLE_AMOUNT_HEADER) ?? void 0;
|
|
71
69
|
if (raw !== void 0) {
|
|
72
70
|
const headers = new Headers(response.headers);
|
|
73
|
-
headers.delete(
|
|
71
|
+
headers.delete(chunkSNW5Z5HB_cjs.SETTLE_AMOUNT_HEADER);
|
|
74
72
|
response = new server.NextResponse(response.body, {
|
|
75
73
|
status: response.status,
|
|
76
74
|
headers
|
|
@@ -81,47 +79,33 @@ function withPayment(config, handler) {
|
|
|
81
79
|
let settleOptions;
|
|
82
80
|
if (gateResult.scheme === "upto") {
|
|
83
81
|
const handled = response;
|
|
84
|
-
const resolved = await
|
|
82
|
+
const resolved = await chunkSNW5Z5HB_cjs.resolveCharge(gateResult, raw, {
|
|
85
83
|
status: handled.status,
|
|
86
84
|
header: (name) => handled.headers.get(name) ?? void 0,
|
|
87
85
|
// Read from a clone: the body streams once, to the client.
|
|
88
|
-
body: () =>
|
|
86
|
+
body: () => chunkSNW5Z5HB_cjs.jsonFromResponse(handled)
|
|
89
87
|
});
|
|
90
88
|
if (resolved.kind === "invalid") {
|
|
91
89
|
handled.body?.cancel().catch(() => {
|
|
92
90
|
});
|
|
93
|
-
return
|
|
94
|
-
chunkPOU7EH3E_cjs.invalidSettleAmountResponse(resolved.reason),
|
|
95
|
-
{
|
|
96
|
-
status: 402
|
|
97
|
-
}
|
|
98
|
-
);
|
|
91
|
+
return fromResult(chunkSNW5Z5HB_cjs.invalidSettleAmountResponse(resolved.reason));
|
|
99
92
|
}
|
|
100
93
|
if (resolved.kind === "skip") return response;
|
|
101
94
|
settleOptions = { amount: resolved.amount };
|
|
102
95
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
} catch {
|
|
96
|
+
const outcome = await chunkSNW5Z5HB_cjs.settleVerified(gateResult, settleOptions);
|
|
97
|
+
if (outcome.settled) {
|
|
98
|
+
const headers = new Headers(response.headers);
|
|
99
|
+
headers.set("payment-response", chunkSNW5Z5HB_cjs.encodeBase64(outcome.confirmation));
|
|
100
|
+
headers.set("access-control-expose-headers", "payment-response");
|
|
101
|
+
return new server.NextResponse(response.body, {
|
|
102
|
+
status: response.status,
|
|
103
|
+
headers
|
|
104
|
+
});
|
|
115
105
|
}
|
|
116
106
|
response.body?.cancel().catch(() => {
|
|
117
107
|
});
|
|
118
|
-
return
|
|
119
|
-
{
|
|
120
|
-
error: "PAYMENT_SETTLEMENT_FAILED",
|
|
121
|
-
message: "Payment could not be confirmed after multiple attempts. Please retry."
|
|
122
|
-
},
|
|
123
|
-
{ status: 402 }
|
|
124
|
-
);
|
|
108
|
+
return fromResult(chunkSNW5Z5HB_cjs.settlementFailureResponse(outcome));
|
|
125
109
|
}
|
|
126
110
|
return response;
|
|
127
111
|
};
|
package/dist/next.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-BkGQ2lPA.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class NextAdapter implements HTTPAdapter {
|
package/dist/next.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
|
-
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, P as PaymentMiddlewareConfig } from './types-BkGQ2lPA.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class NextAdapter implements HTTPAdapter {
|
package/dist/next.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isJsonContentType, createPaymentGate, raceTimeout, HANDLER_TIMEOUT, SETTLE_AMOUNT_HEADER, resolveCharge, jsonFromResponse, invalidSettleAmountResponse, encodeBase64 } from './chunk-
|
|
2
|
-
import './chunk-
|
|
1
|
+
import { isJsonContentType, createPaymentGate, raceTimeout, HANDLER_TIMEOUT, SETTLE_AMOUNT_HEADER, resolveCharge, jsonFromResponse, invalidSettleAmountResponse, settleVerified, encodeBase64, settlementFailureResponse } from './chunk-GXPUE3ES.js';
|
|
2
|
+
import './chunk-QAECRSWF.js';
|
|
3
3
|
import { NextResponse } from 'next/server';
|
|
4
4
|
|
|
5
5
|
// src/next/adapter.ts
|
|
@@ -34,6 +34,12 @@ var NextAdapter = class {
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
// src/next/index.ts
|
|
37
|
+
function fromResult(result) {
|
|
38
|
+
return new NextResponse(JSON.stringify(result.body), {
|
|
39
|
+
status: result.status,
|
|
40
|
+
headers: { "Content-Type": "application/json", ...result.headers }
|
|
41
|
+
});
|
|
42
|
+
}
|
|
37
43
|
function withPayment(config, handler) {
|
|
38
44
|
const gate = createPaymentGate(config);
|
|
39
45
|
return async (req) => {
|
|
@@ -41,15 +47,7 @@ function withPayment(config, handler) {
|
|
|
41
47
|
if (gateResult.type === "pass") {
|
|
42
48
|
return handler(req);
|
|
43
49
|
}
|
|
44
|
-
if (gateResult.type === "error")
|
|
45
|
-
return new NextResponse(JSON.stringify(gateResult.result.body), {
|
|
46
|
-
status: gateResult.result.status,
|
|
47
|
-
headers: {
|
|
48
|
-
...gateResult.result.headers,
|
|
49
|
-
"Content-Type": "application/json"
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
}
|
|
50
|
+
if (gateResult.type === "error") return fromResult(gateResult.result);
|
|
53
51
|
let response;
|
|
54
52
|
try {
|
|
55
53
|
const outcome = await raceTimeout(
|
|
@@ -88,38 +86,24 @@ function withPayment(config, handler) {
|
|
|
88
86
|
if (resolved.kind === "invalid") {
|
|
89
87
|
handled.body?.cancel().catch(() => {
|
|
90
88
|
});
|
|
91
|
-
return
|
|
92
|
-
invalidSettleAmountResponse(resolved.reason),
|
|
93
|
-
{
|
|
94
|
-
status: 402
|
|
95
|
-
}
|
|
96
|
-
);
|
|
89
|
+
return fromResult(invalidSettleAmountResponse(resolved.reason));
|
|
97
90
|
}
|
|
98
91
|
if (resolved.kind === "skip") return response;
|
|
99
92
|
settleOptions = { amount: resolved.amount };
|
|
100
93
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
} catch {
|
|
94
|
+
const outcome = await settleVerified(gateResult, settleOptions);
|
|
95
|
+
if (outcome.settled) {
|
|
96
|
+
const headers = new Headers(response.headers);
|
|
97
|
+
headers.set("payment-response", encodeBase64(outcome.confirmation));
|
|
98
|
+
headers.set("access-control-expose-headers", "payment-response");
|
|
99
|
+
return new NextResponse(response.body, {
|
|
100
|
+
status: response.status,
|
|
101
|
+
headers
|
|
102
|
+
});
|
|
113
103
|
}
|
|
114
104
|
response.body?.cancel().catch(() => {
|
|
115
105
|
});
|
|
116
|
-
return
|
|
117
|
-
{
|
|
118
|
-
error: "PAYMENT_SETTLEMENT_FAILED",
|
|
119
|
-
message: "Payment could not be confirmed after multiple attempts. Please retry."
|
|
120
|
-
},
|
|
121
|
-
{ status: 402 }
|
|
122
|
-
);
|
|
106
|
+
return fromResult(settlementFailureResponse(outcome));
|
|
123
107
|
}
|
|
124
108
|
return response;
|
|
125
109
|
};
|
|
@@ -117,6 +117,8 @@ interface PaymentRequirements {
|
|
|
117
117
|
interface PaymentRequired {
|
|
118
118
|
x402Version: number;
|
|
119
119
|
error?: string;
|
|
120
|
+
/** The facilitator's message for `error`, when it answered with one. */
|
|
121
|
+
message?: string;
|
|
120
122
|
resource: ResourceInfo;
|
|
121
123
|
accepts: PaymentRequirements[];
|
|
122
124
|
extensions?: Record<string, unknown>;
|
|
@@ -134,6 +136,24 @@ interface SettleOptions {
|
|
|
134
136
|
/** Amount actually charged, in the asset's smallest unit. */
|
|
135
137
|
amount?: string;
|
|
136
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* What `FacilitatorClient.settle` sends besides the payment: the metered
|
|
141
|
+
* amount, and the settle token the facilitator issued at verify. The payment
|
|
142
|
+
* gate fills the token in itself; a direct caller of the client passes the one
|
|
143
|
+
* `verify` returned.
|
|
144
|
+
*/
|
|
145
|
+
interface FacilitatorSettleOptions extends SettleOptions {
|
|
146
|
+
settleToken?: string;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* What a successful `FacilitatorClient.verify` returns: the settle token the
|
|
150
|
+
* facilitator issued for this payment, to present back at settle. It is a
|
|
151
|
+
* secret for the life of the request: keep it in memory, never log it, never
|
|
152
|
+
* send it to the payer.
|
|
153
|
+
*/
|
|
154
|
+
interface VerifyResult {
|
|
155
|
+
settleToken: string;
|
|
156
|
+
}
|
|
137
157
|
interface PaymentConfirmation {
|
|
138
158
|
success: true;
|
|
139
159
|
txHash: Address;
|
|
@@ -143,6 +163,11 @@ interface PaymentConfirmation {
|
|
|
143
163
|
networkId: string;
|
|
144
164
|
/** Amount actually charged (smallest unit) — metered routes only. */
|
|
145
165
|
amountSettled?: string;
|
|
166
|
+
/**
|
|
167
|
+
* Present when the facilitator answered from the outcome an earlier settle
|
|
168
|
+
* of the same payment recorded: this call broadcast nothing.
|
|
169
|
+
*/
|
|
170
|
+
alreadySettled?: true;
|
|
146
171
|
}
|
|
147
172
|
interface PaymentFailure {
|
|
148
173
|
success: false;
|
|
@@ -150,6 +175,10 @@ interface PaymentFailure {
|
|
|
150
175
|
code: string;
|
|
151
176
|
message: string;
|
|
152
177
|
hint?: string;
|
|
178
|
+
/** The facilitator's HTTP status, when the refusal is its answer. */
|
|
179
|
+
status?: number;
|
|
180
|
+
/** The facilitator's `Retry-After` header, when it sent one. */
|
|
181
|
+
retryAfter?: string;
|
|
153
182
|
};
|
|
154
183
|
}
|
|
155
184
|
type FacilitatorResponse = PaymentConfirmation | PaymentFailure;
|
|
@@ -182,4 +211,4 @@ type PaymentGateResult = {
|
|
|
182
211
|
result: MiddlewareResult;
|
|
183
212
|
} | VerifiedPayment;
|
|
184
213
|
|
|
185
|
-
export type { FacilitatorConfig as F, HTTPAdapter as H, MiddlewareResult as M, PaymentMiddlewareConfig as P, RunHandlerOptions as R, SettleOptions as S,
|
|
214
|
+
export type { FacilitatorConfig as F, HTTPAdapter as H, MiddlewareResult as M, PaymentMiddlewareConfig as P, RunHandlerOptions as R, SettleOptions as S, VerifyResult as V, RunHandlerResult as a, FacilitatorPaymentRequirements as b, FacilitatorSettleOptions as c, FacilitatorResponse as d, PaymentConfirmation as e, PaymentGateResult as f, PassResult as g, VerifiedPayment as h, MeterContext as i, MeterFunction as j, PaymentFailure as k, PaymentRequired as l, PaymentRequirements as m, PriceContext as n, PriceFunction as o, ResourceInfo as p, RouteConfig as q };
|
|
@@ -117,6 +117,8 @@ interface PaymentRequirements {
|
|
|
117
117
|
interface PaymentRequired {
|
|
118
118
|
x402Version: number;
|
|
119
119
|
error?: string;
|
|
120
|
+
/** The facilitator's message for `error`, when it answered with one. */
|
|
121
|
+
message?: string;
|
|
120
122
|
resource: ResourceInfo;
|
|
121
123
|
accepts: PaymentRequirements[];
|
|
122
124
|
extensions?: Record<string, unknown>;
|
|
@@ -134,6 +136,24 @@ interface SettleOptions {
|
|
|
134
136
|
/** Amount actually charged, in the asset's smallest unit. */
|
|
135
137
|
amount?: string;
|
|
136
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* What `FacilitatorClient.settle` sends besides the payment: the metered
|
|
141
|
+
* amount, and the settle token the facilitator issued at verify. The payment
|
|
142
|
+
* gate fills the token in itself; a direct caller of the client passes the one
|
|
143
|
+
* `verify` returned.
|
|
144
|
+
*/
|
|
145
|
+
interface FacilitatorSettleOptions extends SettleOptions {
|
|
146
|
+
settleToken?: string;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* What a successful `FacilitatorClient.verify` returns: the settle token the
|
|
150
|
+
* facilitator issued for this payment, to present back at settle. It is a
|
|
151
|
+
* secret for the life of the request: keep it in memory, never log it, never
|
|
152
|
+
* send it to the payer.
|
|
153
|
+
*/
|
|
154
|
+
interface VerifyResult {
|
|
155
|
+
settleToken: string;
|
|
156
|
+
}
|
|
137
157
|
interface PaymentConfirmation {
|
|
138
158
|
success: true;
|
|
139
159
|
txHash: Address;
|
|
@@ -143,6 +163,11 @@ interface PaymentConfirmation {
|
|
|
143
163
|
networkId: string;
|
|
144
164
|
/** Amount actually charged (smallest unit) — metered routes only. */
|
|
145
165
|
amountSettled?: string;
|
|
166
|
+
/**
|
|
167
|
+
* Present when the facilitator answered from the outcome an earlier settle
|
|
168
|
+
* of the same payment recorded: this call broadcast nothing.
|
|
169
|
+
*/
|
|
170
|
+
alreadySettled?: true;
|
|
146
171
|
}
|
|
147
172
|
interface PaymentFailure {
|
|
148
173
|
success: false;
|
|
@@ -150,6 +175,10 @@ interface PaymentFailure {
|
|
|
150
175
|
code: string;
|
|
151
176
|
message: string;
|
|
152
177
|
hint?: string;
|
|
178
|
+
/** The facilitator's HTTP status, when the refusal is its answer. */
|
|
179
|
+
status?: number;
|
|
180
|
+
/** The facilitator's `Retry-After` header, when it sent one. */
|
|
181
|
+
retryAfter?: string;
|
|
153
182
|
};
|
|
154
183
|
}
|
|
155
184
|
type FacilitatorResponse = PaymentConfirmation | PaymentFailure;
|
|
@@ -182,4 +211,4 @@ type PaymentGateResult = {
|
|
|
182
211
|
result: MiddlewareResult;
|
|
183
212
|
} | VerifiedPayment;
|
|
184
213
|
|
|
185
|
-
export type { FacilitatorConfig as F, HTTPAdapter as H, MiddlewareResult as M, PaymentMiddlewareConfig as P, RunHandlerOptions as R, SettleOptions as S,
|
|
214
|
+
export type { FacilitatorConfig as F, HTTPAdapter as H, MiddlewareResult as M, PaymentMiddlewareConfig as P, RunHandlerOptions as R, SettleOptions as S, VerifyResult as V, RunHandlerResult as a, FacilitatorPaymentRequirements as b, FacilitatorSettleOptions as c, FacilitatorResponse as d, PaymentConfirmation as e, PaymentGateResult as f, PassResult as g, VerifiedPayment as h, MeterContext as i, MeterFunction as j, PaymentFailure as k, PaymentRequired as l, PaymentRequirements as m, PriceContext as n, PriceFunction as o, ResourceInfo as p, RouteConfig as q };
|
package/package.json
CHANGED