Skip to content

Commit 0378576

Browse files
EommUzlopakjsumners
authored
Refactoring (v3) (#96)
* feat!: new api * test: update code * fix: issue * update bench * docs * ts * jsdoc * fix typings * Update README.md Co-authored-by: Aras Abbasi <[email protected]> * Update README.md Co-authored-by: James Sumners <[email protected]> * new api * new api ts * fix docs * Implement function scoped Warning (#97) * Apply suggestions from code review Co-authored-by: Aras Abbasi <[email protected]> Co-authored-by: James Sumners <[email protected]> * fix feedback * fix docs --------- Co-authored-by: uzlopak <[email protected]> Co-authored-by: James Sumners <[email protected]>
1 parent 9f67874 commit 0378576

File tree

14 files changed

+400
-282
lines changed

14 files changed

+400
-282
lines changed

README.md

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
A small utility for generating consistent warning objects across your codebase.
88
It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once (unless configured otherwise).
99

10-
This module is used by the [Fastify](https://fastify.io) framework and it was called `fastify-warning` prior to version 1.0.0.
10+
_This module is used by the [Fastify](https://fastify.io) framework and it was called `fastify-warning` prior to version 1.0.0._
1111

1212
### Install
1313

@@ -17,15 +17,26 @@ npm i process-warning
1717

1818
### Usage
1919

20-
The module exports a builder function that returns a utility for creating warnings and emitting them.
20+
The module exports two builder functions for creating warnings.
2121

2222
```js
23-
const warning = require('process-warning')()
23+
const {
24+
createWarning,
25+
createDeprecation
26+
} = require('process-warning')
27+
28+
const warning = createWarning({
29+
name: 'ExampleWarning',
30+
code: 'EXP_WRN_001',
31+
message: 'Hello %s',
32+
unlimited: true
33+
})
34+
warning('world')
2435
```
2536

2637
#### Methods
2738

28-
##### `warning.create(name, code, message[, options])`
39+
##### `createWarning({ name, code, message[, unlimited] })`
2940

3041
- `name` (`string`, required) - The error name, you can access it later with
3142
`error.name`. For consistency, we recommend prefixing module error names
@@ -41,51 +52,53 @@ properties:
4152
once? Defaults to `false`.
4253

4354

44-
##### `warning.createDeprecation(code, message[, options])`
55+
##### `createDeprecation({code, message[, options]})`
4556

46-
This is a wrapper for `warning.create`. It is equivalent to invoking
47-
`warning.create` with the `name` parameter set to "DeprecationWarning".
57+
This is a wrapper for `createWarning`. It is equivalent to invoking
58+
`createWarning` with the `name` parameter set to "DeprecationWarning".
4859

4960
Deprecation warnings have extended support for the Node.js CLI options:
5061
`--throw-deprecation`, `--no-deprecation`, and `--trace-deprecation`.
5162

52-
##### `warning.emit(code [, a [, b [, c]]])`
63+
##### `warning([, a [, b [, c]]])`
5364

54-
The utility also contains an `emit` function that you can use for emitting the
55-
warnings you have previously created by passing their respective code.
65+
The returned `warning` function can used for emitting warnings.
5666
A warning is guaranteed to be emitted at least once.
5767

58-
- `code` (`string`, required) - The warning code you intend to emit.
5968
- `[, a [, b [, c]]]` (`any`, optional) - Parameters for string interpolation.
6069

6170
```js
62-
const warning = require('process-warning')()
63-
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'message')
64-
warning.emit('FST_ERROR_CODE')
71+
const { createWarning } = require('process-warning')
72+
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'message' })
73+
FST_ERROR_CODE()
6574
```
6675

6776
How to use an interpolated string:
6877
```js
69-
const warning = require('process-warning')()
70-
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s')
71-
warning.emit('FST_ERROR_CODE', 'world')
78+
const { createWarning } = require('process-warning')
79+
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'})
80+
FST_ERROR_CODE('world')
7281
```
7382

74-
The module also exports an `warning.emitted` [Map](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Map), which contains all the warnings already emitted. Useful for testing.
83+
The `warning` object has methods and properties for managing the warning's state. Useful for testing.
7584
```js
76-
const warning = require('process-warning')()
77-
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s')
78-
console.log(warning.emitted.get('FST_ERROR_CODE')) // false
79-
warning.emit('FST_ERROR_CODE', 'world')
80-
console.log(warning.emitted.get('FST_ERROR_CODE')) // true
85+
const { createWarning } = require('process-warning')
86+
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s'})
87+
console.log(FST_ERROR_CODE.emitted) // false
88+
FST_ERROR_CODE('world')
89+
console.log(FST_ERROR_CODE.emitted) // true
90+
91+
const FST_ERROR_CODE_2 = createWarning('MyAppWarning', 'FST_ERROR_CODE_2', 'Hello %s')
92+
FST_ERROR_CODE_2.emitted = true
93+
FST_ERROR_CODE_2('world') // will not be emitted because it is not unlimited
8194
```
8295

8396
How to use an unlimited warning:
8497
```js
85-
const warning = require('process-warning')()
86-
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s', { unlimited: true })
87-
warning.emit('FST_ERROR_CODE', 'world') // will be emitted
88-
warning.emit('FST_ERROR_CODE', 'world') // will be emitted again
98+
const { createWarning } = require('process-warning')
99+
const FST_ERROR_CODE = createWarning({ name: 'MyAppWarning', code: 'FST_ERROR_CODE', message: 'Hello %s', unlimited: true })
100+
FST_ERROR_CODE('world') // will be emitted
101+
FST_ERROR_CODE('world') // will be emitted again
89102
```
90103

91104
#### Suppressing warnings

benchmarks/warn.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
'use strict'
22

33
const { Suite } = require('benchmark')
4-
const warning = require('..')()
4+
const { createWarning } = require('..')
5+
6+
const err1 = createWarning({
7+
name: 'TestWarning',
8+
code: 'TST_ERROR_CODE_1',
9+
message: 'message'
10+
})
11+
const err2 = createWarning({
12+
name: 'TestWarning',
13+
code: 'TST_ERROR_CODE_2',
14+
message: 'message'
15+
})
516

6-
warning.create('FastifyWarning', 'FST_ERROR_CODE_1', 'message')
7-
warning.create('FastifyWarning', 'FST_ERROR_CODE_2', 'message')
8-
warning.create('FastifyWarning', 'FST_ERROR_CODE_3', 'message')
917
new Suite()
1018
.add('warn', function () {
11-
warning.emit('FST_ERROR_CODE_1')
12-
warning.emit('FST_ERROR_CODE_3')
19+
err1()
20+
err2()
1321
})
1422
.on('cycle', function (event) {
1523
console.log(String(event.target))

examples/example.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
'use strict'
22

3-
const warning = require('..')()
3+
const { createWarning } = require('..')
44

5-
warning.create('DeprecationWarning', 'CUSTDEP001', 'This is a deprecation warning')
5+
const CUSTDEP001 = createWarning({
6+
name: 'DeprecationWarning',
7+
code: 'CUSTDEP001',
8+
message: 'This is a deprecation warning'
9+
})
610

7-
warning.emit('CUSTDEP001')
11+
CUSTDEP001()

0 commit comments

Comments
 (0)