Skip to content

Commit 240c43d

Browse files
authored
fix: add sanitize axios err func (#40)
1 parent 2501074 commit 240c43d

File tree

4 files changed

+106
-84
lines changed

4 files changed

+106
-84
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@envoy/envoy-integrations-sdk",
3-
"version": "2.0.0-beta.29",
3+
"version": "2.0.0-beta.30",
44
"description": "SDK for building Envoy integrations.",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/sdk/EnvoyPluginAPI.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import EnvoyStorageCommand from '../internal/EnvoyStorageCommand';
55
import EnvoyStorageResult from '../internal/EnvoyStorageResult';
66
import { envoyBaseURL, envoyClientId, envoyClientSecret } from '../constants';
77
import { EnvoyMetaAuth } from './EnvoyMeta';
8+
import { sanitizeAxiosError } from '../util/axiosConstructor';
89

910
/**
1011
* API endpoints for *plugin-scoped* tokens.
@@ -70,21 +71,25 @@ export default class EnvoyPluginAPI extends EnvoyAPI {
7071
* Gets a plugin access token using `client_credentials` as the grant type.
7172
*/
7273
static async loginAsPlugin(id = envoyClientId, secret = envoyClientSecret): Promise<EnvoyMetaAuth> {
73-
const { data } = await axios({
74-
auth: {
75-
username: id,
76-
password: secret,
77-
},
78-
method: 'POST',
79-
data: {
80-
grant_type: 'client_credentials',
81-
client_id: id,
82-
client_secret: secret,
83-
scope: 'plugin,token.refresh',
84-
},
85-
url: '/a/auth/v0/token',
86-
baseURL: envoyBaseURL,
87-
});
88-
return data;
74+
try {
75+
const { data } = await axios({
76+
auth: {
77+
username: id,
78+
password: secret,
79+
},
80+
method: 'POST',
81+
data: {
82+
grant_type: 'client_credentials',
83+
client_id: id,
84+
client_secret: secret,
85+
scope: 'plugin,token.refresh',
86+
},
87+
url: '/a/auth/v0/token',
88+
baseURL: envoyBaseURL,
89+
});
90+
return data;
91+
} catch (error) {
92+
throw sanitizeAxiosError(error);
93+
}
8994
}
9095
}

src/sdk/EnvoyUserAPI.ts

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { UserModel } from '../resources/UserResource';
1717
import { envoyBaseURL, envoyClientId, envoyClientSecret } from '../constants';
1818
import { EnvoyMetaAuth } from './EnvoyMeta';
19+
import { sanitizeAxiosError } from '../util/axiosConstructor';
1920

2021
export type EnvoyUserAPIScope =
2122
'flows.read' |
@@ -256,22 +257,26 @@ export default class EnvoyUserAPI extends EnvoyAPI {
256257
clientId = envoyClientId,
257258
clientSecret = envoyClientSecret,
258259
): Promise<EnvoyMetaAuth> {
259-
const { data } = await axios({
260-
auth: {
261-
username: clientId,
262-
password: clientSecret,
263-
},
264-
method: 'POST',
265-
data: {
266-
grant_type: 'password',
267-
scope,
268-
username,
269-
password,
270-
},
271-
url: '/a/auth/v0/token',
272-
baseURL: envoyBaseURL,
273-
});
274-
return data;
260+
try {
261+
const { data } = await axios({
262+
auth: {
263+
username: clientId,
264+
password: clientSecret,
265+
},
266+
method: 'POST',
267+
data: {
268+
grant_type: 'password',
269+
scope,
270+
username,
271+
password,
272+
},
273+
url: '/a/auth/v0/token',
274+
baseURL: envoyBaseURL,
275+
});
276+
return data;
277+
} catch (error) {
278+
throw sanitizeAxiosError(error);
279+
}
275280
}
276281

277282
/**
@@ -283,21 +288,25 @@ export default class EnvoyUserAPI extends EnvoyAPI {
283288
clientId = envoyClientId,
284289
clientSecret = envoyClientSecret,
285290
): Promise<EnvoyMetaAuth> {
286-
const { data } = await axios({
287-
auth: {
288-
username: clientId,
289-
password: clientSecret,
290-
},
291-
method: 'POST',
292-
data: {
293-
grant_type: 'authorization_code',
294-
scope,
295-
code,
296-
},
297-
url: '/a/auth/v0/token',
298-
baseURL: envoyBaseURL,
299-
});
300-
return data;
291+
try {
292+
const { data } = await axios({
293+
auth: {
294+
username: clientId,
295+
password: clientSecret,
296+
},
297+
method: 'POST',
298+
data: {
299+
grant_type: 'authorization_code',
300+
scope,
301+
code,
302+
},
303+
url: '/a/auth/v0/token',
304+
baseURL: envoyBaseURL,
305+
});
306+
return data;
307+
} catch (error) {
308+
throw sanitizeAxiosError(error);
309+
}
301310
}
302311

303312
/**
@@ -308,19 +317,23 @@ export default class EnvoyUserAPI extends EnvoyAPI {
308317
clientId = envoyClientId,
309318
clientSecret = envoyClientSecret,
310319
): Promise<EnvoyMetaAuth> {
311-
const { data } = await axios({
312-
auth: {
313-
username: clientId,
314-
password: clientSecret,
315-
},
316-
method: 'POST',
317-
data: {
318-
grant_type: 'plugin_install',
319-
install_id: installId,
320-
},
321-
url: '/a/auth/v0/token',
322-
baseURL: envoyBaseURL,
323-
});
324-
return data;
320+
try {
321+
const { data } = await axios({
322+
auth: {
323+
username: clientId,
324+
password: clientSecret,
325+
},
326+
method: 'POST',
327+
data: {
328+
grant_type: 'plugin_install',
329+
install_id: installId,
330+
},
331+
url: '/a/auth/v0/token',
332+
baseURL: envoyBaseURL,
333+
});
334+
return data;
335+
} catch (error) {
336+
throw sanitizeAxiosError(error);
337+
}
325338
}
326339
}

src/util/axiosConstructor.ts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,33 @@ export function createAxiosClient(config?: AxiosRequestConfig | undefined): Axio
55
client.interceptors.response.use((response) => {
66
return response;
77
}, (error) => {
8-
const safeError = {
9-
code: error.code,
10-
request: {
11-
baseURL: error.request?.baseURL,
12-
url: error.request?.url,
13-
method: error.request?.method,
14-
},
15-
response: {
16-
code: error.response?.code,
17-
status: error.response?.status,
18-
statusText: error.response?.statusText,
19-
data: error.response?.data,
20-
},
21-
message: error.message,
22-
name: error.name,
23-
baseURL: error.request?.baseURL ?? error.config?.baseURL,
24-
url: error.request?.url ?? error.config?.url,
25-
method: error.request?.method ?? error.config?.method,
26-
stack: error.stack,
27-
data: error.data,
28-
}
29-
return Promise.reject(safeError);
8+
return Promise.reject(sanitizeAxiosError(error));
309
});
3110

3211
return client;
12+
}
13+
14+
export function sanitizeAxiosError(error: any) {
15+
const safeError = {
16+
code: error.code,
17+
request: {
18+
baseURL: error.request?.baseURL,
19+
url: error.request?.url,
20+
method: error.request?.method,
21+
},
22+
response: {
23+
code: error.response?.code,
24+
status: error.response?.status,
25+
statusText: error.response?.statusText,
26+
data: error.response?.data,
27+
},
28+
message: error.message,
29+
name: error.name,
30+
baseURL: error.request?.baseURL ?? error.config?.baseURL,
31+
url: error.request?.url ?? error.config?.url,
32+
method: error.request?.method ?? error.config?.method,
33+
stack: error.stack,
34+
data: error.data,
35+
};
36+
return safeError;
3337
}

0 commit comments

Comments
 (0)