-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasisTheory.ts
311 lines (260 loc) · 8.15 KB
/
BasisTheory.ts
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import type {
BasisTheoryElements,
BasisTheoryElementsInternal,
CardElement,
CardNumberElement,
CardExpirationDateElement,
CardVerificationCodeElement,
CreateCardElementOptions,
CreateCardNumberElementOptions,
CreateCardExpirationDateElementOptions,
CreateCardVerificationCodeElementOptions,
CreateTextElementOptions,
CustomizableElementOptions,
ElementType,
TextElement,
Tokenize as ElementsTokenize,
TokenizeData as ElementsTokenizeData,
Tokens as ElementsTokens,
} from '@/types/elements';
import type { TokenizeData } from '@/types/models';
import type {
Applications,
BasisTheory as IBasisTheory,
BasisTheoryInit,
Logs,
Permissions,
ReactorFormulas,
Reactors,
RequestOptions,
Tenants,
Tokenize,
Tokens,
BasisTheoryInitOptions,
BasisTheoryInitOptionsWithElements,
BasisTheoryInitOptionsWithoutElements,
BasisTheoryInitStatus,
Proxies,
} from '@/types/sdk';
import { BasisTheoryApplications } from './applications';
import {
assertInit,
CLIENT_BASE_PATHS,
DEFAULT_BASE_URL,
DEFAULT_ELEMENTS_BASE_URL,
} from './common';
import { delegateTokenize, delegateTokens, loadElements } from './elements';
import { ELEMENTS_INIT_ERROR_MESSAGE } from './elements/constants';
import { BasisTheoryLogs } from './logs';
import { BasisTheoryPermissions } from './permissions';
import { BasisTheoryProxies } from './proxies';
import { BasisTheoryReactorFormulas } from './reactor-formulas';
import { BasisTheoryReactors } from './reactors';
import { BasisTheoryTenants } from './tenants';
const defaultInitOptions: Required<BasisTheoryInitOptionsWithoutElements> = {
apiBaseUrl: DEFAULT_BASE_URL,
elements: false,
appInfo: {},
};
export class BasisTheory
implements BasisTheoryInit, IBasisTheory, BasisTheoryElements {
private _initStatus: BasisTheoryInitStatus = 'not-started';
private _initOptions?: Required<BasisTheoryInitOptions>;
private _tokens?: Tokens & ElementsTokens;
private _tokenize?: Tokenize & ElementsTokenize;
private _elements?: BasisTheoryElementsInternal;
private _applications?: BasisTheoryApplications;
private _tenants?: BasisTheoryTenants;
private _logs?: BasisTheoryLogs;
private _reactorFormulas?: BasisTheoryReactorFormulas;
private _reactors?: BasisTheoryReactors;
private _permissions?: BasisTheoryPermissions;
private _proxies?: Proxies;
public init(
apiKey: string | undefined,
options?: BasisTheoryInitOptionsWithoutElements
): Promise<IBasisTheory>;
public init(
apiKey: string | undefined,
options: BasisTheoryInitOptionsWithElements
): Promise<IBasisTheory & BasisTheoryElements>;
public async init(
apiKey: string,
options: BasisTheoryInitOptions = {}
): Promise<IBasisTheory & BasisTheoryElements> {
if (this._initStatus !== 'not-started' && this._initStatus !== 'error') {
throw new Error(
'This BasisTheory instance has been already initialized.'
);
}
this._initStatus = 'in-progress';
try {
this._initOptions = Object.freeze({
...defaultInitOptions,
...options,
});
let baseUrl = this._initOptions.apiBaseUrl;
try {
const baseUrlObject = new URL(this.initOptions.apiBaseUrl);
if (baseUrlObject.hostname === 'localhost') {
baseUrlObject.protocol = 'http';
} else {
baseUrlObject.protocol = 'https';
}
baseUrl = baseUrlObject.toString().replace(/\/$/u, '');
} catch {
throw new Error('Invalid format for the given API base url.');
}
const appInfo = this._initOptions.appInfo;
if ((this._initOptions as BasisTheoryInitOptionsWithElements).elements) {
await this.loadElements(apiKey);
}
this._tokens = new (delegateTokens(this._elements))({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.tokens, baseUrl).toString(),
appInfo,
});
this._tokenize = new (delegateTokenize(this._elements))({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.tokenize, baseUrl).toString(),
appInfo,
});
this._applications = new BasisTheoryApplications({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.applications, baseUrl).toString(),
appInfo,
});
this._tenants = new BasisTheoryTenants({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.tenants, baseUrl).toString(),
appInfo,
});
this._logs = new BasisTheoryLogs({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.logs, baseUrl).toString(),
appInfo,
});
this._reactorFormulas = new BasisTheoryReactorFormulas({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.reactorFormulas, baseUrl).toString(),
appInfo,
});
this._reactors = new BasisTheoryReactors({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.reactors, baseUrl).toString(),
appInfo,
});
this._permissions = new BasisTheoryPermissions({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.permissions, baseUrl).toString(),
appInfo,
});
this._proxies = new BasisTheoryProxies({
apiKey,
baseURL: new URL(CLIENT_BASE_PATHS.proxies, baseUrl).toString(),
appInfo,
});
this._initStatus = 'done';
} catch (error) {
this._initStatus = 'error';
throw error;
}
return this;
}
public createElement(
type: 'card',
options?: CreateCardElementOptions
): CardElement;
public createElement(
type: 'text',
options: CreateTextElementOptions
): TextElement;
public createElement(
type: 'cardNumber',
options: CreateCardNumberElementOptions
): CardNumberElement;
public createElement(
type: 'cardExpirationDate',
options: CreateCardExpirationDateElementOptions
): CardExpirationDateElement;
public createElement(
type: 'cardVerificationCode',
options: CreateCardVerificationCodeElementOptions
): CardVerificationCodeElement;
public createElement(
type: ElementType,
options: CustomizableElementOptions
):
| CardElement
| TextElement
| CardNumberElement
| CardExpirationDateElement
| CardVerificationCodeElement {
if (!this._elements) {
throw new Error(ELEMENTS_INIT_ERROR_MESSAGE);
}
return this._elements.createElement(type as never, options);
}
public tokenize(
tokens: TokenizeData & ElementsTokenizeData,
options?: RequestOptions
): Promise<TokenizeData> {
return assertInit(this._tokenize).tokenize(tokens, options);
}
private async loadElements(apiKey: string): Promise<void> {
let elementsBaseUrl: URL;
try {
elementsBaseUrl = new URL(
(this.initOptions as BasisTheoryInitOptionsWithElements)
.elementsBaseUrl || DEFAULT_ELEMENTS_BASE_URL
);
} catch {
throw new Error('Invalid format for the given Elements base url.');
}
const elements = await loadElements();
await (elements as BasisTheoryElementsInternal).init(
apiKey,
elementsBaseUrl.toString().replace(/\/$/u, '')
);
this.elements = elements;
}
// these should be set by the init call only.
/* eslint-disable accessor-pairs */
public get initOptions(): Required<BasisTheoryInitOptions> {
return assertInit(this._initOptions);
}
public get tokens(): Tokens & ElementsTokens {
return assertInit(this._tokens);
}
public get applications(): Applications {
return assertInit(this._applications);
}
public get tenants(): Tenants {
return assertInit(this._tenants);
}
public get logs(): Logs {
return assertInit(this._logs);
}
public get reactorFormulas(): ReactorFormulas {
return assertInit(this._reactorFormulas);
}
public get reactors(): Reactors {
return assertInit(this._reactors);
}
public get permissions(): Permissions {
return assertInit(this._permissions);
}
public get proxies(): Proxies {
return assertInit(this._proxies);
}
/* eslint-enable accessor-pairs */
/**
* @deprecated
*/
public get elements(): BasisTheoryElements {
return assertInit(this._elements);
}
public set elements(elements: BasisTheoryElements) {
this._elements = elements as BasisTheoryElementsInternal;
}
}