process-warning
Advanced tools
Comparing version 2.1.0 to 2.2.0
@@ -8,7 +8,9 @@ 'use strict' | ||
const emitted = new Map() | ||
const opts = Object.create(null) | ||
function create (name, code, message) { | ||
function create (name, code, message, { unlimited = false } = {}) { | ||
if (!name) throw new Error('Warning name must not be empty') | ||
if (!code) throw new Error('Warning code must not be empty') | ||
if (!message) throw new Error('Warning message must not be empty') | ||
if (typeof unlimited !== 'boolean') throw new Error('Warning opts.unlimited must be a boolean') | ||
@@ -41,3 +43,4 @@ code = code.toUpperCase() | ||
emitted.set(code, false) | ||
Object.assign(opts, { unlimited }) | ||
emitted.set(code, unlimited) | ||
codes[code] = buildWarnOpts | ||
@@ -49,3 +52,3 @@ | ||
function emit (code, a, b, c) { | ||
if (emitted.get(code) === true) return | ||
if (emitted.get(code) === true && opts.unlimited === false) return | ||
if (codes[code] === undefined) throw new Error(`The code '${code}' does not exist`) | ||
@@ -52,0 +55,0 @@ emitted.set(code, true) |
{ | ||
"name": "process-warning", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A small utility for creating warnings and emitting them.", | ||
@@ -9,2 +9,3 @@ "main": "index.js", | ||
"lint": "standard", | ||
"lint:fix": "standard --fix", | ||
"test": "npm run test:unit && npm run test:jest && npm run test:typescript", | ||
@@ -39,4 +40,4 @@ "test:jest": "jest jest.test.js", | ||
"tap": "^16.3.0", | ||
"tsd": "^0.24.1" | ||
"tsd": "^0.28.0" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # process-warning | ||
A small utility for generating consistent warning objects across your codebase. | ||
It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once. | ||
It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once (unless configured otherwise). | ||
@@ -30,3 +30,3 @@ This module is used by the [Fastify](https://fastify.io) framework and it was called `fastify-warning` prior to version 1.0.0. | ||
``` | ||
warning.create(name, code, message) | ||
warning.create(name, code, message[, options]) | ||
``` | ||
@@ -37,2 +37,4 @@ | ||
- `message` (`string`, required) - The warning message. You can also use interpolated strings for formatting the message. | ||
- `options` (`object`, optional) - Optional options with the following properties: | ||
- `unlimited` (`boolean`, optional) - Should the warning be emitted more than once? Defaults to `false`. | ||
@@ -70,4 +72,12 @@ The utility also contains an `emit` function that you can use for emitting the warnings you have previously created by passing their respective code. A warning is guaranteed to be emitted only once. | ||
How to use an unlimited warning: | ||
```js | ||
const warning = require('process-warning')() | ||
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s', { unlimited: true }) | ||
warning.emit('FST_ERROR_CODE', 'world') // will be emitted | ||
warning.emit('FST_ERROR_CODE', 'world') // will be emitted again | ||
``` | ||
## License | ||
Licensed under [MIT](./LICENSE). |
@@ -92,1 +92,9 @@ 'use strict' | ||
}) | ||
test('Cannot set unlimited other than boolean', t => { | ||
t.plan(1) | ||
const { create } = build() | ||
t.throws(() => create('FastifyWarning', 'CODE', 'Msg', { unlimited: 42 }), new Error('Warning opts.unlimited must be a boolean')) | ||
}) |
@@ -5,3 +5,3 @@ type ProcessWarning = () => processWarning.Warning | ||
export interface Warning { | ||
create(name: string, code: string, message: string): BuildWarnOptsFn, | ||
create(name: string, code: string, message: string, opts?: ProcessWarningOptions): BuildWarnOptsFn, | ||
emit(cod: string, a?: any, b?: any, c?: any): void, | ||
@@ -13,2 +13,6 @@ emitted: Map<string, boolean> | ||
export type ProcessWarningOptions = { | ||
unlimited?: boolean, | ||
} | ||
export interface WarnOpts { | ||
@@ -15,0 +19,0 @@ code: string; |
@@ -15,1 +15,4 @@ import { expectType } from 'tsd' | ||
expectType<Map<string, boolean>>(warning.emitted) | ||
const buildWarnUnlimited = warning.create('FastifyWarning', 'CODE', 'message', { unlimited: true }) | ||
expectType<BuildWarnOptsFn>(buildWarnUnlimited) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
14702
15
259
80
0