diff --git a/LICENSE b/LICENSE index 08250b8..24f78a7 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020 Fastify +Copyright (c) Fastify Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 275deb1..3b55cee 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,19 @@ -# fastify-warning +# process-warning -![CI](https://github.com/fastify/fastify-warning/workflows/CI/badge.svg) -[![NPM version](https://img.shields.io/npm/v/fastify-warning.svg?style=flat)](https://www.npmjs.com/package/fastify-warning) -[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-warning/badge.svg)](https://snyk.io/test/github/fastify/fastify-warning) +![CI](https://github.com/fastify/process-warning/workflows/CI/badge.svg) +[![NPM version](https://img.shields.io/npm/v/process-warning.svg?style=flat)](https://www.npmjs.com/package/process-warning) +[![Known Vulnerabilities](https://snyk.io/test/github/fastify/process-warning/badge.svg)](https://snyk.io/test/github/fastify/process-warning) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) -A small utility, used by Fastify itself, for generating consistent warning objects across your codebase and plugins. +A small utility for generating consistent warning objects across your codebase. It also exposes a utility for emitting those warnings, guaranteeing that they are issued only once. +This module is used by the [Fastify](https://fastify.io) framework and it was called `fastify-warning` prior to version 1.0.0. + ### Install +t ``` -npm i fastify-warning +npm i process-warning ``` ### Usage @@ -18,7 +21,7 @@ npm i fastify-warning The module exports a builder function that returns a utility for creating warnings and emitting them. ```js -const warning = require('fastify-warning')() +const warning = require('process-warning')() ``` #### Methods @@ -27,8 +30,8 @@ const warning = require('fastify-warning')() 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. +- `name` (`string`, required) - The error name, you can access it later with `error.name`. For consistency, we recommend prefixing module error names with `{YourModule}Warning` +- `code` (`string`, required) - The warning code, you can access it later with `error.code`. For consistency, we recommend prefixing plugin error codes with `{ThreeLetterModuleName}_`, e.g. `FST_`. 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. @@ -41,21 +44,21 @@ warning.emit(code [, a [, b [, c]]]) - `[, a [, b [, c]]]` (`any`, optional) - Parameters for string interpolation. ```js -const warning = require('fastify-warning')() +const warning = require('process-warning')() warning.create('FastifyWarning', 'FST_ERROR_CODE', 'message') warning.emit('FST_ERROR_CODE') ``` How to use an interpolated string: ```js -const warning = require('fastify-warning')() +const warning = require('process-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')() +const warning = require('process-warning')() warning.create('FastifyWarning', 'FST_ERROR_CODE', 'Hello %s') console.log(warning.emitted.get('FST_ERROR_CODE')) // false warning.emit('FST_ERROR_CODE', 'world') diff --git a/index.js b/index.js index 569f157..d0f0b6b 100644 --- a/index.js +++ b/index.js @@ -7,9 +7,9 @@ function build () { const emitted = new Map() function create (name, code, message) { - if (!name) throw new Error('Fastify warning name must not be empty') - if (!code) throw new Error('Fastify warning code must not be empty') - if (!message) throw new Error('Fastify warning message must not be empty') + if (!name) throw new Error('Warning name must not be empty') + if (!code) throw new Error('Warning code must not be empty') + if (!message) throw new Error('Warning message must not be empty') code = code.toUpperCase() diff --git a/package.json b/package.json index f0330d8..04e6457 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "fastify-warning", + "name": "process-warning", "version": "0.2.0", "description": "A small utility for creating warnings and emitting them.", "main": "index.js", @@ -9,7 +9,7 @@ }, "repository": { "type": "git", - "url": "git+https://github.com/fastify/fastify-warning.git" + "url": "git+https://github.com/fastify/processs-warning.git" }, "keywords": [ "fastify", diff --git a/test.js b/test.js index 76fc4b9..392aa1d 100644 --- a/test.js +++ b/test.js @@ -46,7 +46,7 @@ test('Should throw when error code has no fastify name', t => { try { create() } catch (err) { - t.is(err.message, 'Fastify warning name must not be empty') + t.is(err.message, 'Warning name must not be empty') } }) @@ -55,7 +55,7 @@ test('Should throw when error has no code', t => { try { create('name') } catch (err) { - t.is(err.message, 'Fastify warning code must not be empty') + t.is(err.message, 'Warning code must not be empty') } }) @@ -64,7 +64,7 @@ test('Should throw when error has no message', t => { try { create('name', 'code') } catch (err) { - t.is(err.message, 'Fastify warning message must not be empty') + t.is(err.message, 'Warning message must not be empty') } })