Gathering detailed insights and metrics for @diotobtea/natus-expedita
Gathering detailed insights and metrics for @diotobtea/natus-expedita
npm install @diotobtea/natus-expedita
Typescript
Module System
Node Version
NPM Version
66
Supply Chain
100
Quality
77.1
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
10
Last Day
2
Last Week
2
Last Month
4
Last Year
10
2,040 Commits
2 Watching
1 Branches
1 Contributors
Latest Version
1.0.0
Package Id
@diotobtea/natus-expedita@1.0.0
Unpacked Size
29.22 kB
Size
10.28 kB
File Count
14
NPM Version
10.5.2
Node Version
20.13.1
Publised On
24 May 2024
Cumulative downloads
Total Downloads
Last day
0%
2
Compared to previous day
Last week
100%
2
Compared to previous week
Last month
0%
4
Compared to previous month
Last year
0%
10
Compared to previous year
27
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.
Why are these APIs here and not included with Fastify?
Because Fastify aims to be as small and focused as possible, every utility that is not essential should be shipped as a standalone plugin.
npm i @diotobtea/natus-expedita
Plugin version | Fastify version |
---|---|
^5.0.0 | ^4.0.0 |
^4.0.0 | ^3.0.0 |
^2.0.0 | ^2.0.0 |
^1.0.0 | ^1.0.0 |
Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin in the table above. See Fastify's LTS policy for more details.
1const fastify = require('fastify')() 2fastify.register(require('@diotobtea/natus-expedita')) 3 4fastify.get('/', (req, reply) => { 5 reply.notFound() 6}) 7 8fastify.get('/async', async (req, reply) => { 9 throw fastify.httpErrors.notFound() 10}) 11 12fastify.get('/async-return', async (req, reply) => { 13 return reply.notFound() 14}) 15 16fastify.listen({ port: 3000 })
If you set the sharedSchemaId
option, a shared JSON Schema is added and can be used in your routes.
1const fastify = require('fastify')() 2fastify.register(require('@diotobtea/natus-expedita'), { 3 sharedSchemaId: 'HttpError' 4}) 5 6fastify.get('/async', { 7 schema: { 8 response: { 9 404: { $ref: 'HttpError' } 10 } 11 }, 12 handler: async (req, reply) => { 13 return reply.notFound() 14 } 15}) 16 17fastify.listen({ port: 3000 })
fastify.httpErrors
Object that exposes createError
and all of the 4xx
and 5xx
error constructors.
Use of 4xx
and 5xx
error constructors follows the same structure as new createError[code || name]([msg]))
in http-errors:
1 // the custom message is optional
2const 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.tooEarly()
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
Use of createError
follows the same structure as createError([status], [message], [properties])
in http-errors:
1var err = fastify.httpErrors.createError(404, 'This video does not exist!')
reply.[httpError]
The reply
interface is decorated with all of the functions declared above, using it is easy:
1fastify.get('/', (req, reply) => { 2 reply.notFound() 3})
reply.vary
The reply
interface is decorated with jshttp/vary
, the API is the same, but you do not need to pass the res object.
1fastify.get('/', (req, reply) => { 2 reply.vary('Accept') 3 reply.send('ok') 4})
reply.cacheControl
The reply
interface is decorated an helper to configure cache control response headers.
1// configure a single type 2fastify.get('/', (req, reply) => { 3 reply.cacheControl('public') 4 reply.send('ok') 5}) 6 7// configure multiple types 8fastify.get('/', (req, reply) => { 9 reply.cacheControl('public') 10 reply.cacheControl('immutable') 11 reply.send('ok') 12}) 13 14// configure a type time 15fastify.get('/', (req, reply) => { 16 reply.cacheControl('max-age', 42) 17 reply.send('ok') 18}) 19 20// the time can be defined as string 21fastify.get('/', (req, reply) => { 22 // all the formats of github.com/vercel/ms are supported 23 reply.cacheControl('max-age', '1d') // will set to 'max-age=86400' 24 reply.send('ok') 25})
reply.preventCache
The reply
interface is decorated with a helper to set the cache control header to a no caching configuration.
1fastify.get('/', (req, reply) => { 2 // will set cache-control to 'no-store, max-age=0, private' 3 // and for HTTP/1.0 compatibility 4 // will set pragma to 'no-cache' and expires to 0 5 reply.preventCache() 6 reply.send('ok') 7})
reply.revalidate
The reply
interface is decorated with a helper to set the cache control header to a no caching configuration.
1fastify.get('/', (req, reply) => { 2 reply.revalidate() // will set to 'max-age=0, must-revalidate' 3 reply.send('ok') 4})
reply.staticCache
The reply
interface is decorated with a helper to set the cache control header to a public and immutable configuration.
1fastify.get('/', (req, reply) => { 2 // the time can be defined as a string 3 reply.staticCache(42) // will set to 'public, max-age=42, immutable' 4 reply.send('ok') 5})
reply.stale
The reply
interface is decorated with a helper to set the cache control header for stale content.
1fastify.get('/', (req, reply) => { 2 // the time can be defined as a string 3 reply.stale('while-revalidate', 42) 4 reply.stale('if-error', 1) 5 reply.send('ok') 6})
reply.maxAge
The reply
interface is decorated with a helper to set max age of the response. It can be used in conjunction with reply.stale
, see here.
1fastify.get('/', (req, reply) => { 2 // the time can be defined as a string 3 reply.maxAge(86400) 4 reply.stale('while-revalidate', 42) 5 reply.send('ok') 6})
request.forwarded
The request
interface is decorated with jshttp/forwarded
, the API is the same, but you do not need to pass the request object:
1fastify.get('/', (req, reply) => { 2 reply.send(req.forwarded()) 3})
request.is
The request
interface is decorated with jshttp/type-is
, the API is the same, but you do not need to pass the request object:
1fastify.get('/', (req, reply) => { 2 reply.send(req.is(['html', 'json'])) 3})
assert
Verify if a given condition is true, if not it throws the specified http error.
Useful if you work with async routes:
1// the custom message is optional 2fastify.assert( 3 req.headers.authorization, 400, 'Missing authorization header' 4)
The assert
API also exposes 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
:
1const [err, user] = await fastify.to( 2 db.findOne({ user: 'tyrion' }) 3)
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!
The project name is inspired by vim-sensible
, an awesome package that if you use vim you should use too.
MIT Copyright © Tomas Della Vedova & Fastify collaborators
No vulnerabilities found.
No security vulnerabilities found.