All Projects → fastify → Fastify Sensible

fastify / Fastify Sensible

Licence: mit
Defaults for Fastify that everyone can agree on

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Fastify Sensible

Kody
.files and environment configuration manager created with node
Stars: ✭ 113 (-23.13%)
Mutual labels:  utility
Interactor
A Simple Website User Interaction Tracker.
Stars: ✭ 128 (-12.93%)
Mutual labels:  utility
Packagehunter
📥 [Android Library] Hunt down all package information
Stars: ✭ 137 (-6.8%)
Mutual labels:  utility
Ridereceipts
🚕 Simple automation desktop app to download and organize your receipts from Uber/Lyft. Try out our new Ride Receipts PRO !
Stars: ✭ 117 (-20.41%)
Mutual labels:  utility
Slickr
A collection of python and bash scripts to collect and analyze frame rendering performance in Android apps.
Stars: ✭ 126 (-14.29%)
Mutual labels:  utility
Calypsobot
A fully customizable bot built with discord.js
Stars: ✭ 131 (-10.88%)
Mutual labels:  utility
Endlines
Easy conversion between new-line conventions
Stars: ✭ 112 (-23.81%)
Mutual labels:  utility
Wallutils
🌆 Utilities for handling monitors, resolutions, wallpapers and timed wallpapers
Stars: ✭ 145 (-1.36%)
Mutual labels:  utility
Elf Strings
elf-strings will programmatically read an ELF binary's string sections within a given binary. This is meant to be much like the strings UNIX utility, however is purpose built for ELF binaries.
Stars: ✭ 127 (-13.61%)
Mutual labels:  utility
React Sizeme
Make your React Components aware of their width and height!
Stars: ✭ 1,770 (+1104.08%)
Mutual labels:  utility
Ngx Context
Angular Context: Easy property binding for router outlet and nested component trees.
Stars: ✭ 118 (-19.73%)
Mutual labels:  utility
Swift Selection Search
Swift Selection Search (SSS) is a simple Firefox add-on that lets you quickly search for some text in a page using your favorite search engines.
Stars: ✭ 125 (-14.97%)
Mutual labels:  utility
Nest Emitter
Strongly 💪🏼 Typed Eventemitter Module For Nestjs Framework 🦁
Stars: ✭ 133 (-9.52%)
Mutual labels:  utility
Tspath
TypeScript path alias resolver
Stars: ✭ 115 (-21.77%)
Mutual labels:  utility
Fac
Easy-to-use CUI for fixing git conflicts
Stars: ✭ 1,738 (+1082.31%)
Mutual labels:  utility
To Case
Simple case detection and conversion for strings.
Stars: ✭ 111 (-24.49%)
Mutual labels:  utility
Asciigraph
Go package to make lightweight ASCII line graph ╭┈╯ in command line apps with no other dependencies.
Stars: ✭ 1,805 (+1127.89%)
Mutual labels:  utility
Georaptor
Python Geohash Compression Tool
Stars: ✭ 143 (-2.72%)
Mutual labels:  utility
Shpotify
A command-line interface to Spotify.
Stars: ✭ 1,782 (+1112.24%)
Mutual labels:  utility
Nightfall
A menu bar utility for toggling dark mode in macOS, written in Swift.
Stars: ✭ 131 (-10.88%)
Mutual labels:  utility

fastify-sensible

js-standard-style Node CI

Defaults for Fastify that everyone can agree on™.
This plugin adds some useful utilities to your Fastify instance, see the API section to learn more.

Supports Fastify versions 3.x. Please refer to this branch and related versions for Fastify 2.x compatibility. Please refer to this branch and related versions for Fastify 1.x compatibility.

Why these APIs are here and not directly into core?
Because Fastify aims to be as small and focused as possible, every utility that is not essential should be shipped as standalone plugin.

Install

npm i fastify-sensible

Usage

const fastify = require('fastify')()
fastify.register(require('fastify-sensible'))

fastify.get('/', (req, reply) => {
  reply.notFound()
})

fastify.get('/async', async (req, reply) => {
  throw fastify.httpErrors.notFound()
})

fastify.listen(3000)

API

fastify.httpErrors

Object that exposes createError and all the 4xx and 5xx error constructors.

Usage of 4xx and 5xx error constructors follows the same structure as new createError[code || name]([msg])) in http-errors:

 // the custom message is optional
const notFoundErr = fastify.httpErrors.notFound('custom message')

4xx

  • fastify.httpErrors.badRequest()
  • fastify.httpErrors.unauthorized()
  • fastify.httpErrors.paymentRequired()
  • fastify.httpErrors.forbidden()
  • fastify.httpErrors.notFound()
  • fastify.httpErrors.methodNotAllowed()
  • fastify.httpErrors.notAcceptable()
  • fastify.httpErrors.proxyAuthenticationRequired()
  • fastify.httpErrors.requestTimeout()
  • fastify.httpErrors.conflict()
  • fastify.httpErrors.gone()
  • fastify.httpErrors.lengthRequired()
  • fastify.httpErrors.preconditionFailed()
  • fastify.httpErrors.payloadTooLarge()
  • fastify.httpErrors.uriTooLong()
  • fastify.httpErrors.unsupportedMediaType()
  • fastify.httpErrors.rangeNotSatisfiable()
  • fastify.httpErrors.expectationFailed()
  • fastify.httpErrors.imateapot()
  • fastify.httpErrors.misdirectedRequest()
  • fastify.httpErrors.unprocessableEntity()
  • fastify.httpErrors.locked()
  • fastify.httpErrors.failedDependency()
  • fastify.httpErrors.unorderedCollection()
  • fastify.httpErrors.upgradeRequired()
  • fastify.httpErrors.preconditionRequired()
  • fastify.httpErrors.tooManyRequests()
  • fastify.httpErrors.requestHeaderFieldsTooLarge()
  • fastify.httpErrors.unavailableForLegalReasons()

5xx

  • fastify.httpErrors.internalServerError()
  • fastify.httpErrors.notImplemented()
  • fastify.httpErrors.badGateway()
  • fastify.httpErrors.serviceUnavailable()
  • fastify.httpErrors.gatewayTimeout()
  • fastify.httpErrors.httpVersionNotSupported()
  • fastify.httpErrors.variantAlsoNegotiates()
  • fastify.httpErrors.insufficientStorage()
  • fastify.httpErrors.loopDetected()
  • fastify.httpErrors.bandwidthLimitExceeded()
  • fastify.httpErrors.notExtended()
  • fastify.httpErrors.networkAuthenticationRequired()

createError

Usage of createError follows the same structure as createError([status], [message], [properties]) in http-errors:

var err = fastify.httpErrors.createError(404, 'This video does not exist!')

reply.[httpError]

The reply interface is decorated with all the functions declared above, use it is very easy:

fastify.get('/', (req, reply) => {
  reply.notFound()
})

reply.vary

The reply interface is decorated with jshttp/vary, the API is the same, but you don't need to pass the res object.

fastify.get('/', (req, reply) => {
  reply.vary('Accept')
  reply.send('ok')
})

request.forwarded

The request interface is decorated with jshttp/forwarded, the API is the same, but you don't need to pass the request object.

fastify.get('/', (req, reply) => {
  reply.send(req.forwarded())
})

request.is

The request interface is decorated with jshttp/type-is, the API is the same, but you don't need to pass the request object.

fastify.get('/', (req, reply) => {
  reply.send(req.is(['html', 'json']))
})

assert

Verify if a given condition is true, if not it throws the specified http error.
Very useful if you work with async routes.

// the custom message is optional
fastify.assert(
  req.headers.authorization, 400, 'Missing authorization header'
)

The assert API exposes also the following methods:

  • fastify.assert.ok()
  • fastify.assert.equal()
  • fastify.assert.notEqual()
  • fastify.assert.strictEqual()
  • fastify.assert.notStrictEqual()
  • fastify.assert.deepEqual()
  • fastify.assert.notDeepEqual()

to

Async await wrapper for easy error handling without try-catch, inspired by await-to-js

const [err, user] = await fastify.to(
  db.findOne({ user: 'tyrion' })
)

Custom error handler

This plugins also adds a custom error handler which hides the error message in case of 500 errors that are being generated by throwed errors, instead it returns Something went wrong.
This is especially useful if you are using async routes, where every uncaught error will be sent back to the user (but do not worry, the original error message is logged as error in any case). If needed, it can be disabled by setting the option errorHandler to false.

If you return a 500 error via the API provided on fastify.httpErrors and reply objects, the custom error message is preserved and returned in the HTTP response body.

Contributing

Do you feel there is some utility that everyone can agree on which is not present?
Open an issue and let's discuss it! Even better a pull request!

Acknowledgements

The project name is inspired by vim-sensible, an awesome package that if you use vim you should use too.

License

MIT Copyright © 2018 Tomas Della Vedova

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].