@puga-labs/x402-mantle-sdk 0.3.8 → 0.3.10

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.
Files changed (49) hide show
  1. package/dist/chunk-3DGAB7HD.js +126 -0
  2. package/dist/chunk-CKBTOS7X.js +86 -0
  3. package/dist/chunk-IEJB5W26.js +113 -0
  4. package/dist/chunk-IXIFGPJ2.js +250 -0
  5. package/dist/chunk-JXMWK3BO.js +96 -0
  6. package/dist/chunk-P5FKQVHW.js +99 -0
  7. package/dist/chunk-T4DIHTBP.js +83 -0
  8. package/dist/chunk-UVYA6H32.js +293 -0
  9. package/dist/express-BWE0nQty.d.cts +68 -0
  10. package/dist/express-BvuN0Lx1.d.ts +68 -0
  11. package/dist/express-DqyVgO5n.d.cts +68 -0
  12. package/dist/express-DxxlKmmF.d.ts +68 -0
  13. package/dist/index.cjs +89 -10
  14. package/dist/index.d.cts +3 -3
  15. package/dist/index.d.ts +3 -3
  16. package/dist/index.js +2 -2
  17. package/dist/nextjs-Bujo9Okf.d.cts +89 -0
  18. package/dist/nextjs-CzSejZe8.d.ts +89 -0
  19. package/dist/nextjs-DGcN_MGa.d.ts +89 -0
  20. package/dist/nextjs-EoISXVEo.d.cts +89 -0
  21. package/dist/server-express.cjs +89 -10
  22. package/dist/server-express.d.cts +2 -2
  23. package/dist/server-express.d.ts +2 -2
  24. package/dist/server-express.js +2 -2
  25. package/dist/server-nextjs.cjs +90 -11
  26. package/dist/server-nextjs.d.cts +2 -2
  27. package/dist/server-nextjs.d.ts +2 -2
  28. package/dist/server-nextjs.js +2 -2
  29. package/dist/server-web.cjs +90 -11
  30. package/dist/server-web.d.cts +2 -2
  31. package/dist/server-web.d.ts +2 -2
  32. package/dist/server-web.js +2 -2
  33. package/dist/server.cjs +141 -12
  34. package/dist/server.d.cts +8 -7
  35. package/dist/server.d.ts +8 -7
  36. package/dist/server.js +4 -4
  37. package/dist/types-B87bD2yo.d.cts +102 -0
  38. package/dist/types-Ba0v9XsC.d.ts +108 -0
  39. package/dist/types-BkGUHT4x.d.cts +108 -0
  40. package/dist/types-DEpSrXCf.d.ts +112 -0
  41. package/dist/types-DrBw0xwj.d.cts +112 -0
  42. package/dist/types-DvKDSdL6.d.ts +106 -0
  43. package/dist/types-X6DeBEgb.d.cts +106 -0
  44. package/dist/types-vicT7qsY.d.ts +102 -0
  45. package/dist/web-standards-BJcdcxD6.d.ts +77 -0
  46. package/dist/web-standards-C6JwCDmd.d.cts +77 -0
  47. package/dist/web-standards-DsCZRJPE.d.ts +77 -0
  48. package/dist/web-standards-QCbyQ14G.d.cts +77 -0
  49. package/package.json +1 -1
@@ -0,0 +1,68 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ import { b as RoutesConfig, M as MinimalPaywallOptions } from './types-vicT7qsY.js';
3
+
4
+ /**
5
+ * Express middleware function type for Mantle paywall.
6
+ */
7
+ type MantleMiddleware = (req: Request, res: Response, next: NextFunction) => Promise<void>;
8
+ /** Config for createPaymentMiddleware. */
9
+ interface PaymentMiddlewareConfig {
10
+ /** Base URL of facilitator, e.g. https://facilitator.nosubs.ai */
11
+ facilitatorUrl: string;
12
+ /** Recipient address (developer). Validated at runtime. */
13
+ receiverAddress: string;
14
+ /** Map of protected routes and their pricing. */
15
+ routes: RoutesConfig;
16
+ /** Optional API key for hosted facilitator billing. */
17
+ apiKey?: string;
18
+ /**
19
+ * Optional hook called whenever a payment is successfully settled.
20
+ */
21
+ onPaymentSettled?: MinimalPaywallOptions["onPaymentSettled"];
22
+ /** Optional: Send usage telemetry for billing/analytics. */
23
+ telemetry?: MinimalPaywallOptions["telemetry"];
24
+ }
25
+ /**
26
+ * Create Express middleware for x402 payment verification on multiple routes.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const middleware = createPaymentMiddleware({
31
+ * facilitatorUrl: 'https://facilitator.nosubs.ai',
32
+ * receiverAddress: '0x...',
33
+ * routes: {
34
+ * 'POST /api/generate': { priceUsdCents: 1, network: 'mantle-mainnet' },
35
+ * 'GET /api/data': { priceUsdCents: 5, network: 'mantle-mainnet' },
36
+ * }
37
+ * });
38
+ *
39
+ * app.use(middleware);
40
+ * ```
41
+ */
42
+ declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): MantleMiddleware;
43
+ /**
44
+ * Simplified wrapper for protecting a single route with x402 payments.
45
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * const pay = mantlePaywall({ priceUsd: 0.01, payTo: "0x..." });
50
+ * app.post('/api/generate', pay, async (req, res) => {
51
+ * // Your handler code here
52
+ * });
53
+ * ```
54
+ *
55
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
56
+ * @returns Express middleware function for single-route protection.
57
+ */
58
+ declare function mantlePaywall(opts: MinimalPaywallOptions): MantleMiddleware;
59
+
60
+ type express_MantleMiddleware = MantleMiddleware;
61
+ type express_PaymentMiddlewareConfig = PaymentMiddlewareConfig;
62
+ declare const express_createPaymentMiddleware: typeof createPaymentMiddleware;
63
+ declare const express_mantlePaywall: typeof mantlePaywall;
64
+ declare namespace express {
65
+ export { type express_MantleMiddleware as MantleMiddleware, type express_PaymentMiddlewareConfig as PaymentMiddlewareConfig, express_createPaymentMiddleware as createPaymentMiddleware, express_mantlePaywall as mantlePaywall };
66
+ }
67
+
68
+ export { type MantleMiddleware as M, type PaymentMiddlewareConfig as P, createPaymentMiddleware as c, express as e, mantlePaywall as m };
package/dist/index.cjs CHANGED
@@ -159,9 +159,20 @@ function createTelemetryEvent(entry, config) {
159
159
  priceUsd: entry.paymentRequirements?.price
160
160
  };
161
161
  }
162
- async function sendTelemetry(event, endpoint) {
162
+ async function sendTelemetry(event, endpoint, debug) {
163
163
  const targetEndpoint = endpoint ?? DEFAULT_TELEMETRY_ENDPOINT;
164
+ if (debug) {
165
+ console.log(`[x402-debug:telemetry] \u{1F4E4} Sending telemetry event:`, {
166
+ event: event.event,
167
+ endpoint: targetEndpoint,
168
+ projectKey: event.projectKey.substring(0, 10) + "...",
169
+ route: event.route,
170
+ responseStatus: event.responseStatus,
171
+ serviceDelivered: event.serviceDelivered
172
+ });
173
+ }
164
174
  if (!targetEndpoint) {
175
+ if (debug) console.log(`[x402-debug:telemetry] \u26A0\uFE0F No endpoint configured, skipping`);
165
176
  return;
166
177
  }
167
178
  try {
@@ -173,17 +184,29 @@ async function sendTelemetry(event, endpoint) {
173
184
  },
174
185
  body: JSON.stringify(event)
175
186
  });
187
+ if (debug) {
188
+ console.log(`[x402-debug:telemetry] ${response.ok ? "\u2705" : "\u274C"} Telemetry sent: HTTP ${response.status}`);
189
+ }
176
190
  if (!response.ok) {
177
191
  console.warn(
178
192
  `[x402-telemetry] Failed to send event: HTTP ${response.status}`
179
193
  );
180
194
  }
181
195
  } catch (err) {
196
+ if (debug) {
197
+ console.error(`[x402-debug:telemetry] \u274C Error sending:`, err);
198
+ }
182
199
  console.error("[x402-telemetry] Error sending telemetry:", err);
183
200
  }
184
201
  }
185
202
 
186
203
  // src/server/core/verifyPayment.ts
204
+ function debugLog(config, prefix, message, data) {
205
+ if (config?.debug) {
206
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
207
+ console.log(`[${timestamp}] [x402-debug:${prefix}]`, message, data || "");
208
+ }
209
+ }
187
210
  async function checkPayment(input) {
188
211
  const {
189
212
  paymentHeader,
@@ -253,12 +276,13 @@ async function checkPayment(input) {
253
276
  isValid: false
254
277
  };
255
278
  }
256
- if (onPaymentSettled) {
279
+ let baseLogEntry = null;
280
+ if (onPaymentSettled || telemetry) {
257
281
  try {
258
282
  const headerObj = decodePaymentHeader(paymentHeader);
259
283
  const { authorization } = headerObj.payload;
260
284
  const assetConfig = getDefaultAssetForNetwork(network);
261
- const logEntry = {
285
+ baseLogEntry = {
262
286
  id: authorization.nonce,
263
287
  from: authorization.from,
264
288
  to: authorization.to,
@@ -270,12 +294,12 @@ async function checkPayment(input) {
270
294
  facilitatorUrl,
271
295
  paymentRequirements
272
296
  };
273
- onPaymentSettled(logEntry);
274
- if (telemetry) {
275
- const event = createTelemetryEvent(logEntry, telemetry);
276
- sendTelemetry(event, telemetry.endpoint).catch(
277
- (err) => console.error("[x402-telemetry] Async send failed:", err)
278
- );
297
+ debugLog(telemetry, "verify", "\u2705 Payment verified", {
298
+ nonce: authorization.nonce,
299
+ route: routeKey
300
+ });
301
+ if (onPaymentSettled) {
302
+ onPaymentSettled(baseLogEntry);
279
303
  }
280
304
  } catch (err) {
281
305
  console.error(
@@ -284,11 +308,41 @@ async function checkPayment(input) {
284
308
  );
285
309
  }
286
310
  }
311
+ const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
312
+ try {
313
+ debugLog(telemetry, "callback", "\u{1F4E4} Telemetry callback invoked", {
314
+ responseStatus,
315
+ hasError: !!error
316
+ });
317
+ const event = createTelemetryEvent(baseLogEntry, telemetry);
318
+ event.responseStatus = responseStatus;
319
+ event.errorMessage = error;
320
+ event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
321
+ sendTelemetry(event, telemetry.endpoint, telemetry.debug).catch(
322
+ (err) => console.error("[x402-telemetry] Async send failed:", err)
323
+ );
324
+ } catch (err) {
325
+ console.error("[x402-telemetry] Error creating telemetry event:", err);
326
+ }
327
+ } : void 0;
328
+ if (telemetry && baseLogEntry) {
329
+ debugLog(telemetry, "callback", "\u2705 Telemetry callback created", {
330
+ route: routeKey,
331
+ hasTelemetryConfig: !!telemetry,
332
+ hasLogEntry: !!baseLogEntry
333
+ });
334
+ } else if (telemetry) {
335
+ debugLog(telemetry, "callback", "\u26A0\uFE0F Telemetry callback NOT created", {
336
+ hasTelemetryConfig: !!telemetry,
337
+ hasLogEntry: !!baseLogEntry
338
+ });
339
+ }
287
340
  return {
288
341
  status: "verified",
289
342
  statusCode: 200,
290
343
  responseBody: null,
291
- isValid: true
344
+ isValid: true,
345
+ sendTelemetryAfterResponse
292
346
  };
293
347
  } catch (err) {
294
348
  console.error(
@@ -309,6 +363,12 @@ async function checkPayment(input) {
309
363
  }
310
364
 
311
365
  // src/server/adapters/express.ts
366
+ function debugLog2(config, prefix, message, data) {
367
+ if (config?.debug) {
368
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
369
+ console.log(`[${timestamp}] [x402-debug:${prefix}]`, message, data || "");
370
+ }
371
+ }
312
372
  function createPaymentMiddleware(config) {
313
373
  const { facilitatorUrl, receiverAddress, routes, apiKey, onPaymentSettled, telemetry } = config;
314
374
  if (!facilitatorUrl) {
@@ -359,6 +419,25 @@ function createPaymentMiddleware(config) {
359
419
  res.status(result.statusCode).json(result.responseBody);
360
420
  return;
361
421
  }
422
+ debugLog2(telemetry, "handler", "\u25B6\uFE0F Handler execution started (Express middleware)");
423
+ if (result.sendTelemetryAfterResponse) {
424
+ res.on("finish", () => {
425
+ const statusCode = res.statusCode;
426
+ debugLog2(telemetry, "handler", `\u2705 Handler completed: ${statusCode}`);
427
+ const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
428
+ debugLog2(telemetry, "callback", `\u{1F4E4} Calling telemetry callback with status ${statusCode}`);
429
+ result.sendTelemetryAfterResponse(statusCode, errorMessage);
430
+ });
431
+ res.on("close", () => {
432
+ if (!res.writableEnded) {
433
+ debugLog2(telemetry, "handler", "\u274C Response closed without finishing");
434
+ debugLog2(telemetry, "callback", "\u{1F4E4} Calling telemetry callback with error");
435
+ result.sendTelemetryAfterResponse(500, "Response closed without finishing");
436
+ }
437
+ });
438
+ } else {
439
+ debugLog2(telemetry, "callback", "\u26A0\uFE0F No telemetry callback to call");
440
+ }
362
441
  next();
363
442
  };
364
443
  }
package/dist/index.d.cts CHANGED
@@ -1,10 +1,10 @@
1
1
  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';
2
2
  export { M as MANTLE_DEFAULTS } from './constants-CsIL25uQ.cjs';
3
- export { M as MantleMiddleware, e as MinimalPaywallOptions, P as PaymentLogEntry, d as PaymentMiddlewareConfig, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig, c as TelemetryEvent } from './types-BWfKovFm.cjs';
4
- export { c as createPaymentMiddleware, m as mantlePaywall } from './express-Pukmnwuu.cjs';
3
+ export { M as MantleMiddleware, e as MinimalPaywallOptions, P as PaymentLogEntry, d as PaymentMiddlewareConfig, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig, c as TelemetryEvent } from './types-DrBw0xwj.cjs';
4
+ export { c as createPaymentMiddleware, m as mantlePaywall } from './express-BWE0nQty.cjs';
5
5
  export { C as CallWithPaymentResult, M as MantleClient, a as MantleClientConfig, b as PaymentClient, P as PaymentClientConfig, c as createMantleClient } from './createMantleClient-CO0uWPb-.cjs';
6
6
  export { createPaymentClient } from './client.cjs';
7
7
  export { UseEthersWalletOptions, UseEthersWalletReturn, UseMantleX402Options, useEthersWallet, useMantleX402 } from './react.cjs';
8
8
  import 'express';
9
- import './types-BmK0G74m.cjs';
9
+ import './types-BkGUHT4x.cjs';
10
10
  import 'ethers';
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  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';
2
2
  export { M as MANTLE_DEFAULTS } from './constants-0ncqvV_O.js';
3
- export { M as MantleMiddleware, e as MinimalPaywallOptions, P as PaymentLogEntry, d as PaymentMiddlewareConfig, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig, c as TelemetryEvent } from './types-DgfVPQFb.js';
4
- export { c as createPaymentMiddleware, m as mantlePaywall } from './express-D8L5Dg1D.js';
3
+ export { M as MantleMiddleware, e as MinimalPaywallOptions, P as PaymentLogEntry, d as PaymentMiddlewareConfig, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig, c as TelemetryEvent } from './types-DEpSrXCf.js';
4
+ export { c as createPaymentMiddleware, m as mantlePaywall } from './express-BvuN0Lx1.js';
5
5
  export { C as CallWithPaymentResult, M as MantleClient, a as MantleClientConfig, b as PaymentClient, P as PaymentClientConfig, c as createMantleClient } from './createMantleClient-CuiPsTa6.js';
6
6
  export { createPaymentClient } from './client.js';
7
7
  export { UseEthersWalletOptions, UseEthersWalletReturn, UseMantleX402Options, useEthersWallet, useMantleX402 } from './react.js';
8
8
  import 'express';
9
- import './types-CXdNC0Ra.js';
9
+ import './types-Ba0v9XsC.js';
10
10
  import 'ethers';
package/dist/index.js CHANGED
@@ -10,8 +10,8 @@ import "./chunk-WO2MYZXT.js";
10
10
  import {
11
11
  createPaymentMiddleware,
12
12
  mantlePaywall
13
- } from "./chunk-B4GST723.js";
14
- import "./chunk-OW2BDRGZ.js";
13
+ } from "./chunk-3DGAB7HD.js";
14
+ import "./chunk-UVYA6H32.js";
15
15
  import {
16
16
  MANTLE_DEFAULTS
17
17
  } from "./chunk-HEZZ74SI.js";
@@ -0,0 +1,89 @@
1
+ import { NextRequest } from 'next/server';
2
+ import { M as MinimalPaywallOptions } from './types-BkGUHT4x.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
+ * Simple promise-or-value helper.
18
+ */
19
+ type Awaitable<T> = T | Promise<T>;
20
+ /**
21
+ * Next.js App Router route handler type.
22
+ *
23
+ * Accepts anything that is a valid `Response` to avoid forcing users
24
+ * to annotate union bodies on every handler.
25
+ */
26
+ type NextJSHandler = (req: NextRequest) => Awaitable<Response>;
27
+ /**
28
+ * Wrapper function that adds x402 payment verification to Next.js route handler.
29
+ * Returns a handler that may return either the original handler's response
30
+ * or a PaywallErrorResponse if payment verification fails.
31
+ */
32
+ type NextJSPaywallWrapper = (handler: NextJSHandler) => NextJSHandler;
33
+ /**
34
+ * Create Next.js App Router middleware for x402 payment verification.
35
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // app/api/generate-image/route.ts
40
+ * import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/nextjs'
41
+ *
42
+ * const pay = mantlePaywall({
43
+ * priceUsd: 0.01,
44
+ * payTo: process.env.PAY_TO!,
45
+ * });
46
+ *
47
+ * // No 'as any' needed! TypeScript correctly infers the union type
48
+ * export const POST = pay(async (req: NextRequest) => {
49
+ * const { prompt } = await req.json();
50
+ * // Your handler code here
51
+ * return NextResponse.json({ success: true, imageUrl: "..." });
52
+ * });
53
+ * // TypeScript knows POST returns: NextResponse<{ success: boolean; imageUrl: string } | PaywallErrorResponse>
54
+ * ```
55
+ *
56
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
57
+ * @returns Function that wraps Next.js route handlers with payment verification.
58
+ */
59
+ declare function mantlePaywall(opts: MinimalPaywallOptions): NextJSPaywallWrapper;
60
+ /**
61
+ * Type guard to check if a response is a paywall error response.
62
+ * Useful for handling the union type returned by the paywall wrapper.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const response = await fetch('/api/protected');
67
+ * const data = await response.json();
68
+ *
69
+ * if (isPaywallErrorResponse(data)) {
70
+ * // Handle error: data.error, data.paymentRequirements, etc.
71
+ * console.error('Payment required:', data.paymentRequirements);
72
+ * } else {
73
+ * // Handle success: data has type T
74
+ * console.log('Success:', data);
75
+ * }
76
+ * ```
77
+ */
78
+ declare function isPaywallErrorResponse(response: unknown): response is PaywallErrorResponse;
79
+
80
+ type nextjs_NextJSHandler = NextJSHandler;
81
+ type nextjs_NextJSPaywallWrapper = NextJSPaywallWrapper;
82
+ type nextjs_PaywallErrorResponse = PaywallErrorResponse;
83
+ declare const nextjs_isPaywallErrorResponse: typeof isPaywallErrorResponse;
84
+ declare const nextjs_mantlePaywall: typeof mantlePaywall;
85
+ declare namespace nextjs {
86
+ export { type nextjs_NextJSHandler as NextJSHandler, type nextjs_NextJSPaywallWrapper as NextJSPaywallWrapper, type nextjs_PaywallErrorResponse as PaywallErrorResponse, nextjs_isPaywallErrorResponse as isPaywallErrorResponse, nextjs_mantlePaywall as mantlePaywall };
87
+ }
88
+
89
+ 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,89 @@
1
+ import { NextRequest } from 'next/server';
2
+ import { M as MinimalPaywallOptions } from './types-Ba0v9XsC.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
+ * Simple promise-or-value helper.
18
+ */
19
+ type Awaitable<T> = T | Promise<T>;
20
+ /**
21
+ * Next.js App Router route handler type.
22
+ *
23
+ * Accepts anything that is a valid `Response` to avoid forcing users
24
+ * to annotate union bodies on every handler.
25
+ */
26
+ type NextJSHandler = (req: NextRequest) => Awaitable<Response>;
27
+ /**
28
+ * Wrapper function that adds x402 payment verification to Next.js route handler.
29
+ * Returns a handler that may return either the original handler's response
30
+ * or a PaywallErrorResponse if payment verification fails.
31
+ */
32
+ type NextJSPaywallWrapper = (handler: NextJSHandler) => NextJSHandler;
33
+ /**
34
+ * Create Next.js App Router middleware for x402 payment verification.
35
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // app/api/generate-image/route.ts
40
+ * import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/nextjs'
41
+ *
42
+ * const pay = mantlePaywall({
43
+ * priceUsd: 0.01,
44
+ * payTo: process.env.PAY_TO!,
45
+ * });
46
+ *
47
+ * // No 'as any' needed! TypeScript correctly infers the union type
48
+ * export const POST = pay(async (req: NextRequest) => {
49
+ * const { prompt } = await req.json();
50
+ * // Your handler code here
51
+ * return NextResponse.json({ success: true, imageUrl: "..." });
52
+ * });
53
+ * // TypeScript knows POST returns: NextResponse<{ success: boolean; imageUrl: string } | PaywallErrorResponse>
54
+ * ```
55
+ *
56
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
57
+ * @returns Function that wraps Next.js route handlers with payment verification.
58
+ */
59
+ declare function mantlePaywall(opts: MinimalPaywallOptions): NextJSPaywallWrapper;
60
+ /**
61
+ * Type guard to check if a response is a paywall error response.
62
+ * Useful for handling the union type returned by the paywall wrapper.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const response = await fetch('/api/protected');
67
+ * const data = await response.json();
68
+ *
69
+ * if (isPaywallErrorResponse(data)) {
70
+ * // Handle error: data.error, data.paymentRequirements, etc.
71
+ * console.error('Payment required:', data.paymentRequirements);
72
+ * } else {
73
+ * // Handle success: data has type T
74
+ * console.log('Success:', data);
75
+ * }
76
+ * ```
77
+ */
78
+ declare function isPaywallErrorResponse(response: unknown): response is PaywallErrorResponse;
79
+
80
+ type nextjs_NextJSHandler = NextJSHandler;
81
+ type nextjs_NextJSPaywallWrapper = NextJSPaywallWrapper;
82
+ type nextjs_PaywallErrorResponse = PaywallErrorResponse;
83
+ declare const nextjs_isPaywallErrorResponse: typeof isPaywallErrorResponse;
84
+ declare const nextjs_mantlePaywall: typeof mantlePaywall;
85
+ declare namespace nextjs {
86
+ export { type nextjs_NextJSHandler as NextJSHandler, type nextjs_NextJSPaywallWrapper as NextJSPaywallWrapper, type nextjs_PaywallErrorResponse as PaywallErrorResponse, nextjs_isPaywallErrorResponse as isPaywallErrorResponse, nextjs_mantlePaywall as mantlePaywall };
87
+ }
88
+
89
+ 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,89 @@
1
+ import { NextRequest } from 'next/server';
2
+ import { M as MinimalPaywallOptions } from './types-vicT7qsY.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
+ * Simple promise-or-value helper.
18
+ */
19
+ type Awaitable<T> = T | Promise<T>;
20
+ /**
21
+ * Next.js App Router route handler type.
22
+ *
23
+ * Accepts anything that is a valid `Response` to avoid forcing users
24
+ * to annotate union bodies on every handler.
25
+ */
26
+ type NextJSHandler = (req: NextRequest) => Awaitable<Response>;
27
+ /**
28
+ * Wrapper function that adds x402 payment verification to Next.js route handler.
29
+ * Returns a handler that may return either the original handler's response
30
+ * or a PaywallErrorResponse if payment verification fails.
31
+ */
32
+ type NextJSPaywallWrapper = (handler: NextJSHandler) => NextJSHandler;
33
+ /**
34
+ * Create Next.js App Router middleware for x402 payment verification.
35
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // app/api/generate-image/route.ts
40
+ * import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/nextjs'
41
+ *
42
+ * const pay = mantlePaywall({
43
+ * priceUsd: 0.01,
44
+ * payTo: process.env.PAY_TO!,
45
+ * });
46
+ *
47
+ * // No 'as any' needed! TypeScript correctly infers the union type
48
+ * export const POST = pay(async (req: NextRequest) => {
49
+ * const { prompt } = await req.json();
50
+ * // Your handler code here
51
+ * return NextResponse.json({ success: true, imageUrl: "..." });
52
+ * });
53
+ * // TypeScript knows POST returns: NextResponse<{ success: boolean; imageUrl: string } | PaywallErrorResponse>
54
+ * ```
55
+ *
56
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
57
+ * @returns Function that wraps Next.js route handlers with payment verification.
58
+ */
59
+ declare function mantlePaywall(opts: MinimalPaywallOptions): NextJSPaywallWrapper;
60
+ /**
61
+ * Type guard to check if a response is a paywall error response.
62
+ * Useful for handling the union type returned by the paywall wrapper.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const response = await fetch('/api/protected');
67
+ * const data = await response.json();
68
+ *
69
+ * if (isPaywallErrorResponse(data)) {
70
+ * // Handle error: data.error, data.paymentRequirements, etc.
71
+ * console.error('Payment required:', data.paymentRequirements);
72
+ * } else {
73
+ * // Handle success: data has type T
74
+ * console.log('Success:', data);
75
+ * }
76
+ * ```
77
+ */
78
+ declare function isPaywallErrorResponse(response: unknown): response is PaywallErrorResponse;
79
+
80
+ type nextjs_NextJSHandler = NextJSHandler;
81
+ type nextjs_NextJSPaywallWrapper = NextJSPaywallWrapper;
82
+ type nextjs_PaywallErrorResponse = PaywallErrorResponse;
83
+ declare const nextjs_isPaywallErrorResponse: typeof isPaywallErrorResponse;
84
+ declare const nextjs_mantlePaywall: typeof mantlePaywall;
85
+ declare namespace nextjs {
86
+ export { type nextjs_NextJSHandler as NextJSHandler, type nextjs_NextJSPaywallWrapper as NextJSPaywallWrapper, type nextjs_PaywallErrorResponse as PaywallErrorResponse, nextjs_isPaywallErrorResponse as isPaywallErrorResponse, nextjs_mantlePaywall as mantlePaywall };
87
+ }
88
+
89
+ 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,89 @@
1
+ import { NextRequest } from 'next/server';
2
+ import { M as MinimalPaywallOptions } from './types-B87bD2yo.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
+ * Simple promise-or-value helper.
18
+ */
19
+ type Awaitable<T> = T | Promise<T>;
20
+ /**
21
+ * Next.js App Router route handler type.
22
+ *
23
+ * Accepts anything that is a valid `Response` to avoid forcing users
24
+ * to annotate union bodies on every handler.
25
+ */
26
+ type NextJSHandler = (req: NextRequest) => Awaitable<Response>;
27
+ /**
28
+ * Wrapper function that adds x402 payment verification to Next.js route handler.
29
+ * Returns a handler that may return either the original handler's response
30
+ * or a PaywallErrorResponse if payment verification fails.
31
+ */
32
+ type NextJSPaywallWrapper = (handler: NextJSHandler) => NextJSHandler;
33
+ /**
34
+ * Create Next.js App Router middleware for x402 payment verification.
35
+ * Uses Mantle mainnet defaults (USDC, exact scheme, etc.).
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * // app/api/generate-image/route.ts
40
+ * import { mantlePaywall } from '@puga-labs/x402-mantle-sdk/server/nextjs'
41
+ *
42
+ * const pay = mantlePaywall({
43
+ * priceUsd: 0.01,
44
+ * payTo: process.env.PAY_TO!,
45
+ * });
46
+ *
47
+ * // No 'as any' needed! TypeScript correctly infers the union type
48
+ * export const POST = pay(async (req: NextRequest) => {
49
+ * const { prompt } = await req.json();
50
+ * // Your handler code here
51
+ * return NextResponse.json({ success: true, imageUrl: "..." });
52
+ * });
53
+ * // TypeScript knows POST returns: NextResponse<{ success: boolean; imageUrl: string } | PaywallErrorResponse>
54
+ * ```
55
+ *
56
+ * @param opts - Minimal configuration (price, payTo, optional facilitator/telemetry).
57
+ * @returns Function that wraps Next.js route handlers with payment verification.
58
+ */
59
+ declare function mantlePaywall(opts: MinimalPaywallOptions): NextJSPaywallWrapper;
60
+ /**
61
+ * Type guard to check if a response is a paywall error response.
62
+ * Useful for handling the union type returned by the paywall wrapper.
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * const response = await fetch('/api/protected');
67
+ * const data = await response.json();
68
+ *
69
+ * if (isPaywallErrorResponse(data)) {
70
+ * // Handle error: data.error, data.paymentRequirements, etc.
71
+ * console.error('Payment required:', data.paymentRequirements);
72
+ * } else {
73
+ * // Handle success: data has type T
74
+ * console.log('Success:', data);
75
+ * }
76
+ * ```
77
+ */
78
+ declare function isPaywallErrorResponse(response: unknown): response is PaywallErrorResponse;
79
+
80
+ type nextjs_NextJSHandler = NextJSHandler;
81
+ type nextjs_NextJSPaywallWrapper = NextJSPaywallWrapper;
82
+ type nextjs_PaywallErrorResponse = PaywallErrorResponse;
83
+ declare const nextjs_isPaywallErrorResponse: typeof isPaywallErrorResponse;
84
+ declare const nextjs_mantlePaywall: typeof mantlePaywall;
85
+ declare namespace nextjs {
86
+ export { type nextjs_NextJSHandler as NextJSHandler, type nextjs_NextJSPaywallWrapper as NextJSPaywallWrapper, type nextjs_PaywallErrorResponse as PaywallErrorResponse, nextjs_isPaywallErrorResponse as isPaywallErrorResponse, nextjs_mantlePaywall as mantlePaywall };
87
+ }
88
+
89
+ export { type NextJSHandler as N, type PaywallErrorResponse as P, type NextJSPaywallWrapper as a, isPaywallErrorResponse as i, mantlePaywall as m, nextjs as n };