forked from chargebee/chargebee-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateChargebee.ts
More file actions
100 lines (99 loc) · 3.29 KB
/
createChargebee.ts
File metadata and controls
100 lines (99 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { RequestWrapper } from './RequestWrapper.js';
import { Environment } from './environment.js';
import { Endpoints } from './resources/api_endpoints.js';
import { extend, sleep } from './util.js';
import { waitForProcessToComplete } from './asyncApiSupport.js';
import {
ResourceType,
EnvType,
ChargebeeType,
Config,
EndpointTuple,
HttpClientInterface,
} from './types.js';
export const CreateChargebee = (httpClient: HttpClientInterface) => {
const Chargebee = function (this: ChargebeeType, conf: Config) {
this._env = { ...Environment };
extend(true, this._env, conf);
// @ts-ignore
this._env.httpClient = httpClient;
this._buildResources();
this._endpoints = Endpoints;
} as any as { new (): ChargebeeType };
Chargebee.prototype = {
_createApiFunc(apiCall: ResourceType, env: EnvType) {
return async function () {
const rw = new RequestWrapper(arguments, apiCall, env);
return rw.getRequest();
};
},
_waitForExport(exportId: string) {
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 sleep(this._env.exportWaitInMillis);
continue;
} else {
return result;
}
}
throw new Error('Export is taking too long');
};
return 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 sleep(this._env.timemachineWaitInMillis);
continue;
} else {
return result;
}
}
throw new Error('The time travel is taking too much time');
};
return waitForProcessToComplete(retrieve);
},
_buildResources() {
for (const res in Endpoints) {
this[res] = {};
// @ts-ignore
const apiCalls = Endpoints[res];
for (let apiIdx = 0; apiIdx < apiCalls.length; apiIdx++) {
const metaArr: EndpointTuple = apiCalls[apiIdx];
const apiCall: ResourceType = {
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],
};
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;
};