From e393b914021a0818a1b92f834bc9313e3d40cb6b Mon Sep 17 00:00:00 2001 From: KaKa Date: Tue, 30 Nov 2021 20:56:53 +0800 Subject: [PATCH 1/3] fix: type regression on fastify --- pino.d.ts | 183 ++++++++++++++++++++------------------ test/types/pino.test-d.ts | 22 ++++- 2 files changed, 116 insertions(+), 89 deletions(-) diff --git a/pino.d.ts b/pino.d.ts index 4198fa373..155e46dd6 100644 --- a/pino.d.ts +++ b/pino.d.ts @@ -18,7 +18,7 @@ import type { EventEmitter } from "events"; // @ts-ignore -- gracefully falls back to `any` if not installed -import type { PrettyOptions } from "pino-pretty"; +import type { PrettyOptions as PinoPrettyOptions } from "pino-pretty"; import type { SonicBoom, SonicBoomOpts } from "sonic-boom"; import type { WorkerOptions } from "worker_threads"; @@ -27,9 +27,6 @@ import * as pinoStdSerializers from "pino-std-serializers"; //// Non-exported types and interfaces -type SerializerFn = (value: any) => any; -type WriteFn = (o: object) => void; - // ToDo https://github.com/pinojs/thread-stream/issues/24 type ThreadStream = any @@ -42,90 +39,6 @@ interface redactOptions { remove?: boolean; } -interface BaseLogger { - /** - * Set this property to the desired logging level. In order of priority, available levels are: - * - * - 'fatal' - * - 'error' - * - 'warn' - * - 'info' - * - 'debug' - * - 'trace' - * - * The logging level is a __minimum__ level. For instance if `logger.level` is `'info'` then all `'fatal'`, `'error'`, `'warn'`, - * and `'info'` logs will be enabled. - * - * You can pass `'silent'` to disable logging. - */ - level: pino.LevelWithSilent | string; - - /** - * Log at `'fatal'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. - * If more args follows `msg`, these will be used to format `msg` using `util.format`. - * - * @typeParam T: the interface of the object being serialized. Default is object. - * @param obj: object to be serialized - * @param msg: the log message to write - * @param ...args: format string values when `msg` is a format string - */ - fatal: pino.LogFn; - /** - * Log at `'error'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. - * If more args follows `msg`, these will be used to format `msg` using `util.format`. - * - * @typeParam T: the interface of the object being serialized. Default is object. - * @param obj: object to be serialized - * @param msg: the log message to write - * @param ...args: format string values when `msg` is a format string - */ - error: pino.LogFn; - /** - * Log at `'warn'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. - * If more args follows `msg`, these will be used to format `msg` using `util.format`. - * - * @typeParam T: the interface of the object being serialized. Default is object. - * @param obj: object to be serialized - * @param msg: the log message to write - * @param ...args: format string values when `msg` is a format string - */ - warn: pino.LogFn; - /** - * Log at `'info'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. - * If more args follows `msg`, these will be used to format `msg` using `util.format`. - * - * @typeParam T: the interface of the object being serialized. Default is object. - * @param obj: object to be serialized - * @param msg: the log message to write - * @param ...args: format string values when `msg` is a format string - */ - info: pino.LogFn; - /** - * Log at `'debug'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. - * If more args follows `msg`, these will be used to format `msg` using `util.format`. - * - * @typeParam T: the interface of the object being serialized. Default is object. - * @param obj: object to be serialized - * @param msg: the log message to write - * @param ...args: format string values when `msg` is a format string - */ - debug: pino.LogFn; - /** - * Log at `'trace'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. - * If more args follows `msg`, these will be used to format `msg` using `util.format`. - * - * @typeParam T: the interface of the object being serialized. Default is object. - * @param obj: object to be serialized - * @param msg: the log message to write - * @param ...args: format string values when `msg` is a format string - */ - trace: pino.LogFn; - /** - * Noop function. - */ - silent: pino.LogFn; -} - interface LoggerExtras extends EventEmitter { /** * Exposes the Pino package version. Also available on the exported pino function. @@ -192,10 +105,98 @@ interface LoggerExtras extends EventEmitter { declare namespace pino { //// Exported types and interfaces + + interface BaseLogger { + /** + * Set this property to the desired logging level. In order of priority, available levels are: + * + * - 'fatal' + * - 'error' + * - 'warn' + * - 'info' + * - 'debug' + * - 'trace' + * + * The logging level is a __minimum__ level. For instance if `logger.level` is `'info'` then all `'fatal'`, `'error'`, `'warn'`, + * and `'info'` logs will be enabled. + * + * You can pass `'silent'` to disable logging. + */ + level: pino.LevelWithSilent | string; + + /** + * Log at `'fatal'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + fatal: pino.LogFn; + /** + * Log at `'error'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + error: pino.LogFn; + /** + * Log at `'warn'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + warn: pino.LogFn; + /** + * Log at `'info'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + info: pino.LogFn; + /** + * Log at `'debug'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + debug: pino.LogFn; + /** + * Log at `'trace'` level the given msg. If the first argument is an object, all its properties will be included in the JSON line. + * If more args follows `msg`, these will be used to format `msg` using `util.format`. + * + * @typeParam T: the interface of the object being serialized. Default is object. + * @param obj: object to be serialized + * @param msg: the log message to write + * @param ...args: format string values when `msg` is a format string + */ + trace: pino.LogFn; + /** + * Noop function. + */ + silent: pino.LogFn; + } + type Bindings = Record; type Level = "fatal" | "error" | "warn" | "info" | "debug" | "trace"; type LevelWithSilent = pino.Level | "silent"; + + type SerializerFn = (value: any) => any; + type WriteFn = (o: object) => void; type LevelChangeEventListener = ( lvl: LevelWithSilent | string, @@ -278,6 +279,8 @@ declare namespace pino { (msg: string, ...args: any[]): void; } + interface PrettyOptions extends PinoPrettyOptions {} + interface LoggerOptions { transport?: TransportSingleOptions | TransportMultiOptions | TransportPipelineOptions /** @@ -756,10 +759,13 @@ export type LevelChangeEventListener = pino.LevelChangeEventListener; export type LogDescriptor = pino.LogDescriptor; export type Logger = pino.Logger; export type SerializedError = pino.SerializedError; +export type SerializerFn = pino.SerializerFn; export type SerializedRequest = pino.SerializedRequest; export type SerializedResponse = pino.SerializedResponse; +export type WriteFn = pino.WriteFn; // Interfaces +export interface BaseLogger extends pino.BaseLogger {} export interface ChildLoggerOptions extends pino.ChildLoggerOptions {} export interface DestinationStream extends pino.DestinationStream {} export interface LevelMapping extends pino.LevelMapping {} @@ -768,6 +774,7 @@ export interface LogFn extends pino.LogFn {} export interface LoggerOptions extends pino.LoggerOptions {} export interface MultiStreamOptions extends pino.MultiStreamOptions {} export interface MultiStreamRes extends pino.MultiStreamRes {} +export interface PrettyOptions extends pino.PrettyOptions {} export interface StreamEntry extends pino.StreamEntry {} export interface TransportBaseOptions extends pino.TransportBaseOptions {} export interface TransportMultiOptions extends pino.TransportMultiOptions {} diff --git a/test/types/pino.test-d.ts b/test/types/pino.test-d.ts index 3ed925957..237bc119e 100644 --- a/test/types/pino.test-d.ts +++ b/test/types/pino.test-d.ts @@ -1,7 +1,8 @@ -import P, { pino } from "../../"; +import P, { pino, SerializerFn } from "../../"; import { IncomingMessage, ServerResponse } from "http"; import { Socket } from "net"; import Logger = P.Logger; +import { expectType } from "tsd"; const log = pino(); const info = log.info; @@ -277,3 +278,22 @@ const logLine: pino.LogDescriptor = { interface CustomLogger extends pino.Logger { customMethod(msg: string, ...args: unknown[]): void; } + +expectType(() => {}) +expectType(() => {}) + +interface CustomBaseLogger extends pino.BaseLogger { + child(): CustomBaseLogger +} + +expectType({ + level: 'info', + fatal() {}, + error() {}, + warn() {}, + info() {}, + debug() {}, + trace() {}, + silent() {}, + child() { return this } +}) From 5834906ff52e8c5ce8dd5de2271a3342310ab37d Mon Sep 17 00:00:00 2001 From: KaKa Date: Tue, 30 Nov 2021 21:15:49 +0800 Subject: [PATCH 2/3] refactor: remove unused import --- test/types/pino.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/types/pino.test-d.ts b/test/types/pino.test-d.ts index 237bc119e..213e9745d 100644 --- a/test/types/pino.test-d.ts +++ b/test/types/pino.test-d.ts @@ -1,4 +1,4 @@ -import P, { pino, SerializerFn } from "../../"; +import P, { pino } from "../../"; import { IncomingMessage, ServerResponse } from "http"; import { Socket } from "net"; import Logger = P.Logger; From e74a8ee208123bbb7d9feee5601f0dff35a07021 Mon Sep 17 00:00:00 2001 From: KaKa Date: Wed, 1 Dec 2021 16:20:52 +0800 Subject: [PATCH 3/3] test: fix failing test --- test/types/pino.test-d.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/types/pino.test-d.ts b/test/types/pino.test-d.ts index 213e9745d..9779e4024 100644 --- a/test/types/pino.test-d.ts +++ b/test/types/pino.test-d.ts @@ -2,7 +2,6 @@ import P, { pino } from "../../"; import { IncomingMessage, ServerResponse } from "http"; import { Socket } from "net"; import Logger = P.Logger; -import { expectType } from "tsd"; const log = pino(); const info = log.info; @@ -279,14 +278,14 @@ interface CustomLogger extends pino.Logger { customMethod(msg: string, ...args: unknown[]): void; } -expectType(() => {}) -expectType(() => {}) +const serializerFunc: pino.SerializerFn = () => {} +const writeFunc: pino.WriteFn = () => {} interface CustomBaseLogger extends pino.BaseLogger { child(): CustomBaseLogger } -expectType({ +const customBaseLogger: CustomBaseLogger = { level: 'info', fatal() {}, error() {}, @@ -296,4 +295,4 @@ expectType({ trace() {}, silent() {}, child() { return this } -}) +}