# fastify-warning

> A small utility for creating warnings and emitting them.

Latest version **0.2.0** (published 2020-07-28) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install fastify-warning
pnpm add fastify-warning
yarn add fastify-warning
bun add fastify-warning
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-07-28 |
| First published | 2020-07-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 38 |
| Author | Tomas Della Vedova |
| Maintainers | delvedor |
| Keywords | fastify, error, warning, utility, plugin, emit, once |

## Links

- npm: https://www.npmjs.com/package/fastify-warning
- Repository: https://github.com/fastify/fastify-warning
- Homepage: https://github.com/fastify/fastify-warning#readme
- Issues: https://github.com/fastify/fastify-warning/issues
- npm.io page: https://npm.io/package/fastify-warning

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 0.2.0 (latest) — 2020-07-28
- 0.1.0 — 2020-07-26

## README

# fastify-warning

[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)  ![build](https://github.com/fastify/fastify-warning/workflows/build/badge.svg)

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.

```js
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 to prefix plugin error names with `FastifWarning{YourPluginName}`
- `code` (`string`, required) - The warning code, you can access it later with `error.code`. For consistency, we recommend to prefix plugin error codes with `FST_{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.

```js
const warning = require('fastify-warning')()
warning.create('FastifyWarning', 'FST_ERROR_CODE', 'message')
warning.emit('FST_ERROR_CODE')
```

How to use a interpolated string:
```js
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](https://developer.mozilla.org/it/docs/Web/JavaScript/Reference/Global_Objects/Map), which contains all the warnings already emitted. Useful for testing.
```js
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](./LICENSE).

---
_Source: https://npm.io/package/fastify-warning · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
