fastify-warning
A small utility, used by Fastify itself, for generating consistent warning objects across your codebase and plugins. It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once.
Install
npm i fastify-warning
Usage
The module exports a builder function that returns an utility for creating warnings and emitting them.
const warning =
Methods
warning.create(name, code, message)
name
(string
, required) - The error name, you can access it later witherror.name
. For consistency, we recommend to prefix plugin error names withFastifWarning{YourPluginName}
code
(string
, required) - The warning code, you can access it later witherror.code
. For consistency, we recommend to prefix plugin error codes withFST_{YourPluginName}_
message
(string
, required) - The warning message. You can also use interpolated strings for formatting the message.
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.
warning.emit(code [, a [, b [, c]]])
code
(string
, required) - The warning code you intend to emit.[, a [, b [, c]]]
(any
, optional) - Parameters for string interpolation.
const warning = warningwarning
How to use a interpolated string:
const warning = warningwarning
The module also exports an warning.emitted
Map, which contains all the warnings already emitted. Useful for testing.
const warning = warningconsole // falsewarningconsole // true
License
Licensed under MIT.