Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pino-http types #699

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion __tests__/levels.spec.ts
@@ -1,5 +1,5 @@
import { Controller, Get, Logger } from '@nestjs/common';
import * as pino from 'pino';
import pino from 'pino';
import { PinoLogger } from '../src';
import { platforms } from './utils/platforms';
import { TestCase } from './utils/test-case';
Expand Down
2 changes: 1 addition & 1 deletion __tests__/use-existing.spec.ts
Expand Up @@ -6,7 +6,7 @@ import {
Logger,
} from '@nestjs/common';
import MemoryStream = require('memorystream');
import * as pino from 'pino';
import pino from 'pino';
import { FastifyAdapter } from '@nestjs/platform-fastify';
import { TestCase } from './utils/test-case';
import { LogsContainer } from './utils/logs';
Expand Down
8 changes: 4 additions & 4 deletions __tests__/utils/test-case.ts
Expand Up @@ -3,8 +3,8 @@ import { AbstractHttpAdapter, NestFactory } from '@nestjs/core';
import { Module, ModuleMetadata, Type } from '@nestjs/common';
import MemoryStream = require('memorystream');
import * as request from 'supertest';
import * as pinoHttp from 'pino-http';
import * as pino from 'pino';
import { Options } from 'pino-http';
import pino from 'pino';
import {
Logger,
LoggerModule,
Expand Down Expand Up @@ -112,15 +112,15 @@ export class TestCase {
return {
...params,
pinoHttp: [
(params!.pinoHttp as [pinoHttp.Options, pino.DestinationStream])[0],
(params!.pinoHttp as [Options, pino.DestinationStream])[0],
this.stream,
],
};
case !!params!.pinoHttp:
return {
...params,
pinoHttp: {
...(params!.pinoHttp as pinoHttp.Options),
...(params!.pinoHttp as Options),
stream: this.stream,
},
};
Expand Down
4,305 changes: 1,940 additions & 2,365 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -47,7 +47,6 @@
"@types/jest": "^27.0.1",
"@types/memorystream": "^0.3.0",
"@types/node": "^16.0.0",
"@types/pino-http": "^5.4.3",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^5.3.1",
"@typescript-eslint/parser": "^5.3.1",
Expand All @@ -57,7 +56,8 @@
"eslint-plugin-prettier": "^4.0.0",
"jest": "27.4.3",
"memorystream": "^0.3.1",
"pino-http": "^6.0.0",
"pino": "^7.5.1",
"pino-http": "github:pinojs/pino-http#master",
"prettier": "^2.3.2",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
Expand Down Expand Up @@ -86,6 +86,7 @@
"testEnvironment": "node"
},
"peerDependencies": {
"pino-http": "^6.0.0"
"pino": "^7.5.1",
"pino-http": "github:pinojs/pino-http#master"
}
}
6 changes: 3 additions & 3 deletions src/Logger.ts
@@ -1,5 +1,5 @@
import { Injectable, LoggerService, Inject } from '@nestjs/common';
import { Level } from 'pino';
import pino from 'pino';
import { PinoLogger } from './PinoLogger';
import { Params, PARAMS_PROVIDER_TOKEN } from './params';

Expand Down Expand Up @@ -34,7 +34,7 @@ export class Logger implements LoggerService {
this.call('error', message, ...optionalParams);
}

private call(level: Level, message: any, ...optionalParams: any[]) {
private call(level: pino.Level, message: any, ...optionalParams: any[]) {
const objArg: Record<string, any> = {};

// optionalParams contains extra params passed to logger
Expand Down Expand Up @@ -80,7 +80,7 @@ export class Logger implements LoggerService {
* @see https://github.com/search?l=TypeScript&q=org%3Anestjs+logger+error+stack&type=Code
*/
private isWrongExceptionsHandlerContract(
level: Level,
level: pino.Level,
message: any,
params: any[],
): params is [string] {
Expand Down
2 changes: 1 addition & 1 deletion src/LoggerModule.ts
Expand Up @@ -9,7 +9,7 @@ import {
} from '@nestjs/common';
import { Provider } from '@nestjs/common/interfaces';
import * as express from 'express';
import * as pinoHttp from 'pino-http';
import pinoHttp from 'pino-http';
import { Logger } from './Logger';
import {
Params,
Expand Down
16 changes: 8 additions & 8 deletions src/PinoLogger.ts
@@ -1,11 +1,11 @@
/* eslint-disable @typescript-eslint/ban-types */
import { Injectable, Inject, Scope } from '@nestjs/common';
import * as pino from 'pino';
import pino from 'pino';
import { Params, isPassedLogger, PARAMS_PROVIDER_TOKEN } from './params';
import { storage } from './storage';

type PinoMethods = Pick<
pino.BaseLogger,
pino.Logger,
'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal'
>;

Expand Down Expand Up @@ -69,37 +69,37 @@ export class PinoLogger implements PinoMethods {
}

trace(msg: string, ...args: any[]): void;
trace(obj: object, msg?: string, ...args: any[]): void;
trace(obj: unknown, msg?: string, ...args: any[]): void;
trace(...args: Parameters<LoggerFn>) {
this.call('trace', ...args);
}

debug(msg: string, ...args: any[]): void;
debug(obj: object, msg?: string, ...args: any[]): void;
debug(obj: unknown, msg?: string, ...args: any[]): void;
debug(...args: Parameters<LoggerFn>) {
this.call('debug', ...args);
}

info(msg: string, ...args: any[]): void;
info(obj: object, msg?: string, ...args: any[]): void;
info(obj: unknown, msg?: string, ...args: any[]): void;
info(...args: Parameters<LoggerFn>) {
this.call('info', ...args);
}

warn(msg: string, ...args: any[]): void;
warn(obj: object, msg?: string, ...args: any[]): void;
warn(obj: unknown, msg?: string, ...args: any[]): void;
warn(...args: Parameters<LoggerFn>) {
this.call('warn', ...args);
}

error(msg: string, ...args: any[]): void;
error(obj: object, msg?: string, ...args: any[]): void;
error(obj: unknown, msg?: string, ...args: any[]): void;
error(...args: Parameters<LoggerFn>) {
this.call('error', ...args);
}

fatal(msg: string, ...args: any[]): void;
fatal(obj: object, msg?: string, ...args: any[]): void;
fatal(obj: unknown, msg?: string, ...args: any[]): void;
fatal(...args: Parameters<LoggerFn>) {
this.call('fatal', ...args);
}
Expand Down
14 changes: 7 additions & 7 deletions src/params.ts
@@ -1,22 +1,22 @@
import * as pinoHttp from 'pino-http';
import * as pino from 'pino';
import { DestinationStream } from 'pino';
import { Options } from 'pino-http';
import pino from 'pino';
import {
MiddlewareConfigProxy,
ModuleMetadata,
} from '@nestjs/common/interfaces';

export type PassedLogger = { logger: pino.Logger };
export type PinoHttpOptions =
| Options
| pino.DestinationStream
| [Options, pino.DestinationStream];

export interface Params {
/**
* Optional parameters for `pino-http` module
* @see https://github.com/pinojs/pino-http#pinohttpopts-stream
*/
pinoHttp?:
| pinoHttp.Options
| DestinationStream
| [pinoHttp.Options, DestinationStream];
pinoHttp?: PinoHttpOptions;

/**
* Optional parameter for routing. It should implement interface of
Expand Down
4 changes: 2 additions & 2 deletions src/storage.ts
@@ -1,8 +1,8 @@
import { AsyncLocalStorage } from 'async_hooks';
import { Logger } from 'pino';
import pino from 'pino';

export class Store {
constructor(public logger: Logger) {}
constructor(public logger: pino.Logger) {}
}

export const storage = new AsyncLocalStorage<Store>();