Skip to content

Commit

Permalink
Add type declarations (#148)
Browse files Browse the repository at this point in the history
* Add type declarations

* Add types field in package.json

* Remove DefinitelyTyped url

* Add new options

* Adds test for types

* No need to redeclare module

* Revert "No need to redeclare module"

This reverts commit 4462d99.

* Include types test in CI

Co-authored-by: Luke Grunau <lukegrunau@Lukes-MBP.fritz.box>
Co-authored-by: Luke Grunau <lukegrunau@lukes-mbp.speedport.ip>
  • Loading branch information
3 people committed Dec 13, 2021
1 parent 8240968 commit b960b85
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
62 changes: 62 additions & 0 deletions index.d.ts
@@ -0,0 +1,62 @@
// Type definitions for hapi-pino 9.0
// Definitions by: Rodrigo Saboya <https://github.com/saboya>
// Todd Bealmear <https://github.com/todd>
// Matt Jeanes <https://github.com/BlooJeans>
// Kyle Gray <https://github.com/GoPro16>
// TypeScript Version: 2.8

/// <reference types='node' />

import type { pino } from 'pino';

import { Plugin, Request } from '@hapi/hapi';

declare module '@hapi/hapi' {
interface Server {
logger: pino.Logger;
}

interface Request {
logger: pino.Logger;
}
}

declare namespace HapiPino {
interface Serializers {
[key: string]: pino.SerializerFn;
}

interface Options {
timestamp?: boolean | (() => string) | undefined;
logQueryParams?: boolean | undefined;
logPayload?: boolean | undefined;
logRouteTags?: boolean | undefined;
logRequestStart?: boolean | ((req: Request) => boolean) | undefined;
logRequestComplete?: boolean | ((req: Request) => boolean) | undefined;
stream?: NodeJS.WriteStream | undefined;
prettyPrint?: boolean | pino.PrettyOptions | undefined;
tags?: { [key in pino.Level]?: string } | undefined;
allTags?: pino.Level | undefined;
serializers?: Serializers | undefined;
getChildBindings?:
| ((req: Request) => {
level?: pino.Level | string | undefined;
serializers?: Serializers | undefined;
[key: string]: any;
})
| undefined;
instance?: pino.Logger | undefined;
logEvents?: string[] | false | null | undefined;
mergeHapiLogData?: boolean | undefined;
ignorePaths?: string[] | undefined;
level?: pino.Level | undefined;
redact?: string[] | pino.redactOptions | undefined;
ignoreTags?: string[] | undefined;
ignoreFunc?: ((options: Options, request: Request) => boolean) | undefined;
ignoredEventTags?: object[] | undefined;
}
}

declare var HapiPino: Plugin<HapiPino.Options>;

export = HapiPino;
45 changes: 45 additions & 0 deletions index.test-d.ts
@@ -0,0 +1,45 @@
import { Request, Server } from '@hapi/hapi';
import pino from 'pino';
import * as HapiPino from '.';
import { expectType } from 'tsd';

const pinoLogger = pino();

const server = new Server();

const options: HapiPino.Options = {
timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`,
logQueryParams: false,
logPayload: false,
logRouteTags: false,
logRequestStart: false,
logRequestComplete: true,
stream: process.stdout,
prettyPrint: process.env.NODE_ENV !== 'PRODUCTION',
tags: {
trace: 'trace',
debug: 'debug',
info: 'info',
warn: 'warn',
error: 'error',
fatal: 'fatal',
},
allTags: 'info',
serializers: {
req: (req: any) => console.log(req),
},
getChildBindings: (req: Request) => ({
'x-request-id': req.headers['x-request-id'],
}),
instance: pinoLogger,
logEvents: false,
mergeHapiLogData: false,
ignorePaths: ['/testRoute'],
level: 'debug',
redact: ['test.property'],
ignoreTags: ['healthcheck'],
ignoreFunc: (options, request) => request.path.startsWith('/static'),
ignoredEventTags: [{ log: ['DEBUG', 'TEST'], request: ['DEBUG', 'TEST'] }],
};

expectType<Promise<void>>(server.register({ plugin: HapiPino, options }));
7 changes: 5 additions & 2 deletions package.json
Expand Up @@ -3,11 +3,12 @@
"version": "9.0.0",
"description": "Hapi plugin for the Pino logger ",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"coverage": "lab test.js -c",
"coverage:lcov": "lab test.js -r lcov -o coverage/lcov.info",
"coveralls": "lab test.js -r lcov | coveralls",
"test": "standard && lab --timeout 100000 test.js"
"test": "standard && lab --timeout 100000 test.js && tsd"
},
"keywords": [
"hapi",
Expand All @@ -28,10 +29,12 @@
"pino-pretty": "^7.2.0",
"pre-commit": "^1.2.2",
"split2": "^3.1.1",
"standard": "^14.3.3"
"standard": "^14.3.3",
"tsd": "^0.18.0"
},
"dependencies": {
"@hapi/hoek": "^9.0.0",
"@types/hapi__hapi": "^20.0.9",
"abstract-logging": "^2.0.0",
"get-caller-file": "^2.0.5",
"pino": "^7.0.0"
Expand Down

0 comments on commit b960b85

Please sign in to comment.