@puga-labs/x402-mantle-sdk 0.3.7 → 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.
Files changed (49) hide show
  1. package/dist/chunk-33WHNZDS.js +74 -0
  2. package/dist/chunk-B4GST723.js +101 -0
  3. package/dist/chunk-CKBTOS7X.js +86 -0
  4. package/dist/chunk-IEJB5W26.js +113 -0
  5. package/dist/chunk-IXIFGPJ2.js +250 -0
  6. package/dist/chunk-OW2BDRGZ.js +239 -0
  7. package/dist/chunk-T4DIHTBP.js +83 -0
  8. package/dist/chunk-T63MVWX3.js +71 -0
  9. package/dist/express-D8L5Dg1D.d.ts +68 -0
  10. package/dist/express-DqyVgO5n.d.cts +68 -0
  11. package/dist/express-DxxlKmmF.d.ts +68 -0
  12. package/dist/express-Pukmnwuu.d.cts +68 -0
  13. package/dist/index.cjs +39 -12
  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-CSUWjcxv.d.cts +89 -0
  18. package/dist/nextjs-DGcN_MGa.d.ts +89 -0
  19. package/dist/nextjs-DUdgN0d_.d.ts +89 -0
  20. package/dist/nextjs-EoISXVEo.d.cts +89 -0
  21. package/dist/server-express.cjs +39 -12
  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 +38 -12
  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 +38 -12
  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 +69 -16
  34. package/dist/server.d.cts +6 -6
  35. package/dist/server.d.ts +6 -6
  36. package/dist/server.js +4 -4
  37. package/dist/types-B87bD2yo.d.cts +102 -0
  38. package/dist/types-BWfKovFm.d.cts +103 -0
  39. package/dist/types-BmK0G74m.d.cts +93 -0
  40. package/dist/types-CXdNC0Ra.d.ts +93 -0
  41. package/dist/types-DgfVPQFb.d.ts +103 -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-BvMLEKlU.d.cts +77 -0
  47. package/dist/web-standards-C6JwCDmd.d.cts +77 -0
  48. package/dist/web-standards-g2rYUpgc.d.ts +77 -0
  49. package/package.json +1 -1
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  createPaymentMiddleware,
3
3
  mantlePaywall
4
- } from "./chunk-MBJTUNDL.js";
5
- import "./chunk-EKEVUVF3.js";
4
+ } from "./chunk-IEJB5W26.js";
5
+ import "./chunk-IXIFGPJ2.js";
6
6
  import {
7
7
  MANTLE_DEFAULTS
8
8
  } from "./chunk-HEZZ74SI.js";
@@ -170,6 +170,7 @@ async function checkPayment(input) {
170
170
  paymentHeader,
171
171
  paymentRequirements,
172
172
  facilitatorUrl,
173
+ apiKey,
173
174
  routeKey,
174
175
  network,
175
176
  asset,
@@ -193,7 +194,8 @@ async function checkPayment(input) {
193
194
  const verifyRes = await fetch(verifyUrl, {
194
195
  method: "POST",
195
196
  headers: {
196
- "Content-Type": "application/json"
197
+ "Content-Type": "application/json",
198
+ ...apiKey && { "Authorization": `Bearer ${apiKey}` }
197
199
  },
198
200
  body: JSON.stringify({
199
201
  x402Version: 1,
@@ -232,12 +234,13 @@ async function checkPayment(input) {
232
234
  isValid: false
233
235
  };
234
236
  }
235
- if (onPaymentSettled) {
237
+ let baseLogEntry = null;
238
+ if (onPaymentSettled || telemetry) {
236
239
  try {
237
240
  const headerObj = decodePaymentHeader(paymentHeader);
238
241
  const { authorization } = headerObj.payload;
239
242
  const assetConfig = getDefaultAssetForNetwork(network);
240
- const logEntry = {
243
+ baseLogEntry = {
241
244
  id: authorization.nonce,
242
245
  from: authorization.from,
243
246
  to: authorization.to,
@@ -249,12 +252,8 @@ async function checkPayment(input) {
249
252
  facilitatorUrl,
250
253
  paymentRequirements
251
254
  };
252
- onPaymentSettled(logEntry);
253
- if (telemetry) {
254
- const event = createTelemetryEvent(logEntry, telemetry);
255
- sendTelemetry(event, telemetry.endpoint).catch(
256
- (err) => console.error("[x402-telemetry] Async send failed:", err)
257
- );
255
+ if (onPaymentSettled) {
256
+ onPaymentSettled(baseLogEntry);
258
257
  }
259
258
  } catch (err) {
260
259
  console.error(
@@ -263,11 +262,25 @@ async function checkPayment(input) {
263
262
  );
264
263
  }
265
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;
266
278
  return {
267
279
  status: "verified",
268
280
  statusCode: 200,
269
281
  responseBody: null,
270
- isValid: true
282
+ isValid: true,
283
+ sendTelemetryAfterResponse
271
284
  };
272
285
  } catch (err) {
273
286
  console.error(
@@ -289,7 +302,7 @@ async function checkPayment(input) {
289
302
 
290
303
  // src/server/adapters/nextjs.ts
291
304
  function mantlePaywall(opts) {
292
- const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
305
+ const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
293
306
  validateAddress(payTo, "payTo");
294
307
  const priceUsdCents = Math.round(priceUsd * 100);
295
308
  return function(handler) {
@@ -318,6 +331,7 @@ function mantlePaywall(opts) {
318
331
  paymentHeader,
319
332
  paymentRequirements,
320
333
  facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
334
+ apiKey,
321
335
  routeKey,
322
336
  network,
323
337
  asset: assetConfig.address,
@@ -329,7 +343,19 @@ function mantlePaywall(opts) {
329
343
  status: result.statusCode
330
344
  });
331
345
  }
332
- 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
+ }
333
359
  };
334
360
  };
335
361
  }
@@ -1,5 +1,5 @@
1
- export { N as NextJSHandler, a as NextJSPaywallWrapper, P as PaywallErrorResponse, i as isPaywallErrorResponse, m as mantlePaywall } from './nextjs-CGOVHJXl.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-CrOsOHcX.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-w61gRvpz.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-CqQ6OgRi.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-5TTSYEOF.js";
5
- import "./chunk-EKEVUVF3.js";
4
+ } from "./chunk-CKBTOS7X.js";
5
+ import "./chunk-IXIFGPJ2.js";
6
6
  import {
7
7
  MANTLE_DEFAULTS
8
8
  } from "./chunk-HEZZ74SI.js";
@@ -166,6 +166,7 @@ async function checkPayment(input) {
166
166
  paymentHeader,
167
167
  paymentRequirements,
168
168
  facilitatorUrl,
169
+ apiKey,
169
170
  routeKey,
170
171
  network,
171
172
  asset,
@@ -189,7 +190,8 @@ async function checkPayment(input) {
189
190
  const verifyRes = await fetch(verifyUrl, {
190
191
  method: "POST",
191
192
  headers: {
192
- "Content-Type": "application/json"
193
+ "Content-Type": "application/json",
194
+ ...apiKey && { "Authorization": `Bearer ${apiKey}` }
193
195
  },
194
196
  body: JSON.stringify({
195
197
  x402Version: 1,
@@ -228,12 +230,13 @@ async function checkPayment(input) {
228
230
  isValid: false
229
231
  };
230
232
  }
231
- if (onPaymentSettled) {
233
+ let baseLogEntry = null;
234
+ if (onPaymentSettled || telemetry) {
232
235
  try {
233
236
  const headerObj = decodePaymentHeader(paymentHeader);
234
237
  const { authorization } = headerObj.payload;
235
238
  const assetConfig = getDefaultAssetForNetwork(network);
236
- const logEntry = {
239
+ baseLogEntry = {
237
240
  id: authorization.nonce,
238
241
  from: authorization.from,
239
242
  to: authorization.to,
@@ -245,12 +248,8 @@ async function checkPayment(input) {
245
248
  facilitatorUrl,
246
249
  paymentRequirements
247
250
  };
248
- onPaymentSettled(logEntry);
249
- if (telemetry) {
250
- const event = createTelemetryEvent(logEntry, telemetry);
251
- sendTelemetry(event, telemetry.endpoint).catch(
252
- (err) => console.error("[x402-telemetry] Async send failed:", err)
253
- );
251
+ if (onPaymentSettled) {
252
+ onPaymentSettled(baseLogEntry);
254
253
  }
255
254
  } catch (err) {
256
255
  console.error(
@@ -259,11 +258,25 @@ async function checkPayment(input) {
259
258
  );
260
259
  }
261
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;
262
274
  return {
263
275
  status: "verified",
264
276
  statusCode: 200,
265
277
  responseBody: null,
266
- isValid: true
278
+ isValid: true,
279
+ sendTelemetryAfterResponse
267
280
  };
268
281
  } catch (err) {
269
282
  console.error(
@@ -285,7 +298,7 @@ async function checkPayment(input) {
285
298
 
286
299
  // src/server/adapters/web-standards.ts
287
300
  function mantlePaywall(opts) {
288
- const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
301
+ const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
289
302
  validateAddress(payTo, "payTo");
290
303
  const priceUsdCents = Math.round(priceUsd * 100);
291
304
  return function(handler) {
@@ -314,6 +327,7 @@ function mantlePaywall(opts) {
314
327
  paymentHeader,
315
328
  paymentRequirements,
316
329
  facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
330
+ apiKey,
317
331
  routeKey,
318
332
  network,
319
333
  asset: assetConfig.address,
@@ -328,7 +342,19 @@ function mantlePaywall(opts) {
328
342
  }
329
343
  });
330
344
  }
331
- 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
+ }
332
358
  };
333
359
  };
334
360
  }
@@ -1,4 +1,4 @@
1
- export { W as WebHandler, a as WebPaywallWrapper, m as mantlePaywall } from './web-standards-BNQyWzBC.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-CrOsOHcX.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-D8j1kZxd.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-CqQ6OgRi.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-F2OTZ3BB.js";
4
- import "./chunk-EKEVUVF3.js";
3
+ } from "./chunk-T4DIHTBP.js";
4
+ import "./chunk-IXIFGPJ2.js";
5
5
  import {
6
6
  MANTLE_DEFAULTS
7
7
  } from "./chunk-HEZZ74SI.js";
package/dist/server.cjs CHANGED
@@ -176,6 +176,7 @@ async function checkPayment(input) {
176
176
  paymentHeader,
177
177
  paymentRequirements,
178
178
  facilitatorUrl,
179
+ apiKey,
179
180
  routeKey,
180
181
  network,
181
182
  asset,
@@ -199,7 +200,8 @@ async function checkPayment(input) {
199
200
  const verifyRes = await fetch(verifyUrl, {
200
201
  method: "POST",
201
202
  headers: {
202
- "Content-Type": "application/json"
203
+ "Content-Type": "application/json",
204
+ ...apiKey && { "Authorization": `Bearer ${apiKey}` }
203
205
  },
204
206
  body: JSON.stringify({
205
207
  x402Version: 1,
@@ -238,12 +240,13 @@ async function checkPayment(input) {
238
240
  isValid: false
239
241
  };
240
242
  }
241
- if (onPaymentSettled) {
243
+ let baseLogEntry = null;
244
+ if (onPaymentSettled || telemetry) {
242
245
  try {
243
246
  const headerObj = decodePaymentHeader(paymentHeader);
244
247
  const { authorization } = headerObj.payload;
245
248
  const assetConfig = getDefaultAssetForNetwork(network);
246
- const logEntry = {
249
+ baseLogEntry = {
247
250
  id: authorization.nonce,
248
251
  from: authorization.from,
249
252
  to: authorization.to,
@@ -255,12 +258,8 @@ async function checkPayment(input) {
255
258
  facilitatorUrl,
256
259
  paymentRequirements
257
260
  };
258
- onPaymentSettled(logEntry);
259
- if (telemetry) {
260
- const event = createTelemetryEvent(logEntry, telemetry);
261
- sendTelemetry(event, telemetry.endpoint).catch(
262
- (err) => console.error("[x402-telemetry] Async send failed:", err)
263
- );
261
+ if (onPaymentSettled) {
262
+ onPaymentSettled(baseLogEntry);
264
263
  }
265
264
  } catch (err) {
266
265
  console.error(
@@ -269,11 +268,25 @@ async function checkPayment(input) {
269
268
  );
270
269
  }
271
270
  }
271
+ const sendTelemetryAfterResponse = telemetry && baseLogEntry ? (responseStatus, error) => {
272
+ try {
273
+ const event = createTelemetryEvent(baseLogEntry, telemetry);
274
+ event.responseStatus = responseStatus;
275
+ event.errorMessage = error;
276
+ event.serviceDelivered = responseStatus >= 200 && responseStatus < 300;
277
+ sendTelemetry(event, telemetry.endpoint).catch(
278
+ (err) => console.error("[x402-telemetry] Async send failed:", err)
279
+ );
280
+ } catch (err) {
281
+ console.error("[x402-telemetry] Error creating telemetry event:", err);
282
+ }
283
+ } : void 0;
272
284
  return {
273
285
  status: "verified",
274
286
  statusCode: 200,
275
287
  responseBody: null,
276
- isValid: true
288
+ isValid: true,
289
+ sendTelemetryAfterResponse
277
290
  };
278
291
  } catch (err) {
279
292
  console.error(
@@ -300,7 +313,7 @@ __export(express_exports, {
300
313
  mantlePaywall: () => mantlePaywall
301
314
  });
302
315
  function createPaymentMiddleware(config) {
303
- const { facilitatorUrl, receiverAddress, routes, onPaymentSettled, telemetry } = config;
316
+ const { facilitatorUrl, receiverAddress, routes, apiKey, onPaymentSettled, telemetry } = config;
304
317
  if (!facilitatorUrl) {
305
318
  throw new Error("facilitatorUrl is required");
306
319
  }
@@ -338,6 +351,7 @@ function createPaymentMiddleware(config) {
338
351
  paymentHeader,
339
352
  paymentRequirements,
340
353
  facilitatorUrl,
354
+ apiKey,
341
355
  routeKey,
342
356
  network,
343
357
  asset: assetConfig.address,
@@ -348,11 +362,23 @@ function createPaymentMiddleware(config) {
348
362
  res.status(result.statusCode).json(result.responseBody);
349
363
  return;
350
364
  }
365
+ if (result.sendTelemetryAfterResponse) {
366
+ res.on("finish", () => {
367
+ const statusCode = res.statusCode;
368
+ const errorMessage = statusCode >= 400 ? `Handler returned ${statusCode}` : void 0;
369
+ result.sendTelemetryAfterResponse(statusCode, errorMessage);
370
+ });
371
+ res.on("close", () => {
372
+ if (!res.writableEnded) {
373
+ result.sendTelemetryAfterResponse(500, "Response closed without finishing");
374
+ }
375
+ });
376
+ }
351
377
  next();
352
378
  };
353
379
  }
354
380
  function mantlePaywall(opts) {
355
- const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
381
+ const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
356
382
  validateAddress(payTo, "payTo");
357
383
  const priceUsdCents = Math.round(priceUsd * 100);
358
384
  return async (req, res, next) => {
@@ -368,6 +394,7 @@ function mantlePaywall(opts) {
368
394
  network: MANTLE_DEFAULTS.NETWORK
369
395
  }
370
396
  },
397
+ apiKey,
371
398
  telemetry,
372
399
  onPaymentSettled
373
400
  });
@@ -383,7 +410,7 @@ __export(nextjs_exports, {
383
410
  });
384
411
  var import_server = require("next/server");
385
412
  function mantlePaywall2(opts) {
386
- const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
413
+ const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
387
414
  validateAddress(payTo, "payTo");
388
415
  const priceUsdCents = Math.round(priceUsd * 100);
389
416
  return function(handler) {
@@ -412,6 +439,7 @@ function mantlePaywall2(opts) {
412
439
  paymentHeader,
413
440
  paymentRequirements,
414
441
  facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
442
+ apiKey,
415
443
  routeKey,
416
444
  network,
417
445
  asset: assetConfig.address,
@@ -423,7 +451,19 @@ function mantlePaywall2(opts) {
423
451
  status: result.statusCode
424
452
  });
425
453
  }
426
- return handler(req);
454
+ try {
455
+ const response = await handler(req);
456
+ if (result.sendTelemetryAfterResponse) {
457
+ result.sendTelemetryAfterResponse(response.status);
458
+ }
459
+ return response;
460
+ } catch (err) {
461
+ const errorMessage = err instanceof Error ? err.message : "Unknown error";
462
+ if (result.sendTelemetryAfterResponse) {
463
+ result.sendTelemetryAfterResponse(500, errorMessage);
464
+ }
465
+ throw err;
466
+ }
427
467
  };
428
468
  };
429
469
  }
@@ -437,7 +477,7 @@ __export(web_standards_exports, {
437
477
  mantlePaywall: () => mantlePaywall3
438
478
  });
439
479
  function mantlePaywall3(opts) {
440
- const { priceUsd, payTo, facilitatorUrl, telemetry, onPaymentSettled } = opts;
480
+ const { priceUsd, payTo, facilitatorUrl, apiKey, telemetry, onPaymentSettled } = opts;
441
481
  validateAddress(payTo, "payTo");
442
482
  const priceUsdCents = Math.round(priceUsd * 100);
443
483
  return function(handler) {
@@ -466,6 +506,7 @@ function mantlePaywall3(opts) {
466
506
  paymentHeader,
467
507
  paymentRequirements,
468
508
  facilitatorUrl: facilitatorUrl || MANTLE_DEFAULTS.FACILITATOR_URL,
509
+ apiKey,
469
510
  routeKey,
470
511
  network,
471
512
  asset: assetConfig.address,
@@ -480,7 +521,19 @@ function mantlePaywall3(opts) {
480
521
  }
481
522
  });
482
523
  }
483
- return handler(request);
524
+ try {
525
+ const response = await handler(request);
526
+ if (result.sendTelemetryAfterResponse) {
527
+ result.sendTelemetryAfterResponse(response.status);
528
+ }
529
+ return response;
530
+ } catch (err) {
531
+ const errorMessage = err instanceof Error ? err.message : "Unknown error";
532
+ if (result.sendTelemetryAfterResponse) {
533
+ result.sendTelemetryAfterResponse(500, errorMessage);
534
+ }
535
+ throw err;
536
+ }
484
537
  };
485
538
  };
486
539
  }
package/dist/server.d.cts CHANGED
@@ -1,10 +1,10 @@
1
- import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-CrOsOHcX.cjs';
2
- export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CrOsOHcX.cjs';
1
+ import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-B87bD2yo.cjs';
2
+ export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-B87bD2yo.cjs';
3
3
  import { c as PaymentHeaderObject } from './types-BFUqKBBO.cjs';
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-CGOVHJXl.cjs';
6
- export { w as web } from './web-standards-BNQyWzBC.cjs';
7
- import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-CoOdbZSp.cjs';
4
+ export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-DqyVgO5n.cjs';
5
+ export { n as nextjs } from './nextjs-EoISXVEo.cjs';
6
+ export { w as web } from './web-standards-C6JwCDmd.cjs';
7
+ import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-X6DeBEgb.cjs';
8
8
  import 'express';
9
9
  import 'next/server';
10
10
 
package/dist/server.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-CqQ6OgRi.js';
2
- export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-CqQ6OgRi.js';
1
+ import { c as PaymentCheckInput, d as PaymentCheckResult } from './types-vicT7qsY.js';
2
+ export { M as MinimalPaywallOptions, P as PaymentLogEntry, R as RouteKey, a as RoutePricingConfig, b as RoutesConfig, T as TelemetryConfig } from './types-vicT7qsY.js';
3
3
  import { c as PaymentHeaderObject } from './types-BFUqKBBO.js';
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-w61gRvpz.js';
6
- export { w as web } from './web-standards-D8j1kZxd.js';
7
- import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-DTzov_EE.js';
4
+ export { M as MantleMiddleware, c as createPaymentMiddleware, e as express, m as mantlePaywall } from './express-DxxlKmmF.js';
5
+ export { n as nextjs } from './nextjs-DGcN_MGa.js';
6
+ export { w as web } from './web-standards-BJcdcxD6.js';
7
+ import { P as PaymentLogEntry, T as TelemetryConfig, c as TelemetryEvent } from './types-DvKDSdL6.js';
8
8
  import 'express';
9
9
  import 'next/server';
10
10
 
package/dist/server.js CHANGED
@@ -3,13 +3,13 @@ import {
3
3
  createPaymentMiddleware,
4
4
  express_exports,
5
5
  mantlePaywall
6
- } from "./chunk-MBJTUNDL.js";
6
+ } from "./chunk-IEJB5W26.js";
7
7
  import {
8
8
  nextjs_exports
9
- } from "./chunk-5TTSYEOF.js";
9
+ } from "./chunk-CKBTOS7X.js";
10
10
  import {
11
11
  web_standards_exports
12
- } from "./chunk-F2OTZ3BB.js";
12
+ } from "./chunk-T4DIHTBP.js";
13
13
  import {
14
14
  DEFAULT_TELEMETRY_ENDPOINT,
15
15
  buildRouteKey,
@@ -18,7 +18,7 @@ import {
18
18
  decodePaymentHeader,
19
19
  sendTelemetry,
20
20
  validateAddress
21
- } from "./chunk-EKEVUVF3.js";
21
+ } from "./chunk-IXIFGPJ2.js";
22
22
  import "./chunk-HEZZ74SI.js";
23
23
  export {
24
24
  DEFAULT_TELEMETRY_ENDPOINT,
@@ -0,0 +1,102 @@
1
+ import { N as NetworkId, P as PaymentRequirements } from './types-BFUqKBBO.cjs';
2
+
3
+ /** Unique key for a protected route, e.g. "GET /api/protected". */
4
+ type RouteKey = string;
5
+ /** Pricing config for a single route. */
6
+ interface RoutePricingConfig {
7
+ /** Price in USD cents, e.g. 1 => $0.01. */
8
+ priceUsdCents: number;
9
+ /** Network identifier (e.g. "mantle-mainnet"). */
10
+ network: NetworkId;
11
+ }
12
+ /** Map of route keys to pricing config. */
13
+ type RoutesConfig = Record<RouteKey, RoutePricingConfig>;
14
+ /** Log entry for a successfully settled payment. */
15
+ interface PaymentLogEntry {
16
+ id: string;
17
+ from: string;
18
+ to: string;
19
+ valueAtomic: string;
20
+ network: NetworkId;
21
+ asset: string;
22
+ route?: RouteKey;
23
+ txHash?: string;
24
+ timestamp: number;
25
+ facilitatorUrl?: string;
26
+ paymentRequirements?: PaymentRequirements;
27
+ }
28
+ /** Config for optional telemetry (billing/analytics). */
29
+ interface TelemetryConfig {
30
+ /** Project key from nosubs.ai dashboard. */
31
+ projectKey: string;
32
+ /**
33
+ * Telemetry endpoint URL.
34
+ * If not specified, uses DEFAULT_TELEMETRY_ENDPOINT (see server/constants.ts).
35
+ */
36
+ endpoint?: string;
37
+ }
38
+ /**
39
+ * Minimal config for mantlePaywall() - simplified API for single-route protection.
40
+ * This is the "sweet path" for Mantle mainnet + USDC with sensible defaults.
41
+ */
42
+ interface MinimalPaywallOptions {
43
+ /** Price in USD (e.g. 0.01 for 1 cent). */
44
+ priceUsd: number;
45
+ /** Recipient address (developer wallet). Validated at runtime. */
46
+ payTo: string;
47
+ /** Optional facilitator URL (defaults to localhost:8080 or NEXT_PUBLIC_FACILITATOR_URL). */
48
+ facilitatorUrl?: string;
49
+ /**
50
+ * Optional API key for hosted facilitator billing.
51
+ * Required when using hosted facilitator (e.g., https://facilitator.x402mantlesdk.xyz).
52
+ * Not needed for self-hosted facilitator.
53
+ */
54
+ apiKey?: string;
55
+ /** Optional telemetry config. */
56
+ telemetry?: TelemetryConfig;
57
+ /** Optional payment settled hook. */
58
+ onPaymentSettled?: (entry: PaymentLogEntry) => void;
59
+ }
60
+ /** Input for checkPayment function. */
61
+ interface PaymentCheckInput {
62
+ /** X-PAYMENT header value (base64 encoded), or null if not present. */
63
+ paymentHeader: string | null;
64
+ /** Payment requirements for this route. */
65
+ paymentRequirements: PaymentRequirements;
66
+ /** Facilitator URL for verification. */
67
+ facilitatorUrl: string;
68
+ /** Optional API key for hosted facilitator billing. */
69
+ apiKey?: string;
70
+ /** Route key for logging, e.g. "POST /api/generate-image". */
71
+ routeKey?: RouteKey;
72
+ /** Network ID for this payment. */
73
+ network: NetworkId;
74
+ /** Asset address (ERC-20 token). */
75
+ asset: string;
76
+ /** Telemetry config (optional). */
77
+ telemetry?: TelemetryConfig;
78
+ /** Payment settled callback (optional). */
79
+ onPaymentSettled?: (entry: PaymentLogEntry) => void;
80
+ }
81
+ /** Result from checkPayment function. */
82
+ interface PaymentCheckResult {
83
+ /** Status of the payment check. */
84
+ status: "require_payment" | "verification_error" | "invalid_payment" | "verified";
85
+ /** HTTP status code to return. */
86
+ statusCode: 402 | 500 | 200;
87
+ /** Response body to return (null if verified). */
88
+ responseBody: any | null;
89
+ /** Whether payment is valid (only true if status is "verified"). */
90
+ isValid: boolean;
91
+ /**
92
+ * Callback to send telemetry after handler execution.
93
+ * Only present when payment is verified and telemetry is enabled.
94
+ * Adapters should call this after the protected route handler completes.
95
+ *
96
+ * @param responseStatus - HTTP status code from handler (200, 500, etc.)
97
+ * @param error - Optional error message if handler threw
98
+ */
99
+ sendTelemetryAfterResponse?: (responseStatus: number, error?: string) => void;
100
+ }
101
+
102
+ export type { MinimalPaywallOptions as M, PaymentLogEntry as P, RouteKey as R, TelemetryConfig as T, RoutePricingConfig as a, RoutesConfig as b, PaymentCheckInput as c, PaymentCheckResult as d };