@aloma.io/integration-sdk 3.8.11 → 3.8.12
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/.prettierrc.cjs +8 -0
- package/build/builder/index.d.mts +7 -11
- package/build/builder/index.mjs +14 -15
- package/build/builder/runtime-context.d.mts +1 -1
- package/build/builder/runtime-context.mjs +11 -16
- package/build/cli.mjs +38 -38
- package/build/controller/index.d.mts +58 -2
- package/build/controller/index.mjs +68 -11
- package/build/index.d.mts +2 -2
- package/build/index.mjs +2 -2
- package/build/internal/connector/config.d.mts +2 -2
- package/build/internal/connector/config.mjs +10 -10
- package/build/internal/connector/index.d.mts +2 -2
- package/build/internal/connector/index.mjs +4 -4
- package/build/internal/connector/metrics.mjs +6 -6
- package/build/internal/connector/server/index.d.mts +3 -3
- package/build/internal/connector/server/index.mjs +7 -7
- package/build/internal/connector/server/on-connect/decrypt-config.mjs +3 -3
- package/build/internal/connector/server/on-connect/finish-oauth.d.mts +3 -3
- package/build/internal/connector/server/on-connect/finish-oauth.mjs +17 -23
- package/build/internal/connector/server/on-connect/index.d.mts +2 -2
- package/build/internal/connector/server/on-connect/index.mjs +13 -13
- package/build/internal/connector/server/on-connect/make-oauth.d.mts +3 -3
- package/build/internal/connector/server/on-connect/make-oauth.mjs +17 -25
- package/build/internal/connector/server/on-connect/start-oauth.mjs +6 -13
- package/build/internal/connector/server/on-message.mjs +1 -1
- package/build/internal/dispatcher/index.mjs +34 -45
- package/build/internal/fetcher/fetcher.d.mts +1 -1
- package/build/internal/fetcher/fetcher.mjs +11 -15
- package/build/internal/fetcher/oauth-fetcher.d.mts +1 -1
- package/build/internal/fetcher/oauth-fetcher.mjs +7 -7
- package/build/internal/index.d.mts +1 -1
- package/build/internal/index.mjs +1 -1
- package/build/internal/util/index.mjs +5 -5
- package/build/internal/util/jwe/cli.mjs +3 -3
- package/build/internal/util/jwe/index.d.mts +1 -1
- package/build/internal/util/jwe/index.mjs +10 -10
- package/build/internal/websocket/config.d.mts +1 -1
- package/build/internal/websocket/config.mjs +7 -7
- package/build/internal/websocket/connection/constants.mjs +3 -3
- package/build/internal/websocket/connection/index.mjs +8 -8
- package/build/internal/websocket/connection/registration.mjs +6 -6
- package/build/internal/websocket/index.d.mts +2 -2
- package/build/internal/websocket/index.mjs +3 -3
- package/build/internal/websocket/transport/durable.mjs +6 -6
- package/build/internal/websocket/transport/index.d.mts +2 -2
- package/build/internal/websocket/transport/index.mjs +19 -19
- package/build/internal/websocket/transport/packet.mjs +1 -1
- package/build/internal/websocket/transport/processor.mjs +5 -5
- package/build/transform/index.mjs +26 -26
- package/package.json +1 -1
- package/src/builder/index.mts +66 -66
- package/src/builder/runtime-context.mts +17 -43
- package/src/cli.mts +48 -53
- package/src/controller/index.mts +72 -12
- package/src/index.mts +2 -2
- package/src/internal/connector/config.mts +11 -19
- package/src/internal/connector/index.mts +8 -9
- package/src/internal/connector/metrics.mts +7 -7
- package/src/internal/connector/server/index.mts +10 -10
- package/src/internal/connector/server/on-connect/decrypt-config.mts +4 -4
- package/src/internal/connector/server/on-connect/finish-oauth.mts +22 -35
- package/src/internal/connector/server/on-connect/index.mts +27 -27
- package/src/internal/connector/server/on-connect/make-oauth.mts +22 -33
- package/src/internal/connector/server/on-connect/start-oauth.mts +7 -16
- package/src/internal/connector/server/on-message.mts +1 -1
- package/src/internal/dispatcher/index.mts +49 -70
- package/src/internal/fetcher/fetcher.mts +44 -49
- package/src/internal/fetcher/oauth-fetcher.mts +12 -13
- package/src/internal/index.mts +2 -2
- package/src/internal/util/index.mts +8 -11
- package/src/internal/util/jwe/cli.mts +3 -3
- package/src/internal/util/jwe/index.mts +17 -21
- package/src/internal/websocket/config.mjs +8 -12
- package/src/internal/websocket/connection/constants.mjs +3 -3
- package/src/internal/websocket/connection/index.mjs +14 -14
- package/src/internal/websocket/connection/registration.mjs +10 -10
- package/src/internal/websocket/index.mjs +6 -6
- package/src/internal/websocket/transport/durable.mjs +9 -9
- package/src/internal/websocket/transport/index.mjs +28 -32
- package/src/internal/websocket/transport/packet.mjs +4 -4
- package/src/internal/websocket/transport/processor.mjs +9 -12
- package/src/transform/index.mts +28 -41
@@ -1,6 +1,6 @@
|
|
1
|
-
import Dispatcher from
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import Dispatcher from '../../../dispatcher/index.mjs';
|
2
|
+
import {OAuth} from '../../../fetcher/oauth-fetcher.mjs';
|
3
|
+
import {Config} from '../../../websocket/config.mjs';
|
4
4
|
|
5
5
|
export const makeOAuth = async ({
|
6
6
|
config,
|
@@ -14,12 +14,12 @@ export const makeOAuth = async ({
|
|
14
14
|
dispatcher: Dispatcher;
|
15
15
|
}) => {
|
16
16
|
const saveOAuthResult = async (what) => {
|
17
|
-
const jwe = await config.validateKeys(
|
18
|
-
const value = await jwe.encrypt(what,
|
17
|
+
const jwe = await config.validateKeys('RSA-OAEP-256');
|
18
|
+
const value = await jwe.encrypt(what, 'none', config.id());
|
19
19
|
|
20
20
|
const packet = transport.newPacket({});
|
21
21
|
|
22
|
-
packet.method(
|
22
|
+
packet.method('connector.config-update');
|
23
23
|
packet.args({
|
24
24
|
value,
|
25
25
|
});
|
@@ -28,22 +28,13 @@ export const makeOAuth = async ({
|
|
28
28
|
};
|
29
29
|
|
30
30
|
const getRefreshToken = async (refreshToken) => {
|
31
|
-
const tokenURL =
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
const
|
37
|
-
|
38
|
-
process.env.OAUTH_CLIENT_ID ||
|
39
|
-
dispatcher._oauth.clientId;
|
40
|
-
if (!clientId) throw new Error("clientId not configured");
|
41
|
-
|
42
|
-
const clientSecret =
|
43
|
-
decrypted.clientSecret ||
|
44
|
-
process.env.OAUTH_CLIENT_SECRET ||
|
45
|
-
dispatcher._oauth.clientSecret;
|
46
|
-
if (!clientSecret) throw new Error("clientSecret not configured");
|
31
|
+
const tokenURL = process.env.OAUTH_TOKEN_URL || decrypted.tokenURL || dispatcher._oauth.tokenURL;
|
32
|
+
|
33
|
+
const clientId = decrypted.clientId || process.env.OAUTH_CLIENT_ID || dispatcher._oauth.clientId;
|
34
|
+
if (!clientId) throw new Error('clientId not configured');
|
35
|
+
|
36
|
+
const clientSecret = decrypted.clientSecret || process.env.OAUTH_CLIENT_SECRET || dispatcher._oauth.clientSecret;
|
37
|
+
if (!clientSecret) throw new Error('clientSecret not configured');
|
47
38
|
|
48
39
|
const useAuthHeader = !!dispatcher._oauth.useAuthHeader;
|
49
40
|
|
@@ -57,16 +48,16 @@ export const makeOAuth = async ({
|
|
57
48
|
}
|
58
49
|
|
59
50
|
const response = await fetch(tokenURL, {
|
60
|
-
method:
|
51
|
+
method: 'POST',
|
61
52
|
body: new URLSearchParams({
|
62
|
-
grant_type:
|
53
|
+
grant_type: 'refresh_token',
|
63
54
|
refresh_token: refreshToken,
|
64
55
|
client_id: clientId,
|
65
56
|
client_secret: clientSecret,
|
66
57
|
}),
|
67
58
|
headers: {
|
68
|
-
|
69
|
-
Accept:
|
59
|
+
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
|
60
|
+
Accept: 'application/json',
|
70
61
|
...headers,
|
71
62
|
},
|
72
63
|
signal: AbortSignal.timeout(60 * 1000),
|
@@ -78,13 +69,11 @@ export const makeOAuth = async ({
|
|
78
69
|
if (status === 200) {
|
79
70
|
return JSON.parse(text);
|
80
71
|
} else {
|
81
|
-
throw new Error(
|
72
|
+
throw new Error('could not get refresh token ' + status + ' ' + text);
|
82
73
|
}
|
83
74
|
};
|
84
75
|
|
85
|
-
const theOAuth = dispatcher._oauth
|
86
|
-
? new OAuth(decrypted.oauthResult, saveOAuthResult, getRefreshToken)
|
87
|
-
: null;
|
76
|
+
const theOAuth = dispatcher._oauth ? new OAuth(decrypted.oauthResult, saveOAuthResult, getRefreshToken) : null;
|
88
77
|
|
89
78
|
if (theOAuth) {
|
90
79
|
clearInterval(dispatcher._refreshOAuthToken);
|
@@ -93,13 +82,13 @@ export const makeOAuth = async ({
|
|
93
82
|
dispatcher._refreshOAuthToken = setInterval(
|
94
83
|
async () => {
|
95
84
|
try {
|
96
|
-
console.log(
|
85
|
+
console.log('refreshing oauth token');
|
97
86
|
await theOAuth.periodicRefresh();
|
98
87
|
} catch (e) {
|
99
|
-
console.log(
|
88
|
+
console.log('periodic refresh', e);
|
100
89
|
}
|
101
90
|
},
|
102
|
-
dispatcher._oauth.tokenRefreshPeriod || 4 * 60 * 60 * 15000
|
91
|
+
dispatcher._oauth.tokenRefreshPeriod || 4 * 60 * 60 * 15000
|
103
92
|
);
|
104
93
|
}
|
105
94
|
}
|
@@ -1,26 +1,17 @@
|
|
1
|
-
export const patchStartOAuth = async ({
|
1
|
+
export const patchStartOAuth = async ({dispatcher, decrypted}) => {
|
2
2
|
dispatcher.startOAuth = async function () {
|
3
|
-
if (!dispatcher._oauth) throw new Error(
|
3
|
+
if (!dispatcher._oauth) throw new Error('oauth not configured');
|
4
4
|
|
5
5
|
const authorizationURL =
|
6
|
-
process.env.OAUTH_AUTHORIZATION_URL ||
|
7
|
-
decrypted.authorizationURL ||
|
8
|
-
dispatcher._oauth.authorizationURL;
|
6
|
+
process.env.OAUTH_AUTHORIZATION_URL || decrypted.authorizationURL || dispatcher._oauth.authorizationURL;
|
9
7
|
|
10
|
-
if (!authorizationURL) throw new Error(
|
8
|
+
if (!authorizationURL) throw new Error('authorizationURL not configured');
|
11
9
|
|
12
|
-
const clientId =
|
13
|
-
decrypted.clientId ||
|
14
|
-
process.env.OAUTH_CLIENT_ID ||
|
15
|
-
dispatcher._oauth.clientId;
|
10
|
+
const clientId = decrypted.clientId || process.env.OAUTH_CLIENT_ID || dispatcher._oauth.clientId;
|
16
11
|
|
17
|
-
if (!clientId) throw new Error(
|
12
|
+
if (!clientId) throw new Error('clientId not configured');
|
18
13
|
|
19
|
-
const scopes =
|
20
|
-
process.env.OAUTH_SCOPE ||
|
21
|
-
decrypted.scope ||
|
22
|
-
dispatcher._oauth.scope ||
|
23
|
-
"";
|
14
|
+
const scopes = process.env.OAUTH_SCOPE || decrypted.scope || dispatcher._oauth.scope || '';
|
24
15
|
const useCodeChallenge = !!dispatcher._oauth.useCodeChallenge;
|
25
16
|
|
26
17
|
return {
|
@@ -7,7 +7,7 @@ export default class Dispatcher {
|
|
7
7
|
_refreshOAuthToken?: any;
|
8
8
|
|
9
9
|
constructor() {
|
10
|
-
this._config = {
|
10
|
+
this._config = {fields: {}};
|
11
11
|
}
|
12
12
|
|
13
13
|
main(what) {
|
@@ -22,9 +22,9 @@ export default class Dispatcher {
|
|
22
22
|
oauth: true,
|
23
23
|
fields: {
|
24
24
|
oauthResult: {
|
25
|
-
name:
|
26
|
-
placeholder:
|
27
|
-
type:
|
25
|
+
name: 'OAuth Result',
|
26
|
+
placeholder: 'will be set by finishing the oauth flow',
|
27
|
+
type: 'managed',
|
28
28
|
},
|
29
29
|
},
|
30
30
|
});
|
@@ -32,19 +32,18 @@ export default class Dispatcher {
|
|
32
32
|
return this;
|
33
33
|
}
|
34
34
|
|
35
|
-
if (!arg.authorizationURL) throw new Error(
|
36
|
-
if (!arg.tokenURL && !arg.finishOAuth)
|
37
|
-
throw new Error("need a tokenURL or finishOAuth()");
|
35
|
+
if (!arg.authorizationURL) throw new Error('need a authorizationURL');
|
36
|
+
if (!arg.tokenURL && !arg.finishOAuth) throw new Error('need a tokenURL or finishOAuth()');
|
38
37
|
|
39
|
-
this._oauth = {
|
38
|
+
this._oauth = {...arg};
|
40
39
|
|
41
40
|
this.config({
|
42
41
|
oauth: true,
|
43
42
|
fields: {
|
44
43
|
oauthResult: {
|
45
|
-
name:
|
46
|
-
placeholder:
|
47
|
-
type:
|
44
|
+
name: 'OAuth Result',
|
45
|
+
placeholder: 'will be set by finishing the oauth flow',
|
46
|
+
type: 'managed',
|
48
47
|
optional: !!arg.connectionOptional,
|
49
48
|
},
|
50
49
|
},
|
@@ -54,17 +53,17 @@ export default class Dispatcher {
|
|
54
53
|
this.config({
|
55
54
|
fields: {
|
56
55
|
clientId: {
|
57
|
-
name:
|
58
|
-
placeholder:
|
59
|
-
type:
|
56
|
+
name: 'OAuth Client ID',
|
57
|
+
placeholder: 'e.g. 1234',
|
58
|
+
type: 'line',
|
60
59
|
optional: !!arg.configurableClientOptional,
|
61
60
|
plain: true,
|
62
61
|
},
|
63
62
|
clientSecret: {
|
64
|
-
name:
|
65
|
-
placeholder:
|
63
|
+
name: 'OAuth Client Secret',
|
64
|
+
placeholder: 'e.g. axd5xde',
|
66
65
|
optional: !!arg.configurableClientOptional,
|
67
|
-
type:
|
66
|
+
type: 'line',
|
68
67
|
},
|
69
68
|
},
|
70
69
|
});
|
@@ -74,9 +73,9 @@ export default class Dispatcher {
|
|
74
73
|
this.config({
|
75
74
|
fields: {
|
76
75
|
scope: {
|
77
|
-
name:
|
78
|
-
placeholder:
|
79
|
-
type:
|
76
|
+
name: 'OAuth Scope',
|
77
|
+
placeholder: 'e.g. x y z',
|
78
|
+
type: 'line',
|
80
79
|
description: `Default Scope:
|
81
80
|
|
82
81
|
${arg.configurableClientScope}
|
@@ -97,17 +96,17 @@ ${arg.configurableClientScope}
|
|
97
96
|
return this;
|
98
97
|
}
|
99
98
|
|
100
|
-
config({
|
99
|
+
config({fields, oauth, description, summary}: any) {
|
101
100
|
this._config.oauth = this._config.oauth || oauth;
|
102
101
|
this._config.description = this._config.description || description;
|
103
102
|
this._config.summary = this._config.summary || summary;
|
104
|
-
this._config.fields = {
|
103
|
+
this._config.fields = {...fields, ...this._config.fields};
|
105
104
|
|
106
105
|
return this;
|
107
106
|
}
|
108
107
|
|
109
108
|
resolvers(what) {
|
110
|
-
this._resolvers = {
|
109
|
+
this._resolvers = {...this._resolvers, ...what};
|
111
110
|
|
112
111
|
return this;
|
113
112
|
}
|
@@ -116,16 +115,16 @@ ${arg.configurableClientScope}
|
|
116
115
|
this.config({
|
117
116
|
fields: {
|
118
117
|
_endpointToken: {
|
119
|
-
name:
|
120
|
-
placeholder:
|
121
|
-
type: !!notOptional ?
|
118
|
+
name: 'Endpoint Token',
|
119
|
+
placeholder: 'e.g. 1234',
|
120
|
+
type: !!notOptional ? 'managed' : 'line',
|
122
121
|
plain: true,
|
123
122
|
optional: !notOptional,
|
124
123
|
},
|
125
124
|
},
|
126
125
|
});
|
127
126
|
|
128
|
-
this.resolvers({
|
127
|
+
this.resolvers({_endpoint: what});
|
129
128
|
|
130
129
|
return this;
|
131
130
|
}
|
@@ -134,28 +133,23 @@ ${arg.configurableClientScope}
|
|
134
133
|
url: string;
|
135
134
|
useCodeChallenge: boolean;
|
136
135
|
}> {
|
137
|
-
throw new Error(
|
136
|
+
throw new Error('oauth not configured');
|
138
137
|
}
|
139
138
|
|
140
|
-
async finishOAuth(arg: {
|
141
|
-
|
142
|
-
redirectURI: string;
|
143
|
-
codeVerifier?: string;
|
144
|
-
}): Promise<{ value: string }> {
|
145
|
-
throw new Error("oauth not configured");
|
139
|
+
async finishOAuth(arg: {code: string; redirectURI: string; codeVerifier?: string}): Promise<{value: string}> {
|
140
|
+
throw new Error('oauth not configured');
|
146
141
|
}
|
147
142
|
|
148
143
|
build() {
|
149
|
-
if (!this._types || !this._resolvers)
|
150
|
-
throw new Error("missing types or resolvers");
|
144
|
+
if (!this._types || !this._resolvers) throw new Error('missing types or resolvers');
|
151
145
|
var local = this;
|
152
146
|
|
153
|
-
const _resolvers = {
|
147
|
+
const _resolvers = {...this._resolvers};
|
154
148
|
|
155
149
|
const main = this._main || (() => {});
|
156
150
|
|
157
151
|
const start = async (arg) => {
|
158
|
-
console.log(
|
152
|
+
console.log('starting ...');
|
159
153
|
await main(arg);
|
160
154
|
};
|
161
155
|
|
@@ -169,32 +163,19 @@ ${arg.configurableClientScope}
|
|
169
163
|
return current;
|
170
164
|
};
|
171
165
|
|
172
|
-
const execute = async ({
|
166
|
+
const execute = async ({query, variables}) => {
|
173
167
|
if (!Array.isArray(query)) query = [query];
|
174
168
|
|
175
169
|
query = query
|
176
170
|
.filter(
|
177
|
-
(what) =>
|
178
|
-
!!what?.trim() &&
|
179
|
-
![
|
180
|
-
"constructor",
|
181
|
-
"__proto__",
|
182
|
-
"toString",
|
183
|
-
"toSource",
|
184
|
-
"prototype",
|
185
|
-
].includes(what),
|
171
|
+
(what) => !!what?.trim() && !['constructor', '__proto__', 'toString', 'toSource', 'prototype'].includes(what)
|
186
172
|
)
|
187
173
|
.slice(0, 20);
|
188
174
|
|
189
175
|
const method = resolveMethod(query);
|
190
|
-
if (!method && !_resolvers.__default)
|
191
|
-
|
192
|
-
|
193
|
-
return method
|
194
|
-
? method(variables)
|
195
|
-
: _resolvers.__default(
|
196
|
-
variables ? { ...variables, __method: query } : variables,
|
197
|
-
);
|
176
|
+
if (!method && !_resolvers.__default) throw new Error(`${query} not found`);
|
177
|
+
|
178
|
+
return method ? method(variables) : _resolvers.__default(variables ? {...variables, __method: query} : variables);
|
198
179
|
};
|
199
180
|
|
200
181
|
const introspect = () => local._types;
|
@@ -202,35 +183,33 @@ ${arg.configurableClientScope}
|
|
202
183
|
|
203
184
|
const processPacket = async (packet) => {
|
204
185
|
switch (packet.method()) {
|
205
|
-
case
|
186
|
+
case 'connector.introspect':
|
206
187
|
// @ts-ignore
|
207
188
|
const intro = await introspect({});
|
208
189
|
|
209
|
-
return {
|
190
|
+
return {configSchema: local._config, introspect: intro};
|
210
191
|
|
211
|
-
case
|
192
|
+
case 'connector.start-oauth':
|
212
193
|
// @ts-ignore
|
213
194
|
return await local.startOAuth(packet.args());
|
214
195
|
|
215
|
-
case
|
196
|
+
case 'connector.finish-oauth':
|
216
197
|
// @ts-ignore
|
217
198
|
return await local.finishOAuth(packet.args());
|
218
199
|
|
219
|
-
case
|
200
|
+
case 'connector.query':
|
220
201
|
const ret = await execute(packet.args());
|
221
202
|
|
222
|
-
return typeof ret ===
|
223
|
-
? ret
|
224
|
-
: { [packet.args().query]: ret };
|
203
|
+
return typeof ret === 'object' && !Array.isArray(ret) ? ret : {[packet.args().query]: ret};
|
225
204
|
|
226
|
-
case
|
227
|
-
await local.onConfig({
|
205
|
+
case 'connector.set-config':
|
206
|
+
await local.onConfig({...packet.args().secrets});
|
228
207
|
|
229
208
|
return;
|
230
209
|
}
|
231
210
|
|
232
|
-
console.dir(packet, {
|
233
|
-
throw new Error(
|
211
|
+
console.dir(packet, {depth: null});
|
212
|
+
throw new Error('cannot handle packet');
|
234
213
|
};
|
235
214
|
|
236
215
|
return {
|
@@ -242,8 +221,8 @@ ${arg.configurableClientScope}
|
|
242
221
|
};
|
243
222
|
}
|
244
223
|
onConfig(arg0: any) {
|
245
|
-
throw new Error(
|
224
|
+
throw new Error('Method not implemented.');
|
246
225
|
}
|
247
226
|
}
|
248
227
|
|
249
|
-
export {
|
228
|
+
export {Dispatcher};
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import {unwrap} from '../util/index.mjs';
|
2
2
|
|
3
3
|
/**
|
4
4
|
* http request fetcher
|
@@ -8,7 +8,17 @@ export default class Fetcher {
|
|
8
8
|
protected baseUrl: any;
|
9
9
|
protected onResponse: any;
|
10
10
|
protected customize0: any;
|
11
|
-
constructor({
|
11
|
+
constructor({
|
12
|
+
retry = 5,
|
13
|
+
baseUrl,
|
14
|
+
onResponse,
|
15
|
+
customize,
|
16
|
+
}: {
|
17
|
+
retry?: number;
|
18
|
+
baseUrl?: string;
|
19
|
+
onResponse?: (response: Response) => void;
|
20
|
+
customize?: (request: {[key: string]: any}) => void;
|
21
|
+
} = {}) {
|
12
22
|
this.retry = retry;
|
13
23
|
this.baseUrl = baseUrl;
|
14
24
|
this.onResponse = onResponse;
|
@@ -23,14 +33,7 @@ export default class Fetcher {
|
|
23
33
|
if (this.customize0) await this.customize0(options, args);
|
24
34
|
}
|
25
35
|
|
26
|
-
protected async onError(
|
27
|
-
e: any,
|
28
|
-
url: string,
|
29
|
-
options: any,
|
30
|
-
retries: number,
|
31
|
-
args: any,
|
32
|
-
rateLimit?,
|
33
|
-
) {
|
36
|
+
protected async onError(e: any, url: string, options: any, retries: number, args: any, rateLimit?) {
|
34
37
|
var local = this;
|
35
38
|
|
36
39
|
return new Promise((resolve, reject) => {
|
@@ -42,7 +45,7 @@ export default class Fetcher {
|
|
42
45
|
reject(e);
|
43
46
|
}
|
44
47
|
},
|
45
|
-
rateLimit ? 10000 : 500
|
48
|
+
rateLimit ? 10000 : 500
|
46
49
|
);
|
47
50
|
});
|
48
51
|
}
|
@@ -55,20 +58,25 @@ export default class Fetcher {
|
|
55
58
|
* @param args optional args for customize()
|
56
59
|
* @returns
|
57
60
|
*/
|
58
|
-
async fetch(
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
61
|
+
async fetch(
|
62
|
+
url: string,
|
63
|
+
options: {
|
64
|
+
/**
|
65
|
+
* request method like GET, POST, PUT, DELETE
|
66
|
+
*/
|
67
|
+
method?: string;
|
68
|
+
/**
|
69
|
+
* request headers like Accept, Content-type
|
70
|
+
*/
|
71
|
+
headers?: {[key: string]: any};
|
72
|
+
/**
|
73
|
+
* request body like "hello world" or {hello: "world"}
|
74
|
+
*/
|
75
|
+
body?: any;
|
76
|
+
} = {},
|
77
|
+
retries?: number,
|
78
|
+
args: any = {}
|
79
|
+
) {
|
72
80
|
var local = this,
|
73
81
|
baseUrl = local.baseUrl;
|
74
82
|
|
@@ -76,12 +84,7 @@ export default class Fetcher {
|
|
76
84
|
|
77
85
|
if (retries == null) retries = local.retry;
|
78
86
|
|
79
|
-
let theURL = !baseUrl
|
80
|
-
? url
|
81
|
-
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(
|
82
|
-
/\/\/+/gi,
|
83
|
-
"/",
|
84
|
-
);
|
87
|
+
let theURL = !baseUrl ? url : `${baseUrl?.endsWith('/') ? baseUrl : baseUrl + '/'}${url}`.replace(/\/\/+/gi, '/');
|
85
88
|
|
86
89
|
try {
|
87
90
|
options0.url = url;
|
@@ -90,40 +93,32 @@ export default class Fetcher {
|
|
90
93
|
url = options0.url;
|
91
94
|
delete options0.url;
|
92
95
|
|
93
|
-
theURL = !baseUrl
|
94
|
-
? url
|
95
|
-
: `${baseUrl?.endsWith("/") ? baseUrl : baseUrl + "/"}${url}`.replace(
|
96
|
-
/\/\/+/gi,
|
97
|
-
"/",
|
98
|
-
);
|
96
|
+
theURL = !baseUrl ? url : `${baseUrl?.endsWith('/') ? baseUrl : baseUrl + '/'}${url}`.replace(/\/\/+/gi, '/');
|
99
97
|
|
100
98
|
if (!options0?.headers || !options0?.headers?.Accept) {
|
101
99
|
options0.headers = {
|
102
100
|
...options0.headers,
|
103
|
-
Accept:
|
101
|
+
Accept: 'application/json',
|
104
102
|
};
|
105
103
|
}
|
106
104
|
|
107
|
-
if (!options0?.headers || !options0?.headers?.[
|
105
|
+
if (!options0?.headers || !options0?.headers?.['Content-type']) {
|
108
106
|
options0.headers = {
|
109
107
|
...options0.headers,
|
110
|
-
|
108
|
+
'Content-type': 'application/json',
|
111
109
|
};
|
112
110
|
}
|
113
111
|
|
114
112
|
if (
|
115
|
-
!(options0?.method ===
|
113
|
+
!(options0?.method === 'GET' || options0?.method === 'HEAD') &&
|
116
114
|
options0?.body &&
|
117
|
-
!(typeof options0.body ===
|
118
|
-
options0?.headers?.[
|
115
|
+
!(typeof options0.body === 'string') &&
|
116
|
+
options0?.headers?.['Content-type'] === 'application/json'
|
119
117
|
) {
|
120
118
|
options0.body = JSON.stringify(options0.body);
|
121
119
|
}
|
122
120
|
|
123
|
-
const timeout = Math.min(
|
124
|
-
options0?.timeout || 30 * 60 * 1000,
|
125
|
-
30 * 60 * 1000,
|
126
|
-
);
|
121
|
+
const timeout = Math.min(options0?.timeout || 30 * 60 * 1000, 30 * 60 * 1000);
|
127
122
|
const ret = await fetch(theURL, {
|
128
123
|
...options0,
|
129
124
|
signal: AbortSignal.timeout(timeout),
|
@@ -132,7 +127,7 @@ export default class Fetcher {
|
|
132
127
|
|
133
128
|
if (status > 399) {
|
134
129
|
const text = await ret.text();
|
135
|
-
const e: any = new Error(status +
|
130
|
+
const e: any = new Error(status + ' ' + text);
|
136
131
|
|
137
132
|
e.status = status;
|
138
133
|
throw e;
|
@@ -143,7 +138,7 @@ export default class Fetcher {
|
|
143
138
|
}
|
144
139
|
|
145
140
|
if (status === 204) {
|
146
|
-
return {
|
141
|
+
return {ok: true};
|
147
142
|
}
|
148
143
|
|
149
144
|
return unwrap(ret, options0);
|
@@ -1,19 +1,18 @@
|
|
1
|
-
import Fetcher from
|
1
|
+
import Fetcher from './fetcher.mjs';
|
2
2
|
|
3
3
|
class OAuthFetcher extends Fetcher {
|
4
4
|
oauth: any;
|
5
5
|
_getToken: any;
|
6
6
|
used: boolean = false;
|
7
|
-
constructor({
|
8
|
-
super({
|
7
|
+
constructor({oauth, retry = 5, getToken, baseUrl, onResponse, customize}) {
|
8
|
+
super({retry, baseUrl, onResponse, customize});
|
9
9
|
|
10
10
|
this.oauth = oauth;
|
11
11
|
this._getToken = getToken;
|
12
12
|
}
|
13
13
|
|
14
14
|
async __healthCheck() {
|
15
|
-
if (this.used && !this.oauth.accessToken())
|
16
|
-
throw new Error("no access token");
|
15
|
+
if (this.used && !this.oauth.accessToken()) throw new Error('no access token');
|
17
16
|
}
|
18
17
|
|
19
18
|
async getToken(force) {
|
@@ -30,7 +29,7 @@ class OAuthFetcher extends Fetcher {
|
|
30
29
|
|
31
30
|
try {
|
32
31
|
if (!refreshToken) {
|
33
|
-
throw new Error(
|
32
|
+
throw new Error('have no access_token and no refresh_token');
|
34
33
|
}
|
35
34
|
|
36
35
|
const ret = await oauth.obtainViaRefreshToken(oauth.refreshToken());
|
@@ -40,7 +39,7 @@ class OAuthFetcher extends Fetcher {
|
|
40
39
|
|
41
40
|
return ret.access_token;
|
42
41
|
} else {
|
43
|
-
throw new Error(
|
42
|
+
throw new Error('could not obtain access token via refresh token');
|
44
43
|
}
|
45
44
|
} catch (e) {
|
46
45
|
oauth.invalidate(e);
|
@@ -59,13 +58,13 @@ class OAuthFetcher extends Fetcher {
|
|
59
58
|
resolve(
|
60
59
|
await local.fetch(url, options, retries, {
|
61
60
|
forceTokenRefresh: e.status === 401,
|
62
|
-
})
|
61
|
+
})
|
63
62
|
);
|
64
63
|
} catch (e) {
|
65
64
|
reject(e);
|
66
65
|
}
|
67
66
|
},
|
68
|
-
rateLimit ? 10000 : 500
|
67
|
+
rateLimit ? 10000 : 500
|
69
68
|
);
|
70
69
|
});
|
71
70
|
}
|
@@ -74,12 +73,12 @@ class OAuthFetcher extends Fetcher {
|
|
74
73
|
const local = this,
|
75
74
|
oauth = local.oauth;
|
76
75
|
|
77
|
-
console.log(
|
76
|
+
console.log('refreshing oauth token, have token', !!oauth.refreshToken());
|
78
77
|
if (!oauth.refreshToken()) return;
|
79
78
|
|
80
79
|
await local.getToken(true);
|
81
80
|
|
82
|
-
console.log(
|
81
|
+
console.log('refreshed oauth token');
|
83
82
|
}
|
84
83
|
|
85
84
|
async customize(options: any, args: any = {}) {
|
@@ -133,7 +132,7 @@ export class OAuth {
|
|
133
132
|
async periodicRefresh() {
|
134
133
|
const clients = this.clients;
|
135
134
|
|
136
|
-
console.log(
|
135
|
+
console.log('refreshing oauth clients', clients.length);
|
137
136
|
|
138
137
|
for (let i = 0; i < clients.length; ++i) {
|
139
138
|
const client = clients[0];
|
@@ -147,7 +146,7 @@ export class OAuth {
|
|
147
146
|
}
|
148
147
|
|
149
148
|
getClient(arg: any = {}) {
|
150
|
-
const client = new OAuthFetcher({
|
149
|
+
const client = new OAuthFetcher({...arg, oauth: this});
|
151
150
|
this.clients.push(client);
|
152
151
|
return client;
|
153
152
|
}
|
package/src/internal/index.mts
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
import {
|
1
|
+
import {Connector} from './connector/index.mjs';
|
2
2
|
|
3
|
-
export {
|
3
|
+
export {Connector};
|