Sentry plugin refactoring - code review changes

This commit is contained in:
Wojciech Adaszynski
2020-05-20 09:57:32 +02:00
parent 0783e07290
commit baf8188db7
17 changed files with 73 additions and 85 deletions
+5 -5
View File
@@ -1,6 +1,6 @@
{
"name": "@backstage/plugin-sentry-backend",
"version": "0.1.1-alpha.5",
"version": "0.1.1-alpha.6",
"main": "dist",
"types": "src/index.ts",
"license": "Apache-2.0",
@@ -13,8 +13,8 @@
"clean": "backstage-cli clean"
},
"dependencies": {
"@backstage/backend-common": "^0.1.1-alpha.5",
"@backstage/core": "^0.1.1-alpha.5",
"@backstage/backend-common": "^0.1.1-alpha.6",
"@backstage/core": "^0.1.1-alpha.6",
"axios": "^0.19.2",
"cors": "^2.8.5",
"express": "^4.17.1",
@@ -23,8 +23,8 @@
"winston": "^3.2.1"
},
"devDependencies": {
"@backstage/cli": "^0.1.1-alpha.5",
"@backstage/dev-utils": "^0.1.1-alpha.5",
"@backstage/cli": "^0.1.1-alpha.6",
"@backstage/dev-utils": "^0.1.1-alpha.6",
"@types/jest": "^25.2.1",
"@types/node": "^12.0.0"
},
+8 -14
View File
@@ -16,25 +16,19 @@
import { Logger } from 'winston';
import Router from 'express-promise-router';
import express from 'express';
import { SentryApiForwarder } from './sentry-api';
import { getSentryApiForwarder } from './sentry-api';
export async function createRouter(
rootLogger: Logger,
): Promise<express.Router> {
export async function createRouter(logger: Logger): Promise<express.Router> {
const router = Router();
const SENTRY_TOKEN = process.env.SENTRY_TOKEN;
if (!SENTRY_TOKEN) {
console.error('Sentry token must be provided in env to start the API.');
process.exit(1);
throw new Error(
'Sentry token must be provided in SENTRY_TOKEN environment variable to start the API.',
);
}
const sentryForwarder = new SentryApiForwarder(SENTRY_TOKEN);
const logger = rootLogger.child({ plugin: 'sentry' });
const sentryForwarder = getSentryApiForwarder(SENTRY_TOKEN, logger);
router.get('*', (req, res) => sentryForwarder.fowardRequest(req, res));
router.use(sentryForwarder);
const app = express();
app.set('logger', logger);
app.use('/', router);
return app;
return router;
}
@@ -13,13 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SentryApiForwarder } from './sentry-api';
import { getRequestHeaders } from './sentry-api';
describe('SentryApiForwarder', () => {
it('should generate headers based on token passed in constructor', () => {
const forwarder = new SentryApiForwarder('testtoken');
expect(forwarder.getRequestHeaders()).toEqual({
expect(getRequestHeaders('testtoken')).toEqual({
headers: {
Authorization: `Bearer testtoken`,
},
@@ -15,29 +15,33 @@
*/
import express from 'express';
import axios from 'axios';
import { Logger } from 'winston';
export class SentryApiForwarder {
constructor(private token: string) {}
export function getRequestHeaders(token: string) {
return {
headers: {
Authorization: `Bearer ${token}`,
},
};
}
// public for testing
public getRequestHeaders() {
return {
headers: {
Authorization: `Bearer ${this.token}`,
},
};
}
public fowardRequest(request: express.Request, response: express.Response) {
export function getSentryApiForwarder(token: string, logger: Logger) {
return function fowardRequest(
request: express.Request,
response: express.Response,
) {
const sentryUrl = request.path;
const effectiveUrl = `https://sentry.io/${sentryUrl}`;
logger.info(`Calling Sentry REST API, ${effectiveUrl}`);
axios
.get(`https://sentry.io/${sentryUrl}`, this.getRequestHeaders())
.then((res) => {
.get(effectiveUrl, getRequestHeaders(token))
.then(res => {
response.send(res.data);
})
.catch((err) => {
.catch(err => {
return response.status(err.response.status).json({
detail: err.response.statusText,
});
});
}
};
}
-11
View File
@@ -1,11 +0,0 @@
{
"extends": "../../packages/backend/tsconfig.json",
"include": [
"./src"
],
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./dist",
"skipLibCheck": true
}
}