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

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/index.cjs CHANGED
@@ -253,12 +253,13 @@ async function checkPayment(input) {
253
253
  isValid: false
254
254
  };
255
255
  }
256
- if (onPaymentSettled) {
256
+ let baseLogEntry = null;
257
+ if (onPaymentSettled || telemetry) {
257
258
  try {
258
259
  const headerObj = decodePaymentHeader(paymentHeader);
259
260
  const { authorization } = headerObj.payload;
260
261
  const assetConfig = getDefaultAssetForNetwork(network);
261
- const logEntry = {
262
+ baseLogEntry = {
262
263
  id: authorization.nonce,
263
264
  from: authorization.from,
264
265
  to: authorization.to,
@@ -270,12 +271,8 @@ async function checkPayment(input) {
270
271
  facilitatorUrl,
271
272
  paymentRequirements
272
273
  };
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
- );
274
+ if (onPaymentSettled) {
275
+ onPaymentSettled(baseLogEntry);
279
276
  }
280
277
  } catch (err) {
281
278
  console.error(
@@ -284,11 +281,25 @@ async function checkPayment(input) {
284
281
  );
285
282
  }
286
283
  }
284
+ const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
285
+ try {
286
+ const event = createTelemetryEvent(baseLogEntry, telemetry);
287
+ event.responseStatus = responseStatus;
288
+ event.errorMessage = error;
289
+ event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
290
+ sendTelemetry(event, telemetry.endpoint).catch(
291
+ (err) => console.error("[x402-telemetry] Async send failed:", err)
292
+ );
293
+ } catch (err) {
294
+ console.error("[x402-telemetry] Error creating telemetry event:", err);
295
+ }
296
+ } : void 0;
287
297
  return {
288
298
  status: "verified",
289
299
  statusCode: 200,
290
300
  responseBody: null,
291
- isValid: true
301
+ isValid: true,
302
+ sendTelemetryAfterResponse
292
303
  };
293
304
  } catch (err) {
294
305
  console.error(
@@ -359,6 +370,18 @@ function createPaymentMiddleware(config) {
359
370
  res.status(result.statusCode).json(result.responseBody);
360
371
  return;
361
372
  }
373
+ if (result.sendTelemetryAfterResponse) {
374
+ res.on("finish", () => {
375
+ const statusCode = res.statusCode;
376
+ const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
377
+ result.sendTelemetryAfterResponse(statusCode, errorMessage);
378
+ });
379
+ res.on("close", () => {
380
+ if (!res.writableEnded) {
381
+ result.sendTelemetryAfterResponse(500, "Response closed without finishing");
382
+ }
383
+ });
384
+ }
362
385
  next();
363
386
  };
364
387
  }
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-X6DeBEgb.cjs';
4
+ export { c as createPaymentMiddleware, m as mantlePaywall } from './express-DqyVgO5n.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-B87bD2yo.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-DvKDSdL6.js';
4
+ export { c as createPaymentMiddleware, m as mantlePaywall } from './express-DxxlKmmF.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-vicT7qsY.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-IEJB5W26.js";
14
+ import "./chunk-IXIFGPJ2.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-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 };
@@ -231,12 +231,13 @@ async function checkPayment(input) {
231
231
  isValid: false
232
232
  };
233
233
  }
234
- if (onPaymentSettled) {
234
+ let baseLogEntry = null;
235
+ if (onPaymentSettled || telemetry) {
235
236
  try {
236
237
  const headerObj = decodePaymentHeader(paymentHeader);
237
238
  const { authorization } = headerObj.payload;
238
239
  const assetConfig = getDefaultAssetForNetwork(network);
239
- const logEntry = {
240
+ baseLogEntry = {
240
241
  id: authorization.nonce,
241
242
  from: authorization.from,
242
243
  to: authorization.to,
@@ -248,12 +249,8 @@ async function checkPayment(input) {
248
249
  facilitatorUrl,
249
250
  paymentRequirements
250
251
  };
251
- onPaymentSettled(logEntry);
252
- if (telemetry) {
253
- const event = createTelemetryEvent(logEntry, telemetry);
254
- sendTelemetry(event, telemetry.endpoint).catch(
255
- (err) => console.error("[x402-telemetry] Async send failed:", err)
256
- );
252
+ if (onPaymentSettled) {
253
+ onPaymentSettled(baseLogEntry);
257
254
  }
258
255
  } catch (err) {
259
256
  console.error(
@@ -262,11 +259,25 @@ async function checkPayment(input) {
262
259
  );
263
260
  }
264
261
  }
262
+ const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
263
+ try {
264
+ const event = createTelemetryEvent(baseLogEntry, telemetry);
265
+ event.responseStatus = responseStatus;
266
+ event.errorMessage = error;
267
+ event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
268
+ sendTelemetry(event, telemetry.endpoint).catch(
269
+ (err) => console.error("[x402-telemetry] Async send failed:", err)
270
+ );
271
+ } catch (err) {
272
+ console.error("[x402-telemetry] Error creating telemetry event:", err);
273
+ }
274
+ } : void 0;
265
275
  return {
266
276
  status: "verified",
267
277
  statusCode: 200,
268
278
  responseBody: null,
269
- isValid: true
279
+ isValid: true,
280
+ sendTelemetryAfterResponse
270
281
  };
271
282
  } catch (err) {
272
283
  console.error(
@@ -337,6 +348,18 @@ function createPaymentMiddleware(config) {
337
348
  res.status(result.statusCode).json(result.responseBody);
338
349
  return;
339
350
  }
351
+ if (result.sendTelemetryAfterResponse) {
352
+ res.on("finish", () => {
353
+ const statusCode = res.statusCode;
354
+ const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
355
+ result.sendTelemetryAfterResponse(statusCode, errorMessage);
356
+ });
357
+ res.on("close", () => {
358
+ if (!res.writableEnded) {
359
+ result.sendTelemetryAfterResponse(500, "Response closed without finishing");
360
+ }
361
+ });
362
+ }
340
363
  next();
341
364
  };
342
365
  }
@@ -1,5 +1,5 @@
1
- export { M as MantleMiddleware, P as PaymentMiddlewareConfig, c as createPaymentMiddleware, m as mantlePaywall } from './express-Pukmnwuu.cjs';
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-BmK0G74m.cjs';
1
+ export { M as MantleMiddleware, P as PaymentMiddlewareConfig, c as createPaymentMiddleware, m as mantlePaywall } from './express-DqyVgO5n.cjs';
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-B87bD2yo.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';
5
5
  import 'express';
@@ -1,5 +1,5 @@
1
- export { M as MantleMiddleware, P as PaymentMiddlewareConfig, c as createPaymentMiddleware, m as mantlePaywall } from './express-D8L5Dg1D.js';
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-CXdNC0Ra.js';
1
+ export { M as MantleMiddleware, P as PaymentMiddlewareConfig, c as createPaymentMiddleware, m as mantlePaywall } from './express-DxxlKmmF.js';
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-vicT7qsY.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';
5
5
  import 'express';
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createPaymentMiddleware,
3
3
  mantlePaywall
4
- } from "./chunk-B4GST723.js";
5
- import "./chunk-OW2BDRGZ.js";
4
+ } from "./chunk-IEJB5W26.js";
5
+ import "./chunk-IXIFGPJ2.js";
6
6
  import {
7
7
  MANTLE_DEFAULTS
8
8
  } from "./chunk-HEZZ74SI.js";
@@ -234,12 +234,13 @@ async function checkPayment(input) {
234
234
  isValid: false
235
235
  };
236
236
  }
237
- if (onPaymentSettled) {
237
+ let baseLogEntry = null;
238
+ if (onPaymentSettled || telemetry) {
238
239
  try {
239
240
  const headerObj = decodePaymentHeader(paymentHeader);
240
241
  const { authorization } = headerObj.payload;
241
242
  const assetConfig = getDefaultAssetForNetwork(network);
242
- const logEntry = {
243
+ baseLogEntry = {
243
244
  id: authorization.nonce,
244
245
  from: authorization.from,
245
246
  to: authorization.to,
@@ -251,12 +252,8 @@ async function checkPayment(input) {
251
252
  facilitatorUrl,
252
253
  paymentRequirements
253
254
  };
254
- onPaymentSettled(logEntry);
255
- if (telemetry) {
256
- const event = createTelemetryEvent(logEntry, telemetry);
257
- sendTelemetry(event, telemetry.endpoint).catch(
258
- (err) => console.error("[x402-telemetry] Async send failed:", err)
259
- );
255
+ if (onPaymentSettled) {
256
+ onPaymentSettled(baseLogEntry);
260
257
  }
261
258
  } catch (err) {
262
259
  console.error(
@@ -265,11 +262,25 @@ async function checkPayment(input) {
265
262
  );
266
263
  }
267
264
  }
265
+ const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
266
+ try {
267
+ const event = createTelemetryEvent(baseLogEntry, telemetry);
268
+ event.responseStatus = responseStatus;
269
+ event.errorMessage = error;
270
+ event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
271
+ sendTelemetry(event, telemetry.endpoint).catch(
272
+ (err) => console.error("[x402-telemetry] Async send failed:", err)
273
+ );
274
+ } catch (err) {
275
+ console.error("[x402-telemetry] Error creating telemetry event:", err);
276
+ }
277
+ } : void 0;
268
278
  return {
269
279
  status: "verified",
270
280
  statusCode: 200,
271
281
  responseBody: null,
272
- isValid: true
282
+ isValid: true,
283
+ sendTelemetryAfterResponse
273
284
  };
274
285
  } catch (err) {
275
286
  console.error(
@@ -332,7 +343,19 @@ function mantlePaywall(opts) {
332
343
  status: result.statusCode
333
344
  });
334
345
  }
335
- return handler(req);
346
+ try {
347
+ const response = await handler(req);
348
+ if (result.sendTelemetryAfterResponse) {
349
+ result.sendTelemetryAfterResponse(response.status);
350
+ }
351
+ return response;
352
+ } catch (err) {
353
+ const errorMessage = err instanceof Error ? err.message : "Unknown error";
354
+ if (result.sendTelemetryAfterResponse) {
355
+ result.sendTelemetryAfterResponse(500, errorMessage);
356
+ }
357
+ throw err;
358
+ }
336
359
  };
337
360
  };
338
361
  }
@@ -1,5 +1,5 @@
1
- export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-CSUWjcxv.cjs';
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-BmK0G74m.cjs';
1
+ export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-EoISXVEo.cjs';
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-B87bD2yo.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';
5
5
  import 'next/server';
@@ -1,5 +1,5 @@
1
- export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-DUdgN0d_.js';
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-CXdNC0Ra.js';
1
+ export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-DGcN_MGa.js';
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-vicT7qsY.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';
5
5
  import 'next/server';
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  isPaywallErrorResponse,
3
3
  mantlePaywall
4
- } from "./chunk-33WHNZDS.js";
5
- import "./chunk-OW2BDRGZ.js";
4
+ } from "./chunk-CKBTOS7X.js";
5
+ import "./chunk-IXIFGPJ2.js";
6
6
  import {
7
7
  MANTLE_DEFAULTS
8
8
  } from "./chunk-HEZZ74SI.js";
@@ -230,12 +230,13 @@ async function checkPayment(input) {
230
230
  isValid: false
231
231
  };
232
232
  }
233
- if (onPaymentSettled) {
233
+ let baseLogEntry = null;
234
+ if (onPaymentSettled || telemetry) {
234
235
  try {
235
236
  const headerObj = decodePaymentHeader(paymentHeader);
236
237
  const { authorization } = headerObj.payload;
237
238
  const assetConfig = getDefaultAssetForNetwork(network);
238
- const logEntry = {
239
+ baseLogEntry = {
239
240
  id: authorization.nonce,
240
241
  from: authorization.from,
241
242
  to: authorization.to,
@@ -247,12 +248,8 @@ async function checkPayment(input) {
247
248
  facilitatorUrl,
248
249
  paymentRequirements
249
250
  };
250
- onPaymentSettled(logEntry);
251
- if (telemetry) {
252
- const event = createTelemetryEvent(logEntry, telemetry);
253
- sendTelemetry(event, telemetry.endpoint).catch(
254
- (err) => console.error("[x402-telemetry] Async send failed:", err)
255
- );
251
+ if (onPaymentSettled) {
252
+ onPaymentSettled(baseLogEntry);
256
253
  }
257
254
  } catch (err) {
258
255
  console.error(
@@ -261,11 +258,25 @@ async function checkPayment(input) {
261
258
  );
262
259
  }
263
260
  }
261
+ const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
262
+ try {
263
+ const event = createTelemetryEvent(baseLogEntry, telemetry);
264
+ event.responseStatus = responseStatus;
265
+ event.errorMessage = error;
266
+ event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
267
+ sendTelemetry(event, telemetry.endpoint).catch(
268
+ (err) => console.error("[x402-telemetry] Async send failed:", err)
269
+ );
270
+ } catch (err) {
271
+ console.error("[x402-telemetry] Error creating telemetry event:", err);
272
+ }
273
+ } : void 0;
264
274
  return {
265
275
  status: "verified",
266
276
  statusCode: 200,
267
277
  responseBody: null,
268
- isValid: true
278
+ isValid: true,
279
+ sendTelemetryAfterResponse
269
280
  };
270
281
  } catch (err) {
271
282
  console.error(
@@ -331,7 +342,19 @@ function mantlePaywall(opts) {
331
342
  }
332
343
  });
333
344
  }
334
- return handler(request);
345
+ try {
346
+ const response = await handler(request);
347
+ if (result.sendTelemetryAfterResponse) {
348
+ result.sendTelemetryAfterResponse(response.status);
349
+ }
350
+ return response;
351
+ } catch (err) {
352
+ const errorMessage = err instanceof Error ? err.message : "Unknown error";
353
+ if (result.sendTelemetryAfterResponse) {
354
+ result.sendTelemetryAfterResponse(500, errorMessage);
355
+ }
356
+ throw err;
357
+ }
335
358
  };
336
359
  };
337
360
  }
@@ -1,4 +1,4 @@
1
- export { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-BvMLEKlU.cjs';
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-BmK0G74m.cjs';
1
+ export { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-C6JwCDmd.cjs';
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-B87bD2yo.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 { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-g2rYUpgc.js';
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-CXdNC0Ra.js';
1
+ export { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-BJcdcxD6.js';
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-vicT7qsY.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,7 +1,7 @@
1
1
  import {
2
2
  mantlePaywall
3
- } from "./chunk-T63MVWX3.js";
4
- import "./chunk-OW2BDRGZ.js";
3
+ } from "./chunk-T4DIHTBP.js";
4
+ import "./chunk-IXIFGPJ2.js";
5
5
  import {
6
6
  MANTLE_DEFAULTS
7
7
  } from "./chunk-HEZZ74SI.js";