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

Add TS types for transports #1050

Merged
merged 1 commit into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"lint": "eslint .",
"test": "npm run lint && tap test/*test.js test/*/*test.js && npm run test-types",
"test-ci": "npm run lint && tap --no-check-coverage test/*test.js test/*/*test.js --coverage-report=lcovonly && npm run test-types",
"test-types": "tsc && tsd",
"test-types": "tsc && tsd && ts-node test/types/pino.ts",
"cov-ui": "tap --coverage-report=html test/*test.js test/*/*test.js",
"bench": "node benchmarks/utils/runbench all",
"bench-basic": "node benchmarks/utils/runbench basic",
Expand Down Expand Up @@ -64,7 +64,7 @@
},
"homepage": "http://getpino.io",
"devDependencies": {
"@types/node": "^15.3.0",
"@types/node": "^15.14.1",
"airtap": "4.0.3",
"benchmark": "^2.1.4",
"bole": "^4.0.0",
Expand All @@ -81,7 +81,7 @@
"import-fresh": "^3.2.1",
"log": "^6.0.0",
"loglevel": "^1.6.7",
"pino-pretty": "^5.0.0",
"pino-pretty": "^5.1.0",
"pre-commit": "^1.2.2",
"proxyquire": "^2.1.3",
"pump": "^3.0.0",
Expand All @@ -92,8 +92,9 @@
"tap": "^15.0.1",
"tape": "^5.0.0",
"through2": "^4.0.0",
"tsd": "^0.15.1",
"typescript": "^4.2.4",
"ts-node": "^10.0.0",
"tsd": "^0.17.0",
"typescript": "^4.3.5",
"winston": "^3.3.3"
},
"dependencies": {
Expand All @@ -103,7 +104,7 @@
"pino-abstract-transport": "^0.2.0",
"pino-std-serializers": "^4.0.0",
"quick-format-unescaped": "^4.0.3",
"sonic-boom": "^2.0.1",
"sonic-boom": "^2.1.0",
"thread-stream": "^0.10.0"
},
"tsd": {
Expand Down
29 changes: 28 additions & 1 deletion pino.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import { EventEmitter } from "events";
import { SonicBoom } from "sonic-boom";
import * as pinoStdSerializers from "pino-std-serializers";
import {WriteStream} from "fs";
import { WriteStream } from "fs";
import { WorkerOptions } from "worker_threads";

export default P;
export { P as pino }
Expand Down Expand Up @@ -200,6 +201,32 @@ declare namespace P {
dest?: string | number | DestinationObjectOptions | DestinationStream | NodeJS.WritableStream,
): SonicBoom;

interface TransportTargetOptions<TransportOptions = Record<string, any>> {
target: string
options: TransportOptions
level: LevelWithSilent
}

interface TransportBaseOptions<TransportOptions = Record<string, any>> {
options?: TransportOptions
worker?: WorkerOptions & { autoEnd?: boolean}
}

interface TransportSingleOptions<TransportOptions = Record<string, any>> extends TransportBaseOptions<TransportOptions>{
target: string
}

interface TransportMultiOptions<TransportOptions = Record<string, any>> extends TransportBaseOptions<TransportOptions>{
targets: readonly TransportTargetOptions<TransportOptions>[]
}

// ToDo https://github.com/pinojs/thread-stream/issues/24
type ThreadStream = any
Copy link
Contributor Author

@kibertoad kibertoad Jul 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering that ThreadStream is at a pre-release version and might potentially change a lot, I don't think it makes much sense to inline its type definitions in pino, especially since it is likely to be simply fed into pino and not manipulated directly.
I'll work on pinojs/thread-stream#24 when there is a green light to do so, and replace this type with a reference to bundled type, but I don't think it should be a blocker here.


function transport<TransportOptions = Record<string, any>>(
options: TransportSingleOptions<TransportOptions> | TransportMultiOptions<TransportOptions>
): ThreadStream

interface MultiStreamOptions {
levels?: Record<string, number>
dedupe?: boolean
Expand Down
47 changes: 47 additions & 0 deletions test/types/pino-transport.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { pino } from '../../pino'

// Single
const transport = pino.transport({
target: '#pino/pretty',
options: { some: 'options for', the: 'transport' }
})
pino(transport)

// Multiple
const transports = pino.transport({targets: [
{
level: 'info',
target: '#pino/pretty',
options: { some: 'options for', the: 'transport' }
},
{
level: 'trace',
target: '#pino/file',
options: { destination: './test.log' }
}
]})
pino(transports)

type TransportConfig = {
id: string
}

// Custom transport params
const customTransport = pino.transport<TransportConfig>({
target: 'custom',
options: { id: 'abc' }
})
pino(customTransport)

// Worker
pino.transport({
target: 'custom',
worker: {
argv: ['a', 'b'],
stdin: false,
stderr: true,
stdout: false,
autoEnd: true,
},
options: { id: 'abc' }
})
40 changes: 40 additions & 0 deletions test/types/pino.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { pino } from '../../pino'
import { join } from 'path'
import { tmpdir } from'os'

const destination = join(
tmpdir(),
'_' + Math.random().toString(36).substr(2, 9)
)

// Single
const transport = pino.transport({
target: '#pino/pretty',
options: { some: 'options for', the: 'transport' }
})
const logger = pino(transport)
logger.info('test2')

const transport2 = pino.transport({
target: '#pino/pretty',
})
const logger2 = pino(transport2)
logger2.info('test2')


// Multiple

const transports = pino.transport({targets: [
{
level: 'info',
target: '#pino/pretty',
options: { some: 'options for', the: 'transport' }
},
{
level: 'trace',
target: '#pino/file',
options: { destination }
}
]})
const loggerMulti = pino(transports)
loggerMulti.info('test2')