@agentcash/router 0.6.1 → 0.6.3
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 +24 -20
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +24 -20
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -536,13 +536,13 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
536
536
|
}, ctx);
|
|
537
537
|
return { response, rawResult };
|
|
538
538
|
}
|
|
539
|
-
function finalize(response, rawResult, meta, pluginCtx) {
|
|
539
|
+
function finalize(response, rawResult, meta, pluginCtx, requestBody) {
|
|
540
540
|
fireProviderQuota(routeEntry, response, rawResult, deps, pluginCtx);
|
|
541
|
-
firePluginResponse(deps, pluginCtx, meta, response);
|
|
541
|
+
firePluginResponse(deps, pluginCtx, meta, response, requestBody, rawResult);
|
|
542
542
|
}
|
|
543
|
-
function fail(status, message, meta, pluginCtx) {
|
|
543
|
+
function fail(status, message, meta, pluginCtx, requestBody) {
|
|
544
544
|
const response = import_server2.NextResponse.json({ success: false, error: message }, { status });
|
|
545
|
-
firePluginResponse(deps, pluginCtx, meta, response);
|
|
545
|
+
firePluginResponse(deps, pluginCtx, meta, response, requestBody);
|
|
546
546
|
return response;
|
|
547
547
|
}
|
|
548
548
|
return async (request) => {
|
|
@@ -561,7 +561,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
561
561
|
} catch (err) {
|
|
562
562
|
const status = err.status ?? 400;
|
|
563
563
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
564
|
-
return fail(status, message, meta, pluginCtx);
|
|
564
|
+
return fail(status, message, meta, pluginCtx, body2.data);
|
|
565
565
|
}
|
|
566
566
|
}
|
|
567
567
|
const { response, rawResult } = await invoke(
|
|
@@ -572,7 +572,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
572
572
|
account2,
|
|
573
573
|
body2.data
|
|
574
574
|
);
|
|
575
|
-
finalize(response, rawResult, meta, pluginCtx);
|
|
575
|
+
finalize(response, rawResult, meta, pluginCtx, body2.data);
|
|
576
576
|
return response;
|
|
577
577
|
}
|
|
578
578
|
if (routeEntry.authMode === "unprotected") {
|
|
@@ -616,7 +616,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
616
616
|
} catch (err) {
|
|
617
617
|
const status = err.status ?? 400;
|
|
618
618
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
619
|
-
return fail(status, message, meta, pluginCtx);
|
|
619
|
+
return fail(status, message, meta, pluginCtx, earlyBodyData);
|
|
620
620
|
}
|
|
621
621
|
}
|
|
622
622
|
}
|
|
@@ -633,7 +633,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
633
633
|
} catch (err) {
|
|
634
634
|
const status = err.status ?? 400;
|
|
635
635
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
636
|
-
return fail(status, message, meta, pluginCtx);
|
|
636
|
+
return fail(status, message, meta, pluginCtx, earlyBodyResult.data);
|
|
637
637
|
}
|
|
638
638
|
}
|
|
639
639
|
if (!request.headers.get("SIGN-IN-WITH-X")) {
|
|
@@ -739,7 +739,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
739
739
|
} catch (err) {
|
|
740
740
|
const status = err.status ?? 400;
|
|
741
741
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
742
|
-
return fail(status, message, meta, pluginCtx);
|
|
742
|
+
return fail(status, message, meta, pluginCtx, body.data);
|
|
743
743
|
}
|
|
744
744
|
}
|
|
745
745
|
let price;
|
|
@@ -750,7 +750,8 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
750
750
|
err.status ?? 500,
|
|
751
751
|
err instanceof Error ? err.message : "Price resolution failed",
|
|
752
752
|
meta,
|
|
753
|
-
pluginCtx
|
|
753
|
+
pluginCtx,
|
|
754
|
+
body.data
|
|
754
755
|
);
|
|
755
756
|
}
|
|
756
757
|
if (!routeEntry.protocols.includes(protocol)) {
|
|
@@ -762,14 +763,15 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
762
763
|
400,
|
|
763
764
|
`This route does not accept ${protocol} payments. Accepted protocols: ${accepted}`,
|
|
764
765
|
meta,
|
|
765
|
-
pluginCtx
|
|
766
|
+
pluginCtx,
|
|
767
|
+
body.data
|
|
766
768
|
);
|
|
767
769
|
}
|
|
768
770
|
if (protocol === "x402") {
|
|
769
771
|
if (!deps.x402Server) {
|
|
770
772
|
const reason = deps.x402InitError ? `x402 facilitator initialization failed: ${deps.x402InitError}` : "x402 server not initialized \u2014 ensure @x402/core, @x402/evm, and @coinbase/x402 are installed";
|
|
771
773
|
console.error(`[router] ${routeEntry.key}: ${reason}`);
|
|
772
|
-
return fail(500, reason, meta, pluginCtx);
|
|
774
|
+
return fail(500, reason, meta, pluginCtx, body.data);
|
|
773
775
|
}
|
|
774
776
|
const payTo = await resolvePayTo(routeEntry, request, deps.payeeAddress);
|
|
775
777
|
const verify = await verifyX402Payment(
|
|
@@ -835,17 +837,17 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
835
837
|
message: `Settlement failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
836
838
|
route: routeEntry.key
|
|
837
839
|
});
|
|
838
|
-
return fail(500, "Settlement failed", meta, pluginCtx);
|
|
840
|
+
return fail(500, "Settlement failed", meta, pluginCtx, body.data);
|
|
839
841
|
}
|
|
840
842
|
}
|
|
841
|
-
finalize(response, rawResult, meta, pluginCtx);
|
|
843
|
+
finalize(response, rawResult, meta, pluginCtx, body.data);
|
|
842
844
|
return response;
|
|
843
845
|
}
|
|
844
846
|
if (protocol === "mpp") {
|
|
845
847
|
if (!deps.mppx) {
|
|
846
848
|
const reason = deps.mppInitError ? `MPP initialization failed: ${deps.mppInitError}` : "MPP not initialized \u2014 ensure mppx is installed and mpp config (secretKey, currency, recipient) is correct";
|
|
847
849
|
console.error(`[router] ${routeEntry.key}: ${reason}`);
|
|
848
|
-
return fail(500, reason, meta, pluginCtx);
|
|
850
|
+
return fail(500, reason, meta, pluginCtx, body.data);
|
|
849
851
|
}
|
|
850
852
|
let mppResult;
|
|
851
853
|
try {
|
|
@@ -858,7 +860,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
858
860
|
message: `MPP charge failed: ${message}`,
|
|
859
861
|
route: routeEntry.key
|
|
860
862
|
});
|
|
861
|
-
return fail(500, `MPP payment processing failed: ${message}`, meta, pluginCtx);
|
|
863
|
+
return fail(500, `MPP payment processing failed: ${message}`, meta, pluginCtx, body.data);
|
|
862
864
|
}
|
|
863
865
|
if (mppResult.status === 402) {
|
|
864
866
|
console.warn(
|
|
@@ -890,10 +892,10 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
890
892
|
);
|
|
891
893
|
if (response.status < 400) {
|
|
892
894
|
const receiptResponse = mppResult.withReceipt(response);
|
|
893
|
-
finalize(receiptResponse, rawResult, meta, pluginCtx);
|
|
895
|
+
finalize(receiptResponse, rawResult, meta, pluginCtx, body.data);
|
|
894
896
|
return receiptResponse;
|
|
895
897
|
}
|
|
896
|
-
finalize(response, rawResult, meta, pluginCtx);
|
|
898
|
+
finalize(response, rawResult, meta, pluginCtx, body.data);
|
|
897
899
|
return response;
|
|
898
900
|
}
|
|
899
901
|
return await build402(request, routeEntry, deps, meta, pluginCtx);
|
|
@@ -1056,13 +1058,15 @@ async function build402(request, routeEntry, deps, meta, pluginCtx, bodyData) {
|
|
|
1056
1058
|
firePluginResponse(deps, pluginCtx, meta, response);
|
|
1057
1059
|
return response;
|
|
1058
1060
|
}
|
|
1059
|
-
function firePluginResponse(deps, pluginCtx, meta, response) {
|
|
1061
|
+
function firePluginResponse(deps, pluginCtx, meta, response, requestBody, responseBody) {
|
|
1060
1062
|
firePluginHook(deps.plugin, "onResponse", pluginCtx, {
|
|
1061
1063
|
statusCode: response.status,
|
|
1062
1064
|
statusText: response.statusText,
|
|
1063
1065
|
duration: Date.now() - meta.startTime,
|
|
1064
1066
|
contentType: response.headers.get("content-type"),
|
|
1065
|
-
headers: Object.fromEntries(response.headers.entries())
|
|
1067
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
1068
|
+
requestBody,
|
|
1069
|
+
responseBody
|
|
1066
1070
|
});
|
|
1067
1071
|
if (response.status >= 400 && response.status !== 402) {
|
|
1068
1072
|
firePluginHook(deps.plugin, "onError", pluginCtx, {
|
package/dist/index.d.cts
CHANGED
|
@@ -89,6 +89,10 @@ interface ResponseMeta {
|
|
|
89
89
|
duration: number;
|
|
90
90
|
contentType: string | null;
|
|
91
91
|
headers: Record<string, string>;
|
|
92
|
+
/** Parsed request body (when .body() was used). undefined when no body was parsed. */
|
|
93
|
+
requestBody?: unknown;
|
|
94
|
+
/** Handler return value. undefined for raw Response returns (streams) or error paths. */
|
|
95
|
+
responseBody?: unknown;
|
|
92
96
|
}
|
|
93
97
|
interface ErrorEvent {
|
|
94
98
|
status: number;
|
|
@@ -406,7 +410,7 @@ interface ServiceRouter<TPriceKeys extends string = never> {
|
|
|
406
410
|
monitors(): MonitorEntry[];
|
|
407
411
|
registry: RouteRegistry;
|
|
408
412
|
}
|
|
409
|
-
declare function createRouter<const P extends Record<string, string> = Record<
|
|
413
|
+
declare function createRouter<const P extends Record<string, string> = Record<never, string>>(config: RouterConfig & {
|
|
410
414
|
prices?: P;
|
|
411
415
|
}): ServiceRouter<Extract<keyof P, string>>;
|
|
412
416
|
|
package/dist/index.d.ts
CHANGED
|
@@ -89,6 +89,10 @@ interface ResponseMeta {
|
|
|
89
89
|
duration: number;
|
|
90
90
|
contentType: string | null;
|
|
91
91
|
headers: Record<string, string>;
|
|
92
|
+
/** Parsed request body (when .body() was used). undefined when no body was parsed. */
|
|
93
|
+
requestBody?: unknown;
|
|
94
|
+
/** Handler return value. undefined for raw Response returns (streams) or error paths. */
|
|
95
|
+
responseBody?: unknown;
|
|
92
96
|
}
|
|
93
97
|
interface ErrorEvent {
|
|
94
98
|
status: number;
|
|
@@ -406,7 +410,7 @@ interface ServiceRouter<TPriceKeys extends string = never> {
|
|
|
406
410
|
monitors(): MonitorEntry[];
|
|
407
411
|
registry: RouteRegistry;
|
|
408
412
|
}
|
|
409
|
-
declare function createRouter<const P extends Record<string, string> = Record<
|
|
413
|
+
declare function createRouter<const P extends Record<string, string> = Record<never, string>>(config: RouterConfig & {
|
|
410
414
|
prices?: P;
|
|
411
415
|
}): ServiceRouter<Extract<keyof P, string>>;
|
|
412
416
|
|
package/dist/index.js
CHANGED
|
@@ -499,13 +499,13 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
499
499
|
}, ctx);
|
|
500
500
|
return { response, rawResult };
|
|
501
501
|
}
|
|
502
|
-
function finalize(response, rawResult, meta, pluginCtx) {
|
|
502
|
+
function finalize(response, rawResult, meta, pluginCtx, requestBody) {
|
|
503
503
|
fireProviderQuota(routeEntry, response, rawResult, deps, pluginCtx);
|
|
504
|
-
firePluginResponse(deps, pluginCtx, meta, response);
|
|
504
|
+
firePluginResponse(deps, pluginCtx, meta, response, requestBody, rawResult);
|
|
505
505
|
}
|
|
506
|
-
function fail(status, message, meta, pluginCtx) {
|
|
506
|
+
function fail(status, message, meta, pluginCtx, requestBody) {
|
|
507
507
|
const response = NextResponse2.json({ success: false, error: message }, { status });
|
|
508
|
-
firePluginResponse(deps, pluginCtx, meta, response);
|
|
508
|
+
firePluginResponse(deps, pluginCtx, meta, response, requestBody);
|
|
509
509
|
return response;
|
|
510
510
|
}
|
|
511
511
|
return async (request) => {
|
|
@@ -524,7 +524,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
524
524
|
} catch (err) {
|
|
525
525
|
const status = err.status ?? 400;
|
|
526
526
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
527
|
-
return fail(status, message, meta, pluginCtx);
|
|
527
|
+
return fail(status, message, meta, pluginCtx, body2.data);
|
|
528
528
|
}
|
|
529
529
|
}
|
|
530
530
|
const { response, rawResult } = await invoke(
|
|
@@ -535,7 +535,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
535
535
|
account2,
|
|
536
536
|
body2.data
|
|
537
537
|
);
|
|
538
|
-
finalize(response, rawResult, meta, pluginCtx);
|
|
538
|
+
finalize(response, rawResult, meta, pluginCtx, body2.data);
|
|
539
539
|
return response;
|
|
540
540
|
}
|
|
541
541
|
if (routeEntry.authMode === "unprotected") {
|
|
@@ -579,7 +579,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
579
579
|
} catch (err) {
|
|
580
580
|
const status = err.status ?? 400;
|
|
581
581
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
582
|
-
return fail(status, message, meta, pluginCtx);
|
|
582
|
+
return fail(status, message, meta, pluginCtx, earlyBodyData);
|
|
583
583
|
}
|
|
584
584
|
}
|
|
585
585
|
}
|
|
@@ -596,7 +596,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
596
596
|
} catch (err) {
|
|
597
597
|
const status = err.status ?? 400;
|
|
598
598
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
599
|
-
return fail(status, message, meta, pluginCtx);
|
|
599
|
+
return fail(status, message, meta, pluginCtx, earlyBodyResult.data);
|
|
600
600
|
}
|
|
601
601
|
}
|
|
602
602
|
if (!request.headers.get("SIGN-IN-WITH-X")) {
|
|
@@ -702,7 +702,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
702
702
|
} catch (err) {
|
|
703
703
|
const status = err.status ?? 400;
|
|
704
704
|
const message = err instanceof Error ? err.message : "Validation failed";
|
|
705
|
-
return fail(status, message, meta, pluginCtx);
|
|
705
|
+
return fail(status, message, meta, pluginCtx, body.data);
|
|
706
706
|
}
|
|
707
707
|
}
|
|
708
708
|
let price;
|
|
@@ -713,7 +713,8 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
713
713
|
err.status ?? 500,
|
|
714
714
|
err instanceof Error ? err.message : "Price resolution failed",
|
|
715
715
|
meta,
|
|
716
|
-
pluginCtx
|
|
716
|
+
pluginCtx,
|
|
717
|
+
body.data
|
|
717
718
|
);
|
|
718
719
|
}
|
|
719
720
|
if (!routeEntry.protocols.includes(protocol)) {
|
|
@@ -725,14 +726,15 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
725
726
|
400,
|
|
726
727
|
`This route does not accept ${protocol} payments. Accepted protocols: ${accepted}`,
|
|
727
728
|
meta,
|
|
728
|
-
pluginCtx
|
|
729
|
+
pluginCtx,
|
|
730
|
+
body.data
|
|
729
731
|
);
|
|
730
732
|
}
|
|
731
733
|
if (protocol === "x402") {
|
|
732
734
|
if (!deps.x402Server) {
|
|
733
735
|
const reason = deps.x402InitError ? `x402 facilitator initialization failed: ${deps.x402InitError}` : "x402 server not initialized \u2014 ensure @x402/core, @x402/evm, and @coinbase/x402 are installed";
|
|
734
736
|
console.error(`[router] ${routeEntry.key}: ${reason}`);
|
|
735
|
-
return fail(500, reason, meta, pluginCtx);
|
|
737
|
+
return fail(500, reason, meta, pluginCtx, body.data);
|
|
736
738
|
}
|
|
737
739
|
const payTo = await resolvePayTo(routeEntry, request, deps.payeeAddress);
|
|
738
740
|
const verify = await verifyX402Payment(
|
|
@@ -798,17 +800,17 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
798
800
|
message: `Settlement failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
799
801
|
route: routeEntry.key
|
|
800
802
|
});
|
|
801
|
-
return fail(500, "Settlement failed", meta, pluginCtx);
|
|
803
|
+
return fail(500, "Settlement failed", meta, pluginCtx, body.data);
|
|
802
804
|
}
|
|
803
805
|
}
|
|
804
|
-
finalize(response, rawResult, meta, pluginCtx);
|
|
806
|
+
finalize(response, rawResult, meta, pluginCtx, body.data);
|
|
805
807
|
return response;
|
|
806
808
|
}
|
|
807
809
|
if (protocol === "mpp") {
|
|
808
810
|
if (!deps.mppx) {
|
|
809
811
|
const reason = deps.mppInitError ? `MPP initialization failed: ${deps.mppInitError}` : "MPP not initialized \u2014 ensure mppx is installed and mpp config (secretKey, currency, recipient) is correct";
|
|
810
812
|
console.error(`[router] ${routeEntry.key}: ${reason}`);
|
|
811
|
-
return fail(500, reason, meta, pluginCtx);
|
|
813
|
+
return fail(500, reason, meta, pluginCtx, body.data);
|
|
812
814
|
}
|
|
813
815
|
let mppResult;
|
|
814
816
|
try {
|
|
@@ -821,7 +823,7 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
821
823
|
message: `MPP charge failed: ${message}`,
|
|
822
824
|
route: routeEntry.key
|
|
823
825
|
});
|
|
824
|
-
return fail(500, `MPP payment processing failed: ${message}`, meta, pluginCtx);
|
|
826
|
+
return fail(500, `MPP payment processing failed: ${message}`, meta, pluginCtx, body.data);
|
|
825
827
|
}
|
|
826
828
|
if (mppResult.status === 402) {
|
|
827
829
|
console.warn(
|
|
@@ -853,10 +855,10 @@ function createRequestHandler(routeEntry, handler, deps) {
|
|
|
853
855
|
);
|
|
854
856
|
if (response.status < 400) {
|
|
855
857
|
const receiptResponse = mppResult.withReceipt(response);
|
|
856
|
-
finalize(receiptResponse, rawResult, meta, pluginCtx);
|
|
858
|
+
finalize(receiptResponse, rawResult, meta, pluginCtx, body.data);
|
|
857
859
|
return receiptResponse;
|
|
858
860
|
}
|
|
859
|
-
finalize(response, rawResult, meta, pluginCtx);
|
|
861
|
+
finalize(response, rawResult, meta, pluginCtx, body.data);
|
|
860
862
|
return response;
|
|
861
863
|
}
|
|
862
864
|
return await build402(request, routeEntry, deps, meta, pluginCtx);
|
|
@@ -1019,13 +1021,15 @@ async function build402(request, routeEntry, deps, meta, pluginCtx, bodyData) {
|
|
|
1019
1021
|
firePluginResponse(deps, pluginCtx, meta, response);
|
|
1020
1022
|
return response;
|
|
1021
1023
|
}
|
|
1022
|
-
function firePluginResponse(deps, pluginCtx, meta, response) {
|
|
1024
|
+
function firePluginResponse(deps, pluginCtx, meta, response, requestBody, responseBody) {
|
|
1023
1025
|
firePluginHook(deps.plugin, "onResponse", pluginCtx, {
|
|
1024
1026
|
statusCode: response.status,
|
|
1025
1027
|
statusText: response.statusText,
|
|
1026
1028
|
duration: Date.now() - meta.startTime,
|
|
1027
1029
|
contentType: response.headers.get("content-type"),
|
|
1028
|
-
headers: Object.fromEntries(response.headers.entries())
|
|
1030
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
1031
|
+
requestBody,
|
|
1032
|
+
responseBody
|
|
1029
1033
|
});
|
|
1030
1034
|
if (response.status >= 400 && response.status !== 402) {
|
|
1031
1035
|
firePluginHook(deps.plugin, "onError", pluginCtx, {
|