@faremeter/middleware 0.15.0 → 0.17.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/dist/src/hono.js CHANGED
@@ -1,31 +1,54 @@
1
- import { handleMiddlewareRequest, createPaymentRequiredResponseCache, } from "./common.js";
1
+ import { handleMiddlewareRequest, createPaymentRequiredResponseCache, resolveSupportedVersions, } from "./common.js";
2
+ /**
3
+ * Creates Hono middleware that gates routes behind x402 payment.
4
+ *
5
+ * The middleware intercepts requests, checks for payment headers, communicates
6
+ * with the facilitator to validate and settle payments, and only allows the
7
+ * request to proceed if payment is successful.
8
+ *
9
+ * @param args - Configuration including facilitator URL and accepted payment types
10
+ * @returns A Hono middleware handler
11
+ */
2
12
  export async function createMiddleware(args) {
3
- const { getPaymentRequiredResponse } = createPaymentRequiredResponseCache(args.cacheConfig);
13
+ // Validate configuration at creation time
14
+ const supportedVersions = resolveSupportedVersions(args.supportedVersions);
15
+ const { getPaymentRequiredResponse, getPaymentRequiredResponseV2 } = createPaymentRequiredResponseCache(args.cacheConfig);
4
16
  return async (c, next) => {
5
17
  return await handleMiddlewareRequest({
6
18
  ...args,
19
+ supportedVersions,
7
20
  resource: c.req.url,
8
21
  getHeader: (key) => c.req.header(key),
22
+ setResponseHeader: (key, value) => c.header(key, value),
9
23
  getPaymentRequiredResponse,
10
- sendJSONResponse: (status, body) => {
24
+ getPaymentRequiredResponseV2,
25
+ sendJSONResponse: (status, body, headers) => {
11
26
  c.status(status);
12
- return c.json(body);
27
+ if (headers) {
28
+ for (const [key, value] of Object.entries(headers)) {
29
+ c.header(key, value);
30
+ }
31
+ }
32
+ if (body) {
33
+ return c.json(body);
34
+ }
35
+ return c.body(null);
13
36
  },
14
37
  body: async ({ verify, settle }) => {
15
38
  if (args.verifyBeforeSettle) {
16
39
  // If configured, try to verify the transaction before running
17
40
  // the next operation.
18
41
  const verifyResult = await verify();
19
- if (verifyResult !== undefined) {
20
- return verifyResult;
42
+ if (!verifyResult.success) {
43
+ return verifyResult.errorResponse;
21
44
  }
22
45
  }
23
46
  else {
24
47
  // Otherwise just settle the payment beforehand, like we've
25
48
  // done historically.
26
49
  const settleResult = await settle();
27
- if (settleResult !== undefined) {
28
- return settleResult;
50
+ if (!settleResult.success) {
51
+ return settleResult.errorResponse;
29
52
  }
30
53
  }
31
54
  await next();
@@ -33,14 +56,14 @@ export async function createMiddleware(args) {
33
56
  // Close out the verification, by actually settling the
34
57
  // payment.
35
58
  const settleResult = await settle();
36
- if (settleResult !== undefined) {
59
+ if (!settleResult.success) {
37
60
  // If the settlement fails, we need to explicitly
38
61
  // overwrite the downstream result. See:
39
62
  //
40
63
  // https://hono.dev/docs/guides/middleware#modify-the-response-after-next
41
64
  //
42
65
  c.res = undefined;
43
- c.res = settleResult;
66
+ c.res = settleResult.errorResponse;
44
67
  }
45
68
  }
46
69
  },
@@ -1,5 +1,31 @@
1
+ /**
2
+ * @title Middleware Package
3
+ * @sidebarTitle Middleware
4
+ * @description Server middleware for gating routes behind x402 payments
5
+ * @packageDocumentation
6
+ */
7
+ /**
8
+ * @title Express Middleware
9
+ * @sidebarTitle Middleware / Express
10
+ * @description x402 payment middleware for Express.js
11
+ */
1
12
  export * as express from "./express.js";
13
+ /**
14
+ * @title Hono Middleware
15
+ * @sidebarTitle Middleware / Hono
16
+ * @description x402 payment middleware for Hono web framework
17
+ */
2
18
  export * as hono from "./hono.js";
19
+ /**
20
+ * @title Common Middleware
21
+ * @sidebarTitle Middleware / Common
22
+ * @description Framework-agnostic middleware utilities and types
23
+ */
3
24
  export * as common from "./common.js";
25
+ /**
26
+ * @title Middleware Cache
27
+ * @sidebarTitle Middleware / Cache
28
+ * @description LRU cache with time-based expiration for payment requirements
29
+ */
4
30
  export * as cache from "./cache.js";
5
31
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH;;;;GAIG;AACH,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC;;;;GAIG;AACH,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B;;;;GAIG;AACH,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC;;;;GAIG;AACH,OAAO,KAAK,KAAK,MAAM,SAAS,CAAC"}
package/dist/src/index.js CHANGED
@@ -1,4 +1,30 @@
1
+ /**
2
+ * @title Middleware Package
3
+ * @sidebarTitle Middleware
4
+ * @description Server middleware for gating routes behind x402 payments
5
+ * @packageDocumentation
6
+ */
7
+ /**
8
+ * @title Express Middleware
9
+ * @sidebarTitle Middleware / Express
10
+ * @description x402 payment middleware for Express.js
11
+ */
1
12
  export * as express from "./express.js";
13
+ /**
14
+ * @title Hono Middleware
15
+ * @sidebarTitle Middleware / Hono
16
+ * @description x402 payment middleware for Hono web framework
17
+ */
2
18
  export * as hono from "./hono.js";
19
+ /**
20
+ * @title Common Middleware
21
+ * @sidebarTitle Middleware / Common
22
+ * @description Framework-agnostic middleware utilities and types
23
+ */
3
24
  export * as common from "./common.js";
25
+ /**
26
+ * @title Middleware Cache
27
+ * @sidebarTitle Middleware / Cache
28
+ * @description LRU cache with time-based expiration for payment requirements
29
+ */
4
30
  export * as cache from "./cache.js";
@@ -1,2 +1,2 @@
1
- export declare const logger: import("@logtape/logtape").Logger;
1
+ export declare const logger: import("@faremeter/logs").Logger;
2
2
  //# sourceMappingURL=logger.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,MAAM,mCAAyC,CAAC"}
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../src/logger.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,MAAM,kCAA+C,CAAC"}
@@ -1,2 +1,2 @@
1
- import { getLogger } from "@logtape/logtape";
2
- export const logger = getLogger(["faremeter", "middleware"]);
1
+ import { getLogger } from "@faremeter/logs";
2
+ export const logger = await getLogger(["faremeter", "middleware"]);