mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 20:40:20 +00:00
Add missing helper middleware types
This commit is contained in:
Vendored
+36
-4
@@ -1,17 +1,47 @@
|
||||
// Type definitions for koa-bunyan-logger 2.0
|
||||
// Type definitions for koa-bunyan-logger 2.1
|
||||
// Project: https://github.com/koajs/bunyan-logger
|
||||
// Definitions by: Steven McDowall <https://github.com/sjmcdowall>
|
||||
// Jan Karlo Dela Cruz <https://github.com/jankdc>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
import { Middleware } from 'koa';
|
||||
import { Middleware, Request, Response } from 'koa';
|
||||
|
||||
import Logger = require('bunyan');
|
||||
|
||||
export = koaBunyanLogger;
|
||||
|
||||
declare function koaBunyanLogger(logger?: Logger): Middleware;
|
||||
|
||||
declare namespace koaBunyanLogger {
|
||||
interface RequestData {
|
||||
req: Request;
|
||||
}
|
||||
|
||||
interface ResponseData {
|
||||
req: Request;
|
||||
res: Response;
|
||||
}
|
||||
|
||||
interface RequestIdContextOptions {
|
||||
header?: string;
|
||||
prop?: string;
|
||||
requestProp?: string;
|
||||
field?: string;
|
||||
}
|
||||
|
||||
interface RequestLoggerOptions {
|
||||
durationField?: string;
|
||||
levelFn?: (status: number, err: Error) => string;
|
||||
updateLogFields?: (data: RequestData) => RequestData;
|
||||
updateRequestLogFields?: (requestData: RequestData) => RequestData;
|
||||
updateResponseLogFields?: (responseData: ResponseData) => ResponseData;
|
||||
formatRequestMessage?: (requestData: RequestData) => string;
|
||||
formatResponseMessage?: (responseData: ResponseData) => string;
|
||||
}
|
||||
|
||||
function requestLogger(opts?: RequestLoggerOptions): Middleware;
|
||||
function requestIdContext(opts?: RequestIdContextOptions): Middleware;
|
||||
}
|
||||
|
||||
// Extend the Koa context to add the logger..
|
||||
declare module 'koa' {
|
||||
// tslint:disable-next-line: interface-name
|
||||
@@ -19,3 +49,5 @@ declare module 'koa' {
|
||||
log: Logger;
|
||||
}
|
||||
}
|
||||
|
||||
export = koaBunyanLogger;
|
||||
|
||||
@@ -3,6 +3,8 @@ import koaBunyanLogger = require('koa-bunyan-logger');
|
||||
|
||||
const app = new Koa();
|
||||
app.use(koaBunyanLogger());
|
||||
app.use(koaBunyanLogger.requestIdContext());
|
||||
app.use(koaBunyanLogger.requestLogger());
|
||||
|
||||
app.use(async (ctx, next) => {
|
||||
ctx.log.info('Got a request from %s for %s', ctx.request.ip, ctx.path);
|
||||
|
||||
Reference in New Issue
Block a user