@agentcash/router 0.4.10 → 0.5.1
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/dist/index.cjs +18 -7
- package/dist/index.d.cts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +18 -7
- package/package.json +4 -3
package/dist/index.cjs
CHANGED
|
@@ -1301,6 +1301,7 @@ var RouteBuilder = class {
|
|
|
1301
1301
|
// src/discovery/well-known.ts
|
|
1302
1302
|
var import_server3 = require("next/server");
|
|
1303
1303
|
function createWellKnownHandler(registry, baseUrl, pricesKeys, options = {}) {
|
|
1304
|
+
const normalizedBase = baseUrl.replace(/\/+$/, "");
|
|
1304
1305
|
let validated = false;
|
|
1305
1306
|
return async (_request) => {
|
|
1306
1307
|
if (!validated && pricesKeys) {
|
|
@@ -1310,7 +1311,7 @@ function createWellKnownHandler(registry, baseUrl, pricesKeys, options = {}) {
|
|
|
1310
1311
|
const x402Set = /* @__PURE__ */ new Set();
|
|
1311
1312
|
const mppSet = /* @__PURE__ */ new Set();
|
|
1312
1313
|
for (const [key, entry] of registry.entries()) {
|
|
1313
|
-
const url = `${
|
|
1314
|
+
const url = `${normalizedBase}/api/${entry.path ?? key}`;
|
|
1314
1315
|
if (entry.authMode !== "unprotected") x402Set.add(url);
|
|
1315
1316
|
if (entry.protocols.includes("mpp")) mppSet.add(url);
|
|
1316
1317
|
}
|
|
@@ -1350,6 +1351,7 @@ function createWellKnownHandler(registry, baseUrl, pricesKeys, options = {}) {
|
|
|
1350
1351
|
// src/discovery/openapi.ts
|
|
1351
1352
|
var import_server4 = require("next/server");
|
|
1352
1353
|
function createOpenAPIHandler(registry, baseUrl, pricesKeys, options) {
|
|
1354
|
+
const normalizedBase = baseUrl.replace(/\/+$/, "");
|
|
1353
1355
|
let cached = null;
|
|
1354
1356
|
let validated = false;
|
|
1355
1357
|
return async (_request) => {
|
|
@@ -1376,7 +1378,7 @@ function createOpenAPIHandler(registry, baseUrl, pricesKeys, options) {
|
|
|
1376
1378
|
version: options.version,
|
|
1377
1379
|
...options.contact && { contact: options.contact }
|
|
1378
1380
|
},
|
|
1379
|
-
servers: [{ url: options.baseUrl ??
|
|
1381
|
+
servers: [{ url: (options.baseUrl ?? normalizedBase).replace(/\/+$/, "") }],
|
|
1380
1382
|
tags: Array.from(tagSet).sort().map((name) => ({ name })),
|
|
1381
1383
|
paths
|
|
1382
1384
|
});
|
|
@@ -1448,12 +1450,20 @@ function createRouter(config) {
|
|
|
1448
1450
|
const registry = new RouteRegistry();
|
|
1449
1451
|
const nonceStore = config.siwx?.nonceStore ?? new MemoryNonceStore();
|
|
1450
1452
|
const network = config.network ?? "eip155:8453";
|
|
1451
|
-
const baseUrl = typeof globalThis.process !== "undefined" ? process.env.NEXT_PUBLIC_BASE_URL
|
|
1453
|
+
const baseUrl = config.baseUrl ?? (typeof globalThis.process !== "undefined" ? process.env.NEXT_PUBLIC_BASE_URL : void 0);
|
|
1452
1454
|
if (config.protocols && config.protocols.length === 0) {
|
|
1453
1455
|
throw new Error(
|
|
1454
1456
|
"RouterConfig.protocols cannot be empty. Omit the field to use default ['x402'] or specify protocols explicitly."
|
|
1455
1457
|
);
|
|
1456
1458
|
}
|
|
1459
|
+
if (!baseUrl) {
|
|
1460
|
+
const msg = "baseUrl is required. Pass it in RouterConfig or set NEXT_PUBLIC_BASE_URL (e.g. https://myapp.com). It is used for discovery URLs, OpenAPI servers, and MPP realm.";
|
|
1461
|
+
if (process.env.NODE_ENV === "production") {
|
|
1462
|
+
throw new Error(msg);
|
|
1463
|
+
}
|
|
1464
|
+
console.warn(`[router] ${msg}`);
|
|
1465
|
+
}
|
|
1466
|
+
const resolvedBaseUrl = (baseUrl ?? "http://localhost:3000").replace(/\/+$/, "");
|
|
1457
1467
|
let x402ConfigError;
|
|
1458
1468
|
let mppConfigError;
|
|
1459
1469
|
if ((!config.protocols || config.protocols.includes("x402")) && !config.payeeAddress) {
|
|
@@ -1477,7 +1487,7 @@ function createRouter(config) {
|
|
|
1477
1487
|
}
|
|
1478
1488
|
if (config.plugin?.init) {
|
|
1479
1489
|
try {
|
|
1480
|
-
const result = config.plugin.init({ origin:
|
|
1490
|
+
const result = config.plugin.init({ origin: resolvedBaseUrl });
|
|
1481
1491
|
if (result && typeof result.catch === "function") {
|
|
1482
1492
|
result.catch(() => {
|
|
1483
1493
|
});
|
|
@@ -1526,7 +1536,8 @@ function createRouter(config) {
|
|
|
1526
1536
|
}
|
|
1527
1537
|
})
|
|
1528
1538
|
],
|
|
1529
|
-
secretKey: config.mpp.secretKey
|
|
1539
|
+
secretKey: config.mpp.secretKey,
|
|
1540
|
+
realm: new URL(resolvedBaseUrl).host
|
|
1530
1541
|
});
|
|
1531
1542
|
} catch (err) {
|
|
1532
1543
|
deps.mppx = null;
|
|
@@ -1546,10 +1557,10 @@ function createRouter(config) {
|
|
|
1546
1557
|
return builder;
|
|
1547
1558
|
},
|
|
1548
1559
|
wellKnown(options) {
|
|
1549
|
-
return createWellKnownHandler(registry,
|
|
1560
|
+
return createWellKnownHandler(registry, resolvedBaseUrl, pricesKeys, options);
|
|
1550
1561
|
},
|
|
1551
1562
|
openapi(options) {
|
|
1552
|
-
return createOpenAPIHandler(registry,
|
|
1563
|
+
return createOpenAPIHandler(registry, resolvedBaseUrl, pricesKeys, options);
|
|
1553
1564
|
},
|
|
1554
1565
|
monitors() {
|
|
1555
1566
|
const result = [];
|
package/dist/index.d.cts
CHANGED
|
@@ -227,6 +227,14 @@ interface RouteEntry {
|
|
|
227
227
|
}
|
|
228
228
|
interface RouterConfig {
|
|
229
229
|
payeeAddress: string;
|
|
230
|
+
/**
|
|
231
|
+
* Production origin URL (e.g. `https://myapp.com`).
|
|
232
|
+
* Used for discovery URLs, OpenAPI servers, and MPP realm.
|
|
233
|
+
*
|
|
234
|
+
* Falls back to `NEXT_PUBLIC_BASE_URL` env var.
|
|
235
|
+
* Required in production — `next build` will fail without it.
|
|
236
|
+
*/
|
|
237
|
+
baseUrl?: string;
|
|
230
238
|
network?: string;
|
|
231
239
|
facilitatorUrl?: string;
|
|
232
240
|
plugin?: RouterPlugin;
|
package/dist/index.d.ts
CHANGED
|
@@ -227,6 +227,14 @@ interface RouteEntry {
|
|
|
227
227
|
}
|
|
228
228
|
interface RouterConfig {
|
|
229
229
|
payeeAddress: string;
|
|
230
|
+
/**
|
|
231
|
+
* Production origin URL (e.g. `https://myapp.com`).
|
|
232
|
+
* Used for discovery URLs, OpenAPI servers, and MPP realm.
|
|
233
|
+
*
|
|
234
|
+
* Falls back to `NEXT_PUBLIC_BASE_URL` env var.
|
|
235
|
+
* Required in production — `next build` will fail without it.
|
|
236
|
+
*/
|
|
237
|
+
baseUrl?: string;
|
|
230
238
|
network?: string;
|
|
231
239
|
facilitatorUrl?: string;
|
|
232
240
|
plugin?: RouterPlugin;
|
package/dist/index.js
CHANGED
|
@@ -1264,6 +1264,7 @@ var RouteBuilder = class {
|
|
|
1264
1264
|
// src/discovery/well-known.ts
|
|
1265
1265
|
import { NextResponse as NextResponse3 } from "next/server";
|
|
1266
1266
|
function createWellKnownHandler(registry, baseUrl, pricesKeys, options = {}) {
|
|
1267
|
+
const normalizedBase = baseUrl.replace(/\/+$/, "");
|
|
1267
1268
|
let validated = false;
|
|
1268
1269
|
return async (_request) => {
|
|
1269
1270
|
if (!validated && pricesKeys) {
|
|
@@ -1273,7 +1274,7 @@ function createWellKnownHandler(registry, baseUrl, pricesKeys, options = {}) {
|
|
|
1273
1274
|
const x402Set = /* @__PURE__ */ new Set();
|
|
1274
1275
|
const mppSet = /* @__PURE__ */ new Set();
|
|
1275
1276
|
for (const [key, entry] of registry.entries()) {
|
|
1276
|
-
const url = `${
|
|
1277
|
+
const url = `${normalizedBase}/api/${entry.path ?? key}`;
|
|
1277
1278
|
if (entry.authMode !== "unprotected") x402Set.add(url);
|
|
1278
1279
|
if (entry.protocols.includes("mpp")) mppSet.add(url);
|
|
1279
1280
|
}
|
|
@@ -1313,6 +1314,7 @@ function createWellKnownHandler(registry, baseUrl, pricesKeys, options = {}) {
|
|
|
1313
1314
|
// src/discovery/openapi.ts
|
|
1314
1315
|
import { NextResponse as NextResponse4 } from "next/server";
|
|
1315
1316
|
function createOpenAPIHandler(registry, baseUrl, pricesKeys, options) {
|
|
1317
|
+
const normalizedBase = baseUrl.replace(/\/+$/, "");
|
|
1316
1318
|
let cached = null;
|
|
1317
1319
|
let validated = false;
|
|
1318
1320
|
return async (_request) => {
|
|
@@ -1339,7 +1341,7 @@ function createOpenAPIHandler(registry, baseUrl, pricesKeys, options) {
|
|
|
1339
1341
|
version: options.version,
|
|
1340
1342
|
...options.contact && { contact: options.contact }
|
|
1341
1343
|
},
|
|
1342
|
-
servers: [{ url: options.baseUrl ??
|
|
1344
|
+
servers: [{ url: (options.baseUrl ?? normalizedBase).replace(/\/+$/, "") }],
|
|
1343
1345
|
tags: Array.from(tagSet).sort().map((name) => ({ name })),
|
|
1344
1346
|
paths
|
|
1345
1347
|
});
|
|
@@ -1411,12 +1413,20 @@ function createRouter(config) {
|
|
|
1411
1413
|
const registry = new RouteRegistry();
|
|
1412
1414
|
const nonceStore = config.siwx?.nonceStore ?? new MemoryNonceStore();
|
|
1413
1415
|
const network = config.network ?? "eip155:8453";
|
|
1414
|
-
const baseUrl = typeof globalThis.process !== "undefined" ? process.env.NEXT_PUBLIC_BASE_URL
|
|
1416
|
+
const baseUrl = config.baseUrl ?? (typeof globalThis.process !== "undefined" ? process.env.NEXT_PUBLIC_BASE_URL : void 0);
|
|
1415
1417
|
if (config.protocols && config.protocols.length === 0) {
|
|
1416
1418
|
throw new Error(
|
|
1417
1419
|
"RouterConfig.protocols cannot be empty. Omit the field to use default ['x402'] or specify protocols explicitly."
|
|
1418
1420
|
);
|
|
1419
1421
|
}
|
|
1422
|
+
if (!baseUrl) {
|
|
1423
|
+
const msg = "baseUrl is required. Pass it in RouterConfig or set NEXT_PUBLIC_BASE_URL (e.g. https://myapp.com). It is used for discovery URLs, OpenAPI servers, and MPP realm.";
|
|
1424
|
+
if (process.env.NODE_ENV === "production") {
|
|
1425
|
+
throw new Error(msg);
|
|
1426
|
+
}
|
|
1427
|
+
console.warn(`[router] ${msg}`);
|
|
1428
|
+
}
|
|
1429
|
+
const resolvedBaseUrl = (baseUrl ?? "http://localhost:3000").replace(/\/+$/, "");
|
|
1420
1430
|
let x402ConfigError;
|
|
1421
1431
|
let mppConfigError;
|
|
1422
1432
|
if ((!config.protocols || config.protocols.includes("x402")) && !config.payeeAddress) {
|
|
@@ -1440,7 +1450,7 @@ function createRouter(config) {
|
|
|
1440
1450
|
}
|
|
1441
1451
|
if (config.plugin?.init) {
|
|
1442
1452
|
try {
|
|
1443
|
-
const result = config.plugin.init({ origin:
|
|
1453
|
+
const result = config.plugin.init({ origin: resolvedBaseUrl });
|
|
1444
1454
|
if (result && typeof result.catch === "function") {
|
|
1445
1455
|
result.catch(() => {
|
|
1446
1456
|
});
|
|
@@ -1489,7 +1499,8 @@ function createRouter(config) {
|
|
|
1489
1499
|
}
|
|
1490
1500
|
})
|
|
1491
1501
|
],
|
|
1492
|
-
secretKey: config.mpp.secretKey
|
|
1502
|
+
secretKey: config.mpp.secretKey,
|
|
1503
|
+
realm: new URL(resolvedBaseUrl).host
|
|
1493
1504
|
});
|
|
1494
1505
|
} catch (err) {
|
|
1495
1506
|
deps.mppx = null;
|
|
@@ -1509,10 +1520,10 @@ function createRouter(config) {
|
|
|
1509
1520
|
return builder;
|
|
1510
1521
|
},
|
|
1511
1522
|
wellKnown(options) {
|
|
1512
|
-
return createWellKnownHandler(registry,
|
|
1523
|
+
return createWellKnownHandler(registry, resolvedBaseUrl, pricesKeys, options);
|
|
1513
1524
|
},
|
|
1514
1525
|
openapi(options) {
|
|
1515
|
-
return createOpenAPIHandler(registry,
|
|
1526
|
+
return createOpenAPIHandler(registry, resolvedBaseUrl, pricesKeys, options);
|
|
1516
1527
|
},
|
|
1517
1528
|
monitors() {
|
|
1518
1529
|
const result = [];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentcash/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"description": "Unified route builder for Next.js App Router APIs with x402, MPP, SIWX, and API key auth",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"@x402/core": "^2.3.0",
|
|
29
29
|
"@x402/evm": "^2.3.0",
|
|
30
30
|
"@x402/extensions": "^2.3.0",
|
|
31
|
-
"mppx": "^0.2
|
|
31
|
+
"mppx": "^0.3.2",
|
|
32
32
|
"next": ">=15.0.0",
|
|
33
33
|
"zod": "^4.0.0",
|
|
34
34
|
"zod-openapi": "^5.0.0"
|
|
@@ -42,12 +42,13 @@
|
|
|
42
42
|
"@changesets/cli": "^2.29.8",
|
|
43
43
|
"@coinbase/x402": "^2.1.0",
|
|
44
44
|
"@eslint/js": "^10.0.1",
|
|
45
|
+
"@modelcontextprotocol/sdk": "^1.26.0",
|
|
45
46
|
"@types/node": "^22.0.0",
|
|
46
47
|
"@x402/core": "^2.3.0",
|
|
47
48
|
"@x402/evm": "^2.3.0",
|
|
48
49
|
"@x402/extensions": "^2.3.0",
|
|
49
50
|
"eslint": "^10.0.0",
|
|
50
|
-
"mppx": "^0.2
|
|
51
|
+
"mppx": "^0.3.2",
|
|
51
52
|
"next": "^15.0.0",
|
|
52
53
|
"prettier": "^3.8.1",
|
|
53
54
|
"react": "^19.0.0",
|