77A small utility for generating consistent warning objects across your codebase.
88It 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
4960Deprecation 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.
5666A 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
6776How 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
8396How 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
0 commit comments