@antglobal/create-sdk-loader 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/main.d.ts +15 -4
- package/lib/main.js +38 -21
- package/package.json +2 -4
package/lib/main.d.ts
CHANGED
|
@@ -1,10 +1,20 @@
|
|
|
1
|
-
type Code = 'NO_REMOTE_CLASS' | 'LOAD_ERROR' | 'INIT_ERROR';
|
|
1
|
+
type Code = 'NO_REMOTE_CLASS' | 'LOAD_ERROR' | 'INIT_ERROR' | 'LOAD_TIMEOUT';
|
|
2
2
|
declare class LoaderError extends Error {
|
|
3
3
|
code: Code;
|
|
4
4
|
constructor(code: Code, message: string);
|
|
5
5
|
}
|
|
6
|
+
interface InitType<ClassType> {
|
|
7
|
+
init: () => Promise<ClassType>;
|
|
8
|
+
}
|
|
6
9
|
interface LoaderConfig<Config, ClassType> {
|
|
10
|
+
/**
|
|
11
|
+
* @description sdk cdn url
|
|
12
|
+
*/
|
|
7
13
|
sdkURL: string;
|
|
14
|
+
/**
|
|
15
|
+
* @description the unit is ms, default 20000
|
|
16
|
+
*/
|
|
17
|
+
timeout?: number;
|
|
8
18
|
globalName: string | (() => new (config: Config) => ClassType);
|
|
9
19
|
/**
|
|
10
20
|
* @description when load error occurs, you can use this callback to monitoring report
|
|
@@ -19,12 +29,13 @@ interface LoaderConfig<Config, ClassType> {
|
|
|
19
29
|
* @description you can use this callback to monitoring report
|
|
20
30
|
* @param fromCache whether the SDK is loaded from cache
|
|
21
31
|
*/
|
|
22
|
-
onLoadComplete?: (
|
|
32
|
+
onLoadComplete?: () => void;
|
|
23
33
|
/**
|
|
24
34
|
* @description when the SDK is initialized successfully, you can use this callback to monitoring report
|
|
25
35
|
*/
|
|
26
36
|
onInitialized?: (instance: ClassType, url: string) => void;
|
|
27
37
|
}
|
|
28
|
-
export declare function
|
|
29
|
-
export declare function
|
|
38
|
+
export declare function createInstance<Config, ClassType extends InitType<ClassType>>(ClassConstructor: new (config: Config) => ClassType, config: Config): Promise<ClassType>;
|
|
39
|
+
export declare function createLoader<Config, ClassType extends InitType<ClassType>>(loadConfig: LoaderConfig<Config, ClassType>): (config: Config) => Promise<ClassType>;
|
|
40
|
+
export declare function loadScript<ClassType extends InitType<ClassType>, Params = any, SDKConstructor = new (config: Params) => ClassType>(url: string, globalName: string | (() => SDKConstructor)): Promise<SDKConstructor>;
|
|
30
41
|
export {};
|
package/lib/main.js
CHANGED
|
@@ -6,32 +6,23 @@ class LoaderError extends Error {
|
|
|
6
6
|
this.code = code;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
export function createInstance(ClassConstructor, config) {
|
|
10
|
+
const instance = new ClassConstructor(config);
|
|
11
|
+
return instance.init();
|
|
12
|
+
}
|
|
9
13
|
export function createLoader(loadConfig) {
|
|
10
|
-
const { sdkURL, globalName, onLoadComplete, onError, onLoadStart, onInitialized } = loadConfig;
|
|
11
|
-
const getRemoteClass = () => {
|
|
12
|
-
if (typeof globalName === 'function') {
|
|
13
|
-
return globalName();
|
|
14
|
-
}
|
|
15
|
-
else {
|
|
16
|
-
return window[globalName];
|
|
17
|
-
}
|
|
18
|
-
};
|
|
14
|
+
const { sdkURL, globalName, timeout = 20000, onLoadComplete, onError, onLoadStart, onInitialized } = loadConfig;
|
|
19
15
|
return (config) => {
|
|
20
16
|
onLoadStart?.();
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return loadScript(sdkURL)
|
|
27
|
-
.then(() => {
|
|
28
|
-
onLoadComplete?.(false);
|
|
29
|
-
const RemoteClass = getRemoteClass();
|
|
17
|
+
const _loadScript = createTimeout(() => loadScript(sdkURL, globalName), timeout);
|
|
18
|
+
return _loadScript()
|
|
19
|
+
.then(async (RemoteClass) => {
|
|
20
|
+
onLoadComplete?.();
|
|
30
21
|
if (!RemoteClass) {
|
|
31
22
|
throw new LoaderError('NO_REMOTE_CLASS', `No variables ${globalName} were found. Cannot be initialized the Class ${globalName}`);
|
|
32
23
|
}
|
|
33
24
|
try {
|
|
34
|
-
const instance =
|
|
25
|
+
const instance = await createInstance(RemoteClass, config);
|
|
35
26
|
onInitialized?.(instance, window.location.href);
|
|
36
27
|
return instance;
|
|
37
28
|
}
|
|
@@ -45,14 +36,28 @@ export function createLoader(loadConfig) {
|
|
|
45
36
|
});
|
|
46
37
|
};
|
|
47
38
|
}
|
|
48
|
-
export function loadScript(url) {
|
|
39
|
+
export function loadScript(url, globalName) {
|
|
49
40
|
return new Promise((resolve, reject) => {
|
|
41
|
+
const getRemoteClass = () => {
|
|
42
|
+
if (typeof globalName === 'function') {
|
|
43
|
+
return globalName();
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
const className = window[globalName];
|
|
47
|
+
return className?.default || className;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const RemoteClass = getRemoteClass();
|
|
51
|
+
if (RemoteClass) {
|
|
52
|
+
return resolve(RemoteClass);
|
|
53
|
+
}
|
|
50
54
|
const existedScript = document.querySelector(`script[src="${url}"]`);
|
|
51
55
|
const script = existedScript || document.createElement('script');
|
|
52
56
|
script.src = url;
|
|
53
57
|
script.async = true;
|
|
54
58
|
script.addEventListener('load', () => {
|
|
55
|
-
|
|
59
|
+
// There is must use getRemoteClass
|
|
60
|
+
resolve(getRemoteClass());
|
|
56
61
|
});
|
|
57
62
|
script.addEventListener('error', (e) => {
|
|
58
63
|
reject(new LoaderError('LOAD_ERROR', `Load script ${url} failed: ${e.message}`));
|
|
@@ -62,3 +67,15 @@ export function loadScript(url) {
|
|
|
62
67
|
}
|
|
63
68
|
});
|
|
64
69
|
}
|
|
70
|
+
function createTimeout(fn, t) {
|
|
71
|
+
return (..._args) => {
|
|
72
|
+
return Promise.race([
|
|
73
|
+
new Promise((_r, reject) => {
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
reject(new LoaderError('LOAD_TIMEOUT', `Load timeout: ${t}`));
|
|
76
|
+
}, t);
|
|
77
|
+
}),
|
|
78
|
+
fn(..._args)
|
|
79
|
+
]);
|
|
80
|
+
};
|
|
81
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@antglobal/create-sdk-loader",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"main": "./lib/main.js",
|
|
5
5
|
"module": "./lib/main.js",
|
|
6
6
|
"types": "./lib/main.d.ts",
|
|
@@ -10,9 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
12
|
"dev": "vite",
|
|
13
|
-
"build": "tsc"
|
|
14
|
-
"preview": "vite preview",
|
|
15
|
-
"postpublish": "node scripts/restorePkg.mjs"
|
|
13
|
+
"build": "tsc"
|
|
16
14
|
},
|
|
17
15
|
"devDependencies": {
|
|
18
16
|
"typescript": "~5.9.3",
|