forked from paypal/paypal-sdk-release
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (34 loc) · 1.22 KB
/
index.js
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
/* @flow */
type Component<T> = {
name : string,
requirer : () => T,
setupHandler : ?string
};
export default function setupSDK(namespace : string, version : string, components : $ReadOnlyArray<Component<mixed>>) {
if (window[namespace]) {
let { version: existingVersion } = window[namespace];
throw new Error(`SDK already loaded with ${ (existingVersion === version) ? 'same' : 'different' } version: ${ existingVersion } (this version: ${ version })`);
}
window[namespace] = window[namespace] || {};
window[namespace].version = version;
for (const { name, requirer, setupHandler } of components) {
let componentExports = {};
try {
// $FlowFixMe
let { [setupHandler]: setupComponent, ...rest } = requirer();
if (setupComponent) {
setupComponent();
}
componentExports = rest;
} catch (err) {
setTimeout(() => {
throw new Error(`Bootstrap Error for ${ name }:\n\n${ err.message }\n\n${ err.stack }`);
}, 1);
continue;
}
window[namespace] = {
...window[namespace],
...componentExports
};
}
}