Skip to content

Commit

Permalink
Add TS types
Browse files Browse the repository at this point in the history
Add index.d.ts as the definition file, add tests that are run with tsd
  • Loading branch information
windupbird144 committed Nov 1, 2021
1 parent 4ae5b38 commit aa18924
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 3 deletions.
87 changes: 87 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Type definitions for pino-abstract-transport 0.4.0
// Project: https://github.com/pinojs/pino-abstract-transport#readme
// Definitions by: Diyar Oktay <https://github.com/windupbird144>

/// <reference types="node" />
/// <reference types="duplexify" />

import { Duplexify } from "duplexify";
import { Transform } from "stream";

type BuildOptions = {
/**
* `parseLine(line)` a function that is used to parse line received from pino.
* @default JSON.parse
*/
parseLine?: (line: string) => unknown;

/**
* `parse` an option to change to data format passed to build function.
* @default undefined
*
*/
parse?: "lines";

/**
* `close(err, cb)` a function that is called to shutdown the transport.
* It's called both on error and non-error shutdowns. It can also return
* a promise. In this case discard the the cb argument.
*
* @example
* ```typescript
* {
* close: function (err, cb) {
* process.nextTick(cb, err)
* }
* }
* ```
* */
close?: (err: Error, cb: Function) => void | Promise<void>;

/**
* `metadata` If set to false, do not add metadata properties to the returned stream
*/
metadata?: false;
};

type DuplexifyBuildOptions = BuildOptions & {
enablePipelining: true;
};

interface OnUnknown {
/**
* `unknown` is the event emitted where an unparsable line is found
*
* @param event 'unknown'
* @param line the unparsable line
* @param error the error that was thrown when parsing the line
*/
on(event: "unknown", listener: (line: string, error: unknown) => void): void;
}

/**
* Create a split2 instance and returns it. This same instance is also passed
* to the given function, which is called synchronously.
*
* @returns {Transform} the split2 instance
*/
declare function build(
fn: (transform: Transform & OnUnknown) => void | Promise<void>,
opts?: BuildOptions
): Transform & OnUnknown;

/**
* Creates a split2 instance and passes it to the given function, which is called synchronously.
* Wraps the split2 instance and the returned stream using duplexify, so they can be concatenated
* into multiple transports.
*
* @returns {duplexify.Duplexify} the wrapped split2 instance
*/
declare function build(
fn: (transform: Transform & OnUnknown) => Transform & OnUnknown,
opts: DuplexifyBuildOptions
): Duplexify;

export { OnUnknown };

export default build;
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"main": "index.js",
"scripts": {
"prepare": "husky install",
"test": "standard | snazzy && tap test/*.test.js",
"test-ci": "standard | snazzy && tap test/*.test.js --coverage-report=lcovonly"
"test": "standard | snazzy && tap test/*.test.js && tsd",
"test-ci": "standard | snazzy && tap test/*.test.js --coverage-report=lcovonly && tsd"
},
"repository": {
"type": "git",
Expand All @@ -27,9 +27,15 @@
"split2": "^4.0.0"
},
"devDependencies": {
"@types/duplexify": "^3.6.0",
"@types/node": "^16.11.6",
"husky": "^7.0.0",
"snazzy": "^9.0.0",
"standard": "^16.0.3",
"tap": "^15.0.2"
"tap": "^15.0.2",
"tsd": "^0.18.0"
},
"tsd": {
"directory" : "./test/types"
}
}
19 changes: 19 additions & 0 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import build, { OnUnknown } from "../../index";
import { expectType } from "tsd";
import { Duplexify } from "duplexify";
import { Transform } from "stream";

/**
* build with enablePipelining returns a Duplexify stream
*/
expectType<Duplexify>(build((source) => source, { enablePipelining: true }));

/**
* build without enablePipelining returns a node stream
*/
expectType<Transform & OnUnknown>(build((source) => {}));

/**
* build also accepts an async function
*/
expectType<Transform & OnUnknown>(build(async (source) => {}));

0 comments on commit aa18924

Please sign in to comment.