@parallel-protocol/x402 0.7.0 → 0.8.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 +53 -20
- package/dist/{chunk-JPMK5YBU.cjs → chunk-KZZY5LS6.cjs} +13 -2
- package/dist/{chunk-POU7EH3E.cjs → chunk-NA7245L5.cjs} +201 -84
- package/dist/{chunk-AFRDHGDT.js → chunk-QAECRSWF.js} +13 -2
- package/dist/{chunk-AENXD4OC.js → chunk-SA5ULSG2.js} +179 -64
- 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 +50 -6
- package/dist/index.d.ts +50 -6
- 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 +2 -2
package/dist/hono.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Next, MiddlewareHandler } from 'hono';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-BkGQ2lPA.cjs';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class HonoAdapter implements HTTPAdapter {
|
package/dist/hono.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Context, Next, MiddlewareHandler } from 'hono';
|
|
2
|
-
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-
|
|
2
|
+
import { H as HTTPAdapter, R as RunHandlerOptions, a as RunHandlerResult, P as PaymentMiddlewareConfig } from './types-BkGQ2lPA.js';
|
|
3
3
|
import 'viem';
|
|
4
4
|
|
|
5
5
|
declare class HonoAdapter implements HTTPAdapter {
|
package/dist/hono.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isJsonContentType, raceTimeout, HANDLER_TIMEOUT, jsonFromResponse, createPaymentMiddleware } from './chunk-
|
|
2
|
-
import './chunk-
|
|
1
|
+
import { isJsonContentType, raceTimeout, HANDLER_TIMEOUT, jsonFromResponse, createPaymentMiddleware } from './chunk-SA5ULSG2.js';
|
|
2
|
+
import './chunk-QAECRSWF.js';
|
|
3
3
|
|
|
4
4
|
// src/hono/adapter.ts
|
|
5
5
|
var TIMEOUT_BODY = JSON.stringify({
|
package/dist/index.cjs
CHANGED
|
@@ -1,75 +1,83 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
var
|
|
3
|
+
var chunkNA7245L5_cjs = require('./chunk-NA7245L5.cjs');
|
|
4
|
+
var chunkKZZY5LS6_cjs = require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "FacilitatorClient", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkNA7245L5_cjs.FacilitatorClient; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "PARALLEL_TOKENS", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkNA7245L5_cjs.PARALLEL_TOKENS; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "SETTLE_AMOUNT_HEADER", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkNA7245L5_cjs.SETTLE_AMOUNT_HEADER; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "createPaymentGate", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkNA7245L5_cjs.createPaymentGate; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "createPaymentMiddleware", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkNA7245L5_cjs.createPaymentMiddleware; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "encodeBase64", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunkNA7245L5_cjs.encodeBase64; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "getDefaultAcceptedTokens", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunkNA7245L5_cjs.getDefaultAcceptedTokens; }
|
|
35
35
|
});
|
|
36
36
|
Object.defineProperty(exports, "parsePrice", {
|
|
37
37
|
enumerable: true,
|
|
38
|
-
get: function () { return
|
|
38
|
+
get: function () { return chunkNA7245L5_cjs.parsePrice; }
|
|
39
39
|
});
|
|
40
40
|
Object.defineProperty(exports, "resolveCharge", {
|
|
41
41
|
enumerable: true,
|
|
42
|
-
get: function () { return
|
|
42
|
+
get: function () { return chunkNA7245L5_cjs.resolveCharge; }
|
|
43
43
|
});
|
|
44
44
|
Object.defineProperty(exports, "resolveSettleAmount", {
|
|
45
45
|
enumerable: true,
|
|
46
|
-
get: function () { return
|
|
46
|
+
get: function () { return chunkNA7245L5_cjs.resolveSettleAmount; }
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "settleVerified", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () { return chunkNA7245L5_cjs.settleVerified; }
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "settlementFailureResponse", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () { return chunkNA7245L5_cjs.settlementFailureResponse; }
|
|
47
55
|
});
|
|
48
56
|
Object.defineProperty(exports, "toEip155Network", {
|
|
49
57
|
enumerable: true,
|
|
50
|
-
get: function () { return
|
|
58
|
+
get: function () { return chunkNA7245L5_cjs.toEip155Network; }
|
|
51
59
|
});
|
|
52
60
|
Object.defineProperty(exports, "validateAddress", {
|
|
53
61
|
enumerable: true,
|
|
54
|
-
get: function () { return
|
|
62
|
+
get: function () { return chunkNA7245L5_cjs.validateAddress; }
|
|
55
63
|
});
|
|
56
64
|
Object.defineProperty(exports, "X402ConfigError", {
|
|
57
65
|
enumerable: true,
|
|
58
|
-
get: function () { return
|
|
66
|
+
get: function () { return chunkKZZY5LS6_cjs.X402ConfigError; }
|
|
59
67
|
});
|
|
60
68
|
Object.defineProperty(exports, "X402PriceRejectedError", {
|
|
61
69
|
enumerable: true,
|
|
62
|
-
get: function () { return
|
|
70
|
+
get: function () { return chunkKZZY5LS6_cjs.X402PriceRejectedError; }
|
|
63
71
|
});
|
|
64
72
|
Object.defineProperty(exports, "X402RuntimeError", {
|
|
65
73
|
enumerable: true,
|
|
66
|
-
get: function () { return
|
|
74
|
+
get: function () { return chunkKZZY5LS6_cjs.X402RuntimeError; }
|
|
67
75
|
});
|
|
68
76
|
Object.defineProperty(exports, "X402_ERROR_CODES", {
|
|
69
77
|
enumerable: true,
|
|
70
|
-
get: function () { return
|
|
78
|
+
get: function () { return chunkKZZY5LS6_cjs.X402_ERROR_CODES; }
|
|
71
79
|
});
|
|
72
80
|
Object.defineProperty(exports, "isPriceRejection", {
|
|
73
81
|
enumerable: true,
|
|
74
|
-
get: function () { return
|
|
82
|
+
get: function () { return chunkKZZY5LS6_cjs.isPriceRejection; }
|
|
75
83
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { F as FacilitatorConfig, b as FacilitatorPaymentRequirements,
|
|
2
|
-
export {
|
|
1
|
+
import { F as FacilitatorConfig, b as FacilitatorPaymentRequirements, V as VerifyResult, c as FacilitatorSettleOptions, d as FacilitatorResponse, e as PaymentConfirmation, P as PaymentMiddlewareConfig, H as HTTPAdapter, f as PaymentGateResult, M as MiddlewareResult, g as PassResult, h as VerifiedPayment, S as SettleOptions } from './types-BkGQ2lPA.cjs';
|
|
2
|
+
export { i as MeterContext, j as MeterFunction, k as PaymentFailure, l as PaymentRequired, m as PaymentRequirements, n as PriceContext, o as PriceFunction, p as ResourceInfo, q as RouteConfig, R as RunHandlerOptions, a as RunHandlerResult } from './types-BkGQ2lPA.cjs';
|
|
3
3
|
import { Address } from 'viem';
|
|
4
4
|
|
|
5
5
|
declare class FacilitatorClient {
|
|
6
6
|
private readonly config;
|
|
7
7
|
constructor(config: FacilitatorConfig);
|
|
8
|
-
verify(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<
|
|
9
|
-
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements, options?:
|
|
8
|
+
verify(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<VerifyResult>;
|
|
9
|
+
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements, options?: FacilitatorSettleOptions): Promise<FacilitatorResponse>;
|
|
10
10
|
pay(paymentHeader: string): Promise<FacilitatorResponse>;
|
|
11
11
|
private buildHeaders;
|
|
12
12
|
}
|
|
@@ -15,6 +15,8 @@ declare const X402_ERROR_CODES: {
|
|
|
15
15
|
readonly FACILITATOR_UNAVAILABLE: "FACILITATOR_UNAVAILABLE";
|
|
16
16
|
readonly FACILITATOR_INVALID_RESPONSE: "FACILITATOR_INVALID_RESPONSE";
|
|
17
17
|
readonly INVALID_PAYMENT: "INVALID_PAYMENT";
|
|
18
|
+
/** The facilitator did not answer a settle within the client's timeout: the payment may still settle. */
|
|
19
|
+
readonly SETTLE_TIMEOUT: "SETTLE_TIMEOUT";
|
|
18
20
|
};
|
|
19
21
|
type X402ErrorCode = (typeof X402_ERROR_CODES)[keyof typeof X402_ERROR_CODES];
|
|
20
22
|
declare class X402ConfigError extends Error {
|
|
@@ -22,7 +24,17 @@ declare class X402ConfigError extends Error {
|
|
|
22
24
|
}
|
|
23
25
|
declare class X402RuntimeError extends Error {
|
|
24
26
|
readonly code: string;
|
|
25
|
-
|
|
27
|
+
/** The facilitator's message for this code, when it answered with one. */
|
|
28
|
+
readonly detail?: string;
|
|
29
|
+
/** The facilitator's HTTP status, when the error comes from its answer. */
|
|
30
|
+
readonly status?: number;
|
|
31
|
+
/** The facilitator's `Retry-After` header, when it sent one. */
|
|
32
|
+
readonly retryAfter?: string;
|
|
33
|
+
constructor(code: string, extra?: {
|
|
34
|
+
detail?: string;
|
|
35
|
+
status?: number;
|
|
36
|
+
retryAfter?: string;
|
|
37
|
+
});
|
|
26
38
|
}
|
|
27
39
|
/**
|
|
28
40
|
* Thrown by a per-request price function to refuse a request before anything
|
|
@@ -85,6 +97,38 @@ interface ChargeResponse {
|
|
|
85
97
|
*/
|
|
86
98
|
declare function resolveCharge(verified: VerifiedPayment, settleHeader: string | undefined, response: ChargeResponse): Promise<SettleAmountResolution>;
|
|
87
99
|
declare function createPaymentGate(config: PaymentMiddlewareConfig): (adapter: GateAdapter) => Promise<PaymentGateResult>;
|
|
100
|
+
/** The 402 body a merchant answers when a verified payment could not be settled. */
|
|
101
|
+
interface SettlementFailureBody {
|
|
102
|
+
/** Kept for the clients that read it: the historical code. */
|
|
103
|
+
error: "PAYMENT_SETTLEMENT_FAILED";
|
|
104
|
+
/**
|
|
105
|
+
* The facilitator's code, or this SDK's: `SETTLE_TIMEOUT`,
|
|
106
|
+
* `FACILITATOR_UNAVAILABLE`, `FACILITATOR_INVALID_RESPONSE`,
|
|
107
|
+
* `AMOUNT_MISMATCH`, `UNKNOWN`.
|
|
108
|
+
*/
|
|
109
|
+
reason: string;
|
|
110
|
+
message: string;
|
|
111
|
+
}
|
|
112
|
+
/** A settlement that did not confirm the payment, and what the agent is told. */
|
|
113
|
+
interface SettlementFailure {
|
|
114
|
+
settled: false;
|
|
115
|
+
reason: string;
|
|
116
|
+
message: string;
|
|
117
|
+
/** The facilitator's `Retry-After`, when it sent one. */
|
|
118
|
+
retryAfter?: string;
|
|
119
|
+
}
|
|
120
|
+
type SettlementOutcome = {
|
|
121
|
+
settled: true;
|
|
122
|
+
confirmation: PaymentConfirmation;
|
|
123
|
+
} | SettlementFailure;
|
|
124
|
+
/**
|
|
125
|
+
* Settles a verified payment once the handler answered, and says what the
|
|
126
|
+
* agent must be told when it could not be. A metered settle must settle the
|
|
127
|
+
* amount this request asked for, whatever else the facilitator recorded.
|
|
128
|
+
*/
|
|
129
|
+
declare function settleVerified(verified: VerifiedPayment, options?: SettleOptions): Promise<SettlementOutcome>;
|
|
130
|
+
/** The 402 for a settlement that failed, with the facilitator's Retry-After when it gave one. */
|
|
131
|
+
declare function settlementFailureResponse(failure: SettlementFailure): MiddlewareResult;
|
|
88
132
|
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (adapter: HTTPAdapter) => Promise<MiddlewareResult | PassResult>;
|
|
89
133
|
|
|
90
134
|
type ParallelNetwork = "ethereum" | "base" | "avalanche" | "hyperevm";
|
|
@@ -108,4 +152,4 @@ declare function validateAddress(address: string): address is Address;
|
|
|
108
152
|
declare function toEip155Network(network: string): string;
|
|
109
153
|
declare function encodeBase64(obj: unknown): string;
|
|
110
154
|
|
|
111
|
-
export { type ChargeResponse, FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentGateResult, PaymentMiddlewareConfig, SETTLE_AMOUNT_HEADER, type SettleAmountResolution, SettleOptions, VerifiedPayment, X402ConfigError, type X402ErrorCode, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, isPriceRejection, parsePrice, resolveCharge, resolveSettleAmount, toEip155Network, validateAddress };
|
|
155
|
+
export { type ChargeResponse, FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, FacilitatorSettleOptions, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentConfirmation, PaymentGateResult, PaymentMiddlewareConfig, SETTLE_AMOUNT_HEADER, type SettleAmountResolution, SettleOptions, type SettlementFailure, type SettlementFailureBody, type SettlementOutcome, VerifiedPayment, VerifyResult, X402ConfigError, type X402ErrorCode, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, isPriceRejection, parsePrice, resolveCharge, resolveSettleAmount, settleVerified, settlementFailureResponse, toEip155Network, validateAddress };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { F as FacilitatorConfig, b as FacilitatorPaymentRequirements,
|
|
2
|
-
export {
|
|
1
|
+
import { F as FacilitatorConfig, b as FacilitatorPaymentRequirements, V as VerifyResult, c as FacilitatorSettleOptions, d as FacilitatorResponse, e as PaymentConfirmation, P as PaymentMiddlewareConfig, H as HTTPAdapter, f as PaymentGateResult, M as MiddlewareResult, g as PassResult, h as VerifiedPayment, S as SettleOptions } from './types-BkGQ2lPA.js';
|
|
2
|
+
export { i as MeterContext, j as MeterFunction, k as PaymentFailure, l as PaymentRequired, m as PaymentRequirements, n as PriceContext, o as PriceFunction, p as ResourceInfo, q as RouteConfig, R as RunHandlerOptions, a as RunHandlerResult } from './types-BkGQ2lPA.js';
|
|
3
3
|
import { Address } from 'viem';
|
|
4
4
|
|
|
5
5
|
declare class FacilitatorClient {
|
|
6
6
|
private readonly config;
|
|
7
7
|
constructor(config: FacilitatorConfig);
|
|
8
|
-
verify(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<
|
|
9
|
-
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements, options?:
|
|
8
|
+
verify(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements): Promise<VerifyResult>;
|
|
9
|
+
settle(paymentHeader: string, paymentRequirements: FacilitatorPaymentRequirements, options?: FacilitatorSettleOptions): Promise<FacilitatorResponse>;
|
|
10
10
|
pay(paymentHeader: string): Promise<FacilitatorResponse>;
|
|
11
11
|
private buildHeaders;
|
|
12
12
|
}
|
|
@@ -15,6 +15,8 @@ declare const X402_ERROR_CODES: {
|
|
|
15
15
|
readonly FACILITATOR_UNAVAILABLE: "FACILITATOR_UNAVAILABLE";
|
|
16
16
|
readonly FACILITATOR_INVALID_RESPONSE: "FACILITATOR_INVALID_RESPONSE";
|
|
17
17
|
readonly INVALID_PAYMENT: "INVALID_PAYMENT";
|
|
18
|
+
/** The facilitator did not answer a settle within the client's timeout: the payment may still settle. */
|
|
19
|
+
readonly SETTLE_TIMEOUT: "SETTLE_TIMEOUT";
|
|
18
20
|
};
|
|
19
21
|
type X402ErrorCode = (typeof X402_ERROR_CODES)[keyof typeof X402_ERROR_CODES];
|
|
20
22
|
declare class X402ConfigError extends Error {
|
|
@@ -22,7 +24,17 @@ declare class X402ConfigError extends Error {
|
|
|
22
24
|
}
|
|
23
25
|
declare class X402RuntimeError extends Error {
|
|
24
26
|
readonly code: string;
|
|
25
|
-
|
|
27
|
+
/** The facilitator's message for this code, when it answered with one. */
|
|
28
|
+
readonly detail?: string;
|
|
29
|
+
/** The facilitator's HTTP status, when the error comes from its answer. */
|
|
30
|
+
readonly status?: number;
|
|
31
|
+
/** The facilitator's `Retry-After` header, when it sent one. */
|
|
32
|
+
readonly retryAfter?: string;
|
|
33
|
+
constructor(code: string, extra?: {
|
|
34
|
+
detail?: string;
|
|
35
|
+
status?: number;
|
|
36
|
+
retryAfter?: string;
|
|
37
|
+
});
|
|
26
38
|
}
|
|
27
39
|
/**
|
|
28
40
|
* Thrown by a per-request price function to refuse a request before anything
|
|
@@ -85,6 +97,38 @@ interface ChargeResponse {
|
|
|
85
97
|
*/
|
|
86
98
|
declare function resolveCharge(verified: VerifiedPayment, settleHeader: string | undefined, response: ChargeResponse): Promise<SettleAmountResolution>;
|
|
87
99
|
declare function createPaymentGate(config: PaymentMiddlewareConfig): (adapter: GateAdapter) => Promise<PaymentGateResult>;
|
|
100
|
+
/** The 402 body a merchant answers when a verified payment could not be settled. */
|
|
101
|
+
interface SettlementFailureBody {
|
|
102
|
+
/** Kept for the clients that read it: the historical code. */
|
|
103
|
+
error: "PAYMENT_SETTLEMENT_FAILED";
|
|
104
|
+
/**
|
|
105
|
+
* The facilitator's code, or this SDK's: `SETTLE_TIMEOUT`,
|
|
106
|
+
* `FACILITATOR_UNAVAILABLE`, `FACILITATOR_INVALID_RESPONSE`,
|
|
107
|
+
* `AMOUNT_MISMATCH`, `UNKNOWN`.
|
|
108
|
+
*/
|
|
109
|
+
reason: string;
|
|
110
|
+
message: string;
|
|
111
|
+
}
|
|
112
|
+
/** A settlement that did not confirm the payment, and what the agent is told. */
|
|
113
|
+
interface SettlementFailure {
|
|
114
|
+
settled: false;
|
|
115
|
+
reason: string;
|
|
116
|
+
message: string;
|
|
117
|
+
/** The facilitator's `Retry-After`, when it sent one. */
|
|
118
|
+
retryAfter?: string;
|
|
119
|
+
}
|
|
120
|
+
type SettlementOutcome = {
|
|
121
|
+
settled: true;
|
|
122
|
+
confirmation: PaymentConfirmation;
|
|
123
|
+
} | SettlementFailure;
|
|
124
|
+
/**
|
|
125
|
+
* Settles a verified payment once the handler answered, and says what the
|
|
126
|
+
* agent must be told when it could not be. A metered settle must settle the
|
|
127
|
+
* amount this request asked for, whatever else the facilitator recorded.
|
|
128
|
+
*/
|
|
129
|
+
declare function settleVerified(verified: VerifiedPayment, options?: SettleOptions): Promise<SettlementOutcome>;
|
|
130
|
+
/** The 402 for a settlement that failed, with the facilitator's Retry-After when it gave one. */
|
|
131
|
+
declare function settlementFailureResponse(failure: SettlementFailure): MiddlewareResult;
|
|
88
132
|
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (adapter: HTTPAdapter) => Promise<MiddlewareResult | PassResult>;
|
|
89
133
|
|
|
90
134
|
type ParallelNetwork = "ethereum" | "base" | "avalanche" | "hyperevm";
|
|
@@ -108,4 +152,4 @@ declare function validateAddress(address: string): address is Address;
|
|
|
108
152
|
declare function toEip155Network(network: string): string;
|
|
109
153
|
declare function encodeBase64(obj: unknown): string;
|
|
110
154
|
|
|
111
|
-
export { type ChargeResponse, FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentGateResult, PaymentMiddlewareConfig, SETTLE_AMOUNT_HEADER, type SettleAmountResolution, SettleOptions, VerifiedPayment, X402ConfigError, type X402ErrorCode, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, isPriceRejection, parsePrice, resolveCharge, resolveSettleAmount, toEip155Network, validateAddress };
|
|
155
|
+
export { type ChargeResponse, FacilitatorClient, FacilitatorConfig, FacilitatorPaymentRequirements, FacilitatorResponse, FacilitatorSettleOptions, HTTPAdapter, MiddlewareResult, PARALLEL_TOKENS, type ParallelNetwork, PassResult, PaymentConfirmation, PaymentGateResult, PaymentMiddlewareConfig, SETTLE_AMOUNT_HEADER, type SettleAmountResolution, SettleOptions, type SettlementFailure, type SettlementFailureBody, type SettlementOutcome, VerifiedPayment, VerifyResult, X402ConfigError, type X402ErrorCode, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, isPriceRejection, parsePrice, resolveCharge, resolveSettleAmount, settleVerified, settlementFailureResponse, toEip155Network, validateAddress };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { FacilitatorClient, PARALLEL_TOKENS, SETTLE_AMOUNT_HEADER, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveCharge, resolveSettleAmount, toEip155Network, validateAddress } from './chunk-
|
|
2
|
-
export { X402ConfigError, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, isPriceRejection } from './chunk-
|
|
1
|
+
export { FacilitatorClient, PARALLEL_TOKENS, SETTLE_AMOUNT_HEADER, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveCharge, resolveSettleAmount, settleVerified, settlementFailureResponse, toEip155Network, validateAddress } from './chunk-SA5ULSG2.js';
|
|
2
|
+
export { X402ConfigError, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, isPriceRejection } from './chunk-QAECRSWF.js';
|
package/dist/llm.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkKZZY5LS6_cjs = require('./chunk-KZZY5LS6.cjs');
|
|
4
4
|
|
|
5
5
|
// src/llm/estimate.ts
|
|
6
6
|
function estimateTokens(text) {
|
|
@@ -71,7 +71,7 @@ function lookup(table, model) {
|
|
|
71
71
|
function staticPrices(table) {
|
|
72
72
|
for (const [model, price] of Object.entries(table)) {
|
|
73
73
|
if (!isValidPrice(price)) {
|
|
74
|
-
throw new
|
|
74
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
75
75
|
`Invalid price for model "${model}": expected { input, output } in USD per million tokens`
|
|
76
76
|
);
|
|
77
77
|
}
|
|
@@ -106,13 +106,13 @@ function shortName(value) {
|
|
|
106
106
|
function parseDuration(value) {
|
|
107
107
|
if (typeof value === "number") {
|
|
108
108
|
if (!Number.isFinite(value) || value < 0) {
|
|
109
|
-
throw new
|
|
109
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(`Invalid duration: ${value}`);
|
|
110
110
|
}
|
|
111
111
|
return value;
|
|
112
112
|
}
|
|
113
113
|
const match = value.trim().match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d)$/i);
|
|
114
114
|
if (!match) {
|
|
115
|
-
throw new
|
|
115
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
116
116
|
`Invalid duration "${value}": expected a number of milliseconds or a value like "24h", "30m", "10s"`
|
|
117
117
|
);
|
|
118
118
|
}
|
|
@@ -180,7 +180,7 @@ function openRouterPrices(options = {}) {
|
|
|
180
180
|
if (canRetry()) await load().catch(() => {
|
|
181
181
|
});
|
|
182
182
|
if (!table) {
|
|
183
|
-
throw new
|
|
183
|
+
throw new chunkKZZY5LS6_cjs.X402PriceRejectedError(
|
|
184
184
|
"PRICING_UNAVAILABLE",
|
|
185
185
|
"Model prices are not available right now, retry shortly",
|
|
186
186
|
503
|
|
@@ -197,7 +197,7 @@ function openRouterPrices(options = {}) {
|
|
|
197
197
|
});
|
|
198
198
|
}
|
|
199
199
|
if (inflight) {
|
|
200
|
-
throw new
|
|
200
|
+
throw new chunkKZZY5LS6_cjs.X402PriceRejectedError(
|
|
201
201
|
"UNKNOWN_MODEL",
|
|
202
202
|
`No price known for model "${shortName(model)}" yet. The price list is being refreshed, retry in a few seconds.`
|
|
203
203
|
);
|
|
@@ -210,7 +210,7 @@ function openRouterPrices(options = {}) {
|
|
|
210
210
|
// src/llm/cap.ts
|
|
211
211
|
var DEFAULT_MARGIN = 0;
|
|
212
212
|
function reject(code, message) {
|
|
213
|
-
throw new
|
|
213
|
+
throw new chunkKZZY5LS6_cjs.X402PriceRejectedError(code, message);
|
|
214
214
|
}
|
|
215
215
|
function isTextOnly(modalities) {
|
|
216
216
|
return Array.isArray(modalities) && modalities.every((modality) => modality === "text");
|
|
@@ -222,17 +222,17 @@ function chatCompletionCap(options) {
|
|
|
222
222
|
const allowed = options.models ? new Set(options.models) : void 0;
|
|
223
223
|
const estimate = options.estimateInputTokens ?? estimateInputTokens;
|
|
224
224
|
if (positiveInteger(maxOutputTokens) === void 0) {
|
|
225
|
-
throw new
|
|
225
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
226
226
|
`chatCompletionCap: maxOutputTokens must be a positive integer, got ${maxOutputTokens}`
|
|
227
227
|
);
|
|
228
228
|
}
|
|
229
229
|
if (!Number.isFinite(margin) || margin < 0) {
|
|
230
|
-
throw new
|
|
230
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
231
231
|
`chatCompletionCap: margin must be a non-negative number, got ${margin}`
|
|
232
232
|
);
|
|
233
233
|
}
|
|
234
234
|
if (allowed?.size === 0) {
|
|
235
|
-
throw new
|
|
235
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError("chatCompletionCap: models must not be empty");
|
|
236
236
|
}
|
|
237
237
|
return async (ctx) => {
|
|
238
238
|
const raw = ctx.body;
|
|
@@ -295,7 +295,7 @@ function chatCompletionCap(options) {
|
|
|
295
295
|
reject("UNKNOWN_MODEL", `No price known for model "${shortName(model)}"`);
|
|
296
296
|
}
|
|
297
297
|
if (!isValidPrice(price)) {
|
|
298
|
-
throw new
|
|
298
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
299
299
|
`chatCompletionCap: the price source returned an invalid price for model "${shortName(model)}"`
|
|
300
300
|
);
|
|
301
301
|
}
|
|
@@ -343,13 +343,13 @@ var failure = (status, error, message) => ({
|
|
|
343
343
|
});
|
|
344
344
|
async function forwardChatCompletion(body, options) {
|
|
345
345
|
if (typeof options.baseUrl !== "string" || !URL.canParse(options.baseUrl)) {
|
|
346
|
-
throw new
|
|
346
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
347
347
|
`forwardChatCompletion: baseUrl must be a URL, got ${JSON.stringify(options.baseUrl)}`
|
|
348
348
|
);
|
|
349
349
|
}
|
|
350
350
|
const maxOutputTokens = options.maxOutputTokens ?? DEFAULT_MAX_OUTPUT_TOKENS;
|
|
351
351
|
if (positiveInteger(maxOutputTokens) === void 0) {
|
|
352
|
-
throw new
|
|
352
|
+
throw new chunkKZZY5LS6_cjs.X402ConfigError(
|
|
353
353
|
`forwardChatCompletion: maxOutputTokens must be a positive integer, got ${maxOutputTokens}`
|
|
354
354
|
);
|
|
355
355
|
}
|
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 chunkNA7245L5_cjs = require('./chunk-NA7245L5.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 (!chunkNA7245L5_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 = chunkNA7245L5_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 chunkNA7245L5_cjs.raceTimeout(
|
|
58
56
|
handler(req),
|
|
59
57
|
gateResult.handlerTimeoutMs
|
|
60
58
|
);
|
|
61
|
-
if (outcome ===
|
|
59
|
+
if (outcome === chunkNA7245L5_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(chunkNA7245L5_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(chunkNA7245L5_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 chunkNA7245L5_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: () => chunkNA7245L5_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(chunkNA7245L5_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 chunkNA7245L5_cjs.settleVerified(gateResult, settleOptions);
|
|
97
|
+
if (outcome.settled) {
|
|
98
|
+
const headers = new Headers(response.headers);
|
|
99
|
+
headers.set("payment-response", chunkNA7245L5_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(chunkNA7245L5_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 {
|