Skip to content

Commit

Permalink
new api ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Eomm committed Dec 12, 2023
1 parent 7a91b1e commit 6ede6dc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
12 changes: 10 additions & 2 deletions benchmarks/warn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
const { Suite } = require('benchmark')
const { createWarning } = require('..')

const err1 = createWarning('FastifyWarning', 'FST_ERROR_CODE_1', 'message')
const err2 = createWarning('FastifyWarning', 'FST_ERROR_CODE_2', 'message')
const err1 = createWarning({
name: 'FastifyWarning',
code: 'FST_ERROR_CODE_1',
message: 'message'
})
const err2 = createWarning({
name: 'FastifyWarning',
code: 'FST_ERROR_CODE_2',
message: 'message'
})

new Suite()
.add('warn', function () {
Expand Down
17 changes: 13 additions & 4 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,26 @@ declare namespace processWarning {
format(a?: any, b?: any, c?: any): string;
}

export type WarningOptions = {
name: string;
code: string;
message: string;
unlimited?: boolean;
}

export type DeprecationOptions = Omit<WarningOptions, 'name'>

export type ProcessWarningOptions = {
unlimited?: boolean;
}

export type ProcessWarning = {
createWarning(name: string, code: string, message: string, opts?: ProcessWarningOptions): WarningItem;
createDeprecation(code: string, message: string, opts?: ProcessWarningOptions): WarningItem;
createWarning(params: WarningOptions): WarningItem;
createDeprecation(params: DeprecationOptions): WarningItem;
}

export function createDeprecation(code: string, message: string, opts?: ProcessWarningOptions): WarningItem;
export function createWarning(name: string, code: string, message: string, opts?: ProcessWarningOptions): WarningItem;
export function createWarning(params: WarningOptions): WarningItem;
export function createDeprecation(params: DeprecationOptions): WarningItem;

const processWarning: ProcessWarning;
export { processWarning as default };
Expand Down
18 changes: 15 additions & 3 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { expectType } from 'tsd'
import { createWarning, createDeprecation } from '..'

const WarnInstance = createWarning('FastifyWarning', 'CODE', 'message')
const WarnInstance = createWarning({
name: 'FastifyWarning',
code: 'CODE',
message: 'message'
})

expectType<string>(WarnInstance.code)
expectType<string>(WarnInstance.message)
Expand All @@ -13,10 +17,18 @@ expectType<void>(WarnInstance.emit())
expectType<void>(WarnInstance.emit('foo'))
expectType<void>(WarnInstance.emit('foo', 'bar'))

const buildWarnUnlimited = createWarning('FastifyWarning', 'CODE', 'message', { unlimited: true })
const buildWarnUnlimited = createWarning({
name: 'FastifyWarning',
code: 'CODE',
message: 'message',
unlimited: true
})
expectType<boolean>(buildWarnUnlimited.unlimited)

const DeprecationInstance = createDeprecation('CODE', 'message')
const DeprecationInstance = createDeprecation({
code: 'CODE',
message: 'message'
})
expectType<string>(DeprecationInstance.code)

DeprecationInstance.emit()
Expand Down

0 comments on commit 6ede6dc

Please sign in to comment.