Skip to content

Commit

Permalink
Finish TS types (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
kibertoad committed Jul 5, 2021
1 parent a8833f4 commit ff3073a
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
13 changes: 7 additions & 6 deletions package.json
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
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

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
@@ -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
@@ -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')

0 comments on commit ff3073a

Please sign in to comment.