@aloma.io/integration-sdk 3.8.6 → 3.8.7
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/build/cli.mjs
CHANGED
File without changes
|
@@ -3,7 +3,7 @@ export declare abstract class AbstractController {
|
|
3
3
|
protected config: {
|
4
4
|
[key: string]: any;
|
5
5
|
};
|
6
|
-
protected client
|
6
|
+
protected client: any;
|
7
7
|
protected start(): Promise<void>;
|
8
8
|
protected stop(isShutdown?: boolean): Promise<void>;
|
9
9
|
protected configQuery(arg: any): Promise<any>;
|
@@ -15,7 +15,7 @@ export declare abstract class AbstractController {
|
|
15
15
|
baseUrl?: string;
|
16
16
|
onResponse?: (response: any) => void;
|
17
17
|
customize?: (request: any) => void;
|
18
|
-
}): Promise<
|
18
|
+
}): Promise<Fetcher>;
|
19
19
|
protected updateTask(name: string, data: any): Promise<string>;
|
20
20
|
protected createBlob({ content, name, size, mimetype, meta, taskId, }: {
|
21
21
|
content: string;
|
@@ -1,15 +1,44 @@
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
2
|
+
/**
|
3
|
+
* http request fetcher
|
4
|
+
*/
|
1
5
|
export default class Fetcher {
|
2
|
-
retry: number;
|
3
|
-
baseUrl: any;
|
4
|
-
onResponse: any;
|
5
|
-
customize0: any;
|
6
|
-
constructor({ retry, baseUrl, onResponse, customize }
|
7
|
-
retry?: number
|
8
|
-
baseUrl
|
9
|
-
onResponse:
|
10
|
-
customize:
|
6
|
+
protected retry: number;
|
7
|
+
protected baseUrl: any;
|
8
|
+
protected onResponse: any;
|
9
|
+
protected customize0: any;
|
10
|
+
constructor({ retry, baseUrl, onResponse, customize }?: {
|
11
|
+
retry?: number;
|
12
|
+
baseUrl?: string;
|
13
|
+
onResponse?: (response: Response) => void;
|
14
|
+
customize?: (request: {
|
15
|
+
[key: string]: any;
|
16
|
+
}) => void;
|
11
17
|
});
|
12
|
-
customize(options?: {}, args?: {}): Promise<void>;
|
13
|
-
onError(e: any, url: string, options: any, retries: number, args: any, rateLimit?: any): Promise<unknown>;
|
14
|
-
|
18
|
+
protected customize(options?: {}, args?: {}): Promise<void>;
|
19
|
+
protected onError(e: any, url: string, options: any, retries: number, args: any, rateLimit?: any): Promise<unknown>;
|
20
|
+
/**
|
21
|
+
* fetch data from a url
|
22
|
+
* @param url url to fetch
|
23
|
+
* @param options request options
|
24
|
+
* @param retries retries left for the request
|
25
|
+
* @param args optional args for customize()
|
26
|
+
* @returns
|
27
|
+
*/
|
28
|
+
fetch(url: string, options?: {
|
29
|
+
/**
|
30
|
+
* request method like GET, POST, PUT, DELETE
|
31
|
+
*/
|
32
|
+
method?: string;
|
33
|
+
/**
|
34
|
+
* request headers like Accept, Content-type
|
35
|
+
*/
|
36
|
+
headers?: {
|
37
|
+
[key: string]: any;
|
38
|
+
};
|
39
|
+
/**
|
40
|
+
* request body like "hello world" or {hello: "world"}
|
41
|
+
*/
|
42
|
+
body?: any;
|
43
|
+
}, retries?: number, args?: any): Promise<any>;
|
15
44
|
}
|
@@ -1,10 +1,13 @@
|
|
1
1
|
import { unwrap } from "../util/index.mjs";
|
2
|
+
/**
|
3
|
+
* http request fetcher
|
4
|
+
*/
|
2
5
|
export default class Fetcher {
|
3
6
|
retry;
|
4
7
|
baseUrl;
|
5
8
|
onResponse;
|
6
9
|
customize0;
|
7
|
-
constructor({ retry = 5, baseUrl, onResponse, customize }) {
|
10
|
+
constructor({ retry = 5, baseUrl, onResponse, customize } = {}) {
|
8
11
|
this.retry = retry;
|
9
12
|
this.baseUrl = baseUrl;
|
10
13
|
this.onResponse = onResponse;
|
@@ -28,42 +31,51 @@ export default class Fetcher {
|
|
28
31
|
}, rateLimit ? 10000 : 500);
|
29
32
|
});
|
30
33
|
}
|
34
|
+
/**
|
35
|
+
* fetch data from a url
|
36
|
+
* @param url url to fetch
|
37
|
+
* @param options request options
|
38
|
+
* @param retries retries left for the request
|
39
|
+
* @param args optional args for customize()
|
40
|
+
* @returns
|
41
|
+
*/
|
31
42
|
async fetch(url, options = {}, retries, args = {}) {
|
32
43
|
var local = this, baseUrl = local.baseUrl;
|
44
|
+
const options0 = { ...options };
|
33
45
|
if (retries == null)
|
34
46
|
retries = local.retry;
|
35
47
|
let theURL = !baseUrl
|
36
48
|
? url
|
37
49
|
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(/\/\/+/gi, "/");
|
38
50
|
try {
|
39
|
-
|
40
|
-
await local.customize(
|
41
|
-
url =
|
42
|
-
delete
|
51
|
+
options0.url = url;
|
52
|
+
await local.customize(options0, args);
|
53
|
+
url = options0.url;
|
54
|
+
delete options0.url;
|
43
55
|
theURL = !baseUrl
|
44
56
|
? url
|
45
57
|
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(/\/\/+/gi, "/");
|
46
|
-
if (!
|
47
|
-
|
48
|
-
...
|
58
|
+
if (!options0?.headers || !options0?.headers?.Accept) {
|
59
|
+
options0.headers = {
|
60
|
+
...options0.headers,
|
49
61
|
Accept: "application/json",
|
50
62
|
};
|
51
63
|
}
|
52
|
-
if (!
|
53
|
-
|
54
|
-
...
|
64
|
+
if (!options0?.headers || !options0?.headers?.["Content-type"]) {
|
65
|
+
options0.headers = {
|
66
|
+
...options0.headers,
|
55
67
|
"Content-type": "application/json",
|
56
68
|
};
|
57
69
|
}
|
58
|
-
if (!(
|
59
|
-
|
60
|
-
!(typeof
|
61
|
-
|
62
|
-
|
70
|
+
if (!(options0?.method === "GET" || options0?.method === "HEAD") &&
|
71
|
+
options0?.body &&
|
72
|
+
!(typeof options0.body === "string") &&
|
73
|
+
options0?.headers?.["Content-type"] === "application/json") {
|
74
|
+
options0.body = JSON.stringify(options0.body);
|
63
75
|
}
|
64
|
-
const timeout = Math.min(
|
76
|
+
const timeout = Math.min(options0?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
65
77
|
const ret = await fetch(theURL, {
|
66
|
-
...
|
78
|
+
...options0,
|
67
79
|
signal: AbortSignal.timeout(timeout),
|
68
80
|
});
|
69
81
|
const status = await ret.status;
|
@@ -79,12 +91,12 @@ export default class Fetcher {
|
|
79
91
|
if (status === 204) {
|
80
92
|
return { ok: true };
|
81
93
|
}
|
82
|
-
return unwrap(ret,
|
94
|
+
return unwrap(ret, options0);
|
83
95
|
}
|
84
96
|
catch (e) {
|
85
97
|
// too many requests
|
86
98
|
if (e.status === 429) {
|
87
|
-
return local.onError(e, url,
|
99
|
+
return local.onError(e, url, options0, retries, args, true);
|
88
100
|
}
|
89
101
|
// bad request
|
90
102
|
if (e.status === 400 || e.status === 422) {
|
@@ -94,7 +106,7 @@ export default class Fetcher {
|
|
94
106
|
console.log(theURL, e);
|
95
107
|
if (retries <= 0)
|
96
108
|
throw e;
|
97
|
-
return local.onError(e, url,
|
109
|
+
return local.onError(e, url, options0, retries, args);
|
98
110
|
}
|
99
111
|
}
|
100
112
|
}
|
package/package.json
CHANGED
package/src/controller/index.mts
CHANGED
@@ -2,7 +2,7 @@ import Fetcher from "../internal/fetcher/fetcher.mjs";
|
|
2
2
|
|
3
3
|
export abstract class AbstractController {
|
4
4
|
protected config: {[key: string]: any} = {};
|
5
|
-
protected client
|
5
|
+
protected client: any;
|
6
6
|
|
7
7
|
protected async start(): Promise<void> {}
|
8
8
|
|
@@ -36,7 +36,7 @@ export abstract class AbstractController {
|
|
36
36
|
baseUrl?: string;
|
37
37
|
onResponse?: (response: any) => void;
|
38
38
|
customize?: (request: any) => void;
|
39
|
-
}): Promise<
|
39
|
+
}): Promise<Fetcher> {
|
40
40
|
throw new Error("not implemented");
|
41
41
|
}
|
42
42
|
|
@@ -1,22 +1,25 @@
|
|
1
1
|
import { unwrap } from "../util/index.mjs";
|
2
2
|
|
3
|
+
/**
|
4
|
+
* http request fetcher
|
5
|
+
*/
|
3
6
|
export default class Fetcher {
|
4
|
-
retry: number;
|
5
|
-
baseUrl: any;
|
6
|
-
onResponse: any;
|
7
|
-
customize0: any;
|
8
|
-
constructor({ retry = 5, baseUrl, onResponse, customize }) {
|
7
|
+
protected retry: number;
|
8
|
+
protected baseUrl: any;
|
9
|
+
protected onResponse: any;
|
10
|
+
protected customize0: any;
|
11
|
+
constructor({ retry = 5, baseUrl, onResponse, customize }: {retry?: number; baseUrl?: string; onResponse?: (response: Response) => void; customize?: (request: {[key: string]: any}) => void} = {}) {
|
9
12
|
this.retry = retry;
|
10
13
|
this.baseUrl = baseUrl;
|
11
14
|
this.onResponse = onResponse;
|
12
15
|
if (customize) this.customize0 = customize;
|
13
16
|
}
|
14
17
|
|
15
|
-
async customize(options = {}, args = {}) {
|
18
|
+
protected async customize(options = {}, args = {}) {
|
16
19
|
if (this.customize0) await this.customize0(options, args);
|
17
20
|
}
|
18
21
|
|
19
|
-
async onError(
|
22
|
+
protected async onError(
|
20
23
|
e: any,
|
21
24
|
url: string,
|
22
25
|
options: any,
|
@@ -40,10 +43,33 @@ export default class Fetcher {
|
|
40
43
|
});
|
41
44
|
}
|
42
45
|
|
43
|
-
|
46
|
+
/**
|
47
|
+
* fetch data from a url
|
48
|
+
* @param url url to fetch
|
49
|
+
* @param options request options
|
50
|
+
* @param retries retries left for the request
|
51
|
+
* @param args optional args for customize()
|
52
|
+
* @returns
|
53
|
+
*/
|
54
|
+
async fetch(url: string, options: {
|
55
|
+
/**
|
56
|
+
* request method like GET, POST, PUT, DELETE
|
57
|
+
*/
|
58
|
+
method?: string;
|
59
|
+
/**
|
60
|
+
* request headers like Accept, Content-type
|
61
|
+
*/
|
62
|
+
headers?: {[key: string]: any};
|
63
|
+
/**
|
64
|
+
* request body like "hello world" or {hello: "world"}
|
65
|
+
*/
|
66
|
+
body?: any;
|
67
|
+
} = {}, retries?: number, args: any = {}) {
|
44
68
|
var local = this,
|
45
69
|
baseUrl = local.baseUrl;
|
46
70
|
|
71
|
+
const options0: any = {...options};
|
72
|
+
|
47
73
|
if (retries == null) retries = local.retry;
|
48
74
|
|
49
75
|
let theURL = !baseUrl
|
@@ -54,11 +80,11 @@ export default class Fetcher {
|
|
54
80
|
);
|
55
81
|
|
56
82
|
try {
|
57
|
-
|
58
|
-
await local.customize(
|
83
|
+
options0.url = url;
|
84
|
+
await local.customize(options0, args);
|
59
85
|
|
60
|
-
url =
|
61
|
-
delete
|
86
|
+
url = options0.url;
|
87
|
+
delete options0.url;
|
62
88
|
|
63
89
|
theURL = !baseUrl
|
64
90
|
? url
|
@@ -67,35 +93,35 @@ export default class Fetcher {
|
|
67
93
|
"/",
|
68
94
|
);
|
69
95
|
|
70
|
-
if (!
|
71
|
-
|
72
|
-
...
|
96
|
+
if (!options0?.headers || !options0?.headers?.Accept) {
|
97
|
+
options0.headers = {
|
98
|
+
...options0.headers,
|
73
99
|
Accept: "application/json",
|
74
100
|
};
|
75
101
|
}
|
76
102
|
|
77
|
-
if (!
|
78
|
-
|
79
|
-
...
|
103
|
+
if (!options0?.headers || !options0?.headers?.["Content-type"]) {
|
104
|
+
options0.headers = {
|
105
|
+
...options0.headers,
|
80
106
|
"Content-type": "application/json",
|
81
107
|
};
|
82
108
|
}
|
83
109
|
|
84
110
|
if (
|
85
|
-
!(
|
86
|
-
|
87
|
-
!(typeof
|
88
|
-
|
111
|
+
!(options0?.method === "GET" || options0?.method === "HEAD") &&
|
112
|
+
options0?.body &&
|
113
|
+
!(typeof options0.body === "string") &&
|
114
|
+
options0?.headers?.["Content-type"] === "application/json"
|
89
115
|
) {
|
90
|
-
|
116
|
+
options0.body = JSON.stringify(options0.body);
|
91
117
|
}
|
92
118
|
|
93
119
|
const timeout = Math.min(
|
94
|
-
|
120
|
+
options0?.timeout || 30 * 60 * 1000,
|
95
121
|
30 * 60 * 1000,
|
96
122
|
);
|
97
123
|
const ret = await fetch(theURL, {
|
98
|
-
...
|
124
|
+
...options0,
|
99
125
|
signal: AbortSignal.timeout(timeout),
|
100
126
|
});
|
101
127
|
const status = await ret.status;
|
@@ -116,11 +142,11 @@ export default class Fetcher {
|
|
116
142
|
return { ok: true };
|
117
143
|
}
|
118
144
|
|
119
|
-
return unwrap(ret,
|
145
|
+
return unwrap(ret, options0);
|
120
146
|
} catch (e: any) {
|
121
147
|
// too many requests
|
122
148
|
if (e.status === 429) {
|
123
|
-
return local.onError(e, url,
|
149
|
+
return local.onError(e, url, options0, retries, args, true);
|
124
150
|
}
|
125
151
|
|
126
152
|
// bad request
|
@@ -134,7 +160,7 @@ export default class Fetcher {
|
|
134
160
|
|
135
161
|
if (retries <= 0) throw e;
|
136
162
|
|
137
|
-
return local.onError(e, url,
|
163
|
+
return local.onError(e, url, options0, retries, args);
|
138
164
|
}
|
139
165
|
}
|
140
166
|
}
|