chargebee 3.16.2 → 3.17.0-beta.2
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/CHANGELOG.md +8 -9
- package/README.md +173 -25
- package/cjs/chargebee.cjs.js +9 -4
- package/cjs/environment.js +1 -1
- package/cjs/resources/webhook/auth.js +27 -0
- package/cjs/resources/webhook/content.js +3 -0
- package/cjs/resources/webhook/handler.js +40 -0
- package/esm/chargebee.esm.js +4 -2
- package/esm/environment.js +1 -1
- package/esm/resources/webhook/auth.js +23 -0
- package/esm/resources/webhook/content.js +2 -0
- package/esm/resources/webhook/handler.js +35 -0
- package/package.json +11 -6
- package/types/index.d.ts +65 -0
- package/types/resources/WebhookEvent.d.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
### v3.
|
|
2
|
-
* * *
|
|
1
|
+
### v3.17.0-beta.2 (2025-12-16)
|
|
3
2
|
|
|
4
|
-
###
|
|
5
|
-
* `
|
|
3
|
+
### Enhancements
|
|
4
|
+
* `WebhookEventType` is now a runtime enum that can be used for event type comparisons at runtime.
|
|
5
|
+
* `WebhookEventType` and `WebhookContentType` are now exported from the main entry points.
|
|
6
6
|
|
|
7
|
-
### v3.
|
|
8
|
-
* * *
|
|
7
|
+
### v3.17.0-beta.1 (2025-12-10)
|
|
9
8
|
|
|
10
|
-
###
|
|
11
|
-
*
|
|
12
|
-
*
|
|
9
|
+
### Enhancements
|
|
10
|
+
* Add webhook event handler to process chargebee-events.
|
|
11
|
+
* Deprecated `WebhookContentType` class, added `WebhookEventType` class.
|
|
13
12
|
|
|
14
13
|
### v3.16.0 (2025-12-01)
|
|
15
14
|
* * *
|
package/README.md
CHANGED
|
@@ -148,6 +148,174 @@ const chargebeeSiteEU = new Chargebee({
|
|
|
148
148
|
});
|
|
149
149
|
```
|
|
150
150
|
|
|
151
|
+
### Handle webhooks
|
|
152
|
+
|
|
153
|
+
Use the webhook handlers to parse and route webhook payloads from Chargebee with full TypeScript support.
|
|
154
|
+
|
|
155
|
+
#### Quick Start: Using the default `webhook` instance
|
|
156
|
+
|
|
157
|
+
The simplest way to handle webhooks is using the pre-configured `webhook` instance:
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import express from 'express';
|
|
161
|
+
import { webhook, type WebhookEvent } from 'chargebee';
|
|
162
|
+
|
|
163
|
+
const app = express();
|
|
164
|
+
app.use(express.json());
|
|
165
|
+
|
|
166
|
+
webhook.on('subscription_created', async (event: WebhookEvent) => {
|
|
167
|
+
console.log(`Subscription created: ${event.id}`);
|
|
168
|
+
const subscription = event.content.subscription;
|
|
169
|
+
console.log(`Customer: ${subscription.customer_id}`);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
webhook.on('error', (err: Error) => {
|
|
173
|
+
console.error('Webhook error:', err.message);
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
app.post('/chargebee/webhooks', (req, res) => {
|
|
177
|
+
webhook.handle(req.body, req.headers);
|
|
178
|
+
res.status(200).send('OK');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
app.listen(8080);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
**Auto-configured Basic Auth:** The default `webhook` instance automatically configures Basic Auth validation if the following environment variables are set:
|
|
185
|
+
|
|
186
|
+
- `CHARGEBEE_WEBHOOK_USERNAME` - The expected username
|
|
187
|
+
- `CHARGEBEE_WEBHOOK_PASSWORD` - The expected password
|
|
188
|
+
|
|
189
|
+
When both are present, incoming webhook requests will be validated against these credentials. If not set, no authentication is applied.
|
|
190
|
+
|
|
191
|
+
#### Creating custom `WebhookHandler` instances
|
|
192
|
+
|
|
193
|
+
For more control or multiple webhook endpoints, create your own instances:
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
import express from 'express';
|
|
197
|
+
import { WebhookHandler, basicAuthValidator } from 'chargebee';
|
|
198
|
+
|
|
199
|
+
const app = express();
|
|
200
|
+
app.use(express.json());
|
|
201
|
+
|
|
202
|
+
const handler = new WebhookHandler();
|
|
203
|
+
|
|
204
|
+
// Register event listeners using .on() - events are fully typed
|
|
205
|
+
handler.on('subscription_created', async (event) => {
|
|
206
|
+
console.log(`Subscription created: ${event.id}`);
|
|
207
|
+
const subscription = event.content.subscription;
|
|
208
|
+
console.log(`Customer: ${subscription.customer_id}`);
|
|
209
|
+
console.log(`Plan: ${subscription.plan_id}`);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
handler.on('payment_succeeded', async (event) => {
|
|
213
|
+
console.log(`Payment succeeded: ${event.id}`);
|
|
214
|
+
const transaction = event.content.transaction;
|
|
215
|
+
const customer = event.content.customer;
|
|
216
|
+
console.log(`Amount: ${transaction.amount}, Customer: ${customer.email}`);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// Optional: Add request validator (e.g., Basic Auth)
|
|
220
|
+
handler.requestValidator = basicAuthValidator((username, password) => {
|
|
221
|
+
return username === 'admin' && password === 'secret';
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
app.post('/chargebee/webhooks', (req, res) => {
|
|
225
|
+
handler.handle(req.body, req.headers);
|
|
226
|
+
res.status(200).send('OK');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
app.listen(8080);
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
#### Low-level: Parse and handle events manually
|
|
233
|
+
|
|
234
|
+
For more control, you can parse webhook events manually:
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
import express from 'express';
|
|
238
|
+
import Chargebee, { type WebhookEvent } from 'chargebee';
|
|
239
|
+
|
|
240
|
+
const app = express();
|
|
241
|
+
app.use(express.json());
|
|
242
|
+
|
|
243
|
+
app.post('/chargebee/webhooks', async (req, res) => {
|
|
244
|
+
try {
|
|
245
|
+
const event = req.body as WebhookEvent;
|
|
246
|
+
|
|
247
|
+
switch (event.event_type) {
|
|
248
|
+
case 'subscription_created':
|
|
249
|
+
// Access event content with proper typing
|
|
250
|
+
const subscription = event.content.subscription;
|
|
251
|
+
console.log('Subscription created:', subscription.id);
|
|
252
|
+
break;
|
|
253
|
+
|
|
254
|
+
case 'payment_succeeded':
|
|
255
|
+
const transaction = event.content.transaction;
|
|
256
|
+
console.log('Payment succeeded:', transaction.amount);
|
|
257
|
+
break;
|
|
258
|
+
|
|
259
|
+
default:
|
|
260
|
+
console.log('Unhandled event type:', event.event_type);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
res.status(200).send('OK');
|
|
264
|
+
} catch (err) {
|
|
265
|
+
console.error('Error processing webhook:', err);
|
|
266
|
+
res.status(500).send('Error processing webhook');
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
app.listen(8080);
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
#### Handling Unhandled Events
|
|
274
|
+
|
|
275
|
+
By default, if an incoming webhook event type is not recognized or you haven't registered a corresponding callback handler, the SDK provides flexible options to handle these scenarios:
|
|
276
|
+
|
|
277
|
+
**Using the `unhandled_event` listener:**
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
import { WebhookHandler } from 'chargebee';
|
|
281
|
+
|
|
282
|
+
const handler = new WebhookHandler();
|
|
283
|
+
|
|
284
|
+
handler.on('subscription_created', async (event) => {
|
|
285
|
+
// Handle subscription created
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
// Gracefully handle events without registered listeners
|
|
289
|
+
handler.on('unhandled_event', async (event) => {
|
|
290
|
+
console.log(`Received unhandled event: ${event.event_type}`);
|
|
291
|
+
// Log for monitoring or store for later processing
|
|
292
|
+
});
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
**Using the `error` listener for error handling:**
|
|
296
|
+
|
|
297
|
+
If an error occurs during webhook processing (e.g., invalid JSON, validator failure), the SDK will emit an `error` event:
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
const handler = new WebhookHandler();
|
|
301
|
+
|
|
302
|
+
handler.on('subscription_created', async (event) => {
|
|
303
|
+
// Handle subscription created
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// Catch any errors during webhook processing
|
|
307
|
+
handler.on('error', (err) => {
|
|
308
|
+
console.error('Webhook processing error:', err);
|
|
309
|
+
// Log to monitoring service, alert team, etc.
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
**Best Practices:**
|
|
314
|
+
|
|
315
|
+
- Use `unhandled_event` listener to acknowledge unknown events (return 200 OK) and log them
|
|
316
|
+
- Use `error` listener to catch and handle exceptions thrown during event processing
|
|
317
|
+
- Both listeners help ensure your webhook endpoint remains stable even when new event types are introduced by Chargebee
|
|
318
|
+
|
|
151
319
|
### Processing Webhooks - API Version Check
|
|
152
320
|
|
|
153
321
|
An attribute `api_version` is added to the [Event](https://apidocs.chargebee.com/docs/api/events) resource, which indicates the API version based on which the event content is structured. In your webhook servers, ensure this `api_version` is the same as the [API version](https://apidocs.chargebee.com/docs/api#versions) used by your webhook server's client library.
|
|
@@ -224,38 +392,18 @@ To improve type safety and gain better autocompletion when working with webhooks
|
|
|
224
392
|
#### Example
|
|
225
393
|
|
|
226
394
|
```ts
|
|
227
|
-
import Chargebee, {
|
|
395
|
+
import Chargebee, { type WebhookContentType, WebhookEvent } from "chargebee";
|
|
228
396
|
|
|
229
397
|
const result = await chargebeeInstance.event.retrieve("{event-id}");
|
|
230
|
-
const
|
|
231
|
-
const subscription =
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
You can also use `WebhookEventType` in switch statements for runtime event handling:
|
|
235
|
-
|
|
236
|
-
```ts
|
|
237
|
-
import { WebhookEventType, WebhookEvent } from "chargebee";
|
|
238
|
-
|
|
239
|
-
function handleWebhook(event: WebhookEvent) {
|
|
240
|
-
switch (event.event_type) {
|
|
241
|
-
case WebhookEventType.SubscriptionCreated:
|
|
242
|
-
console.log("Subscription created:", event.content.subscription?.id);
|
|
243
|
-
break;
|
|
244
|
-
case WebhookEventType.PaymentSucceeded:
|
|
245
|
-
console.log("Payment succeeded:", event.content.transaction?.id);
|
|
246
|
-
break;
|
|
247
|
-
default:
|
|
248
|
-
console.log("Unhandled event:", event.event_type);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
398
|
+
const subscripitonActivatedEvent: WebhookEvent<WebhookContentType.SubscriptionActivated> = result.event;
|
|
399
|
+
const subscription = subscripitonActivatedEvent.content.subscription;
|
|
251
400
|
```
|
|
252
401
|
|
|
253
402
|
#### Notes
|
|
254
403
|
|
|
255
404
|
* `WebhookEvent<T>` provides type hinting for the event payload, making it easier to work with specific event structures.
|
|
256
|
-
* Use `
|
|
257
|
-
*
|
|
258
|
-
* `WebhookContentType` is deprecated but still available for backward compatibility.
|
|
405
|
+
* Use the `WebhookContentType` to specify the exact event type (e.g., `SubscriptionCreated`, `InvoiceGenerated`, etc.).
|
|
406
|
+
* This approach ensures you get proper IntelliSense and compile-time checks when accessing event fields.
|
|
259
407
|
|
|
260
408
|
### Custom HTTP Client
|
|
261
409
|
|
package/cjs/chargebee.cjs.js
CHANGED
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const createChargebee_js_1 = require("./createChargebee.js");
|
|
4
4
|
const FetchClient_js_1 = require("./net/FetchClient.js");
|
|
5
|
-
const
|
|
5
|
+
const handler_js_1 = require("./resources/webhook/handler.js");
|
|
6
|
+
const handler_js_2 = require("./resources/webhook/handler.js");
|
|
7
|
+
const auth_js_1 = require("./resources/webhook/auth.js");
|
|
6
8
|
const httpClient = new FetchClient_js_1.FetchHttpClient();
|
|
7
9
|
const Chargebee = (0, createChargebee_js_1.CreateChargebee)(httpClient);
|
|
8
10
|
module.exports = Chargebee;
|
|
9
11
|
module.exports.Chargebee = Chargebee;
|
|
10
12
|
module.exports.default = Chargebee;
|
|
11
|
-
// Export webhook
|
|
12
|
-
module.exports.
|
|
13
|
-
module.exports.
|
|
13
|
+
// Export webhook modules
|
|
14
|
+
module.exports.WebhookHandler = handler_js_1.WebhookHandler;
|
|
15
|
+
module.exports.WebhookEventType = handler_js_1.WebhookEventType;
|
|
16
|
+
module.exports.WebhookContentType = handler_js_1.WebhookContentType;
|
|
17
|
+
module.exports.webhook = handler_js_2.default;
|
|
18
|
+
module.exports.basicAuthValidator = auth_js_1.basicAuthValidator;
|
package/cjs/environment.js
CHANGED
|
@@ -11,7 +11,7 @@ exports.Environment = {
|
|
|
11
11
|
hostSuffix: '.chargebee.com',
|
|
12
12
|
apiPath: '/api/v2',
|
|
13
13
|
timeout: DEFAULT_TIME_OUT,
|
|
14
|
-
clientVersion: '
|
|
14
|
+
clientVersion: '3.17.0-beta.2',
|
|
15
15
|
port: DEFAULT_PORT,
|
|
16
16
|
timemachineWaitInMillis: DEFAULT_TIME_MACHINE_WAIT,
|
|
17
17
|
exportWaitInMillis: DEFAULT_EXPORT_WAIT,
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.basicAuthValidator = void 0;
|
|
4
|
+
const basicAuthValidator = (validateCredentials) => {
|
|
5
|
+
return (headers) => {
|
|
6
|
+
const authHeader = headers['authorization'] || headers['Authorization'];
|
|
7
|
+
if (!authHeader) {
|
|
8
|
+
throw new Error('Invalid authorization header');
|
|
9
|
+
}
|
|
10
|
+
const authStr = Array.isArray(authHeader) ? authHeader[0] : authHeader;
|
|
11
|
+
if (!authStr) {
|
|
12
|
+
throw new Error('Invalid authorization header');
|
|
13
|
+
}
|
|
14
|
+
const parts = authStr.split(' ');
|
|
15
|
+
if (parts.length !== 2 || parts[0] !== 'Basic') {
|
|
16
|
+
throw new Error('Invalid authorization header');
|
|
17
|
+
}
|
|
18
|
+
const credentials = Buffer.from(parts[1], 'base64').toString().split(':');
|
|
19
|
+
if (credentials.length !== 2) {
|
|
20
|
+
throw new Error('Invalid credentials');
|
|
21
|
+
}
|
|
22
|
+
if (!validateCredentials(credentials[0], credentials[1])) {
|
|
23
|
+
throw new Error('Invalid credentials');
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
exports.basicAuthValidator = basicAuthValidator;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebhookHandler = exports.WebhookContentType = exports.WebhookEventType = void 0;
|
|
4
|
+
const node_events_1 = require("node:events");
|
|
5
|
+
const auth_js_1 = require("./auth.js");
|
|
6
|
+
const eventType_js_1 = require("./eventType.js");
|
|
7
|
+
Object.defineProperty(exports, "WebhookEventType", { enumerable: true, get: function () { return eventType_js_1.WebhookEventType; } });
|
|
8
|
+
Object.defineProperty(exports, "WebhookContentType", { enumerable: true, get: function () { return eventType_js_1.WebhookContentType; } });
|
|
9
|
+
class WebhookHandler extends node_events_1.EventEmitter {
|
|
10
|
+
constructor() {
|
|
11
|
+
super({ captureRejections: true });
|
|
12
|
+
}
|
|
13
|
+
handle(body, headers) {
|
|
14
|
+
try {
|
|
15
|
+
if (this.requestValidator && headers) {
|
|
16
|
+
this.requestValidator(headers);
|
|
17
|
+
}
|
|
18
|
+
const event = typeof body === 'string' ? JSON.parse(body) : body;
|
|
19
|
+
const eventType = event.event_type;
|
|
20
|
+
if (this.listenerCount(eventType) > 0) {
|
|
21
|
+
this.emit(eventType, event);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
this.emit('unhandled_event', event);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
this.emit('error', err instanceof Error ? err : new Error(String(err)));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.WebhookHandler = WebhookHandler;
|
|
33
|
+
const webhook = new WebhookHandler();
|
|
34
|
+
// Auto-configure basic auth if env vars are present
|
|
35
|
+
const username = process.env.CHARGEBEE_WEBHOOK_USERNAME;
|
|
36
|
+
const password = process.env.CHARGEBEE_WEBHOOK_PASSWORD;
|
|
37
|
+
if (username && password) {
|
|
38
|
+
webhook.requestValidator = (0, auth_js_1.basicAuthValidator)((u, p) => u === username && p === password);
|
|
39
|
+
}
|
|
40
|
+
exports.default = webhook;
|
package/esm/chargebee.esm.js
CHANGED
|
@@ -3,5 +3,7 @@ import { FetchHttpClient } from './net/FetchClient.js';
|
|
|
3
3
|
const httpClient = new FetchHttpClient();
|
|
4
4
|
const Chargebee = CreateChargebee(httpClient);
|
|
5
5
|
export default Chargebee;
|
|
6
|
-
// Export webhook
|
|
7
|
-
export { WebhookEventType, WebhookContentType, } from './resources/webhook/
|
|
6
|
+
// Export webhook modules
|
|
7
|
+
export { WebhookHandler, WebhookEventType, WebhookContentType, } from './resources/webhook/handler.js';
|
|
8
|
+
export { default as webhook } from './resources/webhook/handler.js';
|
|
9
|
+
export { basicAuthValidator } from './resources/webhook/auth.js';
|
package/esm/environment.js
CHANGED
|
@@ -8,7 +8,7 @@ export const Environment = {
|
|
|
8
8
|
hostSuffix: '.chargebee.com',
|
|
9
9
|
apiPath: '/api/v2',
|
|
10
10
|
timeout: DEFAULT_TIME_OUT,
|
|
11
|
-
clientVersion: '
|
|
11
|
+
clientVersion: '3.17.0-beta.2',
|
|
12
12
|
port: DEFAULT_PORT,
|
|
13
13
|
timemachineWaitInMillis: DEFAULT_TIME_MACHINE_WAIT,
|
|
14
14
|
exportWaitInMillis: DEFAULT_EXPORT_WAIT,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const basicAuthValidator = (validateCredentials) => {
|
|
2
|
+
return (headers) => {
|
|
3
|
+
const authHeader = headers['authorization'] || headers['Authorization'];
|
|
4
|
+
if (!authHeader) {
|
|
5
|
+
throw new Error('Invalid authorization header');
|
|
6
|
+
}
|
|
7
|
+
const authStr = Array.isArray(authHeader) ? authHeader[0] : authHeader;
|
|
8
|
+
if (!authStr) {
|
|
9
|
+
throw new Error('Invalid authorization header');
|
|
10
|
+
}
|
|
11
|
+
const parts = authStr.split(' ');
|
|
12
|
+
if (parts.length !== 2 || parts[0] !== 'Basic') {
|
|
13
|
+
throw new Error('Invalid authorization header');
|
|
14
|
+
}
|
|
15
|
+
const credentials = Buffer.from(parts[1], 'base64').toString().split(':');
|
|
16
|
+
if (credentials.length !== 2) {
|
|
17
|
+
throw new Error('Invalid credentials');
|
|
18
|
+
}
|
|
19
|
+
if (!validateCredentials(credentials[0], credentials[1])) {
|
|
20
|
+
throw new Error('Invalid credentials');
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { basicAuthValidator } from './auth.js';
|
|
3
|
+
import { WebhookEventType, WebhookContentType } from './eventType.js';
|
|
4
|
+
export { WebhookEventType, WebhookContentType };
|
|
5
|
+
export class WebhookHandler extends EventEmitter {
|
|
6
|
+
constructor() {
|
|
7
|
+
super({ captureRejections: true });
|
|
8
|
+
}
|
|
9
|
+
handle(body, headers) {
|
|
10
|
+
try {
|
|
11
|
+
if (this.requestValidator && headers) {
|
|
12
|
+
this.requestValidator(headers);
|
|
13
|
+
}
|
|
14
|
+
const event = typeof body === 'string' ? JSON.parse(body) : body;
|
|
15
|
+
const eventType = event.event_type;
|
|
16
|
+
if (this.listenerCount(eventType) > 0) {
|
|
17
|
+
this.emit(eventType, event);
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
this.emit('unhandled_event', event);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
this.emit('error', err instanceof Error ? err : new Error(String(err)));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const webhook = new WebhookHandler();
|
|
29
|
+
// Auto-configure basic auth if env vars are present
|
|
30
|
+
const username = process.env.CHARGEBEE_WEBHOOK_USERNAME;
|
|
31
|
+
const password = process.env.CHARGEBEE_WEBHOOK_PASSWORD;
|
|
32
|
+
if (username && password) {
|
|
33
|
+
webhook.requestValidator = basicAuthValidator((u, p) => u === username && p === password);
|
|
34
|
+
}
|
|
35
|
+
export default webhook;
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chargebee",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.17.0-beta.2",
|
|
4
4
|
"description": "A library for integrating with Chargebee.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"prepack": "npm install && npm run build",
|
|
7
|
+
"test": "mocha -r ts-node/register 'test/**/*.test.ts'",
|
|
7
8
|
"build": "npm run build-esm && npm run build-cjs",
|
|
8
9
|
"build-esm": "rm -rf esm && mkdir -p esm && tsc -p tsconfig.esm.json && echo '{\"type\":\"module\"}' > esm/package.json",
|
|
9
10
|
"build-cjs": "rm -rf cjs && mkdir -p cjs && tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > cjs/package.json",
|
|
@@ -32,8 +33,6 @@
|
|
|
32
33
|
"url": "http://github.com/chargebee/chargebee-node/blob/master/LICENSE"
|
|
33
34
|
}
|
|
34
35
|
],
|
|
35
|
-
"dependencies": {
|
|
36
|
-
},
|
|
37
36
|
"exports": {
|
|
38
37
|
"types": "./types/index.d.ts",
|
|
39
38
|
"browser": {
|
|
@@ -62,13 +61,19 @@
|
|
|
62
61
|
}
|
|
63
62
|
},
|
|
64
63
|
"devDependencies": {
|
|
65
|
-
"@types/
|
|
64
|
+
"@types/chai": "^4.3.5",
|
|
65
|
+
"@types/mocha": "^10.0.10",
|
|
66
|
+
"@types/node": "20.12.0",
|
|
67
|
+
"chai": "^4.3.7",
|
|
68
|
+
"mocha": "^10.2.0",
|
|
66
69
|
"prettier": "^3.3.3",
|
|
67
|
-
"
|
|
70
|
+
"ts-node": "^10.9.1",
|
|
71
|
+
"typescript": "^5.5.4",
|
|
72
|
+
"undici-types": "^7.16.0"
|
|
68
73
|
},
|
|
69
74
|
"prettier": {
|
|
70
75
|
"semi": true,
|
|
71
76
|
"singleQuote": true,
|
|
72
77
|
"parser": "typescript"
|
|
73
78
|
}
|
|
74
|
-
}
|
|
79
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -250,4 +250,69 @@ declare module 'chargebee' {
|
|
|
250
250
|
virtualBankAccount: VirtualBankAccount.VirtualBankAccountResource;
|
|
251
251
|
webhookEndpoint: WebhookEndpoint.WebhookEndpointResource;
|
|
252
252
|
}
|
|
253
|
+
|
|
254
|
+
// Webhook Handler
|
|
255
|
+
export type WebhookEventName = EventTypeEnum | 'unhandled_event';
|
|
256
|
+
export type WebhookEventTypeValue = `${WebhookEventType}`;
|
|
257
|
+
/** @deprecated Use WebhookEventTypeValue instead */
|
|
258
|
+
export type WebhookContentTypeValue = WebhookEventTypeValue;
|
|
259
|
+
|
|
260
|
+
export type WebhookEventListener<
|
|
261
|
+
T extends WebhookEventType = WebhookEventType,
|
|
262
|
+
> = (event: WebhookEvent<T>) => Promise<void> | void;
|
|
263
|
+
export type WebhookErrorListener = (error: Error) => Promise<void> | void;
|
|
264
|
+
|
|
265
|
+
// Helper type to map string literal to enum member
|
|
266
|
+
type StringToWebhookEventType<S extends WebhookEventTypeValue> = {
|
|
267
|
+
[K in WebhookEventType]: `${K}` extends S ? K : never;
|
|
268
|
+
}[WebhookEventType];
|
|
269
|
+
|
|
270
|
+
export class WebhookHandler {
|
|
271
|
+
on<T extends WebhookEventType>(
|
|
272
|
+
eventName: T,
|
|
273
|
+
listener: WebhookEventListener<T>,
|
|
274
|
+
): this;
|
|
275
|
+
on<S extends WebhookEventTypeValue>(
|
|
276
|
+
eventName: S,
|
|
277
|
+
listener: WebhookEventListener<StringToWebhookEventType<S>>,
|
|
278
|
+
): this;
|
|
279
|
+
on(eventName: 'unhandled_event', listener: WebhookEventListener): this;
|
|
280
|
+
on(eventName: 'error', listener: WebhookErrorListener): this;
|
|
281
|
+
once<T extends WebhookEventType>(
|
|
282
|
+
eventName: T,
|
|
283
|
+
listener: WebhookEventListener<T>,
|
|
284
|
+
): this;
|
|
285
|
+
once<S extends WebhookEventTypeValue>(
|
|
286
|
+
eventName: S,
|
|
287
|
+
listener: WebhookEventListener<StringToWebhookEventType<S>>,
|
|
288
|
+
): this;
|
|
289
|
+
once(eventName: 'unhandled_event', listener: WebhookEventListener): this;
|
|
290
|
+
once(eventName: 'error', listener: WebhookErrorListener): this;
|
|
291
|
+
off<T extends WebhookEventType>(
|
|
292
|
+
eventName: T,
|
|
293
|
+
listener: WebhookEventListener<T>,
|
|
294
|
+
): this;
|
|
295
|
+
off<S extends WebhookEventTypeValue>(
|
|
296
|
+
eventName: S,
|
|
297
|
+
listener: WebhookEventListener<StringToWebhookEventType<S>>,
|
|
298
|
+
): this;
|
|
299
|
+
off(eventName: 'unhandled_event', listener: WebhookEventListener): this;
|
|
300
|
+
off(eventName: 'error', listener: WebhookErrorListener): this;
|
|
301
|
+
handle(
|
|
302
|
+
body: string | object,
|
|
303
|
+
headers?: Record<string, string | string[] | undefined>,
|
|
304
|
+
): void;
|
|
305
|
+
onError?: (error: any) => void;
|
|
306
|
+
requestValidator?: (
|
|
307
|
+
headers: Record<string, string | string[] | undefined>,
|
|
308
|
+
) => void;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// Webhook Auth
|
|
312
|
+
export function basicAuthValidator(
|
|
313
|
+
validateCredentials: (username: string, password: string) => boolean,
|
|
314
|
+
): (headers: Record<string, string | string[] | undefined>) => void;
|
|
315
|
+
|
|
316
|
+
// Default webhook handler instance
|
|
317
|
+
export const webhook: WebhookHandler;
|
|
253
318
|
}
|