Giter Site home page Giter Site logo

fastify-warning's Introduction

fastify-warning

CI NPM version Known Vulnerabilities js-standard-style

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 a utility for creating warnings and emitting them.

const warning = require('fastify-warning')()

Methods

warning.create(name, code, message)
  • name (string, required) - The error name, you can access it later with error.name. For consistency, we recommend prefixing plugin error names with FastifWarning{YourPluginName}
  • code (string, required) - The warning code, you can access it later with error.code. For consistency, we recommend prefixing plugin error codes with FST_{YourPluginName}_. NOTE: codes should be all uppercase.
  • 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 = require('fastify-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'message')
warning.emit('FST_ERROR_CODE')

How to use an interpolated string:

const warning = require('fastify-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s')
warning.emit('FST_ERROR_CODE', 'world')

The module also exports an warning.emitted Map, which contains all the warnings already emitted. Useful for testing.

const warning = require('fastify-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s')
console.log(warning.emitted.get('FST_ERROR_CODE')) // false
warning.emit('FST_ERROR_CODE', 'world')
console.log(warning.emitted.get('FST_ERROR_CODE')) // true

License

Licensed under MIT.

fastify-warning's People

Contributors

dependabot[bot] avatar delvedor avatar fdawgs avatar github-actions[bot] avatar humphd avatar paolochiodi avatar salmanm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.