@cryptonly/x402-express 0.6.0 → 0.8.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/README.md +18 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Accept x402 payments in Express, with Cryptonly as the facilitator.
|
|
|
12
12
|
|
|
13
13
|
---
|
|
14
14
|
|
|
15
|
-
Cryptonly verifies and settles each payment, and pays the network fee. Payments go straight to your wallet. Your prices live in the Cryptonly dashboard, so a price change needs no deploy.
|
|
15
|
+
Cryptonly verifies and settles each payment, and pays the network fee. Payments go straight to your wallet. Your prices live in the Cryptonly dashboard, so a price change needs no deploy. An endpoint can have a price in each currency you accept, such as USDC on Base and on Solana: the `402` offers all of them and the buyer picks one.
|
|
16
16
|
|
|
17
17
|
You need a Cryptonly API key with the `x402.verify` and `x402.settle` scopes. The [quickstart](https://docs.cryptonly.net/guides/x402/quickstart/) covers getting one.
|
|
18
18
|
|
|
@@ -63,12 +63,29 @@ The buyer is charged only when the handler succeeds. A `4xx`/`5xx` response or a
|
|
|
63
63
|
| `svmSchemes` | `[new ExactSvmScheme()]` if `@x402/svm` is installed | Schemes for Solana. |
|
|
64
64
|
| `logger` | `console` | Gets reload failures and skipped routes. |
|
|
65
65
|
| `paywallConfig` | — | Passed to the stock `paymentMiddleware`. |
|
|
66
|
+
| `pathPrefix` | — | Prefix a proxy strips before the request reaches the app. See [Behind a proxy](#behind-a-proxy). |
|
|
67
|
+
| `publicPath` | — | Maps the app's path to the public path, for other rewrites. |
|
|
68
|
+
| `publicBaseUrl` | the request's | Public origin buyers call, for `resource.url` in the `402`. |
|
|
66
69
|
| `createMiddleware` | stock `paymentMiddleware` | Builds the inner middleware, for example to add hooks. |
|
|
67
70
|
|
|
68
71
|
The middleware also has `ready()`, `refresh()`, `routes()` and `close()`.
|
|
69
72
|
|
|
70
73
|
On NestJS, use [`@cryptonly/x402-nestjs`](https://www.npmjs.com/package/@cryptonly/x402-nestjs) for decorators. This middleware also works there, applied with `consumer.apply(payments).forRoutes(...)`.
|
|
71
74
|
|
|
75
|
+
## Behind a proxy
|
|
76
|
+
|
|
77
|
+
Dashboard prices are keyed by the public path. If a reverse proxy rewrites the path, tell the middleware how, or the paths don't match and the routes are served **for free**: the middleware only gates the paths it knows.
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
// The proxy serves /v1/weather from the app's /weather:
|
|
81
|
+
cryptonlyPaymentMiddleware({ apiKey, pathPrefix: '/v1', publicBaseUrl: 'https://api.example.com' });
|
|
82
|
+
|
|
83
|
+
// The proxy adds /internal:
|
|
84
|
+
cryptonlyPaymentMiddleware({ apiKey, publicPath: (path) => path.replace(/^\/internal/, '') });
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`publicBaseUrl` sets the URL buyers see in the `402`. Without it, the URL is built from the request's host and protocol. Behind a proxy, set Express `trust proxy` so the protocol is right.
|
|
88
|
+
|
|
72
89
|
## Solana
|
|
73
90
|
|
|
74
91
|
Install `@x402/svm` and `@solana/kit`, and add a Solana payout wallet in the dashboard. Solana prices then work with no other change.
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import type { NextFunction, Request, Response } from 'express';
|
|
|
2
2
|
import type { RoutesConfig } from '@x402/core/server';
|
|
3
3
|
import type { SchemeNetworkServer } from '@x402/core/types';
|
|
4
4
|
import { type PaywallConfig } from '@x402/express';
|
|
5
|
-
import { type CryptonlyFacilitatorClient, type X402Config, type X402Logger, type X402RouteSourceOptions } from '@cryptonly/x402';
|
|
5
|
+
import { type CryptonlyFacilitatorClient, type X402Config, type X402Logger, type X402PublicRequestMapper, type X402RouteSourceOptions } from '@cryptonly/x402';
|
|
6
6
|
/** An Express middleware. */
|
|
7
7
|
export type ExpressMiddleware = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
|
|
8
8
|
/** What {@link InnerMiddlewareFactory} gets besides the routes. */
|
|
@@ -14,6 +14,8 @@ export type InnerMiddlewareContext = {
|
|
|
14
14
|
/** Registered on each Solana network of the account. */
|
|
15
15
|
svmSchemes: readonly SchemeNetworkServer[];
|
|
16
16
|
paywallConfig?: PaywallConfig;
|
|
17
|
+
/** Maps requests to their public URL; set with `pathPrefix`, `publicPath` or `publicBaseUrl`. */
|
|
18
|
+
publicRequest?: X402PublicRequestMapper;
|
|
17
19
|
};
|
|
18
20
|
/**
|
|
19
21
|
* Builds the payment middleware for one set of routes. The default is the
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC/D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAmC,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AACpF,OAAO,EAML,KAAK,0BAA0B,EAC/B,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,uBAAuB,EAE5B,KAAK,sBAAsB,EAC5B,MAAM,iBAAiB,CAAC;AAEzB,6BAA6B;AAC7B,MAAM,MAAM,iBAAiB,GAAG,CAC9B,GAAG,EAAE,OA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M,sBAAsB,EAAE,sBAWlC,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,0BAA0B,CACxC,OAAO,EAAE,iCAAiC,GACzC,0BAA0B,CAqC5B;AAED,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACpE,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ exports.cryptonlyPaymentMiddleware = cryptonlyPaymentMiddleware;
|
|
|
5
5
|
const express_1 = require("@x402/express");
|
|
6
6
|
const x402_1 = require("@cryptonly/x402");
|
|
7
7
|
/** The default {@link InnerMiddlewareFactory}: stock `@x402/express`. */
|
|
8
|
-
const stockPaymentMiddleware = (routes, { config, facilitator, schemes, svmSchemes, paywallConfig }) => (0, express_1.
|
|
8
|
+
const stockPaymentMiddleware = (routes, { config, facilitator, schemes, svmSchemes, paywallConfig, publicRequest }) => (0, express_1.paymentMiddlewareFromHTTPServer)((0, x402_1.createHTTPResourceServer)((0, x402_1.createResourceServer)(config, facilitator, schemes, svmSchemes), routes, publicRequest), paywallConfig);
|
|
9
9
|
exports.stockPaymentMiddleware = stockPaymentMiddleware;
|
|
10
10
|
/**
|
|
11
11
|
* Express middleware whose paid routes and prices come from the Cryptonly
|
|
@@ -29,12 +29,14 @@ exports.stockPaymentMiddleware = stockPaymentMiddleware;
|
|
|
29
29
|
*/
|
|
30
30
|
function cryptonlyPaymentMiddleware(options) {
|
|
31
31
|
const createMiddleware = options.createMiddleware ?? exports.stockPaymentMiddleware;
|
|
32
|
+
const publicRequest = (0, x402_1.createPublicRequestMapper)(options);
|
|
32
33
|
const source = (0, x402_1.createX402RouteSource)(options, (routes, config) => createMiddleware(routes, {
|
|
33
34
|
config,
|
|
34
35
|
facilitator: source.facilitator,
|
|
35
36
|
schemes: source.schemes,
|
|
36
37
|
svmSchemes: source.svmSchemes,
|
|
37
38
|
...(options.paywallConfig ? { paywallConfig: options.paywallConfig } : {}),
|
|
39
|
+
...(publicRequest ? { publicRequest } : {}),
|
|
38
40
|
}));
|
|
39
41
|
const middleware = async (req, res, next) => {
|
|
40
42
|
if (!source.current()) {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAiHA,gEAuCC;AArJD,2CAAoF;AACpF,0CAYyB;AA+DzB,yEAAyE;AAClE,MAAM,sBAAsB,GAA2B,CAC5D,MAAM,EACN,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,aAAa,EAAE,EAC1E,EAAE,CACF,IAAA,yCAA+B,EAC7B,IAAA,+BAAwB,EACtB,IAAA,2BAAoB,EAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,CAAC,EAC9D,MAAM,EACN,aAAa,CACd,EACD,aAAa,CACd,CAAC;AAXS,QAAA,sBAAsB,0BAW/B;AAEJ;;;;;;;;;;;;;;;;;;;GAmBG;AACH,SAAgB,0BAA0B,CACxC,OAA0C;IAE1C,MAAM,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,IAAI,8BAAsB,CAAC;IAC5E,MAAM,aAAa,GAAG,IAAA,gCAAyB,EAAC,OAAO,CAAC,CAAC;IACzD,MAAM,MAAM,GAAuC,IAAA,4BAAqB,EACtE,OAAO,EACP,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,CACjB,gBAAgB,CAAC,MAAM,EAAE;QACvB,MAAM;QACN,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,GAAG,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,aAAa,EA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x402-express",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Express middleware for x402 payments with Cryptonly as the facilitator; prices come from the Cryptonly dashboard.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"private": false,
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"access": "public"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@cryptonly/x402": "0.
|
|
54
|
+
"@cryptonly/x402": "0.8.0"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
57
|
"@x402/core": "^2",
|