Skip to content

Commit

Permalink
fix: add sanitize axios err func (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed-envoy authored Jul 13, 2023
1 parent 2501074 commit 240c43d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 84 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@envoy/envoy-integrations-sdk",
"version": "2.0.0-beta.29",
"version": "2.0.0-beta.30",
"description": "SDK for building Envoy integrations.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
37 changes: 21 additions & 16 deletions src/sdk/EnvoyPluginAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import EnvoyStorageCommand from '../internal/EnvoyStorageCommand';
import EnvoyStorageResult from '../internal/EnvoyStorageResult';
import { envoyBaseURL, envoyClientId, envoyClientSecret } from '../constants';
import { EnvoyMetaAuth } from './EnvoyMeta';
import { sanitizeAxiosError } from '../util/axiosConstructor';

/**
* API endpoints for *plugin-scoped* tokens.
Expand Down Expand Up @@ -70,21 +71,25 @@ export default class EnvoyPluginAPI extends EnvoyAPI {
* Gets a plugin access token using `client_credentials` as the grant type.
*/
static async loginAsPlugin(id = envoyClientId, secret = envoyClientSecret): Promise<EnvoyMetaAuth> {
const { data } = await axios({
auth: {
username: id,
password: secret,
},
method: 'POST',
data: {
grant_type: 'client_credentials',
client_id: id,
client_secret: secret,
scope: 'plugin,token.refresh',
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
try {
const { data } = await axios({
auth: {
username: id,
password: secret,
},
method: 'POST',
data: {
grant_type: 'client_credentials',
client_id: id,
client_secret: secret,
scope: 'plugin,token.refresh',
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
} catch (error) {
throw sanitizeAxiosError(error);
}
}
}
103 changes: 58 additions & 45 deletions src/sdk/EnvoyUserAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { UserModel } from '../resources/UserResource';
import { envoyBaseURL, envoyClientId, envoyClientSecret } from '../constants';
import { EnvoyMetaAuth } from './EnvoyMeta';
import { sanitizeAxiosError } from '../util/axiosConstructor';

export type EnvoyUserAPIScope =
'flows.read' |
Expand Down Expand Up @@ -256,22 +257,26 @@ export default class EnvoyUserAPI extends EnvoyAPI {
clientId = envoyClientId,
clientSecret = envoyClientSecret,
): Promise<EnvoyMetaAuth> {
const { data } = await axios({
auth: {
username: clientId,
password: clientSecret,
},
method: 'POST',
data: {
grant_type: 'password',
scope,
username,
password,
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
try {
const { data } = await axios({
auth: {
username: clientId,
password: clientSecret,
},
method: 'POST',
data: {
grant_type: 'password',
scope,
username,
password,
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
} catch (error) {
throw sanitizeAxiosError(error);
}
}

/**
Expand All @@ -283,21 +288,25 @@ export default class EnvoyUserAPI extends EnvoyAPI {
clientId = envoyClientId,
clientSecret = envoyClientSecret,
): Promise<EnvoyMetaAuth> {
const { data } = await axios({
auth: {
username: clientId,
password: clientSecret,
},
method: 'POST',
data: {
grant_type: 'authorization_code',
scope,
code,
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
try {
const { data } = await axios({
auth: {
username: clientId,
password: clientSecret,
},
method: 'POST',
data: {
grant_type: 'authorization_code',
scope,
code,
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
} catch (error) {
throw sanitizeAxiosError(error);
}
}

/**
Expand All @@ -308,19 +317,23 @@ export default class EnvoyUserAPI extends EnvoyAPI {
clientId = envoyClientId,
clientSecret = envoyClientSecret,
): Promise<EnvoyMetaAuth> {
const { data } = await axios({
auth: {
username: clientId,
password: clientSecret,
},
method: 'POST',
data: {
grant_type: 'plugin_install',
install_id: installId,
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
try {
const { data } = await axios({
auth: {
username: clientId,
password: clientSecret,
},
method: 'POST',
data: {
grant_type: 'plugin_install',
install_id: installId,
},
url: '/a/auth/v0/token',
baseURL: envoyBaseURL,
});
return data;
} catch (error) {
throw sanitizeAxiosError(error);
}
}
}
48 changes: 26 additions & 22 deletions src/util/axiosConstructor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,33 @@ export function createAxiosClient(config?: AxiosRequestConfig | undefined): Axio
client.interceptors.response.use((response) => {
return response;
}, (error) => {
const safeError = {
code: error.code,
request: {
baseURL: error.request?.baseURL,
url: error.request?.url,
method: error.request?.method,
},
response: {
code: error.response?.code,
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
},
message: error.message,
name: error.name,
baseURL: error.request?.baseURL ?? error.config?.baseURL,
url: error.request?.url ?? error.config?.url,
method: error.request?.method ?? error.config?.method,
stack: error.stack,
data: error.data,
}
return Promise.reject(safeError);
return Promise.reject(sanitizeAxiosError(error));
});

return client;
}

export function sanitizeAxiosError(error: any) {
const safeError = {
code: error.code,
request: {
baseURL: error.request?.baseURL,
url: error.request?.url,
method: error.request?.method,
},
response: {
code: error.response?.code,
status: error.response?.status,
statusText: error.response?.statusText,
data: error.response?.data,
},
message: error.message,
name: error.name,
baseURL: error.request?.baseURL ?? error.config?.baseURL,
url: error.request?.url ?? error.config?.url,
method: error.request?.method ?? error.config?.method,
stack: error.stack,
data: error.data,
};
return safeError;
}

0 comments on commit 240c43d

Please sign in to comment.