@odatano/x402 0.1.0 → 0.2.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.
@@ -32,7 +32,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
32
32
  exports.gateService = gateService;
33
33
  const cds_1 = __importDefault(require("@sap/cds"));
34
34
  const requirements_1 = require("../core/requirements");
35
- const verify_1 = require("../facilitator/verify");
35
+ const adapter_1 = require("../facilitator/adapter");
36
36
  const errors_1 = require("../core/errors");
37
37
  const log = cds_1.default.log('x402');
38
38
  /**
@@ -96,6 +96,7 @@ function gateService(srv, opts) {
96
96
  if (opts.priceUnits == null && !opts.routePricing) {
97
97
  throw new Error('gateService: priceUnits or routePricing is required');
98
98
  }
99
+ const facilitator = opts.facilitator ?? (0, adapter_1.localFacilitator)();
99
100
  srv.before('*', async function x402CapGate(req) {
100
101
  const priceUnits = pickPriceUnits(req, opts);
101
102
  if (priceUnits == null)
@@ -134,7 +135,7 @@ function gateService(srv, opts) {
134
135
  // would translate the 402 into a 500. ─────────────────────────
135
136
  let result;
136
137
  try {
137
- result = await (0, verify_1.process)(processArgs);
138
+ result = await facilitator.verifyAndSettle(processArgs);
138
139
  }
139
140
  catch (err) {
140
141
  log.error('x402 CAP gate internal error', err);
@@ -17,6 +17,7 @@
17
17
  * can extract the code without breaking wire format.
18
18
  */
19
19
  import type { Request, RequestHandler } from 'express';
20
+ import { type Facilitator } from '../facilitator/adapter';
20
21
  import type { AssetTransferMethod, PaymentClaim, Network } from '../core/types';
21
22
  declare module 'express-serve-static-core' {
22
23
  interface Request {
@@ -60,6 +61,13 @@ export interface X402MiddlewareOptions {
60
61
  * block serving the response.
61
62
  */
62
63
  onAccepted?: (claim: PaymentClaim, req: Request) => void | Promise<void>;
64
+ /**
65
+ * Facilitator implementation handling verify+settle. Default
66
+ * `localFacilitator()` — runs the pipeline in-process via
67
+ * `@odatano/core`. Use `httpFacilitator({ url, apiKey })` to delegate
68
+ * to a hosted service.
69
+ */
70
+ facilitator?: Facilitator;
63
71
  }
64
72
  /** Build the Express middleware. */
65
73
  export declare function x402Middleware(opts: X402MiddlewareOptions): RequestHandler;
@@ -24,7 +24,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
24
24
  exports.x402Middleware = x402Middleware;
25
25
  const cds_1 = __importDefault(require("@sap/cds"));
26
26
  const requirements_1 = require("../core/requirements");
27
- const verify_1 = require("../facilitator/verify");
27
+ const adapter_1 = require("../facilitator/adapter");
28
28
  const errors_1 = require("../core/errors");
29
29
  const log = cds_1.default.log('x402');
30
30
  function pickPriceUnits(req, opts) {
@@ -53,6 +53,7 @@ function x402Middleware(opts) {
53
53
  throw new Error('x402Middleware: priceUnits or routePricing is required');
54
54
  }
55
55
  const skipPaths = opts.skipPaths ?? /(^\/?$|\$metadata|\$batch|^\/?\?|^\/index)/i;
56
+ const facilitator = opts.facilitator ?? (0, adapter_1.localFacilitator)();
56
57
  return async function x402Express(req, res, next) {
57
58
  try {
58
59
  if (skipPaths.test(req.path))
@@ -88,7 +89,7 @@ function x402Middleware(opts) {
88
89
  if (opts.onAccepted) {
89
90
  processArgs.onAccepted = (claim) => opts.onAccepted(claim, req);
90
91
  }
91
- const result = await (0, verify_1.process)(processArgs);
92
+ const result = await facilitator.verifyAndSettle(processArgs);
92
93
  if (result.kind === 'accepted') {
93
94
  res.setHeader('X-PAYMENT-RESPONSE', result.paymentResponseB64);
94
95
  req.payment = result.payment;