Skip to content

Commit

Permalink
Pino 7 Type Definition (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xslipk authored Nov 22, 2021
1 parent 646d3b9 commit 279215e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
69 changes: 69 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Type definitions for pino-http 6.0
// Project: https://github.com/pinojs/pino-http#readme
// Definitions by: Christian Rackerseder <https://github.com/screendriver>
// Jeremy Forsythe <https://github.com/jdforsythe>
// Griffin Yourick <https://github.com/tough-griff>
// Jorge Barnaby <https://github.com/yorch>
// Jose Ramirez <https://github.com/jarcodallo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 4.0
/// <reference types="node"/>

import { IncomingMessage, ServerResponse } from 'http';
import pino from 'pino';
import { SerializedError, SerializedRequest, SerializedResponse } from 'pino-std-serializers';

declare function pinoHttp(opts?: Options, stream?: pino.DestinationStream): HttpLogger;

declare function pinoHttp(stream?: pino.DestinationStream): HttpLogger;

interface HttpLogger {
(req: IncomingMessage, res: ServerResponse, next?: () => void): void;
logger: pino.Logger;
}
type ReqId = number | string | object;

interface Options extends pino.LoggerOptions {
logger?: pino.Logger | undefined;
genReqId?: GenReqId | undefined;
useLevel?: pino.Level | undefined;
stream?: pino.DestinationStream | undefined;
autoLogging?: boolean | AutoLoggingOptions | undefined;
customLogLevel?: ((res: ServerResponse, error: Error) => pino.Level) | undefined;
customSuccessMessage?: ((res: ServerResponse) => string) | undefined;
customErrorMessage?: ((error: Error, res: ServerResponse) => string) | undefined;
customAttributeKeys?: CustomAttributeKeys | undefined;
wrapSerializers?: boolean | undefined;
reqCustomProps?: ((req: IncomingMessage, res: ServerResponse) => object) | undefined;
quietReqLogger?: boolean | undefined;
}

interface GenReqId {
(req: IncomingMessage): ReqId;
}

interface AutoLoggingOptions {
ignore?: ((req: IncomingMessage) => boolean);
ignorePaths?: Array<string | RegExp> | undefined;
getPath?: ((req: IncomingMessage) => string | undefined) | undefined;
}

interface CustomAttributeKeys {
req?: string | undefined;
res?: string | undefined;
err?: string | undefined;
reqId?: string | undefined;
responseTime?: string | undefined;
}

interface StdSerializers {
err: SerializedError;
req: SerializedRequest;
res: SerializedResponse;
}

export default pinoHttp;

export const startTime: unique symbol;

export const stdSerializers: StdSerializers;
35 changes: 35 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

/// <reference path="./index.d.ts"/>

import { Writable } from 'stream';
import { IncomingMessage, ServerResponse } from 'http';
import pino from 'pino';
import pinoHttp from './';

const logger = pino();

pinoHttp({ logger });
pinoHttp({ logger }).logger = logger;
pinoHttp({ genReqId: (req: IncomingMessage) => req.statusCode || 200 });
pinoHttp({ genReqId: (req: IncomingMessage) => 'foo' });
pinoHttp({ genReqId: (req: IncomingMessage) => Buffer.allocUnsafe(16) });
pinoHttp({ useLevel: 'error' });
pinoHttp({ prettyPrint: true }); // deprecated but still present in pino.
pinoHttp({ transport: { target: 'pino-pretty', options: { colorize: true } } });
pinoHttp({ autoLogging: false });
pinoHttp({ autoLogging: { ignore: (req: IncomingMessage) => req.headers['user-agent'] === 'ELB-HealthChecker/2.0' } });
pinoHttp({ autoLogging: { ignorePaths: ['/health'] } });
pinoHttp({ autoLogging: { ignorePaths: [/\/health/] } });
pinoHttp({ autoLogging: { ignorePaths: ['/health'], getPath: (req: IncomingMessage) => req.url } });
pinoHttp({ customSuccessMessage: (req: ServerResponse) => 'Success' });
pinoHttp({ customErrorMessage: (error: Error, res: ServerResponse) => `Error - ${error}` });
pinoHttp({ customAttributeKeys: { req: 'req' } });
pinoHttp({ customAttributeKeys: { res: 'res' } });
pinoHttp({ customAttributeKeys: { err: 'err' } });
pinoHttp({ customAttributeKeys: { responseTime: 'responseTime' } });
pinoHttp({ customAttributeKeys: { req: 'req', res: 'res', err: 'err', responseTime: 'responseTime' } });
pinoHttp({ customLogLevel: (res: ServerResponse, error: Error) => 'info' });
pinoHttp({ reqCustomProps: (req: IncomingMessage, res: ServerResponse) => ({ key1: 'value1', 'x-key-2': 'value2' }) });
pinoHttp({ wrapSerializers: false });
pinoHttp(new Writable());
pinoHttp({ quietReqLogger: true, customAttributeKeys: { reqId: 'reqId' }});
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
"version": "6.0.0",
"description": "High-speed HTTP logger for Node.js",
"main": "logger.js",
"types": "index.d.ts",
"dependencies": {
"fast-url-parser": "^1.1.3",
"get-caller-file": "^2.0.5",
"pino": "^7.0.5",
"pino-std-serializers": "^4.0.0"
},
"devDependencies": {
"@types/node": "^16.11.0",
"autocannon": "^7.3.0",
"coveralls": "^3.0.0",
"http-ndjson": "^3.1.0",
"pino-pretty": "^7.1.0",
"pre-commit": "^1.1.2",
"split2": "^4.0.0",
"standard": "^16.0.3",
"tap": "^15.0.0"
"tap": "^15.0.0",
"ts-node": "^10.3.0",
"tsd": "^0.18.0",
"typescript": "^4.4.4"
},
"scripts": {
"benchmark": "bash ./scripts/benchmark-all",
"test": "standard && tap test.js",
"fix": "standard --fix"
"test": "standard && tap test.js && npm run test-types",
"fix": "standard --fix",
"test-types": "tsc && tsd && ts-node index.test-d.ts"
},
"author": "David Mark Clements",
"contributors": [
Expand Down
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es6",
"esModuleInterop": true,
"lib": [ "es2015" ],
"module": "commonjs",
"noEmit": true,
"strict": true,
"moduleResolution": "node",
},
"include": [
"./*.test-d.ts",
"./*.d.ts"
]
}

0 comments on commit 279215e

Please sign in to comment.