-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.ts
46 lines (41 loc) · 1.13 KB
/
index.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
/// <reference types="@globalpayments/js" />
import version from "@globalpayments/js/src/lib/version";
/**
* Prevent multiple copies of the Global Payments JavaScript
* library from being loaded by only using a single Promise.
*/
let libraryLoaded: Promise<typeof window.GlobalPayments>;
/**
* Loads the Global Payments JavaScript library asynchronously, using a
* resolved `Promise` to signal that the library has loaded.
*
* @param url
*
* @returns The resolved library
*/
export function loadLibrary(
url?: string
): Promise<typeof window.GlobalPayments> {
if (libraryLoaded) {
return libraryLoaded;
}
if (window.GlobalPayments) {
return (libraryLoaded = Promise.resolve(window.GlobalPayments));
}
if (!url) {
url = `https://js.globalpay.com/${version}/globalpayments.js`;
}
return (libraryLoaded = new Promise((resolve) => {
try {
const script = document.createElement("script");
script.defer = true;
script.src = url || "";
script.onload = () => {
resolve(window.GlobalPayments);
};
document.body.appendChild(script);
} catch (e) {
/** */
}
}));
}