Skip to content

Commit

Permalink
remove axios dependency from rollbar-backend
Browse files Browse the repository at this point in the history
Signed-off-by: Colton Padden <colton.padden@fastmail.com>
  • Loading branch information
cmpadden committed Dec 22, 2021
1 parent 60ecf7e commit c5e175c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-flies-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-rollbar-backend': patch
---

Replace the usage of `axios` with `node-fetch` in the Rollbar API
4 changes: 3 additions & 1 deletion plugins/rollbar-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
"dependencies": {
"@backstage/backend-common": "^0.9.12",
"@backstage/config": "^0.1.10",
"@backstage/test-utils": "^0.1.24",
"@types/express": "^4.17.6",
"axios": "^0.24.0",
"camelcase-keys": "^6.2.2",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand All @@ -44,12 +44,14 @@
"helmet": "^4.0.0",
"lodash": "^4.17.21",
"morgan": "^1.10.0",
"node-fetch": "^2.6.1",
"winston": "^3.2.1",
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/cli": "^0.10.0",
"@types/supertest": "^2.0.8",
"msw": "^0.36.3",
"supertest": "^6.1.3"
},
"files": [
Expand Down
34 changes: 33 additions & 1 deletion plugins/rollbar-backend/src/api/RollbarApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
* limitations under the License.
*/

import { getRequestHeaders } from './RollbarApi';
import { getRequestHeaders, RollbarApi } from './RollbarApi';
import { setupRequestMockHandlers } from '@backstage/test-utils';
import { rest } from 'msw';
import { setupServer } from 'msw/node';
import { getVoidLogger } from '@backstage/backend-common';
import { RollbarProject } from './types';

describe('RollbarApi', () => {
describe('getRequestHeaders', () => {
Expand All @@ -26,4 +31,31 @@ describe('RollbarApi', () => {
});
});
});

describe('getAllProjects', () => {
const server = setupServer();
setupRequestMockHandlers(server);

const mockBaseUrl = 'https://api.rollbar.com/api/1';

const mockProjects: RollbarProject[] = [
{ id: 123, name: 'abc', accountId: 1, status: 'enabled' },
{ id: 456, name: 'xyz', accountId: 1, status: 'enabled' },
];

const setupHandlers = () => {
server.use(
rest.get(`${mockBaseUrl}/projects`, (_, res, ctx) => {
return res(ctx.json({ result: mockProjects }));
}),
);
};

it('should return all projects with a name attribute', async () => {
setupHandlers();
const api = new RollbarApi('my-access-token', getVoidLogger());
const projects = await api.getAllProjects();
expect(projects).toEqual(mockProjects);
});
});
});
13 changes: 7 additions & 6 deletions plugins/rollbar-backend/src/api/RollbarApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import axios from 'axios';
import { Logger } from 'winston';
import camelcaseKeys from 'camelcase-keys';
import { buildQuery } from '../util';
Expand All @@ -25,6 +24,7 @@ import {
RollbarProjectAccessToken,
RollbarTopActiveItem,
} from './types';
import fetch from 'node-fetch';

const baseUrl = 'https://api.rollbar.com/api/1';

Expand Down Expand Up @@ -110,11 +110,12 @@ export class RollbarApi {
this.logger.info(`Calling Rollbar REST API, ${fullUrl}`);
}

return axios
.get(fullUrl, getRequestHeaders(accessToken || this.accessToken || ''))
.then(response =>
camelcaseKeys<T>(response?.data?.result, { deep: true }),
);
return fetch(
fullUrl,
getRequestHeaders(accessToken || this.accessToken || ''),
)
.then(response => response.json())
.then(json => camelcaseKeys<T>(json?.result, { deep: true }));
}

private async getForProject<T>(
Expand Down

0 comments on commit c5e175c

Please sign in to comment.