@puga-labs/x402-mantle-sdk 0.3.3 → 0.3.4

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.
@@ -0,0 +1,73 @@
1
+ import {
2
+ buildRouteKey,
3
+ checkPayment,
4
+ validateAddress
5
+ } from "./chunk-ZLCKBFVJ.js";
6
+ import {
7
+ MANTLE_DEFAULTS,
8
+ __export,
9
+ getDefaultAssetForNetwork,
10
+ usdCentsToAtomic
11
+ } from "./chunk-HEZZ74SI.js";
12
+
13
+ // src/server/adapters/nextjs.ts
14
+ var nextjs_exports = {};
15
+ __export(nextjs_exports, {
16
+ isPaywallErrorResponse: () => isPaywallErrorResponse,
17
+ mantlePaywall: () => mantlePaywall
18
+ });
19
+ import { NextResponse } from "next/server";
20
+ function mantlePaywall(opts) {
21
+ const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
22
+ validateAddress(payTo, "payTo");
23
+ const priceUsdCents = Math.round(priceUsd * 100);
24
+ return function(handler) {
25
+ return async (req) => {
26
+ const url = new URL(req.url);
27
+ const method = req.method;
28
+ const path = url.pathname;
29
+ const routeKey = buildRouteKey(method, path);
30
+ const network = MANTLE_DEFAULTS.NETWORK;
31
+ const assetConfig = getDefaultAssetForNetwork(network);
32
+ const maxAmountRequiredBigInt = usdCentsToAtomic(
33
+ priceUsdCents,
34
+ assetConfig.decimals
35
+ );
36
+ const paymentRequirements = {
37
+ scheme: "exact",
38
+ network,
39
+ asset: assetConfig.address,
40
+ maxAmountRequired: maxAmountRequiredBigInt.toString(),
41
+ payTo,
42
+ price: `$${(priceUsdCents / 100).toFixed(2)}`,
43
+ currency: "USD"
44
+ };
45
+ const paymentHeader = req.headers.get("X-PAYMENT") || req.headers.get("x-payment") || null;
46
+ const result = await checkPayment({
47
+ paymentHeader,
48
+ paymentRequirements,
49
+ facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
50
+ routeKey,
51
+ network,
52
+ asset: assetConfig.address,
53
+ telemetry,
54
+ onPaymentSettled
55
+ });
56
+ if (!result.isValid) {
57
+ return NextResponse.json(result.responseBody, {
58
+ status: result.statusCode
59
+ });
60
+ }
61
+ return handler(req);
62
+ };
63
+ };
64
+ }
65
+ function isPaywallErrorResponse(response) {
66
+ return typeof response === "object" && response !== null && "error" in response && typeof response.error === "string";
67
+ }
68
+
69
+ export {
70
+ mantlePaywall,
71
+ isPaywallErrorResponse,
72
+ nextjs_exports
73
+ };
@@ -0,0 +1,82 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ import { M as MinimalPaywallOptions } from './types-CqQ6OgRi.js';
3
+ import { P as PaymentRequirements } from './types-BFUqKBBO.js';
4
+
5
+ /**
6
+ * Error response returned when payment verification fails.
7
+ * This can be due to missing payment, invalid payment, or verification errors.
8
+ */
9
+ interface PaywallErrorResponse {
10
+ error: string;
11
+ paymentRequirements?: PaymentRequirements;
12
+ paymentHeader?: null;
13
+ invalidReason?: string | null;
14
+ details?: string;
15
+ }
16
+ /**
17
+ * Next.js App Router route handler type.
18
+ */
19
+ type NextJSHandler<T = any> = (req: NextRequest) => Promise<NextResponse<T>>;
20
+ /**
21
+ * Wrapper function that adds x402 payment verification to Next.js route handler.
22
+ * Returns a handler that may return either the original handler's response type (T)
23
+ * or a PaywallErrorResponse if payment verification fails.
24
+ */
25
+ type NextJSPaywallWrapper = <T>(handler: NextJSHandler<T>) => NextJSHandler<T | PaywallErrorResponse>;
26
+ /**
27
+ * Create Next.js App Router middleware for x402 payment verification.
28
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // app/api/generate-image/route.ts
33
+ * import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/nextjs'
34
+ *
35
+ * const pay = mantlePaywall({
36
+ * priceUsd: 0.01,
37
+ * payTo: process.env.PAY_TO!,
38
+ * });
39
+ *
40
+ * // No 'as any' needed! TypeScript correctly infers the union type
41
+ * export const POST = pay(async (req: NextRequest) => {
42
+ * const { prompt } = await req.json();
43
+ * // Your handler code here
44
+ * return NextResponse.json({ success: true, imageUrl: "..." });
45
+ * });
46
+ * // TypeScript knows POST returns: NextResponse<{ success: boolean; imageUrl: string } | PaywallErrorResponse>
47
+ * ```
48
+ *
49
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
50
+ * @returns Function that wraps Next.js route handlers with payment verification.
51
+ */
52
+ declare function mantlePaywall(opts: MinimalPaywallOptions): NextJSPaywallWrapper;
53
+ /**
54
+ * Type guard to check if a response is a paywall error response.
55
+ * Useful for handling the union type returned by the paywall wrapper.
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const response = await fetch('/api/protected');
60
+ * const data = await response.json();
61
+ *
62
+ * if (isPaywallErrorResponse(data)) {
63
+ * // Handle error: data.error, data.paymentRequirements, etc.
64
+ * console.error('Payment required:', data.paymentRequirements);
65
+ * } else {
66
+ * // Handle success: data has type T
67
+ * console.log('Success:', data);
68
+ * }
69
+ * ```
70
+ */
71
+ declare function isPaywallErrorResponse(response: unknown): response is PaywallErrorResponse;
72
+
73
+ type nextjs_NextJSHandler<T = any> = NextJSHandler<T>;
74
+ type nextjs_NextJSPaywallWrapper = NextJSPaywallWrapper;
75
+ type nextjs_PaywallErrorResponse = PaywallErrorResponse;
76
+ declare const nextjs_isPaywallErrorResponse: typeof isPaywallErrorResponse;
77
+ declare const nextjs_mantlePaywall: typeof mantlePaywall;
78
+ declare namespace nextjs {
79
+ export { type nextjs_NextJSHandler as NextJSHandler, type nextjs_NextJSPaywallWrapper as NextJSPaywallWrapper, type nextjs_PaywallErrorResponse as PaywallErrorResponse, nextjs_isPaywallErrorResponse as isPaywallErrorResponse, nextjs_mantlePaywall as mantlePaywall };
80
+ }
81
+
82
+ export { type NextJSHandler as N, type PaywallErrorResponse as P, type NextJSPaywallWrapper as a, isPaywallErrorResponse as i, mantlePaywall as m, nextjs as n };
@@ -0,0 +1,82 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ import { M as MinimalPaywallOptions } from './types-CrOsOHcX.cjs';
3
+ import { P as PaymentRequirements } from './types-BFUqKBBO.cjs';
4
+
5
+ /**
6
+ * Error response returned when payment verification fails.
7
+ * This can be due to missing payment, invalid payment, or verification errors.
8
+ */
9
+ interface PaywallErrorResponse {
10
+ error: string;
11
+ paymentRequirements?: PaymentRequirements;
12
+ paymentHeader?: null;
13
+ invalidReason?: string | null;
14
+ details?: string;
15
+ }
16
+ /**
17
+ * Next.js App Router route handler type.
18
+ */
19
+ type NextJSHandler<T = any> = (req: NextRequest) => Promise<NextResponse<T>>;
20
+ /**
21
+ * Wrapper function that adds x402 payment verification to Next.js route handler.
22
+ * Returns a handler that may return either the original handler's response type (T)
23
+ * or a PaywallErrorResponse if payment verification fails.
24
+ */
25
+ type NextJSPaywallWrapper = <T>(handler: NextJSHandler<T>) => NextJSHandler<T | PaywallErrorResponse>;
26
+ /**
27
+ * Create Next.js App Router middleware for x402 payment verification.
28
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * // app/api/generate-image/route.ts
33
+ * import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/nextjs'
34
+ *
35
+ * const pay = mantlePaywall({
36
+ * priceUsd: 0.01,
37
+ * payTo: process.env.PAY_TO!,
38
+ * });
39
+ *
40
+ * // No 'as any' needed! TypeScript correctly infers the union type
41
+ * export const POST = pay(async (req: NextRequest) => {
42
+ * const { prompt } = await req.json();
43
+ * // Your handler code here
44
+ * return NextResponse.json({ success: true, imageUrl: "..." });
45
+ * });
46
+ * // TypeScript knows POST returns: NextResponse<{ success: boolean; imageUrl: string } | PaywallErrorResponse>
47
+ * ```
48
+ *
49
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
50
+ * @returns Function that wraps Next.js route handlers with payment verification.
51
+ */
52
+ declare function mantlePaywall(opts: MinimalPaywallOptions): NextJSPaywallWrapper;
53
+ /**
54
+ * Type guard to check if a response is a paywall error response.
55
+ * Useful for handling the union type returned by the paywall wrapper.
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * const response = await fetch('/api/protected');
60
+ * const data = await response.json();
61
+ *
62
+ * if (isPaywallErrorResponse(data)) {
63
+ * // Handle error: data.error, data.paymentRequirements, etc.
64
+ * console.error('Payment required:', data.paymentRequirements);
65
+ * } else {
66
+ * // Handle success: data has type T
67
+ * console.log('Success:', data);
68
+ * }
69
+ * ```
70
+ */
71
+ declare function isPaywallErrorResponse(response: unknown): response is PaywallErrorResponse;
72
+
73
+ type nextjs_NextJSHandler<T = any> = NextJSHandler<T>;
74
+ type nextjs_NextJSPaywallWrapper = NextJSPaywallWrapper;
75
+ type nextjs_PaywallErrorResponse = PaywallErrorResponse;
76
+ declare const nextjs_isPaywallErrorResponse: typeof isPaywallErrorResponse;
77
+ declare const nextjs_mantlePaywall: typeof mantlePaywall;
78
+ declare namespace nextjs {
79
+ export { type nextjs_NextJSHandler as NextJSHandler, type nextjs_NextJSPaywallWrapper as NextJSPaywallWrapper, type nextjs_PaywallErrorResponse as PaywallErrorResponse, nextjs_isPaywallErrorResponse as isPaywallErrorResponse, nextjs_mantlePaywall as mantlePaywall };
80
+ }
81
+
82
+ export { type NextJSHandler as N, type PaywallErrorResponse as P, type NextJSPaywallWrapper as a, isPaywallErrorResponse as i, mantlePaywall as m, nextjs as n };
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var server_nextjs_exports = {};
22
22
  __export(server_nextjs_exports, {
23
23
  MANTLE_DEFAULTS: () => MANTLE_DEFAULTS,
24
+ isPaywallErrorResponse: () => isPaywallErrorResponse,
24
25
  mantlePaywall: () => mantlePaywall
25
26
  });
26
27
  module.exports = __toCommonJS(server_nextjs_exports);
@@ -332,8 +333,12 @@ function mantlePaywall(opts) {
332
333
  };
333
334
  };
334
335
  }
336
+ function isPaywallErrorResponse(response) {
337
+ return typeof response === "object" && response !== null && "error" in response && typeof response.error === "string";
338
+ }
335
339
  // Annotate the CommonJS export names for ESM import in node:
336
340
  0 && (module.exports = {
337
341
  MANTLE_DEFAULTS,
342
+ isPaywallErrorResponse,
338
343
  mantlePaywall
339
344
  });
@@ -1,4 +1,4 @@
1
- export { N as NextJSHandler, a as NextJSPaywallWrapper, m as mantlePaywall } from './nextjs-Duecps0q.cjs';
1
+ export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-CY13JXkV.cjs';
2
2
  export { M as MinimalPaywallOptions, c as PaymentCheckInput, d as PaymentCheckResult, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CrOsOHcX.cjs';
3
3
  export { A as AssetConfig, a as Authorization, E as EIP1193Provider, N as NetworkId, d as PaymentHeaderBase64, c as PaymentHeaderObject, b as PaymentHeaderPayload, P as PaymentRequirements } from './types-BFUqKBBO.cjs';
4
4
  export { M as MANTLE_DEFAULTS } from './constants-CsIL25uQ.cjs';
@@ -1,4 +1,4 @@
1
- export { N as NextJSHandler, a as NextJSPaywallWrapper, m as mantlePaywall } from './nextjs-TFhFiQuL.js';
1
+ export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-BgyAWzT3.js';
2
2
  export { M as MinimalPaywallOptions, c as PaymentCheckInput, d as PaymentCheckResult, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CqQ6OgRi.js';
3
3
  export { A as AssetConfig, a as Authorization, E as EIP1193Provider, N as NetworkId, d as PaymentHeaderBase64, c as PaymentHeaderObject, b as PaymentHeaderPayload, P as PaymentRequirements } from './types-BFUqKBBO.js';
4
4
  export { M as MANTLE_DEFAULTS } from './constants-0ncqvV_O.js';
@@ -1,11 +1,13 @@
1
1
  import {
2
+ isPaywallErrorResponse,
2
3
  mantlePaywall
3
- } from "./chunk-CXRILT3C.js";
4
+ } from "./chunk-IT6VV6YL.js";
4
5
  import "./chunk-ZLCKBFVJ.js";
5
6
  import {
6
7
  MANTLE_DEFAULTS
7
8
  } from "./chunk-HEZZ74SI.js";
8
9
  export {
9
10
  MANTLE_DEFAULTS,
11
+ isPaywallErrorResponse,
10
12
  mantlePaywall
11
13
  };
package/dist/server.cjs CHANGED
@@ -378,6 +378,7 @@ function mantlePaywall(opts) {
378
378
  // src/server/adapters/nextjs.ts
379
379
  var nextjs_exports = {};
380
380
  __export(nextjs_exports, {
381
+ isPaywallErrorResponse: () => isPaywallErrorResponse,
381
382
  mantlePaywall: () => mantlePaywall2
382
383
  });
383
384
  var import_server = require("next/server");
@@ -426,6 +427,9 @@ function mantlePaywall2(opts) {
426
427
  };
427
428
  };
428
429
  }
430
+ function isPaywallErrorResponse(response) {
431
+ return typeof response === "object" && response !== null && "error" in response && typeof response.error === "string";
432
+ }
429
433
 
430
434
  // src/server/adapters/web-standards.ts
431
435
  var web_standards_exports = {};
package/dist/server.d.cts CHANGED
@@ -2,7 +2,7 @@ import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-CrOsOHc
2
2
  export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CrOsOHcX.cjs';
3
3
  import { c as PaymentHeaderObject } from './types-BFUqKBBO.cjs';
4
4
  export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-eQOPxfnI.cjs';
5
- export { n as nextjs } from './nextjs-Duecps0q.cjs';
5
+ export { n as nextjs } from './nextjs-CY13JXkV.cjs';
6
6
  export { w as web } from './web-standards-BNQyWzBC.cjs';
7
7
  import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-CoOdbZSp.cjs';
8
8
  import 'express';
package/dist/server.d.ts CHANGED
@@ -2,7 +2,7 @@ import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-CqQ6OgR
2
2
  export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CqQ6OgRi.js';
3
3
  import { c as PaymentHeaderObject } from './types-BFUqKBBO.js';
4
4
  export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-D8EwEcOL.js';
5
- export { n as nextjs } from './nextjs-TFhFiQuL.js';
5
+ export { n as nextjs } from './nextjs-BgyAWzT3.js';
6
6
  export { w as web } from './web-standards-D8j1kZxd.js';
7
7
  import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-DTzov_EE.js';
8
8
  import 'express';
package/dist/server.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  } from "./chunk-QWQUSRLY.js";
7
7
  import {
8
8
  nextjs_exports
9
- } from "./chunk-CXRILT3C.js";
9
+ } from "./chunk-IT6VV6YL.js";
10
10
  import {
11
11
  web_standards_exports
12
12
  } from "./chunk-ZROK2XOB.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@puga-labs/x402-mantle-sdk",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "x402 payments SDK for Mantle (USDC, gasless, facilitator-based)",
5
5
  "license": "MIT",
6
6
  "author": "Evgenii Pugachev <cyprus.pugamuga@gmail.com>",