@bankofai/x402-extensions 2.6.0-beta.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 (34) hide show
  1. package/README.md +792 -0
  2. package/dist/cjs/bazaar/index.d.ts +3 -0
  3. package/dist/cjs/bazaar/index.js +629 -0
  4. package/dist/cjs/bazaar/index.js.map +1 -0
  5. package/dist/cjs/index-DPJQYYGe.d.ts +662 -0
  6. package/dist/cjs/index.d.ts +360 -0
  7. package/dist/cjs/index.js +1880 -0
  8. package/dist/cjs/index.js.map +1 -0
  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 +1091 -0
  13. package/dist/cjs/sign-in-with-x/index.js +845 -0
  14. package/dist/cjs/sign-in-with-x/index.js.map +1 -0
  15. package/dist/esm/bazaar/index.d.mts +3 -0
  16. package/dist/esm/bazaar/index.mjs +33 -0
  17. package/dist/esm/bazaar/index.mjs.map +1 -0
  18. package/dist/esm/chunk-MTWK6ERV.mjs +777 -0
  19. package/dist/esm/chunk-MTWK6ERV.mjs.map +1 -0
  20. package/dist/esm/chunk-RERA4OZZ.mjs +233 -0
  21. package/dist/esm/chunk-RERA4OZZ.mjs.map +1 -0
  22. package/dist/esm/chunk-UHTNEDIJ.mjs +580 -0
  23. package/dist/esm/chunk-UHTNEDIJ.mjs.map +1 -0
  24. package/dist/esm/index-DPJQYYGe.d.mts +662 -0
  25. package/dist/esm/index.d.mts +360 -0
  26. package/dist/esm/index.mjs +323 -0
  27. package/dist/esm/index.mjs.map +1 -0
  28. package/dist/esm/payment-identifier/index.d.mts +345 -0
  29. package/dist/esm/payment-identifier/index.mjs +39 -0
  30. package/dist/esm/payment-identifier/index.mjs.map +1 -0
  31. package/dist/esm/sign-in-with-x/index.d.mts +1091 -0
  32. package/dist/esm/sign-in-with-x/index.mjs +71 -0
  33. package/dist/esm/sign-in-with-x/index.mjs.map +1 -0
  34. package/package.json +99 -0
@@ -0,0 +1,580 @@
1
+ // src/bazaar/http/types.ts
2
+ var isQueryExtensionConfig = (config) => {
3
+ return !("bodyType" in config) && !("toolName" in config);
4
+ };
5
+ var isBodyExtensionConfig = (config) => {
6
+ return "bodyType" in config;
7
+ };
8
+
9
+ // src/bazaar/mcp/types.ts
10
+ var isMcpExtensionConfig = (config) => {
11
+ return "toolName" in config;
12
+ };
13
+
14
+ // src/bazaar/types.ts
15
+ var BAZAAR = { key: "bazaar" };
16
+
17
+ // src/bazaar/http/resourceService.ts
18
+ function createQueryDiscoveryExtension({
19
+ method,
20
+ input = {},
21
+ inputSchema = { properties: {} },
22
+ output
23
+ }) {
24
+ return {
25
+ info: {
26
+ input: {
27
+ type: "http",
28
+ ...method ? { method } : {},
29
+ ...input ? { queryParams: input } : {}
30
+ },
31
+ ...output?.example ? {
32
+ output: {
33
+ type: "json",
34
+ example: output.example
35
+ }
36
+ } : {}
37
+ },
38
+ schema: {
39
+ $schema: "https://json-schema.org/draft/2020-12/schema",
40
+ type: "object",
41
+ properties: {
42
+ input: {
43
+ type: "object",
44
+ properties: {
45
+ type: {
46
+ type: "string",
47
+ const: "http"
48
+ },
49
+ method: {
50
+ type: "string",
51
+ enum: ["GET", "HEAD", "DELETE"]
52
+ },
53
+ ...inputSchema ? {
54
+ queryParams: {
55
+ type: "object",
56
+ ...typeof inputSchema === "object" ? inputSchema : {}
57
+ }
58
+ } : {}
59
+ },
60
+ required: ["type"],
61
+ additionalProperties: false
62
+ },
63
+ ...output?.example ? {
64
+ output: {
65
+ type: "object",
66
+ properties: {
67
+ type: {
68
+ type: "string"
69
+ },
70
+ example: {
71
+ type: "object",
72
+ ...output.schema && typeof output.schema === "object" ? output.schema : {}
73
+ }
74
+ },
75
+ required: ["type"]
76
+ }
77
+ } : {}
78
+ },
79
+ required: ["input"]
80
+ }
81
+ };
82
+ }
83
+ function createBodyDiscoveryExtension({
84
+ method,
85
+ input = {},
86
+ inputSchema = { properties: {} },
87
+ bodyType,
88
+ output
89
+ }) {
90
+ return {
91
+ info: {
92
+ input: {
93
+ type: "http",
94
+ ...method ? { method } : {},
95
+ bodyType,
96
+ body: input
97
+ },
98
+ ...output?.example ? {
99
+ output: {
100
+ type: "json",
101
+ example: output.example
102
+ }
103
+ } : {}
104
+ },
105
+ schema: {
106
+ $schema: "https://json-schema.org/draft/2020-12/schema",
107
+ type: "object",
108
+ properties: {
109
+ input: {
110
+ type: "object",
111
+ properties: {
112
+ type: {
113
+ type: "string",
114
+ const: "http"
115
+ },
116
+ method: {
117
+ type: "string",
118
+ enum: ["POST", "PUT", "PATCH"]
119
+ },
120
+ bodyType: {
121
+ type: "string",
122
+ enum: ["json", "form-data", "text"]
123
+ },
124
+ body: inputSchema
125
+ },
126
+ required: ["type", "bodyType", "body"],
127
+ additionalProperties: false
128
+ },
129
+ ...output?.example ? {
130
+ output: {
131
+ type: "object",
132
+ properties: {
133
+ type: {
134
+ type: "string"
135
+ },
136
+ example: {
137
+ type: "object",
138
+ ...output.schema && typeof output.schema === "object" ? output.schema : {}
139
+ }
140
+ },
141
+ required: ["type"]
142
+ }
143
+ } : {}
144
+ },
145
+ required: ["input"]
146
+ }
147
+ };
148
+ }
149
+
150
+ // src/bazaar/mcp/resourceService.ts
151
+ function createMcpDiscoveryExtension({
152
+ toolName,
153
+ description,
154
+ transport,
155
+ inputSchema,
156
+ example,
157
+ output
158
+ }) {
159
+ return {
160
+ info: {
161
+ input: {
162
+ type: "mcp",
163
+ toolName,
164
+ ...description !== void 0 ? { description } : {},
165
+ ...transport !== void 0 ? { transport } : {},
166
+ inputSchema,
167
+ ...example !== void 0 ? { example } : {}
168
+ },
169
+ ...output?.example ? {
170
+ output: {
171
+ type: "json",
172
+ example: output.example
173
+ }
174
+ } : {}
175
+ },
176
+ schema: {
177
+ $schema: "https://json-schema.org/draft/2020-12/schema",
178
+ type: "object",
179
+ properties: {
180
+ input: {
181
+ type: "object",
182
+ properties: {
183
+ type: {
184
+ type: "string",
185
+ const: "mcp"
186
+ },
187
+ toolName: {
188
+ type: "string"
189
+ },
190
+ ...description !== void 0 ? {
191
+ description: {
192
+ type: "string"
193
+ }
194
+ } : {},
195
+ ...transport !== void 0 ? {
196
+ transport: {
197
+ type: "string",
198
+ enum: ["streamable-http", "sse"]
199
+ }
200
+ } : {},
201
+ inputSchema: {
202
+ type: "object"
203
+ },
204
+ ...example !== void 0 ? {
205
+ example: {
206
+ type: "object"
207
+ }
208
+ } : {}
209
+ },
210
+ required: ["type", "toolName", "inputSchema"],
211
+ additionalProperties: false
212
+ },
213
+ ...output?.example ? {
214
+ output: {
215
+ type: "object",
216
+ properties: {
217
+ type: {
218
+ type: "string"
219
+ },
220
+ example: {
221
+ type: "object",
222
+ ...output.schema && typeof output.schema === "object" ? output.schema : {}
223
+ }
224
+ },
225
+ required: ["type"]
226
+ }
227
+ } : {}
228
+ },
229
+ required: ["input"]
230
+ }
231
+ };
232
+ }
233
+
234
+ // src/bazaar/resourceService.ts
235
+ function declareDiscoveryExtension(config) {
236
+ if ("toolName" in config) {
237
+ const extension2 = createMcpDiscoveryExtension(config);
238
+ return { bazaar: extension2 };
239
+ }
240
+ const bodyType = config.bodyType;
241
+ const isBodyMethod2 = bodyType !== void 0;
242
+ const extension = isBodyMethod2 ? createBodyDiscoveryExtension(config) : createQueryDiscoveryExtension(config);
243
+ return { bazaar: extension };
244
+ }
245
+
246
+ // src/bazaar/server.ts
247
+ function isHTTPRequestContext(ctx) {
248
+ return ctx !== null && typeof ctx === "object" && "method" in ctx && "adapter" in ctx;
249
+ }
250
+ var bazaarResourceServerExtension = {
251
+ key: BAZAAR.key,
252
+ enrichDeclaration: (declaration, transportContext) => {
253
+ if (!isHTTPRequestContext(transportContext)) {
254
+ return declaration;
255
+ }
256
+ const extension = declaration;
257
+ if (extension.info?.input?.type === "mcp") {
258
+ return declaration;
259
+ }
260
+ const method = transportContext.method;
261
+ const existingInputProps = extension.schema?.properties?.input?.properties || {};
262
+ const updatedInputProps = {
263
+ ...existingInputProps,
264
+ method: {
265
+ type: "string",
266
+ enum: [method]
267
+ }
268
+ };
269
+ return {
270
+ ...extension,
271
+ info: {
272
+ ...extension.info || {},
273
+ input: {
274
+ ...extension.info?.input || {},
275
+ method
276
+ }
277
+ },
278
+ schema: {
279
+ ...extension.schema || {},
280
+ properties: {
281
+ ...extension.schema?.properties || {},
282
+ input: {
283
+ ...extension.schema?.properties?.input || {},
284
+ properties: updatedInputProps,
285
+ required: [
286
+ ...extension.schema?.properties?.input?.required || [],
287
+ ...!(extension.schema?.properties?.input?.required || []).includes("method") ? ["method"] : []
288
+ ]
289
+ }
290
+ }
291
+ }
292
+ };
293
+ }
294
+ };
295
+
296
+ // src/bazaar/facilitator.ts
297
+ import Ajv from "ajv/dist/2020.js";
298
+
299
+ // src/bazaar/v1/facilitator.ts
300
+ function hasV1OutputSchema(obj) {
301
+ return obj !== null && typeof obj === "object" && "input" in obj && obj.input !== null && typeof obj.input === "object" && "type" in obj.input && obj.input.type === "http" && "method" in obj.input;
302
+ }
303
+ function isQueryMethod(method) {
304
+ const upperMethod = method.toUpperCase();
305
+ return upperMethod === "GET" || upperMethod === "HEAD" || upperMethod === "DELETE";
306
+ }
307
+ function isBodyMethod(method) {
308
+ const upperMethod = method.toUpperCase();
309
+ return upperMethod === "POST" || upperMethod === "PUT" || upperMethod === "PATCH";
310
+ }
311
+ function extractQueryParams(v1Input) {
312
+ if (v1Input.queryParams && typeof v1Input.queryParams === "object") {
313
+ return v1Input.queryParams;
314
+ }
315
+ if (v1Input.query_params && typeof v1Input.query_params === "object") {
316
+ return v1Input.query_params;
317
+ }
318
+ if (v1Input.query && typeof v1Input.query === "object") {
319
+ return v1Input.query;
320
+ }
321
+ if (v1Input.params && typeof v1Input.params === "object") {
322
+ return v1Input.params;
323
+ }
324
+ return void 0;
325
+ }
326
+ function extractBodyInfo(v1Input) {
327
+ let bodyType = "json";
328
+ const bodyTypeField = v1Input.bodyType || v1Input.body_type;
329
+ if (bodyTypeField && typeof bodyTypeField === "string") {
330
+ const type = bodyTypeField.toLowerCase();
331
+ if (type.includes("form") || type.includes("multipart")) {
332
+ bodyType = "form-data";
333
+ } else if (type.includes("text") || type.includes("plain")) {
334
+ bodyType = "text";
335
+ } else {
336
+ bodyType = "json";
337
+ }
338
+ }
339
+ let body = {};
340
+ if (v1Input.bodyFields && typeof v1Input.bodyFields === "object") {
341
+ body = v1Input.bodyFields;
342
+ } else if (v1Input.body_fields && v1Input.body_fields !== null && typeof v1Input.body_fields === "object") {
343
+ body = v1Input.body_fields;
344
+ } else if (v1Input.bodyParams && typeof v1Input.bodyParams === "object") {
345
+ body = v1Input.bodyParams;
346
+ } else if (v1Input.body && typeof v1Input.body === "object") {
347
+ body = v1Input.body;
348
+ } else if (v1Input.data && typeof v1Input.data === "object") {
349
+ body = v1Input.data;
350
+ } else if (v1Input.properties && typeof v1Input.properties === "object") {
351
+ body = v1Input.properties;
352
+ }
353
+ return { body, bodyType };
354
+ }
355
+ function extractDiscoveryInfoV1(paymentRequirements) {
356
+ const { outputSchema } = paymentRequirements;
357
+ if (!outputSchema || !hasV1OutputSchema(outputSchema)) {
358
+ return null;
359
+ }
360
+ const v1Input = outputSchema.input;
361
+ const isDiscoverable = v1Input.discoverable ?? true;
362
+ if (!isDiscoverable) {
363
+ return null;
364
+ }
365
+ const method = typeof v1Input.method === "string" ? v1Input.method.toUpperCase() : "";
366
+ const headersRaw = v1Input.headerFields || v1Input.header_fields || v1Input.headers;
367
+ const headers = headersRaw && typeof headersRaw === "object" ? headersRaw : void 0;
368
+ const output = outputSchema.output ? {
369
+ type: "json",
370
+ example: outputSchema.output
371
+ } : void 0;
372
+ if (isQueryMethod(method)) {
373
+ const queryParams = extractQueryParams(v1Input);
374
+ const discoveryInfo = {
375
+ input: {
376
+ type: "http",
377
+ method,
378
+ ...queryParams ? { queryParams } : {},
379
+ ...headers ? { headers } : {}
380
+ },
381
+ ...output ? { output } : {}
382
+ };
383
+ return discoveryInfo;
384
+ } else if (isBodyMethod(method)) {
385
+ const { body, bodyType } = extractBodyInfo(v1Input);
386
+ const queryParams = extractQueryParams(v1Input);
387
+ const discoveryInfo = {
388
+ input: {
389
+ type: "http",
390
+ method,
391
+ bodyType,
392
+ body,
393
+ ...queryParams ? { queryParams } : {},
394
+ ...headers ? { headers } : {}
395
+ },
396
+ ...output ? { output } : {}
397
+ };
398
+ return discoveryInfo;
399
+ }
400
+ return null;
401
+ }
402
+ function isDiscoverableV1(paymentRequirements) {
403
+ return extractDiscoveryInfoV1(paymentRequirements) !== null;
404
+ }
405
+ function extractResourceMetadataV1(paymentRequirements) {
406
+ return {
407
+ url: paymentRequirements.resource,
408
+ description: paymentRequirements.description,
409
+ mimeType: paymentRequirements.mimeType
410
+ };
411
+ }
412
+
413
+ // src/bazaar/facilitator.ts
414
+ function validateDiscoveryExtension(extension) {
415
+ try {
416
+ const ajv = new Ajv({ strict: false, allErrors: true });
417
+ const validate = ajv.compile(extension.schema);
418
+ const valid = validate(extension.info);
419
+ if (valid) {
420
+ return { valid: true };
421
+ }
422
+ const errors = validate.errors?.map((err) => {
423
+ const path = err.instancePath || "(root)";
424
+ return `${path}: ${err.message}`;
425
+ }) || ["Unknown validation error"];
426
+ return { valid: false, errors };
427
+ } catch (error) {
428
+ return {
429
+ valid: false,
430
+ errors: [
431
+ `Schema validation failed: ${error instanceof Error ? error.message : String(error)}`
432
+ ]
433
+ };
434
+ }
435
+ }
436
+ function extractDiscoveryInfo(paymentPayload, paymentRequirements, validate = true) {
437
+ let discoveryInfo = null;
438
+ let resourceUrl;
439
+ if (paymentPayload.x402Version === 2) {
440
+ resourceUrl = paymentPayload.resource?.url ?? "";
441
+ if (paymentPayload.extensions) {
442
+ const bazaarExtension = paymentPayload.extensions[BAZAAR.key];
443
+ if (bazaarExtension && typeof bazaarExtension === "object") {
444
+ try {
445
+ const extension = bazaarExtension;
446
+ if (validate) {
447
+ const result = validateDiscoveryExtension(extension);
448
+ if (!result.valid) {
449
+ console.warn(
450
+ `V2 discovery extension validation failed: ${result.errors?.join(", ")}`
451
+ );
452
+ } else {
453
+ discoveryInfo = extension.info;
454
+ }
455
+ } else {
456
+ discoveryInfo = extension.info;
457
+ }
458
+ } catch (error) {
459
+ console.warn(`V2 discovery extension extraction failed: ${error}`);
460
+ }
461
+ }
462
+ }
463
+ } else if (paymentPayload.x402Version === 1) {
464
+ const requirementsV1 = paymentRequirements;
465
+ resourceUrl = requirementsV1.resource;
466
+ discoveryInfo = extractDiscoveryInfoV1(requirementsV1);
467
+ } else {
468
+ return null;
469
+ }
470
+ if (!discoveryInfo) {
471
+ return null;
472
+ }
473
+ const url = new URL(resourceUrl);
474
+ const normalizedResourceUrl = `${url.origin}${url.pathname}`;
475
+ let description;
476
+ let mimeType;
477
+ if (paymentPayload.x402Version === 2) {
478
+ description = paymentPayload.resource?.description;
479
+ mimeType = paymentPayload.resource?.mimeType;
480
+ } else if (paymentPayload.x402Version === 1) {
481
+ const requirementsV1 = paymentRequirements;
482
+ description = requirementsV1.description;
483
+ mimeType = requirementsV1.mimeType;
484
+ }
485
+ const base = {
486
+ resourceUrl: normalizedResourceUrl,
487
+ description,
488
+ mimeType,
489
+ x402Version: paymentPayload.x402Version,
490
+ discoveryInfo
491
+ };
492
+ if (discoveryInfo.input.type === "mcp") {
493
+ return { ...base, toolName: discoveryInfo.input.toolName };
494
+ }
495
+ return { ...base, method: discoveryInfo.input.method };
496
+ }
497
+ function extractDiscoveryInfoFromExtension(extension, validate = true) {
498
+ if (validate) {
499
+ const result = validateDiscoveryExtension(extension);
500
+ if (!result.valid) {
501
+ throw new Error(
502
+ `Invalid discovery extension: ${result.errors?.join(", ") || "Unknown error"}`
503
+ );
504
+ }
505
+ }
506
+ return extension.info;
507
+ }
508
+ function validateAndExtract(extension) {
509
+ const result = validateDiscoveryExtension(extension);
510
+ if (result.valid) {
511
+ return {
512
+ valid: true,
513
+ info: extension.info
514
+ };
515
+ }
516
+ return {
517
+ valid: false,
518
+ errors: result.errors
519
+ };
520
+ }
521
+
522
+ // src/bazaar/facilitatorClient.ts
523
+ function withBazaar(client) {
524
+ const existingExtensions = client.extensions ?? {};
525
+ const extended = client;
526
+ extended.extensions = {
527
+ ...existingExtensions,
528
+ discovery: {
529
+ async listResources(params) {
530
+ let headers = {
531
+ "Content-Type": "application/json"
532
+ };
533
+ const authHeaders = await client.createAuthHeaders("discovery");
534
+ headers = { ...headers, ...authHeaders.headers };
535
+ const queryParams = new URLSearchParams();
536
+ if (params?.type !== void 0) {
537
+ queryParams.set("type", params.type);
538
+ }
539
+ if (params?.limit !== void 0) {
540
+ queryParams.set("limit", params.limit.toString());
541
+ }
542
+ if (params?.offset !== void 0) {
543
+ queryParams.set("offset", params.offset.toString());
544
+ }
545
+ const queryString = queryParams.toString();
546
+ const endpoint = `${client.url}/discovery/resources${queryString ? `?${queryString}` : ""}`;
547
+ const response = await fetch(endpoint, {
548
+ method: "GET",
549
+ headers
550
+ });
551
+ if (!response.ok) {
552
+ const errorText = await response.text().catch(() => response.statusText);
553
+ throw new Error(
554
+ `Facilitator listDiscoveryResources failed (${response.status}): ${errorText}`
555
+ );
556
+ }
557
+ return await response.json();
558
+ }
559
+ }
560
+ };
561
+ return extended;
562
+ }
563
+
564
+ export {
565
+ isQueryExtensionConfig,
566
+ isBodyExtensionConfig,
567
+ isMcpExtensionConfig,
568
+ BAZAAR,
569
+ declareDiscoveryExtension,
570
+ bazaarResourceServerExtension,
571
+ extractDiscoveryInfoV1,
572
+ isDiscoverableV1,
573
+ extractResourceMetadataV1,
574
+ validateDiscoveryExtension,
575
+ extractDiscoveryInfo,
576
+ extractDiscoveryInfoFromExtension,
577
+ validateAndExtract,
578
+ withBazaar
579
+ };
580
+ //# sourceMappingURL=chunk-UHTNEDIJ.mjs.map