@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
@@ -1,562 +1,3 @@
1
- import { QueryParamMethods, BodyMethods, HTTPFacilitatorClient } from '@x402/core/http';
2
- import { ResourceServerExtension, PaymentPayload, PaymentRequirements, PaymentRequirementsV1 } from '@x402/core/types';
3
-
4
- /**
5
- * Shared type utilities for x402 extensions
6
- */
7
- /**
8
- * Type utility to merge extensions properly when chaining.
9
- * If T already has extensions, merge them; otherwise add new extensions.
10
- *
11
- * @example
12
- * ```ts
13
- * // Chaining multiple extensions preserves all types:
14
- * const client = withBazaar(withOtherExtension(new HTTPFacilitatorClient()));
15
- * // Type: HTTPFacilitatorClient & { extensions: OtherExtension & BazaarExtension }
16
- * ```
17
- */
18
- type WithExtensions<T, E> = T extends {
19
- extensions: infer Existing;
20
- } ? Omit<T, "extensions"> & {
21
- extensions: Existing & E;
22
- } : T & {
23
- extensions: E;
24
- };
25
-
26
- /**
27
- * Type definitions for the Bazaar Discovery Extension
28
- */
29
-
30
- /**
31
- * Extension identifier constant for the Bazaar discovery extension
32
- */
33
- declare const BAZAAR = "bazaar";
34
- /**
35
- * Discovery info for query parameter methods (GET, HEAD, DELETE)
36
- */
37
- interface QueryDiscoveryInfo {
38
- input: {
39
- type: "http";
40
- method: QueryParamMethods;
41
- queryParams?: Record<string, unknown>;
42
- headers?: Record<string, string>;
43
- };
44
- output?: {
45
- type?: string;
46
- format?: string;
47
- example?: unknown;
48
- };
49
- }
50
- /**
51
- * Discovery info for body methods (POST, PUT, PATCH)
52
- */
53
- interface BodyDiscoveryInfo {
54
- input: {
55
- type: "http";
56
- method: BodyMethods;
57
- bodyType: "json" | "form-data" | "text";
58
- body: Record<string, unknown>;
59
- queryParams?: Record<string, unknown>;
60
- headers?: Record<string, string>;
61
- };
62
- output?: {
63
- type?: string;
64
- format?: string;
65
- example?: unknown;
66
- };
67
- }
68
- /**
69
- * Combined discovery info type
70
- */
71
- type DiscoveryInfo = QueryDiscoveryInfo | BodyDiscoveryInfo;
72
- /**
73
- * Discovery extension for query parameter methods (GET, HEAD, DELETE)
74
- */
75
- interface QueryDiscoveryExtension {
76
- info: QueryDiscoveryInfo;
77
- schema: {
78
- $schema: "https://json-schema.org/draft/2020-12/schema";
79
- type: "object";
80
- properties: {
81
- input: {
82
- type: "object";
83
- properties: {
84
- type: {
85
- type: "string";
86
- const: "http";
87
- };
88
- method: {
89
- type: "string";
90
- enum: QueryParamMethods[];
91
- };
92
- queryParams?: {
93
- type: "object";
94
- properties?: Record<string, unknown>;
95
- required?: string[];
96
- additionalProperties?: boolean;
97
- };
98
- headers?: {
99
- type: "object";
100
- additionalProperties: {
101
- type: "string";
102
- };
103
- };
104
- };
105
- required: ("type" | "method")[];
106
- additionalProperties?: boolean;
107
- };
108
- output?: {
109
- type: "object";
110
- properties?: Record<string, unknown>;
111
- required?: readonly string[];
112
- additionalProperties?: boolean;
113
- };
114
- };
115
- required: ["input"];
116
- };
117
- }
118
- /**
119
- * Discovery extension for body methods (POST, PUT, PATCH)
120
- */
121
- interface BodyDiscoveryExtension {
122
- info: BodyDiscoveryInfo;
123
- schema: {
124
- $schema: "https://json-schema.org/draft/2020-12/schema";
125
- type: "object";
126
- properties: {
127
- input: {
128
- type: "object";
129
- properties: {
130
- type: {
131
- type: "string";
132
- const: "http";
133
- };
134
- method: {
135
- type: "string";
136
- enum: BodyMethods[];
137
- };
138
- bodyType: {
139
- type: "string";
140
- enum: ["json", "form-data", "text"];
141
- };
142
- body: Record<string, unknown>;
143
- queryParams?: {
144
- type: "object";
145
- properties?: Record<string, unknown>;
146
- required?: string[];
147
- additionalProperties?: boolean;
148
- };
149
- headers?: {
150
- type: "object";
151
- additionalProperties: {
152
- type: "string";
153
- };
154
- };
155
- };
156
- required: ("type" | "method" | "bodyType" | "body")[];
157
- additionalProperties?: boolean;
158
- };
159
- output?: {
160
- type: "object";
161
- properties?: Record<string, unknown>;
162
- required?: readonly string[];
163
- additionalProperties?: boolean;
164
- };
165
- };
166
- required: ["input"];
167
- };
168
- }
169
- /**
170
- * Combined discovery extension type
171
- */
172
- type DiscoveryExtension = QueryDiscoveryExtension | BodyDiscoveryExtension;
173
- interface DeclareQueryDiscoveryExtensionConfig {
174
- method?: QueryParamMethods;
175
- input?: Record<string, unknown>;
176
- inputSchema?: Record<string, unknown>;
177
- output?: {
178
- example?: unknown;
179
- schema?: Record<string, unknown>;
180
- };
181
- }
182
- interface DeclareBodyDiscoveryExtensionConfig {
183
- method?: BodyMethods;
184
- input?: Record<string, unknown>;
185
- inputSchema?: Record<string, unknown>;
186
- bodyType: "json" | "form-data" | "text";
187
- output?: {
188
- example?: unknown;
189
- schema?: Record<string, unknown>;
190
- };
191
- }
192
- type DeclareDiscoveryExtensionConfig = DeclareQueryDiscoveryExtensionConfig | DeclareBodyDiscoveryExtensionConfig;
193
- /**
194
- * Distributive Omit - properly distributes Omit over union types.
195
- *
196
- * Standard `Omit<A | B, K>` collapses to common properties only,
197
- * losing discriminant properties like `bodyType`.
198
- *
199
- * This type uses conditional type distribution to preserve the union:
200
- * `DistributiveOmit<A | B, K>` = `Omit<A, K> | Omit<B, K>`
201
- */
202
- type DistributiveOmit<T, K extends keyof T> = T extends T ? Omit<T, K> : never;
203
- /**
204
- * Config type for declareDiscoveryExtension function.
205
- * Uses DistributiveOmit to preserve bodyType discriminant in the union.
206
- */
207
- type DeclareDiscoveryExtensionInput = DistributiveOmit<DeclareDiscoveryExtensionConfig, "method">;
208
-
209
- /**
210
- * Resource Service functions for creating Bazaar discovery extensions
211
- *
212
- * These functions help servers declare the shape of their endpoints
213
- * for facilitator discovery and cataloging in the Bazaar.
214
- */
215
-
216
- /**
217
- * Create a discovery extension for any HTTP method
218
- *
219
- * This function helps servers declare how their endpoint should be called,
220
- * including the expected input parameters/body and output format.
221
- *
222
- * @param config - Configuration object for the discovery extension
223
- * @returns A discovery extension object with both info and schema
224
- *
225
- * @example
226
- * ```typescript
227
- * // For a GET endpoint with no input
228
- * const getExtension = declareDiscoveryExtension({
229
- * method: "GET",
230
- * output: {
231
- * example: { message: "Success", timestamp: "2024-01-01T00:00:00Z" }
232
- * }
233
- * });
234
- *
235
- * // For a GET endpoint with query params
236
- * const getWithParams = declareDiscoveryExtension({
237
- * method: "GET",
238
- * input: { query: "example" },
239
- * inputSchema: {
240
- * properties: {
241
- * query: { type: "string" }
242
- * },
243
- * required: ["query"]
244
- * }
245
- * });
246
- *
247
- * // For a POST endpoint with JSON body
248
- * const postExtension = declareDiscoveryExtension({
249
- * method: "POST",
250
- * input: { name: "John", age: 30 },
251
- * inputSchema: {
252
- * properties: {
253
- * name: { type: "string" },
254
- * age: { type: "number" }
255
- * },
256
- * required: ["name"]
257
- * },
258
- * bodyType: "json",
259
- * output: {
260
- * example: { success: true, id: "123" }
261
- * }
262
- * });
263
- * ```
264
- */
265
- declare function declareDiscoveryExtension(config: DeclareDiscoveryExtensionInput): Record<string, DiscoveryExtension>;
266
-
267
- declare const bazaarResourceServerExtension: ResourceServerExtension;
268
-
269
- /**
270
- * Facilitator functions for validating and extracting Bazaar discovery extensions
271
- *
272
- * These functions help facilitators validate extension data against schemas
273
- * and extract the discovery information for cataloging in the Bazaar.
274
- *
275
- * Supports both v2 (extensions in PaymentRequired) and v1 (outputSchema in PaymentRequirements).
276
- */
277
-
278
- /**
279
- * Validation result for discovery extensions
280
- */
281
- interface ValidationResult {
282
- valid: boolean;
283
- errors?: string[];
284
- }
285
- /**
286
- * Validates a discovery extension's info against its schema
287
- *
288
- * @param extension - The discovery extension containing info and schema
289
- * @returns Validation result indicating if the info matches the schema
290
- *
291
- * @example
292
- * ```typescript
293
- * const extension = declareDiscoveryExtension(...);
294
- * const result = validateDiscoveryExtension(extension);
295
- *
296
- * if (result.valid) {
297
- * console.log("Extension is valid");
298
- * } else {
299
- * console.error("Validation errors:", result.errors);
300
- * }
301
- * ```
302
- */
303
- declare function validateDiscoveryExtension(extension: DiscoveryExtension): ValidationResult;
304
- /**
305
- * Extracts the discovery info from payment payload and requirements
306
- *
307
- * This function handles both v2 (extensions) and v1 (outputSchema) formats.
308
- *
309
- * For v2: Discovery info is in PaymentPayload.extensions (client copied it from PaymentRequired)
310
- * For v1: Discovery info is in PaymentRequirements.outputSchema
311
- *
312
- * V1 data is automatically transformed to v2 DiscoveryInfo format, making smart
313
- * assumptions about field names (queryParams/query/params for GET, bodyFields/body/data for POST, etc.)
314
- *
315
- * @param paymentPayload - The payment payload containing extensions (v2) and version info
316
- * @param paymentRequirements - The payment requirements (contains outputSchema for v1)
317
- * @param validate - Whether to validate v2 extensions before extracting (default: true)
318
- * @returns The discovery info in v2 format if present, or null if not discoverable
319
- *
320
- * @example
321
- * ```typescript
322
- * // V2 - extensions are in PaymentPayload
323
- * const info = extractDiscoveryInfo(paymentPayload, paymentRequirements);
324
- *
325
- * // V1 - discovery info is in PaymentRequirements.outputSchema
326
- * const info = extractDiscoveryInfo(paymentPayloadV1, paymentRequirementsV1);
327
- *
328
- * if (info) {
329
- * // Both v1 and v2 return the same DiscoveryInfo structure
330
- * console.log("Method:", info.input.method);
331
- * }
332
- * ```
333
- */
334
- interface DiscoveredResource {
335
- resourceUrl: string;
336
- method: string;
337
- x402Version: number;
338
- discoveryInfo: DiscoveryInfo;
339
- }
340
- /**
341
- * Extracts discovery information from payment payload and requirements.
342
- * Combines resource URL, HTTP method, version, and discovery info into a single object.
343
- *
344
- * @param paymentPayload - The payment payload containing extensions and resource info
345
- * @param paymentRequirements - The payment requirements to validate against
346
- * @param validate - Whether to validate the discovery info against the schema (default: true)
347
- * @returns Discovered resource info with URL, method, version and discovery data, or null if not found
348
- */
349
- declare function extractDiscoveryInfo(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements | PaymentRequirementsV1, validate?: boolean): DiscoveredResource | null;
350
- /**
351
- * Extracts discovery info from a v2 extension directly
352
- *
353
- * This is a lower-level function for when you already have the extension object.
354
- * For general use, prefer the main extractDiscoveryInfo function.
355
- *
356
- * @param extension - The discovery extension to extract info from
357
- * @param validate - Whether to validate before extracting (default: true)
358
- * @returns The discovery info if valid
359
- * @throws Error if validation fails and validate is true
360
- */
361
- declare function extractDiscoveryInfoFromExtension(extension: DiscoveryExtension, validate?: boolean): DiscoveryInfo;
362
- /**
363
- * Validates and extracts discovery info in one step
364
- *
365
- * This is a convenience function that combines validation and extraction,
366
- * returning both the validation result and the info if valid.
367
- *
368
- * @param extension - The discovery extension to validate and extract
369
- * @returns Object containing validation result and info (if valid)
370
- *
371
- * @example
372
- * ```typescript
373
- * const extension = declareDiscoveryExtension(...);
374
- * const { valid, info, errors } = validateAndExtract(extension);
375
- *
376
- * if (valid && info) {
377
- * // Store info in Bazaar catalog
378
- * } else {
379
- * console.error("Validation errors:", errors);
380
- * }
381
- * ```
382
- */
383
- declare function validateAndExtract(extension: DiscoveryExtension): {
384
- valid: boolean;
385
- info?: DiscoveryInfo;
386
- errors?: string[];
387
- };
388
-
389
- /**
390
- * V1 Facilitator functions for extracting Bazaar discovery information
391
- *
392
- * In v1, discovery information is stored in the `outputSchema` field
393
- * of PaymentRequirements, which has a different structure than v2.
394
- *
395
- * This module transforms v1 data into v2 DiscoveryInfo format.
396
- */
397
-
398
- /**
399
- * Extracts discovery info from v1 PaymentRequirements and transforms to v2 format
400
- *
401
- * In v1, the discovery information is stored in the `outputSchema` field,
402
- * which contains both input (endpoint shape) and output (response schema) information.
403
- *
404
- * This function makes smart assumptions to normalize v1 data into v2 DiscoveryInfo format:
405
- * - For GET/HEAD/DELETE: Looks for queryParams, query, or params fields
406
- * - For POST/PUT/PATCH: Looks for bodyFields, body, or data fields and normalizes bodyType
407
- * - Extracts optional headers if present
408
- *
409
- * @param paymentRequirements - V1 payment requirements
410
- * @returns Discovery info in v2 format if present and valid, or null if not discoverable
411
- *
412
- * @example
413
- * ```typescript
414
- * const requirements: PaymentRequirementsV1 = {
415
- * scheme: "exact",
416
- * network: "eip155:8453",
417
- * maxAmountRequired: "100000",
418
- * resource: "https://api.example.com/data",
419
- * description: "Get data",
420
- * mimeType: "application/json",
421
- * outputSchema: {
422
- * input: {
423
- * type: "http",
424
- * method: "GET",
425
- * discoverable: true,
426
- * queryParams: { query: "string" }
427
- * },
428
- * output: { type: "object" }
429
- * },
430
- * payTo: "0x...",
431
- * maxTimeoutSeconds: 300,
432
- * asset: "0x...",
433
- * extra: {}
434
- * };
435
- *
436
- * const info = extractDiscoveryInfoV1(requirements);
437
- * if (info) {
438
- * console.log("Endpoint method:", info.input.method);
439
- * }
440
- * ```
441
- */
442
- declare function extractDiscoveryInfoV1(paymentRequirements: PaymentRequirementsV1): DiscoveryInfo | null;
443
- /**
444
- * Checks if v1 PaymentRequirements contains discoverable information
445
- *
446
- * @param paymentRequirements - V1 payment requirements
447
- * @returns True if the requirements contain valid discovery info
448
- *
449
- * @example
450
- * ```typescript
451
- * if (isDiscoverableV1(requirements)) {
452
- * const info = extractDiscoveryInfoV1(requirements);
453
- * // Catalog info in Bazaar
454
- * }
455
- * ```
456
- */
457
- declare function isDiscoverableV1(paymentRequirements: PaymentRequirementsV1): boolean;
458
- /**
459
- * Extracts resource metadata from v1 PaymentRequirements
460
- *
461
- * In v1, resource information is embedded directly in the payment requirements
462
- * rather than in a separate resource object.
463
- *
464
- * @param paymentRequirements - V1 payment requirements
465
- * @returns Resource metadata
466
- *
467
- * @example
468
- * ```typescript
469
- * const metadata = extractResourceMetadataV1(requirements);
470
- * console.log("Resource URL:", metadata.url);
471
- * console.log("Description:", metadata.description);
472
- * ```
473
- */
474
- declare function extractResourceMetadataV1(paymentRequirements: PaymentRequirementsV1): {
475
- url: string;
476
- description: string;
477
- mimeType: string;
478
- };
479
-
480
- /**
481
- * Client extensions for querying Bazaar discovery resources
482
- */
483
-
484
- /**
485
- * Parameters for listing discovery resources.
486
- * All parameters are optional and used for filtering/pagination.
487
- */
488
- interface ListDiscoveryResourcesParams {
489
- /**
490
- * Filter by protocol type (e.g., "http", "mcp").
491
- * Currently, the only supported protocol type is "http".
492
- */
493
- type?: string;
494
- /**
495
- * The number of discovered x402 resources to return per page.
496
- */
497
- limit?: number;
498
- /**
499
- * The offset of the first discovered x402 resource to return.
500
- */
501
- offset?: number;
502
- }
503
- /**
504
- * A discovered x402 resource from the bazaar.
505
- */
506
- interface DiscoveryResource {
507
- /** The URL of the discovered resource */
508
- url: string;
509
- /** The protocol type of the resource */
510
- type: string;
511
- /** Additional metadata about the resource */
512
- metadata?: Record<string, unknown>;
513
- }
514
- /**
515
- * Response from listing discovery resources.
516
- */
517
- interface DiscoveryResourcesResponse {
518
- /** The list of discovered resources */
519
- resources: DiscoveryResource[];
520
- /** Total count of resources matching the query */
521
- total?: number;
522
- /** The limit used for this query */
523
- limit?: number;
524
- /** The offset used for this query */
525
- offset?: number;
526
- }
527
- /**
528
- * Bazaar client extension interface providing discovery query functionality.
529
- */
530
- interface BazaarClientExtension {
531
- discovery: {
532
- /**
533
- * List x402 discovery resources from the bazaar.
534
- *
535
- * @param params - Optional filtering and pagination parameters
536
- * @returns A promise resolving to the discovery resources response
537
- */
538
- listResources(params?: ListDiscoveryResourcesParams): Promise<DiscoveryResourcesResponse>;
539
- };
540
- }
541
- /**
542
- * Extends a facilitator client with Bazaar discovery query functionality.
543
- * Preserves and merges with any existing extensions from prior chaining.
544
- *
545
- * @param client - The facilitator client to extend
546
- * @returns The client extended with bazaar discovery capabilities
547
- *
548
- * @example
549
- * ```ts
550
- * // Basic usage
551
- * const client = withBazaar(new HTTPFacilitatorClient());
552
- * const resources = await client.extensions.discovery.listResources({ type: "http" });
553
- *
554
- * // Chaining with other extensions
555
- * const client = withBazaar(withOtherExtension(new HTTPFacilitatorClient()));
556
- * await client.extensions.other.someMethod();
557
- * await client.extensions.discovery.listResources();
558
- * ```
559
- */
560
- declare function withBazaar<T extends HTTPFacilitatorClient>(client: T): WithExtensions<T, BazaarClientExtension>;
561
-
562
- export { BAZAAR, type BazaarClientExtension, type BodyDiscoveryExtension, type BodyDiscoveryInfo, type DiscoveryExtension, type DiscoveryInfo, type DiscoveryResource, type DiscoveryResourcesResponse, type ListDiscoveryResourcesParams, type QueryDiscoveryExtension, type QueryDiscoveryInfo, type ValidationResult, type WithExtensions as W, bazaarResourceServerExtension, declareDiscoveryExtension, extractDiscoveryInfo, extractDiscoveryInfoFromExtension, extractDiscoveryInfoV1, extractResourceMetadataV1, isDiscoverableV1, validateAndExtract, validateDiscoveryExtension, withBazaar };
1
+ export { B as BAZAAR, a as BazaarClientExtension, b as BodyDiscoveryExtension, c as BodyDiscoveryInfo, D as DiscoveredResource, d as DiscoveryExtension, e as DiscoveryInfo, f as DiscoveryResource, g as DiscoveryResourcesResponse, L as ListDiscoveryResourcesParams, Q as QueryDiscoveryExtension, h as QueryDiscoveryInfo, V as ValidationResult, i as bazaarResourceServerExtension, j as declareDiscoveryExtension, k as extractDiscoveryInfo, l as extractDiscoveryInfoFromExtension, m as extractDiscoveryInfoV1, n as extractResourceMetadataV1, o as isDiscoverableV1, v as validateAndExtract, p as validateDiscoveryExtension, w as withBazaar } from '../index-DvDlinmy.js';
2
+ import '@x402/core/http';
3
+ import '@x402/core/types';
@@ -412,8 +412,20 @@ function extractDiscoveryInfo(paymentPayload, paymentRequirements, validate = tr
412
412
  }
413
413
  const url = new URL(resourceUrl);
414
414
  const normalizedResourceUrl = `${url.origin}${url.pathname}`;
415
+ let description;
416
+ let mimeType;
417
+ if (paymentPayload.x402Version === 2) {
418
+ description = paymentPayload.resource?.description;
419
+ mimeType = paymentPayload.resource?.mimeType;
420
+ } else if (paymentPayload.x402Version === 1) {
421
+ const requirementsV1 = paymentRequirements;
422
+ description = requirementsV1.description;
423
+ mimeType = requirementsV1.mimeType;
424
+ }
415
425
  return {
416
426
  resourceUrl: normalizedResourceUrl,
427
+ description,
428
+ mimeType,
417
429
  method: discoveryInfo.input.method,
418
430
  x402Version: paymentPayload.x402Version,
419
431
  discoveryInfo