Skip to content

Commit

Permalink
Add TypeScript types (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed May 26, 2021
1 parent 7693cd4 commit 488b2cf
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 4 deletions.
110 changes: 110 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Type definitions for pino-pretty 4.7
// Project: https://github.com/pinojs/pino-pretty#readme
// Definitions by: Adam Vigneaux <https://github.com/AdamVig>
// Minimum TypeScript Version: 3.0

type LogDescriptor = Record<string, unknown>;

declare function PinoPretty(options: PinoPretty.PrettyOptions): PinoPretty.Prettifier;

declare namespace PinoPretty {
type Prettifier = (inputData: string | object) => string;
type MessageFormatFunc = (log: LogDescriptor, messageKey: string, levelLabel: string) => string;

interface PrettyOptions {
/**
* Hide objects from output (but not error object).
* @default false
*/
hideObject?: boolean;
/**
* Translate the epoch time value into a human readable date and time string. This flag also can set the format
* string to apply when translating the date to human readable format. For a list of available pattern letters
* see the {@link https://www.npmjs.com/package/dateformat|dateformat documentation}.
* - The default format is `yyyy-mm-dd HH:MM:ss.l o` in UTC.
* - Requires a `SYS:` prefix to translate time to the local system's timezone. Use the shortcut `SYS:standard`
* to translate time to `yyyy-mm-dd HH:MM:ss.l o` in system timezone.
* @default false
*/
translateTime?: boolean | string;
/**
* If set to true, it will print the name of the log level as the first field in the log line.
* @default false
*/
levelFirst?: boolean;
/**
* Define the key that contains the level of the log.
* @default "level"
*/
levelKey?: string;
/**
* Output the log level using the specified label.
* @default "levelLabel"
*/
levelLabel?: string;
/**
* The key in the JSON object to use as the highlighted message.
* @default "msg"
*/
messageKey?: string;
/**
* Print each log message on a single line (errors will still be multi-line).
* @default false
*/
singleLine?: boolean;
/**
* The key in the JSON object to use for timestamp display.
* @default "time"
*/
timestampKey?: string;
/**
* Format output of message, e.g. {level} - {pid} will output message: INFO - 1123
* @default false
*
* @example
* ```typescript
* {
* messageFormat: (log, messageKey) => {
* const message = log[messageKey];
* if (log.requestId) return `[${log.requestId}] ${message}`;
* return message;
* }
* }
* ```
*/
messageFormat?: false | string | MessageFormatFunc;
/**
* If set to true, will add color information to the formatted output message.
* @default false
*/
colorize?: boolean;
/**
* Appends carriage return and line feed, instead of just a line feed, to the formatted log line.
* @default false
*/
crlf?: boolean;
/**
* Define the log keys that are associated with error like objects.
* @default ["err", "error"]
*/
errorLikeObjectKeys?: string[];
/**
* When formatting an error object, display this list of properties.
* The list should be a comma separated list of properties.
* @default ""
*/
errorProps?: string;
/**
* Specify a search pattern according to {@link http://jmespath.org|jmespath}
*/
search?: string;
/**
* Ignore one or several keys.
* @example "time,hostname"
*/
ignore?: string;
}
}

export default PinoPretty;
export { PinoPretty }
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const defaultOptions = {
singleLine: false
}

module.exports = function prettyFactory (options) {
function prettyFactory (options) {
const opts = Object.assign({}, defaultOptions, options)
const EOL = opts.crlf ? '\r\n' : '\n'
const IDENT = ' '
Expand Down Expand Up @@ -164,3 +164,7 @@ module.exports = function prettyFactory (options) {
return line
}
}

prettyFactory.prettyFactory = prettyFactory
prettyFactory.default = prettyFactory
module.exports = prettyFactory
15 changes: 12 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
"name": "pino-pretty",
"version": "4.8.0",
"description": "Prettifier for Pino log lines",
"type": "commonjs",
"main": "index.js",
"types": "index.d.ts",
"bin": {
"pino-pretty": "./bin.js"
},
"scripts": {
"ci": "standard && tap 'test/**/*.test.js' --coverage-report=lcovonly",
"ci": "standard && npm run test-types && tap 'test/**/*.test.js' --coverage-report=lcovonly",
"lint": "standard | snazzy",
"test": "tap --100 'test/**/*.test.js'"
"test": "tap --100 'test/**/*.test.js'",
"test-types": "tsc && tsd"
},
"repository": {
"type": "git",
Expand All @@ -30,6 +33,7 @@
],
"dependencies": {
"@hapi/bourne": "^2.0.0",
"@types/node": "^15.3.0",
"args": "^5.0.1",
"chalk": "^4.0.0",
"dateformat": "^4.5.1",
Expand All @@ -48,6 +52,11 @@
"rimraf": "^3.0.2",
"snazzy": "^9.0.0",
"standard": "^16.0.3",
"tap": "^14.10.7"
"tap": "^14.10.7",
"tsd": "^0.15.1",
"typescript": "^4.2.4"
},
"tsd": {
"directory": "./test/types"
}
}
41 changes: 41 additions & 0 deletions test/types/pino-pretty.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import prettyFactory from "../../";
import {expectType} from "tsd";

import PinoPretty, { PinoPretty as PinoPrettyNamed } from "../../";
import PinoPrettyDefault from "../../";
import * as PinoPrettyStar from "../../";
import PinoPrettyCjsImport = require ("../../");
import Prettifier = PinoPretty.Prettifier;
const PinoPrettyCjs = require("../../");

const options: PinoPretty.PrettyOptions = {
colorize: true,
crlf: false,
errorLikeObjectKeys: ["err", "error"],
errorProps: "",
hideObject: true,
levelKey: "level",
levelLabel: "foo",
messageFormat: false,
ignore: "",
levelFirst: false,
messageKey: "msg",
timestampKey: "timestamp",
translateTime: "UTC:h:MM:ss TT Z",
search: "foo == `bar`",
singleLine: false,
};

const pretty = prettyFactory(options);
expectType<Prettifier>(pretty)

expectType<Prettifier>(PinoPrettyNamed(options));
expectType<Prettifier>(PinoPrettyDefault(options));
expectType<Prettifier>(PinoPrettyStar.PinoPretty(options));
expectType<Prettifier>(PinoPrettyStar.default(options));
expectType<Prettifier>(PinoPrettyCjsImport.PinoPretty(options));
expectType<Prettifier>(PinoPrettyCjsImport.default(options));
expectType<any>(PinoPrettyCjs(options));

expectType<string>(pretty({ foo: "bar" }));
expectType<string>(pretty('dummy'));
13 changes: 13 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "es6",
"lib": [ "es2015" ],
"module": "commonjs",
"noEmit": true,
"strict": true
},
"include": [
"./test/types/pino-pretty.test.d.ts",
"./index.d.ts"
]
}

0 comments on commit 488b2cf

Please sign in to comment.