chargebee
Version:
A library for integrating with Chargebee.
131 lines (130 loc) • 5.91 kB
JavaScript
;
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CreateChargebee = void 0;
const RequestWrapper_js_1 = require("./RequestWrapper.js");
const environment_js_1 = require("./environment.js");
const api_endpoints_js_1 = require("./resources/api_endpoints.js");
const util_js_1 = require("./util.js");
const asyncApiSupport_js_1 = require("./asyncApiSupport.js");
const handler_js_1 = require("./resources/webhook/handler.js");
const CreateChargebee = (httpClient) => {
const Chargebee = function (conf) {
this._env = Object.assign({}, environment_js_1.Environment);
const { telemetryAdapter, httpClient: configHttpClient } = conf, confToMerge = __rest(conf, ["telemetryAdapter", "httpClient"]);
(0, util_js_1.extend)(true, this._env, confToMerge);
// @ts-ignore
this._env.httpClient =
configHttpClient != null ? configHttpClient : httpClient;
if (telemetryAdapter !== undefined) {
this._env.telemetryAdapter = telemetryAdapter;
}
this._buildResources();
this._endpoints = api_endpoints_js_1.Endpoints;
// Initialize webhooks handler with auto-configured Basic Auth (if env vars are set)
const handler = (0, handler_js_1.createDefaultHandler)();
this.__clientIdentifier = (serviceName) => {
(0, util_js_1.extend)(true, this._env, { userAgentSuffix: serviceName });
};
// Create webhooks namespace with handler methods + createHandler factory
this.webhooks = Object.assign(handler, {
createHandler(options) {
return new handler_js_1.WebhookHandler(options);
},
});
};
Chargebee.prototype = {
_createApiFunc(apiCall, env) {
return async function () {
const rw = new RequestWrapper_js_1.RequestWrapper(arguments, apiCall, env);
return rw.getRequest();
};
},
_waitForExport(exportId) {
let count = 0;
const retrieve = async () => {
while (count++ < 30) {
const result = await this.export.retrieve(exportId).request();
const exportObj = result.export;
if (exportObj.status === 'completed') {
return result;
}
else if (exportObj.status === 'in_process') {
await (0, util_js_1.sleep)(this._env.exportWaitInMillis);
continue;
}
else {
return result;
}
}
throw new Error('Export is taking too long');
};
return (0, asyncApiSupport_js_1.waitForProcessToComplete)(retrieve);
},
_timeMachineWait() {
let count = 0;
const retrieve = async () => {
while (count++ < 30) {
const result = await this.time_machine.retrieve('delorean').request();
const time_machine = result.time_machine;
if (time_machine.time_travel_status === 'succeeded') {
return result;
}
else if (time_machine.time_travel_status === 'in_progress') {
await (0, util_js_1.sleep)(this._env.timemachineWaitInMillis);
continue;
}
else {
return result;
}
}
throw new Error('The time travel is taking too much time');
};
return (0, asyncApiSupport_js_1.waitForProcessToComplete)(retrieve);
},
_buildResources() {
for (const res in api_endpoints_js_1.Endpoints) {
this[res] = {};
// @ts-ignore
const apiCalls = api_endpoints_js_1.Endpoints[res];
for (let apiIdx = 0; apiIdx < apiCalls.length; apiIdx++) {
const metaArr = apiCalls[apiIdx];
const apiCall = {
resource: res,
methodName: metaArr[0],
httpMethod: metaArr[1],
urlPrefix: metaArr[2],
urlSuffix: metaArr[3],
hasIdInUrl: metaArr[4],
isListReq: metaArr[0] === 'list',
subDomain: metaArr[5],
isJsonRequest: metaArr[6],
jsonKeys: metaArr[7],
options: metaArr[8],
};
if (this._env.enableValidation) {
// Store resource and action for lazy schema loading in RequestWrapper
apiCall.resourceKey = res;
apiCall.actionName = metaArr[0];
}
this[res][apiCall.methodName] = this._createApiFunc(apiCall, this._env);
}
}
this.export.wait_for_export_completion = this._waitForExport.bind(this);
this.timeMachine.wait_for_time_travel_completion =
this._timeMachineWait.bind(this);
},
};
return Chargebee;
};
exports.CreateChargebee = CreateChargebee;