chargebee 3.3.0 → 3.4.0

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/cjs/util.js CHANGED
@@ -164,47 +164,52 @@ function encodeListParams(paramObj) {
164
164
  }
165
165
  return encodeParams(paramObj);
166
166
  }
167
- function getHost(env) {
167
+ function getHost(env, subDomain) {
168
+ if (subDomain != null) {
169
+ return env.site + '.' + subDomain + env.hostSuffix;
170
+ }
168
171
  return env.site + env.hostSuffix;
169
172
  }
170
- function encodeParams(paramObj, serialized, scope, index) {
173
+ function encodeParams(paramObj, serialized, scope, index, jsonKeys, level = 0) {
171
174
  let key, value;
172
175
  if (typeof serialized === 'undefined' || serialized === null) {
173
176
  serialized = [];
174
177
  }
175
178
  for (key in paramObj) {
176
179
  value = paramObj[key];
180
+ const originalKey = key;
177
181
  if (scope) {
178
- key = '' + scope + '[' + key + ']';
182
+ key = `${scope}[${key}]`;
179
183
  }
180
184
  if (typeof index !== 'undefined' && index !== null) {
181
- key = key + '[' + index + ']';
185
+ key = `${key}[${index}]`;
186
+ }
187
+ if (jsonKeys && jsonKeys[originalKey] === level) {
188
+ let attrVal = '';
189
+ if (value !== null) {
190
+ attrVal = encodeURIComponent(Object.prototype.toString.call(value) === '[object String]'
191
+ ? (0, exports.trim)(value)
192
+ : JSON.stringify(value));
193
+ }
194
+ serialized.push(encodeURIComponent(key) + '=' + attrVal);
182
195
  }
183
- if ((0, exports.isArray)(value)) {
196
+ else if ((0, exports.isArray)(value) &&
197
+ !(jsonKeys && jsonKeys[originalKey] === level)) {
184
198
  for (let arrIdx = 0; arrIdx < value.length; arrIdx++) {
185
199
  if (typeof value[arrIdx] === 'object' || (0, exports.isArray)(value[arrIdx])) {
186
- encodeParams(value[arrIdx], serialized, key, arrIdx);
200
+ encodeParams(value[arrIdx], serialized, key, arrIdx, jsonKeys, level + 1);
187
201
  }
188
202
  else {
189
203
  if (typeof value[arrIdx] !== 'undefined') {
190
- serialized.push(encodeURIComponent(key + '[' + arrIdx + ']') +
204
+ serialized.push(encodeURIComponent(`${key}[${arrIdx}]`) +
191
205
  '=' +
192
206
  encodeURIComponent((0, exports.trim)(value[arrIdx]) !== '' ? value[arrIdx] : ''));
193
207
  }
194
208
  }
195
209
  }
196
210
  }
197
- else if (key === 'meta_data' || key === 'metadata') {
198
- let attrVal = '';
199
- if (value !== null) {
200
- attrVal = encodeURIComponent(Object.prototype.toString.call(value) === '[object String]'
201
- ? (0, exports.trim)(value)
202
- : JSON.stringify(value));
203
- }
204
- serialized.push(encodeURIComponent(key) + '=' + attrVal);
205
- }
206
211
  else if (typeof value === 'object' && !(0, exports.isArray)(value)) {
207
- encodeParams(value, serialized, key);
212
+ encodeParams(value, serialized, key, undefined, jsonKeys, level + 1);
208
213
  }
209
214
  else {
210
215
  if (typeof value !== 'undefined') {
@@ -51,21 +51,27 @@ export class RequestWrapper {
51
51
  path += '?' + queryParam;
52
52
  params = {};
53
53
  }
54
- let data = encodeParams(params);
54
+ const jsonKeys = this.apiCall.jsonKeys;
55
+ let data = this.apiCall.isJsonRequest
56
+ ? JSON.stringify(params)
57
+ : encodeParams(params, undefined, undefined, undefined, jsonKeys);
55
58
  if (data.length) {
56
59
  extend(true, this.httpHeaders, {
57
60
  'Content-Length': data.length,
58
61
  });
59
62
  }
63
+ const contentType = this.apiCall.isJsonRequest
64
+ ? 'application/json;charset=UTF-8'
65
+ : 'application/x-www-form-urlencoded; charset=utf-8';
60
66
  extend(true, this.httpHeaders, {
61
67
  Authorization: 'Basic ' + Buffer.from(env.apiKey + ':').toString('base64'),
62
68
  Accept: 'application/json',
63
- 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
69
+ 'Content-Type': contentType,
64
70
  'User-Agent': 'Chargebee-NodeJs-Client ' + env.clientVersion,
65
71
  'Lang-Version': typeof process === 'undefined' ? '' : process.version,
66
72
  });
67
73
  const resp = await this.envArg.httpClient.makeApiRequest({
68
- host: getHost(env),
74
+ host: getHost(env, this.apiCall.subDomain),
69
75
  port: env.port,
70
76
  path,
71
77
  method: this.apiCall.httpMethod,
package/esm/coreCommon.js CHANGED
@@ -31,6 +31,7 @@ export const handleResponse = async (callback, response) => {
31
31
  res.isIdempotencyReplayed = headers[IDEMPOTENCY_REPLAYED_HEADER];
32
32
  }
33
33
  res.headers = headers;
34
+ res.httpStatusCode = status;
34
35
  return callback(null, res);
35
36
  }
36
37
  }
@@ -75,6 +75,9 @@ export const CreateChargebee = (httpClient) => {
75
75
  urlSuffix: metaArr[3],
76
76
  hasIdInUrl: metaArr[4],
77
77
  isListReq: metaArr[0] === 'list',
78
+ subDomain: metaArr[5],
79
+ isJsonRequest: metaArr[6],
80
+ jsonKeys: metaArr[7],
78
81
  };
79
82
  this[res][apiCall.methodName] = this._createApiFunc(apiCall, this._env);
80
83
  }
@@ -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: 'v3.3.0',
11
+ clientVersion: 'v3.4.0',
12
12
  port: DEFAULT_PORT,
13
13
  timemachineWaitInMillis: DEFAULT_TIME_MACHINE_WAIT,
14
14
  exportWaitInMillis: DEFAULT_EXPORT_WAIT,