@backstage/backend-common
Advanced tools
Comparing version 0.1.1-alpha.4 to 0.1.1-alpha.5
@@ -0,2 +1,3 @@ | ||
export * from './errors'; | ||
export * from './logging'; | ||
export * from './middleware'; |
@@ -17,8 +17,16 @@ "use strict"; | ||
*/ | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./logging")); | ||
__export(require("./middleware")); | ||
__exportStar(require("./errors"), exports); | ||
__exportStar(require("./logging"), exports); | ||
__exportStar(require("./middleware"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -17,7 +17,14 @@ "use strict"; | ||
*/ | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./rootLogger")); | ||
__exportStar(require("./rootLogger"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -21,4 +21,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var winston_1 = __importDefault(require("winston")); | ||
var rootLogger = winston_1.default.createLogger({ | ||
exports.setRootLogger = exports.getRootLogger = void 0; | ||
const winston_1 = __importDefault(require("winston")); | ||
let rootLogger = winston_1.default.createLogger({ | ||
level: process.env.LOG_LEVEL || 'info', | ||
@@ -25,0 +26,0 @@ format: process.env.NODE_ENV === 'production' |
@@ -21,13 +21,8 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var stream_1 = require("stream"); | ||
var winston_1 = __importDefault(require("winston")); | ||
var rootLogger_1 = require("./rootLogger"); | ||
describe('rootLogger', function () { | ||
it('can replace the default logger', function () { | ||
var logger = winston_1.default.createLogger({ | ||
transports: [ | ||
new winston_1.default.transports.Stream({ stream: new stream_1.PassThrough() }), | ||
], | ||
}); | ||
jest.spyOn(logger, 'info'); | ||
const winston_1 = __importDefault(require("winston")); | ||
const rootLogger_1 = require("./rootLogger"); | ||
describe('rootLogger', () => { | ||
it('can replace the default logger', () => { | ||
const logger = winston_1.default.createLogger(); | ||
jest.spyOn(logger, 'info').mockReturnValue(logger); | ||
rootLogger_1.setRootLogger(logger); | ||
@@ -34,0 +29,0 @@ rootLogger_1.getRootLogger().info('testing'); |
import { ErrorRequestHandler } from 'express'; | ||
export declare type ErrorHandlerOptions = { | ||
/** | ||
* Whether error response bodies should show error stack traces or not. | ||
* | ||
* If not specified, by default shows stack traces only in development mode. | ||
*/ | ||
showStackTraces?: boolean; | ||
}; | ||
/** | ||
* Express middleware to handle errors during request processing. | ||
* | ||
* This is commonly the second to last middleware in the chain (before the | ||
* notFoundHandler). | ||
* This is commonly the very last middleware in the chain. | ||
* | ||
@@ -16,2 +23,2 @@ * Its primary purpose is not to do translation of business logic exceptions, | ||
*/ | ||
export declare function errorHandler(): ErrorRequestHandler; | ||
export declare function errorHandler(options?: ErrorHandlerOptions): ErrorRequestHandler; |
@@ -17,8 +17,28 @@ "use strict"; | ||
*/ | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.errorHandler = void 0; | ||
const errors = __importStar(require("../errors")); | ||
/** | ||
* Express middleware to handle errors during request processing. | ||
* | ||
* This is commonly the second to last middleware in the chain (before the | ||
* notFoundHandler). | ||
* This is commonly the very last middleware in the chain. | ||
* | ||
@@ -33,7 +53,12 @@ * Its primary purpose is not to do translation of business logic exceptions, | ||
*/ | ||
function errorHandler() { | ||
function errorHandler(options = {}) { | ||
var _a; | ||
const showStackTraces = (_a = options.showStackTraces) !== null && _a !== void 0 ? _a : process.env.NODE_ENV === 'development'; | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
return function (error, _request, response, _next) { | ||
var status = getStatusCode(error); | ||
var message = error.message; | ||
return (error, _request, response, next) => { | ||
if (response.headersSent) { | ||
next(error); | ||
} | ||
const status = getStatusCode(error); | ||
const message = showStackTraces ? error.stack : error.message; | ||
response.status(status).send(message); | ||
@@ -44,6 +69,6 @@ }; | ||
function getStatusCode(error) { | ||
var knownStatusCodeFields = ['statusCode', 'status']; | ||
for (var _i = 0, knownStatusCodeFields_1 = knownStatusCodeFields; _i < knownStatusCodeFields_1.length; _i++) { | ||
var field = knownStatusCodeFields_1[_i]; | ||
var statusCode = error[field]; | ||
// Look for common http library status codes | ||
const knownStatusCodeFields = ['statusCode', 'status']; | ||
for (const field of knownStatusCodeFields) { | ||
const statusCode = error[field]; | ||
if (typeof statusCode === 'number' && | ||
@@ -56,4 +81,20 @@ (statusCode | 0) === statusCode && // is whole integer | ||
} | ||
// Handle well-known error types | ||
switch (error.name) { | ||
case errors.InputError.name: | ||
return 400; | ||
case errors.AuthenticationError.name: | ||
return 401; | ||
case errors.NotAllowedError.name: | ||
return 403; | ||
case errors.NotFoundError.name: | ||
return 404; | ||
case errors.ConflictError.name: | ||
return 409; | ||
default: | ||
break; | ||
} | ||
// Fall back to internal server error | ||
return 500; | ||
} | ||
//# sourceMappingURL=errorHandler.js.map |
@@ -17,38 +17,21 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -58,46 +41,54 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = __importDefault(require("express")); | ||
var http_errors_1 = __importDefault(require("http-errors")); | ||
var supertest_1 = __importDefault(require("supertest")); | ||
var errorHandler_1 = require("./errorHandler"); | ||
describe('errorHandler', function () { | ||
it('gives default code and message', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var app, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
app = express_1.default(); | ||
app.use('/breaks', function () { | ||
throw new Error('some message'); | ||
}); | ||
app.use(errorHandler_1.errorHandler()); | ||
return [4 /*yield*/, supertest_1.default(app).get('/breaks')]; | ||
case 1: | ||
response = _a.sent(); | ||
expect(response.status).toBe(500); | ||
expect(response.text).toBe('some message'); | ||
return [2 /*return*/]; | ||
} | ||
const express_1 = __importDefault(require("express")); | ||
const http_errors_1 = __importDefault(require("http-errors")); | ||
const supertest_1 = __importDefault(require("supertest")); | ||
const errors = __importStar(require("../errors")); | ||
const errorHandler_1 = require("./errorHandler"); | ||
describe('errorHandler', () => { | ||
it('gives default code and message', async () => { | ||
const app = express_1.default(); | ||
app.use('/breaks', () => { | ||
throw new Error('some message'); | ||
}); | ||
}); }); | ||
it('takes code from StatusCodeError', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var app, response; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
app = express_1.default(); | ||
app.use('/breaks', function () { | ||
throw http_errors_1.default(432, 'Some Message'); | ||
}); | ||
app.use(errorHandler_1.errorHandler()); | ||
return [4 /*yield*/, supertest_1.default(app).get('/breaks')]; | ||
case 1: | ||
response = _a.sent(); | ||
expect(response.status).toBe(432); | ||
expect(response.text).toContain('Some Message'); | ||
return [2 /*return*/]; | ||
} | ||
app.use(errorHandler_1.errorHandler()); | ||
const response = await supertest_1.default(app).get('/breaks'); | ||
expect(response.status).toBe(500); | ||
expect(response.text).toBe('some message'); | ||
}); | ||
it('takes code from http-errors library errors', async () => { | ||
const app = express_1.default(); | ||
app.use('/breaks', () => { | ||
throw http_errors_1.default(432, 'Some Message'); | ||
}); | ||
}); }); | ||
app.use(errorHandler_1.errorHandler()); | ||
const response = await supertest_1.default(app).get('/breaks'); | ||
expect(response.status).toBe(432); | ||
expect(response.text).toContain('Some Message'); | ||
}); | ||
it('handles well-known error classes', async () => { | ||
const app = express_1.default(); | ||
app.use('/InputError', () => { | ||
throw new errors.InputError(); | ||
}); | ||
app.use('/AuthenticationError', () => { | ||
throw new errors.AuthenticationError(); | ||
}); | ||
app.use('/NotAllowedError', () => { | ||
throw new errors.NotAllowedError(); | ||
}); | ||
app.use('/NotFoundError', () => { | ||
throw new errors.NotFoundError(); | ||
}); | ||
app.use('/ConflictError', () => { | ||
throw new errors.ConflictError(); | ||
}); | ||
app.use(errorHandler_1.errorHandler()); | ||
const r = supertest_1.default(app); | ||
expect((await r.get('/InputError')).status).toBe(400); | ||
expect((await r.get('/AuthenticationError')).status).toBe(401); | ||
expect((await r.get('/NotAllowedError')).status).toBe(403); | ||
expect((await r.get('/NotFoundError')).status).toBe(404); | ||
expect((await r.get('/ConflictError')).status).toBe(409); | ||
}); | ||
}); | ||
//# sourceMappingURL=errorHandler.test.js.map |
@@ -17,9 +17,16 @@ "use strict"; | ||
*/ | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./errorHandler")); | ||
__export(require("./notFoundHandler")); | ||
__export(require("./requestLoggingHandler")); | ||
__exportStar(require("./errorHandler"), exports); | ||
__exportStar(require("./notFoundHandler"), exports); | ||
__exportStar(require("./requestLoggingHandler"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -18,2 +18,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.notFoundHandler = void 0; | ||
/** | ||
@@ -29,3 +30,3 @@ * Express middleware to handle requests for missing routes. | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
return function (_request, response, _next) { | ||
return (_request, response, _next) => { | ||
response.status(404).send(); | ||
@@ -32,0 +33,0 @@ }; |
@@ -17,38 +17,2 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -58,27 +22,16 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = __importDefault(require("express")); | ||
var supertest_1 = __importDefault(require("supertest")); | ||
var notFoundHandler_1 = require("./notFoundHandler"); | ||
describe('notFoundHandler', function () { | ||
it('handles only missing routes', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var app, existsResponse, doesNotExistResponse; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
app = express_1.default(); | ||
app.use('/exists', function (_, res) { return res.status(200).send(); }); | ||
app.use(notFoundHandler_1.notFoundHandler()); | ||
return [4 /*yield*/, supertest_1.default(app).get('/exists')]; | ||
case 1: | ||
existsResponse = _a.sent(); | ||
return [4 /*yield*/, supertest_1.default(app).get('/doesNotExist')]; | ||
case 2: | ||
doesNotExistResponse = _a.sent(); | ||
expect(existsResponse.status).toBe(200); | ||
expect(doesNotExistResponse.status).toBe(404); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
const express_1 = __importDefault(require("express")); | ||
const supertest_1 = __importDefault(require("supertest")); | ||
const notFoundHandler_1 = require("./notFoundHandler"); | ||
describe('notFoundHandler', () => { | ||
it('handles only missing routes', async () => { | ||
const app = express_1.default(); | ||
app.use('/exists', (_, res) => res.status(200).send()); | ||
app.use(notFoundHandler_1.notFoundHandler()); | ||
const existsResponse = await supertest_1.default(app).get('/exists'); | ||
const doesNotExistResponse = await supertest_1.default(app).get('/doesNotExist'); | ||
expect(existsResponse.status).toBe(200); | ||
expect(doesNotExistResponse.status).toBe(404); | ||
}); | ||
}); | ||
//# sourceMappingURL=notFoundHandler.test.js.map |
@@ -21,4 +21,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var morgan_1 = __importDefault(require("morgan")); | ||
var logging_1 = require("../logging"); | ||
exports.requestLoggingHandler = void 0; | ||
const morgan_1 = __importDefault(require("morgan")); | ||
const logging_1 = require("../logging"); | ||
/** | ||
@@ -31,3 +32,3 @@ * Logs incoming requests. | ||
function requestLoggingHandler(logger) { | ||
var actualLogger = (logger || logging_1.getRootLogger()).child({ | ||
const actualLogger = (logger || logging_1.getRootLogger()).child({ | ||
type: 'incomingRequest', | ||
@@ -37,3 +38,3 @@ }); | ||
stream: { | ||
write: function (message) { | ||
write(message) { | ||
actualLogger.info(message); | ||
@@ -40,0 +41,0 @@ }, |
@@ -17,38 +17,2 @@ "use strict"; | ||
*/ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -58,38 +22,22 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var express_1 = __importDefault(require("express")); | ||
var stream_1 = require("stream"); | ||
var supertest_1 = __importDefault(require("supertest")); | ||
var winston_1 = __importDefault(require("winston")); | ||
var requestLoggingHandler_1 = require("./requestLoggingHandler"); | ||
describe('requestLoggingHandler', function () { | ||
it('emits logs for each request', function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var logger, app, r; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
logger = winston_1.default.createLogger({ | ||
transports: [ | ||
new winston_1.default.transports.Stream({ stream: new stream_1.PassThrough() }), | ||
], | ||
}); | ||
jest.spyOn(logger, 'info'); | ||
app = express_1.default(); | ||
app.use(requestLoggingHandler_1.requestLoggingHandler(logger)); | ||
app.use('/exists1', function (_, res) { return res.status(200).send(); }); | ||
app.use('/exists2', function (_, res) { return res.status(201).send(); }); | ||
r = supertest_1.default(app); | ||
return [4 /*yield*/, r.get('/exists1')]; | ||
case 1: | ||
_a.sent(); | ||
return [4 /*yield*/, r.get('/exists2')]; | ||
case 2: | ||
_a.sent(); | ||
expect(logger.info).toHaveBeenCalledTimes(2); | ||
expect(logger.info).toHaveBeenNthCalledWith(1, expect.stringContaining('200')); | ||
expect(logger.info).toHaveBeenNthCalledWith(2, expect.stringContaining('201')); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
const express_1 = __importDefault(require("express")); | ||
const supertest_1 = __importDefault(require("supertest")); | ||
const winston_1 = __importDefault(require("winston")); | ||
const requestLoggingHandler_1 = require("./requestLoggingHandler"); | ||
describe('requestLoggingHandler', () => { | ||
it('emits logs for each request', async () => { | ||
const logger = winston_1.default.createLogger(); | ||
jest.spyOn(logger, 'info').mockReturnValue(logger); | ||
const app = express_1.default(); | ||
app.use(requestLoggingHandler_1.requestLoggingHandler(logger)); | ||
app.use('/exists1', (_, res) => res.status(200).send()); | ||
app.use('/exists2', (_, res) => res.status(201).send()); | ||
const r = supertest_1.default(app); | ||
await r.get('/exists1'); | ||
await r.get('/exists2'); | ||
expect(logger.info).toHaveBeenCalledTimes(2); | ||
expect(logger.info).toHaveBeenNthCalledWith(1, expect.stringContaining('200')); | ||
expect(logger.info).toHaveBeenNthCalledWith(2, expect.stringContaining('201')); | ||
}); | ||
}); | ||
//# sourceMappingURL=requestLoggingHandler.test.js.map |
{ | ||
"name": "@backstage/backend-common", | ||
"description": "Common functionality library for Backstage backends", | ||
"version": "0.1.1-alpha.4", | ||
"version": "0.1.1-alpha.5", | ||
"main": "dist", | ||
@@ -32,3 +32,3 @@ "private": false, | ||
"devDependencies": { | ||
"@backstage/cli": "^0.1.1-alpha.4", | ||
"@backstage/cli": "^0.1.1-alpha.5", | ||
"@types/express": "^4.17.6", | ||
@@ -38,2 +38,3 @@ "@types/http-errors": "^1.6.3", | ||
"@types/supertest": "^2.0.8", | ||
"@types/yaml": "^1.9.7", | ||
"get-port": "^5.1.1", | ||
@@ -44,3 +45,3 @@ "http-errors": "^1.7.3", | ||
"supertest": "^4.0.2", | ||
"typescript": "^3.8.3" | ||
"typescript": "^3.9.2" | ||
}, | ||
@@ -50,3 +51,3 @@ "files": [ | ||
], | ||
"gitHead": "da7522b671762590b38a9ecf81718c12db220393" | ||
"gitHead": "114ef404c242bfafe3aa3e0a64472b2edab5975b" | ||
} |
@@ -27,4 +27,4 @@ # @backstage/backend-common | ||
app.use('/home', myHomeRouter); | ||
app.use(notFoundHandler()); | ||
app.use(errorHandler()); | ||
app.use(notFoundHandler()); | ||
@@ -31,0 +31,0 @@ app.listen(PORT, () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
184074
47
740
12
5