@t402/core 2.0.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 +293 -0
- package/dist/cjs/client/index.d.ts +103 -0
- package/dist/cjs/client/index.js +510 -0
- package/dist/cjs/client/index.js.map +1 -0
- package/dist/cjs/facilitator/index.d.ts +192 -0
- package/dist/cjs/facilitator/index.js +398 -0
- package/dist/cjs/facilitator/index.js.map +1 -0
- package/dist/cjs/http/index.d.ts +52 -0
- package/dist/cjs/http/index.js +763 -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/mechanisms-CmrqNl1M.d.ts +238 -0
- package/dist/cjs/mechanisms-DsJn3ZiM.d.ts +238 -0
- package/dist/cjs/server/index.d.ts +2 -0
- package/dist/cjs/server/index.js +1241 -0
- package/dist/cjs/server/index.js.map +1 -0
- package/dist/cjs/t402HTTPClient-m6cjzTek.d.ts +243 -0
- package/dist/cjs/t402HTTPResourceServer-B-xmYMwj.d.ts +719 -0
- package/dist/cjs/t402HTTPResourceServer-Bcfxp2UO.d.ts +719 -0
- package/dist/cjs/types/index.d.ts +1 -0
- package/dist/cjs/types/index.js +19 -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 +48 -0
- package/dist/cjs/utils/index.js +108 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/esm/chunk-3IUBYRYG.mjs +78 -0
- package/dist/esm/chunk-3IUBYRYG.mjs.map +1 -0
- package/dist/esm/chunk-3VTYR43U.mjs +7 -0
- package/dist/esm/chunk-3VTYR43U.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-D5DYKKCZ.mjs +722 -0
- package/dist/esm/chunk-D5DYKKCZ.mjs.map +1 -0
- package/dist/esm/client/index.d.mts +103 -0
- package/dist/esm/client/index.mjs +356 -0
- package/dist/esm/client/index.mjs.map +1 -0
- package/dist/esm/facilitator/index.d.mts +192 -0
- package/dist/esm/facilitator/index.mjs +373 -0
- package/dist/esm/facilitator/index.mjs.map +1 -0
- package/dist/esm/http/index.d.mts +52 -0
- package/dist/esm/http/index.mjs +28 -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/mechanisms-CmrqNl1M.d.mts +238 -0
- package/dist/esm/server/index.d.mts +2 -0
- package/dist/esm/server/index.mjs +562 -0
- package/dist/esm/server/index.mjs.map +1 -0
- package/dist/esm/t402HTTPClient-C285YGCp.d.mts +243 -0
- package/dist/esm/t402HTTPResourceServer-k_l3d8ua.d.mts +719 -0
- package/dist/esm/types/index.d.mts +1 -0
- package/dist/esm/types/index.mjs +1 -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 +48 -0
- package/dist/esm/utils/index.mjs +20 -0
- package/dist/esm/utils/index.mjs.map +1 -0
- package/package.json +129 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { c as PaymentRequired, P as PaymentRequirements, a as PaymentPayload, N as Network, d as SchemeNetworkClient, S as SettleResponse } from './mechanisms-CmrqNl1M.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Client Hook Context Interfaces
|
|
5
|
+
*/
|
|
6
|
+
interface PaymentCreationContext {
|
|
7
|
+
paymentRequired: PaymentRequired;
|
|
8
|
+
selectedRequirements: PaymentRequirements;
|
|
9
|
+
}
|
|
10
|
+
interface PaymentCreatedContext extends PaymentCreationContext {
|
|
11
|
+
paymentPayload: PaymentPayload;
|
|
12
|
+
}
|
|
13
|
+
interface PaymentCreationFailureContext extends PaymentCreationContext {
|
|
14
|
+
error: Error;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Client Hook Type Definitions
|
|
18
|
+
*/
|
|
19
|
+
type BeforePaymentCreationHook = (context: PaymentCreationContext) => Promise<void | {
|
|
20
|
+
abort: true;
|
|
21
|
+
reason: string;
|
|
22
|
+
}>;
|
|
23
|
+
type AfterPaymentCreationHook = (context: PaymentCreatedContext) => Promise<void>;
|
|
24
|
+
type OnPaymentCreationFailureHook = (context: PaymentCreationFailureContext) => Promise<void | {
|
|
25
|
+
recovered: true;
|
|
26
|
+
payload: PaymentPayload;
|
|
27
|
+
}>;
|
|
28
|
+
type SelectPaymentRequirements = (t402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements;
|
|
29
|
+
/**
|
|
30
|
+
* A policy function that filters or transforms payment requirements.
|
|
31
|
+
* Policies are applied in order before the selector chooses the final option.
|
|
32
|
+
*
|
|
33
|
+
* @param t402Version - The t402 protocol version
|
|
34
|
+
* @param paymentRequirements - Array of payment requirements to filter/transform
|
|
35
|
+
* @returns Filtered array of payment requirements
|
|
36
|
+
*/
|
|
37
|
+
type PaymentPolicy = (t402Version: number, paymentRequirements: PaymentRequirements[]) => PaymentRequirements[];
|
|
38
|
+
/**
|
|
39
|
+
* Configuration for registering a payment scheme with a specific network
|
|
40
|
+
*/
|
|
41
|
+
interface SchemeRegistration {
|
|
42
|
+
/**
|
|
43
|
+
* The network identifier (e.g., 'eip155:8453', 'solana:mainnet')
|
|
44
|
+
*/
|
|
45
|
+
network: Network;
|
|
46
|
+
/**
|
|
47
|
+
* The scheme client implementation for this network
|
|
48
|
+
*/
|
|
49
|
+
client: SchemeNetworkClient;
|
|
50
|
+
/**
|
|
51
|
+
* The t402 protocol version to use for this scheme
|
|
52
|
+
*
|
|
53
|
+
* @default 2
|
|
54
|
+
*/
|
|
55
|
+
t402Version?: number;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Configuration options for the fetch wrapper
|
|
59
|
+
*/
|
|
60
|
+
interface t402ClientConfig {
|
|
61
|
+
/**
|
|
62
|
+
* Array of scheme registrations defining which payment methods are supported
|
|
63
|
+
*/
|
|
64
|
+
schemes: SchemeRegistration[];
|
|
65
|
+
/**
|
|
66
|
+
* Policies to apply to the client
|
|
67
|
+
*/
|
|
68
|
+
policies?: PaymentPolicy[];
|
|
69
|
+
/**
|
|
70
|
+
* Custom payment requirements selector function
|
|
71
|
+
* If not provided, uses the default selector (first available option)
|
|
72
|
+
*/
|
|
73
|
+
paymentRequirementsSelector?: SelectPaymentRequirements;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Core client for managing t402 payment schemes and creating payment payloads.
|
|
77
|
+
*
|
|
78
|
+
* Handles registration of payment schemes, policy-based filtering of payment requirements,
|
|
79
|
+
* and creation of payment payloads based on server requirements.
|
|
80
|
+
*/
|
|
81
|
+
declare class t402Client {
|
|
82
|
+
private readonly paymentRequirementsSelector;
|
|
83
|
+
private readonly registeredClientSchemes;
|
|
84
|
+
private readonly policies;
|
|
85
|
+
private beforePaymentCreationHooks;
|
|
86
|
+
private afterPaymentCreationHooks;
|
|
87
|
+
private onPaymentCreationFailureHooks;
|
|
88
|
+
/**
|
|
89
|
+
* Creates a new t402Client instance.
|
|
90
|
+
*
|
|
91
|
+
* @param paymentRequirementsSelector - Function to select payment requirements from available options
|
|
92
|
+
*/
|
|
93
|
+
constructor(paymentRequirementsSelector?: SelectPaymentRequirements);
|
|
94
|
+
/**
|
|
95
|
+
* Creates a new t402Client instance from a configuration object.
|
|
96
|
+
*
|
|
97
|
+
* @param config - The client configuration including schemes, policies, and payment requirements selector
|
|
98
|
+
* @returns A configured t402Client instance
|
|
99
|
+
*/
|
|
100
|
+
static fromConfig(config: t402ClientConfig): t402Client;
|
|
101
|
+
/**
|
|
102
|
+
* Registers a scheme client for the current t402 version.
|
|
103
|
+
*
|
|
104
|
+
* @param network - The network to register the client for
|
|
105
|
+
* @param client - The scheme network client to register
|
|
106
|
+
* @returns The t402Client instance for chaining
|
|
107
|
+
*/
|
|
108
|
+
register(network: Network, client: SchemeNetworkClient): t402Client;
|
|
109
|
+
/**
|
|
110
|
+
* Registers a scheme client for t402 version 1.
|
|
111
|
+
*
|
|
112
|
+
* @param network - The v1 network identifier (e.g., 'base-sepolia', 'solana-devnet')
|
|
113
|
+
* @param client - The scheme network client to register
|
|
114
|
+
* @returns The t402Client instance for chaining
|
|
115
|
+
*/
|
|
116
|
+
registerV1(network: string, client: SchemeNetworkClient): t402Client;
|
|
117
|
+
/**
|
|
118
|
+
* Registers a policy to filter or transform payment requirements.
|
|
119
|
+
*
|
|
120
|
+
* Policies are applied in order after filtering by registered schemes
|
|
121
|
+
* and before the selector chooses the final payment requirement.
|
|
122
|
+
*
|
|
123
|
+
* @param policy - Function to filter/transform payment requirements
|
|
124
|
+
* @returns The t402Client instance for chaining
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* // Prefer cheaper options
|
|
129
|
+
* client.registerPolicy((version, reqs) =>
|
|
130
|
+
* reqs.filter(r => BigInt(r.value) < BigInt('1000000'))
|
|
131
|
+
* );
|
|
132
|
+
*
|
|
133
|
+
* // Prefer specific networks
|
|
134
|
+
* client.registerPolicy((version, reqs) =>
|
|
135
|
+
* reqs.filter(r => r.network.startsWith('eip155:'))
|
|
136
|
+
* );
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
registerPolicy(policy: PaymentPolicy): t402Client;
|
|
140
|
+
/**
|
|
141
|
+
* Register a hook to execute before payment payload creation.
|
|
142
|
+
* Can abort creation by returning { abort: true, reason: string }
|
|
143
|
+
*
|
|
144
|
+
* @param hook - The hook function to register
|
|
145
|
+
* @returns The t402Client instance for chaining
|
|
146
|
+
*/
|
|
147
|
+
onBeforePaymentCreation(hook: BeforePaymentCreationHook): t402Client;
|
|
148
|
+
/**
|
|
149
|
+
* Register a hook to execute after successful payment payload creation.
|
|
150
|
+
*
|
|
151
|
+
* @param hook - The hook function to register
|
|
152
|
+
* @returns The t402Client instance for chaining
|
|
153
|
+
*/
|
|
154
|
+
onAfterPaymentCreation(hook: AfterPaymentCreationHook): t402Client;
|
|
155
|
+
/**
|
|
156
|
+
* Register a hook to execute when payment payload creation fails.
|
|
157
|
+
* Can recover from failure by returning { recovered: true, payload: PaymentPayload }
|
|
158
|
+
*
|
|
159
|
+
* @param hook - The hook function to register
|
|
160
|
+
* @returns The t402Client instance for chaining
|
|
161
|
+
*/
|
|
162
|
+
onPaymentCreationFailure(hook: OnPaymentCreationFailureHook): t402Client;
|
|
163
|
+
/**
|
|
164
|
+
* Creates a payment payload based on a PaymentRequired response.
|
|
165
|
+
*
|
|
166
|
+
* Automatically extracts t402Version, resource, and extensions from the PaymentRequired
|
|
167
|
+
* response and constructs a complete PaymentPayload with the accepted requirements.
|
|
168
|
+
*
|
|
169
|
+
* @param paymentRequired - The PaymentRequired response from the server
|
|
170
|
+
* @returns Promise resolving to the complete payment payload
|
|
171
|
+
*/
|
|
172
|
+
createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload>;
|
|
173
|
+
/**
|
|
174
|
+
* Selects appropriate payment requirements based on registered clients and policies.
|
|
175
|
+
*
|
|
176
|
+
* Selection process:
|
|
177
|
+
* 1. Filter by registered schemes (network + scheme support)
|
|
178
|
+
* 2. Apply all registered policies in order
|
|
179
|
+
* 3. Use selector to choose final requirement
|
|
180
|
+
*
|
|
181
|
+
* @param t402Version - The t402 protocol version
|
|
182
|
+
* @param paymentRequirements - Array of available payment requirements
|
|
183
|
+
* @returns The selected payment requirements
|
|
184
|
+
*/
|
|
185
|
+
private selectPaymentRequirements;
|
|
186
|
+
/**
|
|
187
|
+
* Internal method to register a scheme client.
|
|
188
|
+
*
|
|
189
|
+
* @param t402Version - The t402 protocol version
|
|
190
|
+
* @param network - The network to register the client for
|
|
191
|
+
* @param client - The scheme network client to register
|
|
192
|
+
* @returns The t402Client instance for chaining
|
|
193
|
+
*/
|
|
194
|
+
private _registerScheme;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* HTTP-specific client for handling t402 payment protocol over HTTP.
|
|
199
|
+
*
|
|
200
|
+
* Wraps a t402Client to provide HTTP-specific encoding/decoding functionality
|
|
201
|
+
* for payment headers and responses while maintaining the builder pattern.
|
|
202
|
+
*/
|
|
203
|
+
declare class t402HTTPClient {
|
|
204
|
+
private readonly client;
|
|
205
|
+
/**
|
|
206
|
+
* Creates a new t402HTTPClient instance.
|
|
207
|
+
*
|
|
208
|
+
* @param client - The underlying t402Client for payment logic
|
|
209
|
+
*/
|
|
210
|
+
constructor(client: t402Client);
|
|
211
|
+
/**
|
|
212
|
+
* Encodes a payment payload into appropriate HTTP headers based on version.
|
|
213
|
+
*
|
|
214
|
+
* @param paymentPayload - The payment payload to encode
|
|
215
|
+
* @returns HTTP headers containing the encoded payment signature
|
|
216
|
+
*/
|
|
217
|
+
encodePaymentSignatureHeader(paymentPayload: PaymentPayload): Record<string, string>;
|
|
218
|
+
/**
|
|
219
|
+
* Extracts payment required information from HTTP response.
|
|
220
|
+
*
|
|
221
|
+
* @param getHeader - Function to retrieve header value by name (case-insensitive)
|
|
222
|
+
* @param body - Optional response body for v1 compatibility
|
|
223
|
+
* @returns The payment required object
|
|
224
|
+
*/
|
|
225
|
+
getPaymentRequiredResponse(getHeader: (name: string) => string | null | undefined, body?: unknown): PaymentRequired;
|
|
226
|
+
/**
|
|
227
|
+
* Extracts payment settlement response from HTTP headers.
|
|
228
|
+
*
|
|
229
|
+
* @param getHeader - Function to retrieve header value by name (case-insensitive)
|
|
230
|
+
* @returns The settlement response object
|
|
231
|
+
*/
|
|
232
|
+
getPaymentSettleResponse(getHeader: (name: string) => string | null | undefined): SettleResponse;
|
|
233
|
+
/**
|
|
234
|
+
* Creates a payment payload for the given payment requirements.
|
|
235
|
+
* Delegates to the underlying t402Client.
|
|
236
|
+
*
|
|
237
|
+
* @param paymentRequired - The payment required response from the server
|
|
238
|
+
* @returns Promise resolving to the payment payload
|
|
239
|
+
*/
|
|
240
|
+
createPaymentPayload(paymentRequired: PaymentRequired): Promise<PaymentPayload>;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
export { type AfterPaymentCreationHook as A, type BeforePaymentCreationHook as B, type OnPaymentCreationFailureHook as O, type PaymentCreationContext as P, type SelectPaymentRequirements as S, type PaymentCreatedContext as a, type PaymentCreationFailureContext as b, type PaymentPolicy as c, type SchemeRegistration as d, t402Client as e, t402HTTPClient as f, type t402ClientConfig as t };
|