### Install @fastify/sensible Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Install the @fastify/sensible package using npm. ```bash npm i @fastify/sensible ``` -------------------------------- ### Get Forwarded Request Information with req.forwarded Source: https://github.com/fastify/fastify-sensible/blob/main/README.md The `req.forwarded()` helper provides access to the forwarded request information, similar to the `jshttp/forwarded` library, without needing to pass the request object explicitly. ```js fastify.get('/', (req, reply) => { reply.send(req.forwarded()) }) ``` -------------------------------- ### Basic Usage of @fastify/sensible Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Register the @fastify/sensible plugin and use its utilities, such as reply.notFound(). ```javascript 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.get('/async-return', async (req, reply) => { return reply.notFound() }) fastify.listen({ port: 3000 }) ``` -------------------------------- ### Handle Not Found Reply Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Use the reply.notFound() decorator to send a 404 Not Found response. This is a shortcut provided by fastify-sensible. ```javascript fastify.get('/', (req, reply) => { reply.notFound() }) ``` -------------------------------- ### fastify.to(promise) Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Async await wrapper for simplified error handling without try-catch. ```APIDOC ## fastify.to(promise) ### Description An async/await wrapper inspired by `await-to-js` that simplifies error handling by returning a tuple `[error, result]` instead of throwing exceptions. ### Method GET (example usage) ### Endpoint / ### Parameters #### Path Parameters - **promise** (Promise) - Required - The promise to wrap. ### Request Example ```js const [err, user] = await fastify.to( db.findOne({ user: 'tyrion' }) ) ``` ### Response Example ```json [null, { "user": "tyrion" }] // On success ``` ```json [Error("Database error"), null] // On error ``` ``` -------------------------------- ### Create a 404 Not Found Error Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Use the notFound constructor from fastify.httpErrors to create a 404 error. A custom message is optional. ```javascript const notFoundErr = fastify.httpErrors.notFound('custom message') ``` -------------------------------- ### fastify.assert methods Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Provides various assertion methods for checking conditions. ```APIDOC ## fastify.assert methods ### Description Additional assertion methods available on `fastify.assert` for detailed condition checking. ### Methods - `fastify.assert.ok(value, message)` - `fastify.assert.equal(actual, expected, message)` - `fastify.assert.notEqual(actual, expected, message)` - `fastify.assert.strictEqual(actual, expected, message)` - `fastify.assert.notStrictEqual(actual, expected, message)` - `fastify.assert.deepEqual(actual, expected, message)` - `fastify.assert.notDeepEqual(actual, expected, message)` ### Request Example ```js fastify.assert.ok(user, 'User not found') ``` ``` -------------------------------- ### fastify.assert(condition, statusCode, message) Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Verifies a condition and throws an HTTP error if it's false. ```APIDOC ## fastify.assert(condition, statusCode, message) ### Description Verifies if a given condition is true. If the condition is false, it throws an HTTP error with the specified status code and message. Useful for async routes. ### Method GET (example usage) ### Endpoint / ### Parameters #### Path Parameters - **condition** (boolean) - Required - The condition to evaluate. - **statusCode** (number) - Required - The HTTP status code to use if the condition is false. - **message** (string) - Optional - A custom error message. ### Request Example ```js fastify.assert( req.headers.authorization, 400, 'Missing authorization header' ) ``` ``` -------------------------------- ### Assert Conditions and Throw HTTP Errors with fastify.assert Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Use `fastify.assert(condition, statusCode, message)` to verify a condition and throw a specified HTTP error if it's false. This is particularly useful in async routes for error handling. ```js // the custom message is optional fastify.assert( req.headers.authorization, 400, 'Missing authorization header' ) ``` -------------------------------- ### Async Error Handling Wrapper with fastify.to Source: https://github.com/fastify/fastify-sensible/blob/main/README.md The `fastify.to(promise)` utility provides an async/await-friendly way to handle errors from promises without using try-catch blocks, inspired by `await-to-js`. ```js const [err, user] = await fastify.to( db.findOne({ user: 'tyrion' }) ) ``` -------------------------------- ### reply.staticCache(time) Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Sets the cache control header to a public and immutable configuration. ```APIDOC ## reply.staticCache(time) ### Description Sets the cache control header to a public and immutable configuration. ### Method GET (example usage) ### Endpoint / ### Parameters #### Query Parameters - **time** (number | string) - Required - The duration for the cache in seconds or a string representation. ### Request Example ```js fastify.get('/', (req, reply) => { reply.staticCache(42) reply.send('ok') }) ``` ### Response Example ``` Cache-Control: public, max-age=42, immutable ``` ``` -------------------------------- ### Set Cache Control for Public and Immutable with reply.staticCache Source: https://github.com/fastify/fastify-sensible/blob/main/README.md Use `reply.staticCache(time)` to set the cache control header to 'public, max-age=