chargebee 2.41.0 → 3.0.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.
Files changed (118) hide show
  1. package/CHANGELOG.md +11 -15
  2. package/LICENSE +1 -1
  3. package/README.md +90 -170
  4. package/cjs/RequestWrapper.js +84 -0
  5. package/cjs/asyncApiSupport.js +39 -0
  6. package/cjs/chargebee.cjs.js +13 -0
  7. package/cjs/chargebee.cjs.worker.js +9 -0
  8. package/cjs/chargebeeError.js +17 -0
  9. package/cjs/coreCommon.js +72 -0
  10. package/cjs/createChargebee.js +92 -0
  11. package/cjs/environment.js +18 -0
  12. package/cjs/filter.js +1 -0
  13. package/cjs/net/ClientInterface.js +40 -0
  14. package/cjs/net/FetchClient.js +86 -0
  15. package/cjs/net/NodeClient.js +61 -0
  16. package/cjs/package.json +1 -0
  17. package/cjs/resources/api_endpoints.js +1086 -0
  18. package/cjs/util.js +218 -0
  19. package/esm/RequestWrapper.js +80 -0
  20. package/esm/asyncApiSupport.js +35 -0
  21. package/esm/chargebee.esm.js +9 -0
  22. package/esm/chargebee.esm.worker.js +5 -0
  23. package/esm/chargebeeError.js +13 -0
  24. package/esm/coreCommon.js +67 -0
  25. package/esm/createChargebee.js +88 -0
  26. package/esm/environment.js +15 -0
  27. package/esm/filter.js +1 -0
  28. package/esm/net/ClientInterface.js +35 -0
  29. package/esm/net/FetchClient.js +81 -0
  30. package/esm/net/NodeClient.js +56 -0
  31. package/esm/package.json +1 -0
  32. package/esm/resources/api_endpoints.js +1083 -0
  33. package/esm/util.js +202 -0
  34. package/package.json +69 -38
  35. package/types/core.d.ts +558 -89
  36. package/types/index.d.ts +90 -55
  37. package/types/resources/Address.d.ts +57 -272
  38. package/types/resources/AdvanceInvoiceSchedule.d.ts +21 -94
  39. package/types/resources/AttachedItem.d.ts +91 -329
  40. package/types/resources/Attribute.d.ts +5 -5
  41. package/types/resources/BusinessEntity.d.ts +49 -69
  42. package/types/resources/BusinessEntityTransfer.d.ts +11 -17
  43. package/types/resources/Card.d.ts +146 -424
  44. package/types/resources/Comment.d.ts +72 -189
  45. package/types/resources/Contact.d.ts +12 -64
  46. package/types/resources/ContractTerm.d.ts +15 -96
  47. package/types/resources/Coupon.d.ts +282 -758
  48. package/types/resources/CouponCode.d.ts +56 -52
  49. package/types/resources/CouponSet.d.ts +92 -234
  50. package/types/resources/CreditNote.d.ts +588 -1758
  51. package/types/resources/CreditNoteEstimate.d.ts +131 -157
  52. package/types/resources/Currency.d.ts +75 -62
  53. package/types/resources/Customer.d.ts +892 -835
  54. package/types/resources/CustomerEntitlement.d.ts +24 -35
  55. package/types/resources/DifferentialPrice.d.ts +116 -314
  56. package/types/resources/Discount.d.ts +20 -125
  57. package/types/resources/Download.d.ts +6 -22
  58. package/types/resources/Entitlement.d.ts +51 -160
  59. package/types/resources/EntitlementOverride.d.ts +58 -139
  60. package/types/resources/Estimate.d.ts +1178 -527
  61. package/types/resources/Event.d.ts +67 -178
  62. package/types/resources/Export.d.ts +563 -723
  63. package/types/resources/Feature.d.ts +117 -433
  64. package/types/resources/GatewayErrorDetail.d.ts +16 -72
  65. package/types/resources/Gift.d.ts +265 -432
  66. package/types/resources/Hierarchy.d.ts +8 -36
  67. package/types/resources/HostedPage.d.ts +1017 -1153
  68. package/types/resources/ImpactedItem.d.ts +13 -50
  69. package/types/resources/ImpactedItemPrice.d.ts +13 -15
  70. package/types/resources/ImpactedSubscription.d.ts +13 -45
  71. package/types/resources/InAppSubscription.d.ts +93 -389
  72. package/types/resources/Installment.d.ts +35 -110
  73. package/types/resources/InstallmentConfig.d.ts +54 -155
  74. package/types/resources/InstallmentDetail.d.ts +19 -27
  75. package/types/resources/Invoice.d.ts +1574 -3157
  76. package/types/resources/InvoiceEstimate.d.ts +131 -161
  77. package/types/resources/Item.d.ts +133 -611
  78. package/types/resources/ItemEntitlement.d.ts +78 -215
  79. package/types/resources/ItemFamily.d.ts +69 -178
  80. package/types/resources/ItemPrice.d.ts +291 -1203
  81. package/types/resources/Metadata.d.ts +3 -5
  82. package/types/resources/Order.d.ts +538 -1471
  83. package/types/resources/PaymentIntent.d.ts +163 -347
  84. package/types/resources/PaymentReferenceNumber.d.ts +7 -29
  85. package/types/resources/PaymentSource.d.ts +476 -1272
  86. package/types/resources/PaymentVoucher.d.ts +87 -306
  87. package/types/resources/PortalSession.d.ts +71 -207
  88. package/types/resources/PriceVariant.d.ts +91 -279
  89. package/types/resources/PricingPageSession.d.ts +92 -41
  90. package/types/resources/PromotionalCredit.d.ts +90 -346
  91. package/types/resources/Purchase.d.ts +185 -315
  92. package/types/resources/Quote.d.ts +1406 -887
  93. package/types/resources/QuoteLineGroup.d.ts +125 -452
  94. package/types/resources/QuotedCharge.d.ts +45 -274
  95. package/types/resources/QuotedSubscription.d.ts +100 -488
  96. package/types/resources/Ramp.d.ts +230 -747
  97. package/types/resources/ResourceMigration.d.ts +25 -80
  98. package/types/resources/SiteMigrationDetail.d.ts +34 -112
  99. package/types/resources/Subscription.d.ts +2390 -4336
  100. package/types/resources/SubscriptionEntitlement.d.ts +59 -160
  101. package/types/resources/SubscriptionEstimate.d.ts +51 -75
  102. package/types/resources/TaxWithheld.d.ts +15 -40
  103. package/types/resources/ThirdPartyPaymentMethod.d.ts +7 -29
  104. package/types/resources/TimeMachine.d.ts +43 -97
  105. package/types/resources/Token.d.ts +15 -85
  106. package/types/resources/Transaction.d.ts +276 -911
  107. package/types/resources/UnbilledCharge.d.ts +189 -164
  108. package/types/resources/Usage.d.ts +89 -279
  109. package/types/resources/VirtualBankAccount.d.ts +103 -255
  110. package/types/resources/filter.d.ts +52 -0
  111. package/.github/ISSUE_TEMPLATE/bug_report.yml +0 -81
  112. package/.github/ISSUE_TEMPLATE/config.yml +0 -6
  113. package/.github/ISSUE_TEMPLATE/feature_request.yml +0 -34
  114. package/.github/PULL_REQUEST_TEMPLATE.md +0 -8
  115. package/lib/chargebee.js +0 -512
  116. package/lib/resources/api_endpoints.js +0 -2935
  117. package/types/resources/NonSubscription.d.ts +0 -30
  118. package/types/resources/Session.d.ts +0 -41
package/CHANGELOG.md CHANGED
@@ -1,21 +1,17 @@
1
- ### v2.41.0 (2024-08-29)
1
+ ### v3.0.0-beta.2 (2024-08-29)
2
2
  * * *
3
3
 
4
- #### New Resource:
5
- * CustomerEntitlement has been added.
6
-
7
- #### New Endpoints:
8
- * Ramp#UpdateRequest has been added to Ramp resource.
9
- * Transaction#ReconcileRequest has been added to Transaction resource.
4
+ #### Bug Fixes:
5
+ * content-length bug fixes for fetch.
10
6
 
11
- ### New Attributes:
12
- * arr has been added to Subscription.
7
+ ### v3.0.0-beta.1 (2024-08-26)
8
+ * * *
13
9
 
14
- ### New Enum Values:
15
- * customer_entitlements_updated has been added to EntityTypeEnum.
16
- * subscription_moved_in has been added in EventType enum.
17
- * subscription_moved_out has been added in EventType enum.
18
- * subscription_movement_failed has been added in EventType enum.
10
+ * Names of the resources, operations etc. have been changed from snake_case to camelCase. For example, configuration option api_key is now apiKey.
11
+ * Minimum Node.js version is 18
12
+ * Class based models are replaced with TypeScript types.
13
+ * Response will contain headers instead of responseHeaders.
14
+ * Response will contain isIdempotencyReplayed field instead of isIdempotencyReplayed() method.
19
15
 
20
16
  ### v2.40.0 (2024-08-14)
21
17
  * * *
@@ -2562,4 +2558,4 @@ Support for deleting the plans & addons. See our API documentation on [Delete a
2562
2558
 
2563
2559
  ### v1.0.0 (2014-02-02)
2564
2560
  * * *
2565
- Initial version of ChargeBee NodeJS binding.
2561
+ Initial version of ChargeBee NodeJS binding.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2011-2024 ChargeBee, Inc.
3
+ Copyright (c) 2011-2024 Chargebee, Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person
6
6
  obtaining a copy of this software and associated documentation
package/README.md CHANGED
@@ -1,228 +1,148 @@
1
- # Chargebee Node.js Client Library
1
+ # Chargebee Node.js / TypeScript client library (Beta)
2
2
 
3
- [![npm](https://img.shields.io/npm/v/chargebee.svg?maxAge=3)](https://www.npmjs.com/package/chargebee)
4
- [![npm](https://img.shields.io/npm/dt/chargebee.svg?maxAge=3)](https://www.npmjs.com/package/chargebee)
3
+ This is the [Node.js](http://nodejs.org/) library for integrating with Chargebee. Sign up for a Chargebee account [here](https://www.chargebee.com).
5
4
 
6
- This is the [node.js](http://nodejs.org/) library for integrating with Chargebee. Sign up for a Chargebee account [here](https://www.chargebee.com).
7
-
8
- > **Note**
9
- > If you’re using [API V1](https://apidocs.chargebee.com/docs/api/v1), head to [chargebee-v1 branch](https://github.com/chargebee/chargebee-node/tree/chargebee-v1).
5
+ Please refer to the [migration guide](https://github.com/chargebee/chargebee-node/wiki/Migration-guide-for-v3) if you are migrating from the older versions of [chargebee-typescript](https://www.npmjs.com/package/chargebee-typescript) or [chargebee](https://www.npmjs.com/package/chargebee/v/2.40.0).
10
6
 
11
7
  ## Requirements
12
8
 
13
- Node 0.6 or higher.
9
+ Node.js 18 or higher.
14
10
 
15
11
  ## Installation
16
12
 
17
- Install the latest version of the library with:
13
+ Install the beta version of the library with npm:
18
14
 
19
15
  ```sh
20
- npm install chargebee
21
- # or
22
- yarn add chargebee
23
- # or
24
- pnpm install chargebee
16
+ npm install chargebee@beta
17
+ ```
18
+ With pnpm:
19
+ ```sh
20
+ pnpm add chargebee@beta
25
21
  ```
26
22
 
27
- ## Usage
23
+ With yarn:
24
+ ```sh
25
+ yarn add chargebee@beta
26
+ ```
28
27
 
29
- The package needs to be configured with your site's API key, which is available under Configure Chargebee Section. Refer [here](https://www.chargebee.com/docs/2.0/api_keys.html) for more details.
28
+ ## Usage
30
29
 
31
- The full documentation can be found on the Chargebee API Docs: [https://apidocs.chargebee.com/docs/api?lang=node](https://apidocs.chargebee.com/docs/api?lang=node)
30
+ The package needs to be configured with your site's API key, which is available under Configure Chargebee Section. Refer [here](https://www.chargebee.com/docs/2.0/api_keys.html) for more details.
32
31
 
32
+ If you're using ESM / TypeScript:
33
33
 
34
- ```js
35
- const chargebee = require('chargebee');
34
+ ```typescript
35
+ import Chargebee from 'chargebee';
36
36
 
37
- chargebee.configure({
38
- site: '<YOUR_SITE_NAME>',
39
- api_key: '<YOUR_API_KEY>',
37
+ const chargebee = new Chargebee({
38
+ site: "{{site}}",
39
+ apiKey: "{{api-key}}",
40
40
  });
41
41
  ```
42
42
 
43
- Or using ES modules,
43
+ Or using Common JS module system:
44
44
 
45
- ```js
46
- import chargebee from 'chargebee';
45
+ ```javascript
46
+ const Chargebee = require('chargebee');
47
47
 
48
- chargebee.configure({
49
- site: '<YOUR_SITE_NAME>',
50
- api_key: '<YOUR_API_KEY>',
48
+ const chargebee = new Chargebee({
49
+ site: "{{site}}",
50
+ apiKey: "{{api-key}}",
51
51
  });
52
-
53
52
  ```
54
53
 
55
54
  ### Using Async / Await
56
55
 
57
- ```js
56
+ ```typescript
58
57
  try {
59
- const result = await chargebee.customer
60
- .create({
61
- email: 'john@test.com',
62
- // other params
63
- })
64
- .request();
65
- // access customer as result.customer;
58
+ const { customer } = await chargebee.customer.create({
59
+ email: "john@test.com"
60
+ // other params
61
+ });
66
62
  } catch (err) {
67
63
  // handle error
68
64
  }
69
65
  ```
70
66
 
71
- ### Using Promises
72
-
73
- ```js
74
- chargebee.customer
75
- .create({
76
- email: 'john@test.com',
77
- // other params
78
- })
79
- .request()
80
- .then((result) => {
81
- // handle result
82
- // access customer as result.customer;
83
- })
84
- .catch((err) => {
85
- // handle error
86
- });
87
- ```
88
-
89
- ### Using callbacks
90
-
91
- ```js
92
- chargebee.customer
93
- .create({
94
- email: 'john@test.com',
95
- // other params
96
- })
97
- .request(function (error, result) {
98
- if (error) {
99
- // handle error
100
- } else {
101
- // handle result
102
- // access customer as result.customer;
103
- }
104
- });
105
- ```
106
-
107
- ### Usage with TypeScript
108
-
109
- You can import the types as shown below.
110
-
111
- ```ts
112
- import chargebee, { Customer } from 'chargebee';
113
-
114
- chargebee.configure({
115
- site: '<YOUR_SITE_NAME>',
116
- api_key: '<YOUR_API_KEY>',
117
- });
118
-
119
- const createCustomer = async () => {
120
- const inputParams: Customer.CreateInputParam = {
121
- email: 'john@test.com',
122
- first_name: 'John',
123
- last_name: 'Doe',
124
- };
125
-
126
- const { customer } = await chargebee.customer.create(inputParams).request();
127
- console.log(customer);
128
- };
129
-
130
- createCustomer();
131
- ```
132
-
133
67
  ### Using filters in the List API
134
68
 
135
- For pagination: `offset` is the parameter that is being used. The value used for this parameter must be the value returned for `next_offset` parameter in the previous API call.
69
+ For pagination, `offset` is the parameter that is being used. The value used for this parameter must be the value returned for `next_offset` parameter in the previous API call.
136
70
 
137
- ```js
138
- const fetchCustomers = async (offset) => {
139
- const result = await chargebee.customer.list({
71
+ ```typescript
72
+ async function getAllCustomers() {
73
+ const allCustomers: Customer[] = [];
74
+ let offset: string | undefined = undefined;
75
+
76
+ do {
77
+ const listCustomersReponse = await chargebee.customer.list({
140
78
  limit: 2,
141
- offset: offset,
142
- first_name: { is: 'John' },
143
- }).request();
144
-
145
- return {
146
- customers: result.list.map((obj) => obj.customer),
147
- next_offset: result.next_offset,
148
- };
149
- };
150
-
151
- const getCustomers = async () => {
152
- const { customers, next_offset } = await fetchCustomers();
153
- console.log('Offset:', next_offset); // Print the offset value
154
-
155
- // Fetching next set of customers
156
- await fetchCustomers(next_offset);
157
- };
158
-
159
- getCustomers().catch((err) => {
160
- console.log(err);
161
- });
79
+ offset,
80
+ first_name: {
81
+ is: "John"
82
+ }
83
+ });
84
+
85
+ const customers = listCustomersReponse.list.map(
86
+ (object) => object.customer
87
+ );
88
+
89
+ allCustomers.push(...customers);
90
+ offset = listCustomersReponse.next_offset;
91
+ } while (offset);
92
+
93
+ console.log(allCustomers);
94
+ }
162
95
  ```
163
96
 
164
- ### Using custom headers and custom fields:
165
-
166
- ```js
167
- const result = await chargebee.customer
168
- .create({ email: 'john@test.com', cf_host_url: 'http://xyz.com' }) //Add custom field in payload
169
- .headers({
170
- 'chargebee-event-email': 'all-disabled', // To disable webhooks
171
- 'chargebee-request-origin-ip': '192.168.1.2',
172
- })
173
- .setIdempotencyKey("safeKey")
174
- .request();
97
+ ### Using custom headers and custom fields
175
98
 
176
- const customer = result.customer;
177
- console.log(customer.cf_host_url);
99
+ ```typescript
100
+ const { customer } = await chargebee.customer.create(
101
+ {
102
+ email: "john@test.com",
103
+ cf_host_url: "http://xyz.com" // `cf_host_url` is a custom field in Customer object
104
+ },
105
+ {
106
+ "chargebee-event-email": "all-disabled" // To disable webhooks
107
+ }
108
+ );
178
109
  ```
179
110
 
180
111
  ### Creating an idempotent request
181
112
 
182
113
  [Idempotency keys](https://apidocs.chargebee.com/docs/api/idempotency?prod_cat_ver=2) are passed along with request headers to allow a safe retry of POST requests.
183
114
 
184
- ```js
185
- const result = await chargebee.customer
186
- .create({ email: 'john@test.com' })
187
- .setIdempotencyKey("safeKey")
188
- .request();
189
- const customer = result.customer;
190
- const headers = result.headers;
191
- const isIdempotencyReplayed = result.isIdempotencyReplayed;
192
- ```
193
-
194
- OR
195
-
196
- ```js
197
- chargebee.customer.create({ email: 'john@test.com', cf_host_url: 'http://xyz.com' })
198
- .setIdempotencyKey("safeKey")
199
- .request(function(error,result) {
200
- if(error){
201
- //handle error
202
- }else{
203
- const customer = result.customer;
204
- const headers = result.headers;
205
- const isIdempotencyReplayed = result.isIdempotencyReplayed;
115
+ ```typescript
116
+ const { customer, isIdempotencyReplayed } = await chargebee.customer.create(
117
+ { email: "john@test.com" },
118
+ {
119
+ "chargebee-idempotency-key": "eBs7iOFQuR7asUKHfddyxDDerOuF1JtFrLmDI" // Add idempotency key
206
120
  }
207
- });
121
+ );
122
+ console.log("isIdempotencyReplayed: ", isIdempotencyReplayed);
208
123
  ```
209
124
 
210
- ### Passing API Keys at request level
125
+ ### Creating multiple instances of Chargebee for different sites
126
+
127
+ ```typescript
128
+ const chargebeeSiteUS = new Chargebee({
129
+ apiKey: "{api-key}",
130
+ site: "my-site-us"
131
+ });
211
132
 
212
- ```js
213
- const newCust = await chargebee.customer.create({
214
- email: 'john@test.com',
215
- first_name: 'John',
216
- last_name: 'Doe'
217
- }).request({
218
- site: '<YOUR_SITE_NAME>',
219
- api_key: '<YOUR_API_KEY>',
133
+ const chargebeeSiteEU = new Chargebee({
134
+ apiKey: "{api-key}",
135
+ site: "my-site-eu"
220
136
  });
221
137
  ```
222
138
 
223
139
  ### Processing Webhooks - API Version Check
224
140
 
225
- An attribute, <b>api_version</b>, 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.
141
+ 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.
142
+
143
+ ## Feedback
144
+
145
+ If you find any bugs or have any feedback, open an issue in this repository or email it to dx@chargebee.com
226
146
 
227
147
  ## License
228
148
 
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestWrapper = void 0;
4
+ const util_js_1 = require("./util.js");
5
+ const coreCommon_js_1 = require("./coreCommon.js");
6
+ const node_buffer_1 = require("node:buffer");
7
+ class RequestWrapper {
8
+ constructor(args, apiCall, envArg) {
9
+ this.getRequest = () => {
10
+ return this.request();
11
+ };
12
+ this.args = args;
13
+ this.apiCall = apiCall;
14
+ this.envArg = envArg;
15
+ this.httpHeaders = {};
16
+ if (this.apiCall.hasIdInUrl) {
17
+ RequestWrapper._validateIdParam(this.args[0]);
18
+ }
19
+ }
20
+ static _validateIdParam(idParam) {
21
+ if (!idParam || typeof idParam !== 'string' || idParam.trim().length < 1) {
22
+ throw new Error('the required id parameter missing or wrong');
23
+ }
24
+ return idParam;
25
+ }
26
+ request() {
27
+ let env = {};
28
+ (0, util_js_1.extend)(true, env, this.envArg);
29
+ const urlIdParam = this.apiCall.hasIdInUrl ? this.args[0] : null;
30
+ let params = this.apiCall.hasIdInUrl
31
+ ? this.args[1]
32
+ : this.args[0];
33
+ let headers = this.apiCall.hasIdInUrl ? this.args[2] : this.args[1];
34
+ Object.assign(this.httpHeaders, headers);
35
+ const promise = new Promise(async (resolve, reject) => {
36
+ function callBackWrapper(err, response) {
37
+ if (err) {
38
+ reject(err);
39
+ }
40
+ else {
41
+ resolve(response);
42
+ }
43
+ }
44
+ let path = (0, util_js_1.getApiURL)(env, this.apiCall.urlPrefix, this.apiCall.urlSuffix, urlIdParam);
45
+ if (typeof params === 'undefined' || params === null) {
46
+ params = {};
47
+ }
48
+ if (this.apiCall.httpMethod === 'GET') {
49
+ params = (0, util_js_1.serialize)(params);
50
+ let queryParam = this.apiCall.isListReq
51
+ ? (0, util_js_1.encodeListParams)(params)
52
+ : (0, util_js_1.encodeParams)(params);
53
+ path += '?' + queryParam;
54
+ params = {};
55
+ }
56
+ let data = (0, util_js_1.encodeParams)(params);
57
+ if (data.length) {
58
+ (0, util_js_1.extend)(true, this.httpHeaders, {
59
+ 'Content-Length': data.length,
60
+ });
61
+ }
62
+ (0, util_js_1.extend)(true, this.httpHeaders, {
63
+ Authorization: 'Basic ' + node_buffer_1.Buffer.from(env.apiKey + ':').toString('base64'),
64
+ Accept: 'application/json',
65
+ 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
66
+ 'User-Agent': 'Chargebee-NodeJs-Client ' + env.clientVersion,
67
+ 'Lang-Version': typeof process === 'undefined' ? '' : process.version,
68
+ });
69
+ const resp = await this.envArg.httpClient.makeApiRequest({
70
+ host: (0, util_js_1.getHost)(env),
71
+ port: env.port,
72
+ path,
73
+ method: this.apiCall.httpMethod,
74
+ protocol: env.protocol,
75
+ headers: this.httpHeaders,
76
+ data: data,
77
+ timeout: env.timeout,
78
+ });
79
+ (0, coreCommon_js_1.handleResponse)(callBackWrapper, resp);
80
+ });
81
+ return (0, util_js_1.callbackifyPromise)(promise);
82
+ }
83
+ }
84
+ exports.RequestWrapper = RequestWrapper;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.waitForProcessToComplete = void 0;
4
+ const util_js_1 = require("./util.js");
5
+ const waitForProcessToComplete = (retrieveHandling, env) => {
6
+ if (typeof retrieveHandling == 'undefined' || !(0, util_js_1.isFunction)(retrieveHandling)) {
7
+ throw new Error('The handling parameter should be a method.');
8
+ }
9
+ const DummyRequestWrapper = function () {
10
+ this.request = _request;
11
+ };
12
+ const _request = function (callBack, envOptions) {
13
+ const jsonConstructor = {}.constructor;
14
+ if (typeof envOptions !== 'undefined') {
15
+ (0, util_js_1.extend)(true, env, envOptions);
16
+ }
17
+ else if (typeof callBack !== 'undefined' &&
18
+ callBack.constructor === jsonConstructor &&
19
+ !(0, util_js_1.isFunction)(callBack)) {
20
+ (0, util_js_1.extend)(true, env, callBack);
21
+ callBack = undefined;
22
+ }
23
+ if (typeof callBack !== 'undefined' && !(0, util_js_1.isFunction)(callBack)) {
24
+ throw new Error('The callback parameter passed is incorrect.');
25
+ }
26
+ const promise = new Promise((resolve, reject) => {
27
+ try {
28
+ const result = retrieveHandling();
29
+ resolve(result);
30
+ }
31
+ catch (err) {
32
+ reject(err);
33
+ }
34
+ });
35
+ return (0, util_js_1.callbackifyPromise)(promise, callBack);
36
+ };
37
+ return new DummyRequestWrapper();
38
+ };
39
+ exports.waitForProcessToComplete = waitForProcessToComplete;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const createChargebee_js_1 = require("./createChargebee.js");
4
+ const NodeClient_js_1 = require("./net/NodeClient.js");
5
+ const FetchClient_js_1 = require("./net/FetchClient.js");
6
+ //@ts-ignore
7
+ const httpClient = globalThis.fetch
8
+ ? new NodeClient_js_1.NodeHttpClient()
9
+ : new FetchClient_js_1.FetchHttpClient();
10
+ const Chargebee = (0, createChargebee_js_1.CreateChargebee)(httpClient);
11
+ module.exports = Chargebee;
12
+ module.exports.Chargebee = Chargebee;
13
+ module.exports.default = Chargebee;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const createChargebee_js_1 = require("./createChargebee.js");
4
+ const FetchClient_js_1 = require("./net/FetchClient.js");
5
+ const httpClient = new FetchClient_js_1.FetchHttpClient();
6
+ const Chargebee = (0, createChargebee_js_1.CreateChargebee)(httpClient);
7
+ module.exports = Chargebee;
8
+ module.exports.Chargebee = Chargebee;
9
+ module.exports.default = Chargebee;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChargebeeError = void 0;
4
+ class ChargebeeError extends Error {
5
+ constructor(rawError, headers) {
6
+ super(rawError.message);
7
+ this.message = rawError.message;
8
+ this.type = rawError.type;
9
+ this.api_error_code = rawError.api_error_code;
10
+ this.http_status_code = rawError.http_status_code;
11
+ this.http_code = rawError.http_code;
12
+ this.error_code = rawError.error_code;
13
+ this.detail = rawError === null || rawError === void 0 ? void 0 : rawError.detail;
14
+ this.headers = headers;
15
+ }
16
+ }
17
+ exports.ChargebeeError = ChargebeeError;
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleResponse = void 0;
4
+ exports.throwError = throwError;
5
+ const chargebeeError_js_1 = require("./chargebeeError.js");
6
+ const util_js_1 = require("./util.js");
7
+ const IDEMPOTENCY_REPLAYED_HEADER = 'chargebee-idempotency-replayed';
8
+ function throwError(callBack, rawError, headers) {
9
+ const error = new chargebeeError_js_1.ChargebeeError({
10
+ message: rawError.message,
11
+ type: rawError.type,
12
+ api_error_code: rawError.errorCode,
13
+ http_status_code: rawError.httpStatusCode,
14
+ http_code: rawError.httpStatusCode,
15
+ error_code: rawError.errorCode,
16
+ }, headers);
17
+ if (rawError['detail'] !== 'undefined') {
18
+ error['detail'] = rawError['detail'];
19
+ }
20
+ return callBack(error, null);
21
+ }
22
+ const handleResponse = async (callback, response) => {
23
+ try {
24
+ const res = await response.toJson();
25
+ const status = response.getStatusCode();
26
+ const headers = response.getHeaders();
27
+ if (status && (status < 200 || status > 299)) {
28
+ res.http_status_code = status;
29
+ res.headers = headers;
30
+ return callback(res, null);
31
+ }
32
+ else {
33
+ res.isIdempotencyReplayed = false;
34
+ if ((0, util_js_1.isNotUndefinedNEmpty)(headers[IDEMPOTENCY_REPLAYED_HEADER])) {
35
+ res.isIdempotencyReplayed = headers[IDEMPOTENCY_REPLAYED_HEADER];
36
+ }
37
+ res.headers = headers;
38
+ return callback(null, res);
39
+ }
40
+ }
41
+ catch (error) {
42
+ const status = response.getStatusCode();
43
+ const headers = response.getHeaders();
44
+ switch (status) {
45
+ case 503:
46
+ return throwError(callback, {
47
+ type: 'internal_temporary_error',
48
+ httpStatusCode: 503,
49
+ errorCode: 'internal_temporary_error',
50
+ message: 'Sorry, the server is currently unable to handle the request due to a temporary overload or scheduled maintenance. Please retry after sometime.',
51
+ detail: error,
52
+ }, headers);
53
+ case 504:
54
+ return throwError(callback, {
55
+ type: 'gateway_timeout',
56
+ httpStatusCode: 504,
57
+ errorCode: 'gateway_timeout',
58
+ message: 'The server did not receive a timely response from an upstream server, request aborted. If this problem persists, contact us at support@chargebee.com.',
59
+ detail: error,
60
+ }, headers);
61
+ default:
62
+ return throwError(callback, {
63
+ type: 'internal_error',
64
+ httpStatusCode: 500,
65
+ errorCode: 'internal_error',
66
+ message: 'Sorry, something went wrong when trying to process the request. If this problem persists, contact us at support@chargebee.com.',
67
+ detail: error,
68
+ }, headers);
69
+ }
70
+ }
71
+ };
72
+ exports.handleResponse = handleResponse;