@agentcash/router 0.5.2 → 0.6.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 +13 -2
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +13 -2
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -504,6 +504,11 @@ function extractBearerToken(header) {
|
|
|
504
504
|
}
|
|
505
505
|
|
|
506
506
|
// src/orchestrate.ts
|
|
507
|
+
async function resolvePayTo(routeEntry, request, fallback) {
|
|
508
|
+
if (!routeEntry.payTo) return fallback;
|
|
509
|
+
if (typeof routeEntry.payTo === "string") return routeEntry.payTo;
|
|
510
|
+
return routeEntry.payTo(request);
|
|
511
|
+
}
|
|
507
512
|
function createRequestHandler(routeEntry, handler, deps) {
|
|
508
513
|
async function invoke(request, meta, pluginCtx, wallet, account, parsedBody) {
|
|
509
514
|
const ctx = {
|
|
@@ -766,12 +771,13 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
766
771
|
console.error(`[router] ${routeEntry.key}: ${reason}`);
|
|
767
772
|
return fail(500, reason, meta, pluginCtx);
|
|
768
773
|
}
|
|
774
|
+
const payTo = await resolvePayTo(routeEntry, request, deps.payeeAddress);
|
|
769
775
|
const verify = await verifyX402Payment(
|
|
770
776
|
deps.x402Server,
|
|
771
777
|
request,
|
|
772
778
|
routeEntry,
|
|
773
779
|
price,
|
|
774
|
-
|
|
780
|
+
payTo,
|
|
775
781
|
deps.network
|
|
776
782
|
);
|
|
777
783
|
if (!verify?.valid) return await build402(request, routeEntry, deps, meta, pluginCtx);
|
|
@@ -1009,12 +1015,13 @@ async function build402(request, routeEntry, deps, meta, pluginCtx, bodyData) {
|
|
|
1009
1015
|
}
|
|
1010
1016
|
if (routeEntry.protocols.includes("x402") && deps.x402Server) {
|
|
1011
1017
|
try {
|
|
1018
|
+
const payTo = await resolvePayTo(routeEntry, request, deps.payeeAddress);
|
|
1012
1019
|
const { encoded } = await buildX402Challenge(
|
|
1013
1020
|
deps.x402Server,
|
|
1014
1021
|
routeEntry,
|
|
1015
1022
|
request,
|
|
1016
1023
|
challengePrice,
|
|
1017
|
-
|
|
1024
|
+
payTo,
|
|
1018
1025
|
deps.network,
|
|
1019
1026
|
extensions
|
|
1020
1027
|
);
|
|
@@ -1114,6 +1121,8 @@ var RouteBuilder = class {
|
|
|
1114
1121
|
/** @internal */
|
|
1115
1122
|
_minPrice;
|
|
1116
1123
|
/** @internal */
|
|
1124
|
+
_payTo;
|
|
1125
|
+
/** @internal */
|
|
1117
1126
|
_bodySchema;
|
|
1118
1127
|
/** @internal */
|
|
1119
1128
|
_querySchema;
|
|
@@ -1156,6 +1165,7 @@ var RouteBuilder = class {
|
|
|
1156
1165
|
if (options?.protocols) next._protocols = options.protocols;
|
|
1157
1166
|
if (options?.maxPrice) next._maxPrice = options.maxPrice;
|
|
1158
1167
|
if (options?.minPrice) next._minPrice = options.minPrice;
|
|
1168
|
+
if (options?.payTo) next._payTo = options.payTo;
|
|
1159
1169
|
if (typeof pricing === "object" && "tiers" in pricing) {
|
|
1160
1170
|
for (const [tierKey, tierConfig] of Object.entries(pricing.tiers)) {
|
|
1161
1171
|
if (!tierKey) {
|
|
@@ -1293,6 +1303,7 @@ var RouteBuilder = class {
|
|
|
1293
1303
|
method: this._method,
|
|
1294
1304
|
maxPrice: this._maxPrice,
|
|
1295
1305
|
minPrice: this._minPrice,
|
|
1306
|
+
payTo: this._payTo,
|
|
1296
1307
|
apiKeyResolver: this._apiKeyResolver,
|
|
1297
1308
|
providerName: this._providerName,
|
|
1298
1309
|
providerConfig: this._providerConfig,
|
package/dist/index.d.cts
CHANGED
|
@@ -171,6 +171,8 @@ interface PaidOptions {
|
|
|
171
171
|
protocols?: ProtocolType[];
|
|
172
172
|
maxPrice?: string;
|
|
173
173
|
minPrice?: string;
|
|
174
|
+
/** Override the payment recipient. String for static, function for dynamic (receives the Request). */
|
|
175
|
+
payTo?: string | ((request: Request) => string | Promise<string>);
|
|
174
176
|
}
|
|
175
177
|
interface HandlerContext<TBody = undefined, TQuery = undefined> {
|
|
176
178
|
body: TBody;
|
|
@@ -220,6 +222,7 @@ interface RouteEntry {
|
|
|
220
222
|
method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH';
|
|
221
223
|
maxPrice?: string;
|
|
222
224
|
minPrice?: string;
|
|
225
|
+
payTo?: string | ((request: Request) => string | Promise<string>);
|
|
223
226
|
apiKeyResolver?: (key: string) => unknown | Promise<unknown>;
|
|
224
227
|
providerName?: string;
|
|
225
228
|
providerConfig?: ProviderConfig;
|
|
@@ -325,6 +328,7 @@ declare class RouteBuilder<TBody = undefined, TQuery = undefined, HasAuth extend
|
|
|
325
328
|
/** @internal */ _protocols: ProtocolType[];
|
|
326
329
|
/** @internal */ _maxPrice: string | undefined;
|
|
327
330
|
/** @internal */ _minPrice: string | undefined;
|
|
331
|
+
/** @internal */ _payTo: string | ((request: Request) => string | Promise<string>) | undefined;
|
|
328
332
|
/** @internal */ _bodySchema: ZodType | undefined;
|
|
329
333
|
/** @internal */ _querySchema: ZodType | undefined;
|
|
330
334
|
/** @internal */ _outputSchema: ZodType | undefined;
|
package/dist/index.d.ts
CHANGED
|
@@ -171,6 +171,8 @@ interface PaidOptions {
|
|
|
171
171
|
protocols?: ProtocolType[];
|
|
172
172
|
maxPrice?: string;
|
|
173
173
|
minPrice?: string;
|
|
174
|
+
/** Override the payment recipient. String for static, function for dynamic (receives the Request). */
|
|
175
|
+
payTo?: string | ((request: Request) => string | Promise<string>);
|
|
174
176
|
}
|
|
175
177
|
interface HandlerContext<TBody = undefined, TQuery = undefined> {
|
|
176
178
|
body: TBody;
|
|
@@ -220,6 +222,7 @@ interface RouteEntry {
|
|
|
220
222
|
method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH';
|
|
221
223
|
maxPrice?: string;
|
|
222
224
|
minPrice?: string;
|
|
225
|
+
payTo?: string | ((request: Request) => string | Promise<string>);
|
|
223
226
|
apiKeyResolver?: (key: string) => unknown | Promise<unknown>;
|
|
224
227
|
providerName?: string;
|
|
225
228
|
providerConfig?: ProviderConfig;
|
|
@@ -325,6 +328,7 @@ declare class RouteBuilder<TBody = undefined, TQuery = undefined, HasAuth extend
|
|
|
325
328
|
/** @internal */ _protocols: ProtocolType[];
|
|
326
329
|
/** @internal */ _maxPrice: string | undefined;
|
|
327
330
|
/** @internal */ _minPrice: string | undefined;
|
|
331
|
+
/** @internal */ _payTo: string | ((request: Request) => string | Promise<string>) | undefined;
|
|
328
332
|
/** @internal */ _bodySchema: ZodType | undefined;
|
|
329
333
|
/** @internal */ _querySchema: ZodType | undefined;
|
|
330
334
|
/** @internal */ _outputSchema: ZodType | undefined;
|
package/dist/index.js
CHANGED
|
@@ -467,6 +467,11 @@ function extractBearerToken(header) {
|
|
|
467
467
|
}
|
|
468
468
|
|
|
469
469
|
// src/orchestrate.ts
|
|
470
|
+
async function resolvePayTo(routeEntry, request, fallback) {
|
|
471
|
+
if (!routeEntry.payTo) return fallback;
|
|
472
|
+
if (typeof routeEntry.payTo === "string") return routeEntry.payTo;
|
|
473
|
+
return routeEntry.payTo(request);
|
|
474
|
+
}
|
|
470
475
|
function createRequestHandler(routeEntry, handler, deps) {
|
|
471
476
|
async function invoke(request, meta, pluginCtx, wallet, account, parsedBody) {
|
|
472
477
|
const ctx = {
|
|
@@ -729,12 +734,13 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
729
734
|
console.error(`[router] ${routeEntry.key}: ${reason}`);
|
|
730
735
|
return fail(500, reason, meta, pluginCtx);
|
|
731
736
|
}
|
|
737
|
+
const payTo = await resolvePayTo(routeEntry, request, deps.payeeAddress);
|
|
732
738
|
const verify = await verifyX402Payment(
|
|
733
739
|
deps.x402Server,
|
|
734
740
|
request,
|
|
735
741
|
routeEntry,
|
|
736
742
|
price,
|
|
737
|
-
|
|
743
|
+
payTo,
|
|
738
744
|
deps.network
|
|
739
745
|
);
|
|
740
746
|
if (!verify?.valid) return await build402(request, routeEntry, deps, meta, pluginCtx);
|
|
@@ -972,12 +978,13 @@ async function build402(request, routeEntry, deps, meta, pluginCtx, bodyData) {
|
|
|
972
978
|
}
|
|
973
979
|
if (routeEntry.protocols.includes("x402") && deps.x402Server) {
|
|
974
980
|
try {
|
|
981
|
+
const payTo = await resolvePayTo(routeEntry, request, deps.payeeAddress);
|
|
975
982
|
const { encoded } = await buildX402Challenge(
|
|
976
983
|
deps.x402Server,
|
|
977
984
|
routeEntry,
|
|
978
985
|
request,
|
|
979
986
|
challengePrice,
|
|
980
|
-
|
|
987
|
+
payTo,
|
|
981
988
|
deps.network,
|
|
982
989
|
extensions
|
|
983
990
|
);
|
|
@@ -1077,6 +1084,8 @@ var RouteBuilder = class {
|
|
|
1077
1084
|
/** @internal */
|
|
1078
1085
|
_minPrice;
|
|
1079
1086
|
/** @internal */
|
|
1087
|
+
_payTo;
|
|
1088
|
+
/** @internal */
|
|
1080
1089
|
_bodySchema;
|
|
1081
1090
|
/** @internal */
|
|
1082
1091
|
_querySchema;
|
|
@@ -1119,6 +1128,7 @@ var RouteBuilder = class {
|
|
|
1119
1128
|
if (options?.protocols) next._protocols = options.protocols;
|
|
1120
1129
|
if (options?.maxPrice) next._maxPrice = options.maxPrice;
|
|
1121
1130
|
if (options?.minPrice) next._minPrice = options.minPrice;
|
|
1131
|
+
if (options?.payTo) next._payTo = options.payTo;
|
|
1122
1132
|
if (typeof pricing === "object" && "tiers" in pricing) {
|
|
1123
1133
|
for (const [tierKey, tierConfig] of Object.entries(pricing.tiers)) {
|
|
1124
1134
|
if (!tierKey) {
|
|
@@ -1256,6 +1266,7 @@ var RouteBuilder = class {
|
|
|
1256
1266
|
method: this._method,
|
|
1257
1267
|
maxPrice: this._maxPrice,
|
|
1258
1268
|
minPrice: this._minPrice,
|
|
1269
|
+
payTo: this._payTo,
|
|
1259
1270
|
apiKeyResolver: this._apiKeyResolver,
|
|
1260
1271
|
providerName: this._providerName,
|
|
1261
1272
|
providerConfig: this._providerConfig,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentcash/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.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": {
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
"@x402/evm": "^2.3.0",
|
|
49
49
|
"@x402/extensions": "^2.3.0",
|
|
50
50
|
"eslint": "^10.0.0",
|
|
51
|
-
"mppx": "^0.3.
|
|
51
|
+
"mppx": "^0.3.4",
|
|
52
52
|
"next": "^15.0.0",
|
|
53
53
|
"prettier": "^3.8.1",
|
|
54
54
|
"react": "^19.0.0",
|