@odatano/x402 0.3.0 → 0.4.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.
- package/.env +0 -0
- package/.github/workflows/test.yaml +10 -1
- package/CHANGELOG.md +27 -2
- package/LICENSE +169 -21
- package/README.md +11 -8
- package/datano-x402.png +0 -0
- package/package.json +5 -4
- package/release-0.4.0.md +58 -0
- package/srv/bridge.d.ts +85 -1
- package/srv/bridge.js +34 -8
- package/srv/client/axios.js +1 -3
- package/srv/client/errors.d.ts +0 -21
- package/srv/client/errors.js +0 -37
- package/srv/client/fetch.js +2 -7
- package/srv/core/decode.js +48 -137
- package/srv/helpers/address.d.ts +31 -0
- package/srv/helpers/address.js +57 -0
- package/srv/helpers/build-unsigned-tx.d.ts +24 -23
- package/srv/helpers/build-unsigned-tx.js +62 -147
- package/srv/middleware/cap.js +27 -2
package/srv/middleware/cap.js
CHANGED
|
@@ -72,6 +72,32 @@ function getResourceUrl(req, opts) {
|
|
|
72
72
|
const httpReq = req;
|
|
73
73
|
return httpReq.http?.req?.originalUrl ?? httpReq.http?.req?.url ?? `cap://${req.event}`;
|
|
74
74
|
}
|
|
75
|
+
function getHttpRes(req) {
|
|
76
|
+
return req.http?.res;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Emit a 402 with the canonical x402 v2 body on the wire.
|
|
80
|
+
*
|
|
81
|
+
* When an Express response is reachable we write the body ourselves so
|
|
82
|
+
* third-party x402 clients see `{ x402Version: 2, accepts: [...] }` at
|
|
83
|
+
* the top level rather than CAP's OData-wrapped `{ error: { message:
|
|
84
|
+
* "<v2-json-string>", ... } }`. The `req.reject` call is always made:
|
|
85
|
+
* its synchronous throw is what terminates CAP's handler pipeline (so
|
|
86
|
+
* the gated `on` handler never runs); CAP's own render attempt no-ops
|
|
87
|
+
* because `headersSent` is already true. Validated against `@sap/cds ^9`.
|
|
88
|
+
*
|
|
89
|
+
* For non-HTTP transports (event invocations, `$batch` reuse, tests
|
|
90
|
+
* without `http.res`) we skip the direct write and let `req.reject` do
|
|
91
|
+
* the whole job — the wrap is irrelevant when there's no HTTP buyer.
|
|
92
|
+
*/
|
|
93
|
+
function send402(req, body) {
|
|
94
|
+
const httpRes = getHttpRes(req);
|
|
95
|
+
if (httpRes && !httpRes.headersSent) {
|
|
96
|
+
httpRes.status(402).json(body);
|
|
97
|
+
}
|
|
98
|
+
req
|
|
99
|
+
.reject(402, JSON.stringify(body));
|
|
100
|
+
}
|
|
75
101
|
/**
|
|
76
102
|
* Attach the x402 gate to a CAP ApplicationService. Returns the service
|
|
77
103
|
* (chainable) so callers can fluently wire multiple middlewares.
|
|
@@ -207,8 +233,7 @@ function gateService(srv, opts) {
|
|
|
207
233
|
if (result.txHash)
|
|
208
234
|
body.transaction = result.txHash;
|
|
209
235
|
}
|
|
210
|
-
req
|
|
211
|
-
.reject(402, JSON.stringify(body));
|
|
236
|
+
send402(req, body);
|
|
212
237
|
});
|
|
213
238
|
return srv;
|
|
214
239
|
}
|