@x402/extensions 2.2.0 → 2.3.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.
Files changed (33) hide show
  1. package/README.md +322 -93
  2. package/dist/cjs/bazaar/index.d.ts +3 -562
  3. package/dist/cjs/bazaar/index.js +12 -0
  4. package/dist/cjs/bazaar/index.js.map +1 -1
  5. package/dist/cjs/index-DvDlinmy.d.ts +575 -0
  6. package/dist/cjs/index.d.ts +4 -1
  7. package/dist/cjs/index.js +1008 -2
  8. package/dist/cjs/index.js.map +1 -1
  9. package/dist/cjs/payment-identifier/index.d.ts +345 -0
  10. package/dist/cjs/payment-identifier/index.js +285 -0
  11. package/dist/cjs/payment-identifier/index.js.map +1 -0
  12. package/dist/cjs/sign-in-with-x/index.d.ts +1054 -1
  13. package/dist/cjs/sign-in-with-x/index.js +766 -0
  14. package/dist/cjs/sign-in-with-x/index.js.map +1 -1
  15. package/dist/esm/bazaar/index.d.mts +3 -562
  16. package/dist/esm/bazaar/index.mjs +1 -1
  17. package/dist/esm/chunk-73HCOE6N.mjs +233 -0
  18. package/dist/esm/chunk-73HCOE6N.mjs.map +1 -0
  19. package/dist/esm/{chunk-WB72GLC2.mjs → chunk-DFJ4ZQFO.mjs} +13 -1
  20. package/dist/esm/chunk-DFJ4ZQFO.mjs.map +1 -0
  21. package/dist/esm/chunk-E3F2XHTI.mjs +719 -0
  22. package/dist/esm/chunk-E3F2XHTI.mjs.map +1 -0
  23. package/dist/esm/index-DvDlinmy.d.mts +575 -0
  24. package/dist/esm/index.d.mts +4 -1
  25. package/dist/esm/index.mjs +102 -3
  26. package/dist/esm/payment-identifier/index.d.mts +345 -0
  27. package/dist/esm/payment-identifier/index.mjs +39 -0
  28. package/dist/esm/sign-in-with-x/index.d.mts +1054 -1
  29. package/dist/esm/sign-in-with-x/index.mjs +66 -1
  30. package/package.json +16 -2
  31. package/dist/esm/chunk-MKFJ5AA3.mjs +0 -1
  32. package/dist/esm/chunk-WB72GLC2.mjs.map +0 -1
  33. /package/dist/esm/{chunk-MKFJ5AA3.mjs.map → payment-identifier/index.mjs.map} +0 -0
@@ -0,0 +1,345 @@
1
+ import { ResourceServerExtension, PaymentPayload } from '@x402/core/types';
2
+
3
+ /**
4
+ * Type definitions for the Payment-Identifier Extension
5
+ *
6
+ * Enables clients to provide an idempotency key that resource servers
7
+ * can use for deduplication of payment requests.
8
+ */
9
+ /**
10
+ * Extension identifier constant for the payment-identifier extension
11
+ */
12
+ declare const PAYMENT_IDENTIFIER = "payment-identifier";
13
+ /**
14
+ * Minimum length for payment identifier
15
+ */
16
+ declare const PAYMENT_ID_MIN_LENGTH = 16;
17
+ /**
18
+ * Maximum length for payment identifier
19
+ */
20
+ declare const PAYMENT_ID_MAX_LENGTH = 128;
21
+ /**
22
+ * Pattern for valid payment identifier characters (alphanumeric, hyphens, underscores)
23
+ */
24
+ declare const PAYMENT_ID_PATTERN: RegExp;
25
+ /**
26
+ * Payment identifier info containing the required flag and client-provided ID
27
+ */
28
+ interface PaymentIdentifierInfo {
29
+ /**
30
+ * Whether the server requires clients to include a payment identifier.
31
+ * When true, clients must provide an `id` or receive a 400 Bad Request.
32
+ */
33
+ required: boolean;
34
+ /**
35
+ * Client-provided unique identifier for idempotency.
36
+ * Must be 16-128 characters, alphanumeric with hyphens and underscores allowed.
37
+ */
38
+ id?: string;
39
+ }
40
+ /**
41
+ * Payment identifier extension with info and schema.
42
+ *
43
+ * Used both for server-side declarations (info without id) and
44
+ * client-side payloads (info with id).
45
+ */
46
+ interface PaymentIdentifierExtension {
47
+ /**
48
+ * The payment identifier info.
49
+ * Server declarations have required only, clients add the id.
50
+ */
51
+ info: PaymentIdentifierInfo;
52
+ /**
53
+ * JSON Schema validating the info structure
54
+ */
55
+ schema: PaymentIdentifierSchema;
56
+ }
57
+ /**
58
+ * JSON Schema type for the payment-identifier extension
59
+ */
60
+ interface PaymentIdentifierSchema {
61
+ $schema: "https://json-schema.org/draft/2020-12/schema";
62
+ type: "object";
63
+ properties: {
64
+ required: {
65
+ type: "boolean";
66
+ };
67
+ id: {
68
+ type: "string";
69
+ minLength: number;
70
+ maxLength: number;
71
+ pattern: string;
72
+ };
73
+ };
74
+ required: ["required"];
75
+ }
76
+
77
+ /**
78
+ * JSON Schema definitions for the Payment-Identifier Extension
79
+ */
80
+
81
+ /**
82
+ * JSON Schema for validating payment identifier info.
83
+ * Compliant with JSON Schema Draft 2020-12.
84
+ */
85
+ declare const paymentIdentifierSchema: PaymentIdentifierSchema;
86
+
87
+ /**
88
+ * Utility functions for the Payment-Identifier Extension
89
+ */
90
+ /**
91
+ * Generates a unique payment identifier.
92
+ *
93
+ * @param prefix - Optional prefix for the ID (e.g., "pay_"). Defaults to "pay_".
94
+ * @returns A unique payment identifier string
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * // With default prefix
99
+ * const id = generatePaymentId(); // "pay_7d5d747be160e280504c099d984bcfe0"
100
+ *
101
+ * // With custom prefix
102
+ * const id = generatePaymentId("txn_"); // "txn_7d5d747be160e280504c099d984bcfe0"
103
+ *
104
+ * // Without prefix
105
+ * const id = generatePaymentId(""); // "7d5d747be160e280504c099d984bcfe0"
106
+ * ```
107
+ */
108
+ declare function generatePaymentId(prefix?: string): string;
109
+ /**
110
+ * Validates that a payment ID meets the format requirements.
111
+ *
112
+ * @param id - The payment ID to validate
113
+ * @returns True if the ID is valid, false otherwise
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * isValidPaymentId("pay_7d5d747be160e280"); // true (exactly 16 chars after prefix removal check)
118
+ * isValidPaymentId("abc"); // false (too short)
119
+ * isValidPaymentId("pay_abc!@#"); // false (invalid characters)
120
+ * ```
121
+ */
122
+ declare function isValidPaymentId(id: string): boolean;
123
+
124
+ /**
125
+ * Client-side utilities for the Payment-Identifier Extension
126
+ */
127
+ /**
128
+ * Appends a payment identifier to the extensions object if the server declared support.
129
+ *
130
+ * This function reads the server's `payment-identifier` declaration from the extensions,
131
+ * and appends the client's ID to it. If the extension is not present (server didn't declare it),
132
+ * the extensions are returned unchanged.
133
+ *
134
+ * @param extensions - The extensions object from PaymentRequired (will be modified in place)
135
+ * @param id - Optional custom payment ID. If not provided, a new ID will be generated.
136
+ * @returns The modified extensions object (same reference as input)
137
+ * @throws Error if the provided ID is invalid
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * import { appendPaymentIdentifierToExtensions } from '@x402/extensions/payment-identifier';
142
+ *
143
+ * // Get extensions from server's PaymentRequired response
144
+ * const extensions = paymentRequired.extensions ?? {};
145
+ *
146
+ * // Append a generated ID (only if server declared payment-identifier)
147
+ * appendPaymentIdentifierToExtensions(extensions);
148
+ *
149
+ * // Or use a custom ID
150
+ * appendPaymentIdentifierToExtensions(extensions, "pay_my_custom_id_12345");
151
+ *
152
+ * // Include in PaymentPayload
153
+ * const paymentPayload = {
154
+ * x402Version: 2,
155
+ * resource: paymentRequired.resource,
156
+ * accepted: selectedPaymentOption,
157
+ * payload: { ... },
158
+ * extensions
159
+ * };
160
+ * ```
161
+ */
162
+ declare function appendPaymentIdentifierToExtensions(extensions: Record<string, unknown>, id?: string): Record<string, unknown>;
163
+
164
+ /**
165
+ * Resource Server utilities for the Payment-Identifier Extension
166
+ */
167
+
168
+ /**
169
+ * Declares the payment-identifier extension for inclusion in PaymentRequired.extensions.
170
+ *
171
+ * Resource servers call this function to advertise support for payment identifiers.
172
+ * The declaration indicates whether a payment identifier is required and includes
173
+ * the schema that clients must follow.
174
+ *
175
+ * @param required - Whether clients must provide a payment identifier. Defaults to false.
176
+ * @returns A PaymentIdentifierExtension object ready for PaymentRequired.extensions
177
+ *
178
+ * @example
179
+ * ```typescript
180
+ * import { declarePaymentIdentifierExtension, PAYMENT_IDENTIFIER } from '@x402/extensions/payment-identifier';
181
+ *
182
+ * // Include in PaymentRequired response (optional identifier)
183
+ * const paymentRequired = {
184
+ * x402Version: 2,
185
+ * resource: { ... },
186
+ * accepts: [ ... ],
187
+ * extensions: {
188
+ * [PAYMENT_IDENTIFIER]: declarePaymentIdentifierExtension()
189
+ * }
190
+ * };
191
+ *
192
+ * // Require payment identifier
193
+ * const paymentRequiredStrict = {
194
+ * x402Version: 2,
195
+ * resource: { ... },
196
+ * accepts: [ ... ],
197
+ * extensions: {
198
+ * [PAYMENT_IDENTIFIER]: declarePaymentIdentifierExtension(true)
199
+ * }
200
+ * };
201
+ * ```
202
+ */
203
+ declare function declarePaymentIdentifierExtension(required?: boolean): PaymentIdentifierExtension;
204
+ /**
205
+ * ResourceServerExtension implementation for payment-identifier.
206
+ *
207
+ * This extension doesn't require any enrichment hooks since the declaration
208
+ * is static. It's provided for consistency with other extensions and for
209
+ * potential future use with the extension registration system.
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * import { paymentIdentifierResourceServerExtension } from '@x402/extensions/payment-identifier';
214
+ *
215
+ * resourceServer.registerExtension(paymentIdentifierResourceServerExtension);
216
+ * ```
217
+ */
218
+ declare const paymentIdentifierResourceServerExtension: ResourceServerExtension;
219
+
220
+ /**
221
+ * Validation and extraction utilities for the Payment-Identifier Extension
222
+ */
223
+
224
+ /**
225
+ * Type guard to check if an object is a valid payment-identifier extension structure.
226
+ *
227
+ * This checks for the basic structure (info object with required boolean),
228
+ * but does not validate the id format if present.
229
+ *
230
+ * @param extension - The object to check
231
+ * @returns True if the object has the expected payment-identifier extension structure
232
+ *
233
+ * @example
234
+ * ```typescript
235
+ * if (isPaymentIdentifierExtension(extensions["payment-identifier"])) {
236
+ * // TypeScript knows this is PaymentIdentifierExtension
237
+ * console.log(extension.info.required);
238
+ * }
239
+ * ```
240
+ */
241
+ declare function isPaymentIdentifierExtension(extension: unknown): extension is PaymentIdentifierExtension;
242
+ /**
243
+ * Result of payment identifier validation
244
+ */
245
+ interface PaymentIdentifierValidationResult {
246
+ /**
247
+ * Whether the payment identifier is valid
248
+ */
249
+ valid: boolean;
250
+ /**
251
+ * Error messages if validation failed
252
+ */
253
+ errors?: string[];
254
+ }
255
+ /**
256
+ * Validates a payment-identifier extension object.
257
+ *
258
+ * Checks both the structure (using JSON Schema) and the ID format.
259
+ *
260
+ * @param extension - The extension object to validate
261
+ * @returns Validation result with errors if invalid
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * const result = validatePaymentIdentifier(paymentPayload.extensions?.["payment-identifier"]);
266
+ * if (!result.valid) {
267
+ * console.error("Invalid payment identifier:", result.errors);
268
+ * }
269
+ * ```
270
+ */
271
+ declare function validatePaymentIdentifier(extension: unknown): PaymentIdentifierValidationResult;
272
+ /**
273
+ * Extracts the payment identifier from a PaymentPayload.
274
+ *
275
+ * @param paymentPayload - The payment payload to extract from
276
+ * @param validate - Whether to validate the ID before returning (default: true)
277
+ * @returns The payment ID string, or null if not present or invalid
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * const id = extractPaymentIdentifier(paymentPayload);
282
+ * if (id) {
283
+ * // Use for idempotency lookup
284
+ * const cached = await idempotencyStore.get(id);
285
+ * }
286
+ * ```
287
+ */
288
+ declare function extractPaymentIdentifier(paymentPayload: PaymentPayload, validate?: boolean): string | null;
289
+ /**
290
+ * Extracts and validates the payment identifier from a PaymentPayload.
291
+ *
292
+ * @param paymentPayload - The payment payload to extract from
293
+ * @returns Object with the ID and validation result
294
+ *
295
+ * @example
296
+ * ```typescript
297
+ * const { id, validation } = extractAndValidatePaymentIdentifier(paymentPayload);
298
+ * if (!validation.valid) {
299
+ * return res.status(400).json({ error: validation.errors });
300
+ * }
301
+ * if (id) {
302
+ * // Use for idempotency
303
+ * }
304
+ * ```
305
+ */
306
+ declare function extractAndValidatePaymentIdentifier(paymentPayload: PaymentPayload): {
307
+ id: string | null;
308
+ validation: PaymentIdentifierValidationResult;
309
+ };
310
+ /**
311
+ * Checks if a PaymentPayload contains a payment-identifier extension.
312
+ *
313
+ * @param paymentPayload - The payment payload to check
314
+ * @returns True if the extension is present
315
+ */
316
+ declare function hasPaymentIdentifier(paymentPayload: PaymentPayload): boolean;
317
+ /**
318
+ * Checks if the server requires a payment identifier based on the extension info.
319
+ *
320
+ * @param extension - The payment-identifier extension from PaymentRequired or PaymentPayload
321
+ * @returns True if the server requires a payment identifier
322
+ */
323
+ declare function isPaymentIdentifierRequired(extension: unknown): boolean;
324
+ /**
325
+ * Validates that a payment identifier is provided when required.
326
+ *
327
+ * Use this to check if a client's PaymentPayload satisfies the server's requirement.
328
+ *
329
+ * @param paymentPayload - The client's payment payload
330
+ * @param serverRequired - Whether the server requires a payment identifier (from PaymentRequired)
331
+ * @returns Validation result - invalid if required but not provided
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * const serverExtension = paymentRequired.extensions?.["payment-identifier"];
336
+ * const serverRequired = isPaymentIdentifierRequired(serverExtension);
337
+ * const result = validatePaymentIdentifierRequirement(paymentPayload, serverRequired);
338
+ * if (!result.valid) {
339
+ * return res.status(400).json({ error: result.errors });
340
+ * }
341
+ * ```
342
+ */
343
+ declare function validatePaymentIdentifierRequirement(paymentPayload: PaymentPayload, serverRequired: boolean): PaymentIdentifierValidationResult;
344
+
345
+ export { PAYMENT_IDENTIFIER, PAYMENT_ID_MAX_LENGTH, PAYMENT_ID_MIN_LENGTH, PAYMENT_ID_PATTERN, type PaymentIdentifierExtension, type PaymentIdentifierInfo, type PaymentIdentifierSchema, type PaymentIdentifierValidationResult, appendPaymentIdentifierToExtensions, declarePaymentIdentifierExtension, extractAndValidatePaymentIdentifier, extractPaymentIdentifier, generatePaymentId, hasPaymentIdentifier, isPaymentIdentifierExtension, isPaymentIdentifierRequired, isValidPaymentId, paymentIdentifierResourceServerExtension, paymentIdentifierSchema, validatePaymentIdentifier, validatePaymentIdentifierRequirement };
@@ -0,0 +1,39 @@
1
+ import {
2
+ PAYMENT_IDENTIFIER,
3
+ PAYMENT_ID_MAX_LENGTH,
4
+ PAYMENT_ID_MIN_LENGTH,
5
+ PAYMENT_ID_PATTERN,
6
+ appendPaymentIdentifierToExtensions,
7
+ declarePaymentIdentifierExtension,
8
+ extractAndValidatePaymentIdentifier,
9
+ extractPaymentIdentifier,
10
+ generatePaymentId,
11
+ hasPaymentIdentifier,
12
+ isPaymentIdentifierExtension,
13
+ isPaymentIdentifierRequired,
14
+ isValidPaymentId,
15
+ paymentIdentifierResourceServerExtension,
16
+ paymentIdentifierSchema,
17
+ validatePaymentIdentifier,
18
+ validatePaymentIdentifierRequirement
19
+ } from "../chunk-73HCOE6N.mjs";
20
+ export {
21
+ PAYMENT_IDENTIFIER,
22
+ PAYMENT_ID_MAX_LENGTH,
23
+ PAYMENT_ID_MIN_LENGTH,
24
+ PAYMENT_ID_PATTERN,
25
+ appendPaymentIdentifierToExtensions,
26
+ declarePaymentIdentifierExtension,
27
+ extractAndValidatePaymentIdentifier,
28
+ extractPaymentIdentifier,
29
+ generatePaymentId,
30
+ hasPaymentIdentifier,
31
+ isPaymentIdentifierExtension,
32
+ isPaymentIdentifierRequired,
33
+ isValidPaymentId,
34
+ paymentIdentifierResourceServerExtension,
35
+ paymentIdentifierSchema,
36
+ validatePaymentIdentifier,
37
+ validatePaymentIdentifierRequirement
38
+ };
39
+ //# sourceMappingURL=index.mjs.map