chargebee 3.20.0 → 3.21.1

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/types/index.d.ts CHANGED
@@ -242,5 +242,195 @@ declare module 'chargebee' {
242
242
  usageFile: UsageFile.UsageFileResource;
243
243
  virtualBankAccount: VirtualBankAccount.VirtualBankAccountResource;
244
244
  webhookEndpoint: WebhookEndpoint.WebhookEndpointResource;
245
+
246
+ /** Webhook handler instance with auto-configured Basic Auth (if env vars are set) */
247
+ webhooks: WebhookHandler & {
248
+ /** Create a new typed webhook handler instance */
249
+ createHandler<ReqT = unknown, ResT = unknown>(
250
+ options?: WebhookHandlerOptions,
251
+ ): WebhookHandler<ReqT, ResT>;
252
+ };
253
+ }
254
+
255
+ // Webhook Handler Types
256
+ export type WebhookEventName = EventTypeEnum | 'unhandled_event';
257
+ export type WebhookEventTypeValue = `${WebhookEventType}`;
258
+ /**
259
+ * @deprecated Renamed to `WebhookEventTypeValue` for clarity. Use `WebhookEventTypeValue` instead.
260
+ * This alias will be removed in the next major version.
261
+ */
262
+ export type WebhookContentTypeValue = WebhookEventTypeValue;
263
+
264
+ /**
265
+ * Context object passed to webhook event listeners.
266
+ * Wraps the event data with optional framework-specific request/response objects.
267
+ */
268
+ export interface WebhookContext<ReqT = unknown, ResT = unknown> {
269
+ /** The parsed webhook event from Chargebee */
270
+ event: WebhookEvent;
271
+ /** Framework-specific request object (Express, Fastify, etc.) */
272
+ request?: ReqT;
273
+ /** Framework-specific response object (Express, Fastify, etc.) */
274
+ response?: ResT;
275
+ }
276
+
277
+ /**
278
+ * Context object passed to webhook error listeners.
279
+ * Contains the request/response objects so errors can be handled appropriately.
280
+ */
281
+ export interface WebhookErrorContext<ReqT = unknown, ResT = unknown> {
282
+ /** Framework-specific request object (Express, Fastify, etc.) */
283
+ request?: ReqT;
284
+ /** Framework-specific response object (Express, Fastify, etc.) */
285
+ response?: ResT;
286
+ }
287
+
288
+ /**
289
+ * Validator function type for authenticating webhook requests.
290
+ * Can be synchronous or asynchronous.
291
+ */
292
+ export type RequestValidator = (
293
+ headers: Record<string, string | string[] | undefined>,
294
+ ) => void | Promise<void>;
295
+
296
+ /**
297
+ * Configuration options for WebhookHandler.
298
+ */
299
+ export interface WebhookHandlerOptions {
300
+ /**
301
+ * Optional validator function to authenticate incoming webhook requests.
302
+ * Typically used for Basic Auth validation.
303
+ * Can be sync or async - throw an error to reject the request.
304
+ */
305
+ requestValidator?: RequestValidator;
306
+ }
307
+
308
+ /**
309
+ * Options for the handle() method.
310
+ */
311
+ export interface HandleOptions<ReqT = unknown, ResT = unknown> {
312
+ /** The raw request body (string) or pre-parsed object */
313
+ body: string | object;
314
+ /** Optional HTTP headers for validation */
315
+ headers?: Record<string, string | string[] | undefined>;
316
+ /** Optional framework-specific request object (Express, Fastify, etc.) */
317
+ request?: ReqT;
318
+ /** Optional framework-specific response object (Express, Fastify, etc.) */
319
+ response?: ResT;
320
+ }
321
+
322
+ export type WebhookEventListener<
323
+ ReqT = unknown,
324
+ ResT = unknown,
325
+ T extends WebhookEventType = WebhookEventType,
326
+ > = (
327
+ context: WebhookContext<ReqT, ResT> & { event: WebhookEvent<T> },
328
+ ) => Promise<void> | void;
329
+ export type WebhookErrorListener<ReqT = unknown, ResT = unknown> = (
330
+ error: Error,
331
+ context: WebhookErrorContext<ReqT, ResT>,
332
+ ) => Promise<void> | void;
333
+
334
+ // Helper type to map string literal to enum member
335
+ type StringToWebhookEventType<S extends WebhookEventTypeValue> = {
336
+ [K in WebhookEventType]: `${K}` extends S ? K : never;
337
+ }[WebhookEventType];
338
+
339
+ export interface WebhookHandler<ReqT = unknown, ResT = unknown> {
340
+ on<T extends WebhookEventType>(
341
+ eventName: T,
342
+ listener: WebhookEventListener<ReqT, ResT, T>,
343
+ ): this;
344
+ on<S extends WebhookEventTypeValue>(
345
+ eventName: S,
346
+ listener: WebhookEventListener<ReqT, ResT, StringToWebhookEventType<S>>,
347
+ ): this;
348
+ on(
349
+ eventName: 'unhandled_event',
350
+ listener: WebhookEventListener<ReqT, ResT>,
351
+ ): this;
352
+ on(eventName: 'error', listener: WebhookErrorListener<ReqT, ResT>): this;
353
+ once<T extends WebhookEventType>(
354
+ eventName: T,
355
+ listener: WebhookEventListener<ReqT, ResT, T>,
356
+ ): this;
357
+ once<S extends WebhookEventTypeValue>(
358
+ eventName: S,
359
+ listener: WebhookEventListener<ReqT, ResT, StringToWebhookEventType<S>>,
360
+ ): this;
361
+ once(
362
+ eventName: 'unhandled_event',
363
+ listener: WebhookEventListener<ReqT, ResT>,
364
+ ): this;
365
+ once(eventName: 'error', listener: WebhookErrorListener<ReqT, ResT>): this;
366
+ off<T extends WebhookEventType>(
367
+ eventName: T,
368
+ listener: WebhookEventListener<ReqT, ResT, T>,
369
+ ): this;
370
+ off<S extends WebhookEventTypeValue>(
371
+ eventName: S,
372
+ listener: WebhookEventListener<ReqT, ResT, StringToWebhookEventType<S>>,
373
+ ): this;
374
+ off(
375
+ eventName: 'unhandled_event',
376
+ listener: WebhookEventListener<ReqT, ResT>,
377
+ ): this;
378
+ off(eventName: 'error', listener: WebhookErrorListener<ReqT, ResT>): this;
379
+ handle(options: HandleOptions<ReqT, ResT>): Promise<void>;
380
+ requestValidator: RequestValidator | undefined;
381
+ }
382
+
383
+ // Webhook Auth
384
+ /**
385
+ * Credential validator function type.
386
+ * Can be synchronous or asynchronous (e.g., for database lookups).
387
+ */
388
+ export type CredentialValidator = (
389
+ username: string,
390
+ password: string,
391
+ ) => boolean | Promise<boolean>;
392
+
393
+ /**
394
+ * Creates a Basic Auth validator for webhook requests.
395
+ */
396
+ export function basicAuthValidator(
397
+ validateCredentials: CredentialValidator,
398
+ ): (headers: Record<string, string | string[] | undefined>) => Promise<void>;
399
+
400
+ // Webhook Error Classes
401
+ /**
402
+ * Base class for all webhook-related errors.
403
+ */
404
+ export class WebhookError extends Error {
405
+ constructor(message: string);
406
+ name: string;
407
+ }
408
+
409
+ /**
410
+ * Authentication error thrown when webhook request authentication fails.
411
+ * Typically maps to HTTP 401 Unauthorized.
412
+ */
413
+ export class WebhookAuthenticationError extends WebhookError {
414
+ constructor(message: string);
415
+ name: string;
416
+ }
417
+
418
+ /**
419
+ * Payload validation error thrown when the webhook payload structure is invalid.
420
+ * Typically maps to HTTP 400 Bad Request.
421
+ */
422
+ export class WebhookPayloadValidationError extends WebhookError {
423
+ constructor(message: string);
424
+ name: string;
425
+ }
426
+
427
+ /**
428
+ * JSON parsing error thrown when the webhook body cannot be parsed as JSON.
429
+ * Typically maps to HTTP 400 Bad Request.
430
+ */
431
+ export class WebhookPayloadParseError extends WebhookError {
432
+ constructor(message: string, rawBody?: string);
433
+ name: string;
434
+ readonly rawBody?: string;
245
435
  }
246
436
  }
@@ -221,9 +221,9 @@ declare module 'chargebee' {
221
221
  PlanCreated = 'plan_created',
222
222
  PlanUpdated = 'plan_updated',
223
223
  }
224
-
225
224
  /**
226
- * @deprecated Use WebhookEventType instead.
225
+ * @deprecated Renamed to `WebhookEventType` for clarity. Use `WebhookEventType` instead.
226
+ * This alias will be removed in the next major version.
227
227
  */
228
228
  export import WebhookContentType = WebhookEventType;
229
229