fastify
Advanced tools
Comparing version 4.26.1 to 4.26.2
@@ -290,2 +290,4 @@ <h1 align="center">Fastify</h1> | ||
try { | ||
await client.connect(); | ||
const postgrator = new Postgrator({ | ||
@@ -292,0 +294,0 @@ migrationPattern: path.join(__dirname, '/migrations/*'), |
@@ -532,2 +532,4 @@ <h1 align="center">Fastify</h1> | ||
OSM plugin to run overpass queries by OpenStreetMap. | ||
- [`fastify-override`](https://github.com/matthyk/fastify-override) | ||
Fastify plugin to override decorators, plugins and hooks for testing purposes | ||
- [`fastify-peekaboo`](https://github.com/simone-sanfratello/fastify-peekaboo) | ||
@@ -534,0 +536,0 @@ Fastify plugin for memoize responses by expressive settings. |
@@ -29,2 +29,3 @@ <h1 align="center">Serverless</h1> | ||
- [Google Cloud Functions](#google-cloud-functions) | ||
- [Google Firebase Functions](#google-firebase-functions) | ||
- [Google Cloud Run](#google-cloud-run) | ||
@@ -207,3 +208,3 @@ - [Netlify Lambda](#netlify-lambda) | ||
export.fastifyFunction = fastifyFunction; | ||
exports.fastifyFunction = fastifyFunction; | ||
``` | ||
@@ -265,2 +266,111 @@ | ||
## Google Firebase Functions | ||
Follow this guide if you want to use Fastify as the HTTP framework for | ||
Firebase Functions instead of the vanilla JavaScript router provided with | ||
`onRequest(async (req, res) => {}`. | ||
### The onRequest() handler | ||
We use the `onRequest` function to wrap our Fastify application instance. | ||
As such, we'll begin with importing it to the code: | ||
```js | ||
const { onRequest } = require("firebase-functions/v2/https") | ||
``` | ||
### Creation of Fastify instance | ||
Create the Fastify instance and encapsulate the returned application instance | ||
in a function which will register routes, await the server's processing of | ||
plugins, hooks and other settings. As follows: | ||
```js | ||
const fastify = require("fastify")({ | ||
logger: true, | ||
}) | ||
const fastifyApp = async (request, reply) => { | ||
await registerRoutes(fastify) | ||
await fastify.ready() | ||
fastify.server.emit("request", request, reply) | ||
} | ||
``` | ||
### Add Custom `contentTypeParser` to Fastify instance and define endpoints | ||
Firebase Function's HTTP layer already parses the request | ||
and makes a JSON payload available. It also provides access | ||
to the raw body, unparsed, which is useful in order to calculate | ||
request signatures to validate HTTP webhooks. | ||
Add as follows to the `registerRoutes()` function: | ||
```js | ||
async function registerRoutes (fastify) { | ||
fastify.addContentTypeParser("application/json", {}, (req, payload, done) => { | ||
// useful to include the request's raw body on the `req` object that will | ||
// later be available in your other routes so you can calculate the HMAC | ||
// if needed | ||
req.rawBody = payload.rawBody | ||
// payload.body is already the parsed JSON so we just fire the done callback | ||
// with it | ||
done(null, payload.body) | ||
}) | ||
// define your endpoints here... | ||
fastify.post("/some-route-here", async (request, reply) => {} | ||
fastify.get('/', async (request, reply) => { | ||
reply.send({message: 'Hello World!'}) | ||
}) | ||
} | ||
``` | ||
### Export the function using Firebase onRequest | ||
Final step is to export the Fastify app instance to Firebase's own | ||
`onRequest()` function so it can pass the request and reply objects to it: | ||
```js | ||
exports.app = onRequest(fastifyApp) | ||
``` | ||
### Local test | ||
Install the Firebase tools functions so you can use the CLI: | ||
```bash | ||
npm i -g firebase-tools | ||
``` | ||
Then you can run your function locally with: | ||
```bash | ||
firebase emulators:start --only functions | ||
``` | ||
### Deploy | ||
Deploy your Firebase Functions with: | ||
```bash | ||
firebase deploy --only functions | ||
``` | ||
#### Read logs | ||
Use the Firebase tools CLI: | ||
```bash | ||
firebase functions:log | ||
``` | ||
### References | ||
- [Fastify on Firebase Functions](https://github.com/lirantal/lemon-squeezy-firebase-webhook-fastify/blob/main/package.json) | ||
- [An article about HTTP webhooks on Firebase Functions and Fastify: A Practical Case Study with Lemon Squeezy](https://lirantal.com/blog/http-webhooks-firebase-functions-fastify-practical-case-study-lemon-squeezy) | ||
## Google Cloud Run | ||
@@ -267,0 +377,0 @@ |
@@ -95,2 +95,3 @@ <h1 align="center">Fastify</h1> | ||
- [FST_ERR_LISTEN_OPTIONS_INVALID](#fst_err_listen_options_invalid) | ||
- [FST_ERR_ERROR_HANDLER_NOT_FN](#fst_err_error_handler_not_fn) | ||
@@ -365,2 +366,3 @@ ### Error Handling In Node.js | ||
| <a id="fst_err_listen_options_invalid">FST_ERR_LISTEN_OPTIONS_INVALID</a> | Invalid listen options. | Check the listen options. | [#4886](https://github.com/fastify/fastify/pull/4886) | | ||
| <a id="fst_err_error_handler_not_fn">FST_ERR_ERROR_HANDLER_NOT_FN</a> | Error Handler must be a function | Provide a function to `setErrorHandler`. | [#5317](https://github.com/fastify/fastify/pull/5317) | | ||
@@ -1096,3 +1096,3 @@ <h1 align="center">Fastify</h1> | ||
If you want to see a detailed example of using this interface check out the | ||
Learn by Example section: [JSON Schema](#jsonschema). | ||
Learn by Example section: [JSON Schema](#json-schema). | ||
@@ -1099,0 +1099,0 @@ ##### fastify.RawRequestDefaultExpression\<[RawServer][RawServerGeneric]\> |
@@ -15,3 +15,3 @@ import * as http from 'http' | ||
import { FastifyErrorCodes } from './types/errors' | ||
import { DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onListenAsyncHookHandler, onListenHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, onRequestAbortHookHandler, onRequestAbortAsyncHookHandler } from './types/hooks' | ||
import { DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onListenAsyncHookHandler, onListenHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, onRequestAbortHookHandler, onRequestAbortAsyncHookHandler, preCloseAsyncHookHandler, preCloseHookHandler } from './types/hooks' | ||
import { FastifyListenOptions, FastifyInstance, PrintRoutesOptions } from './types/instance' | ||
@@ -156,3 +156,3 @@ import { FastifyBaseLogger, FastifyLoggerInstance, FastifyLoggerOptions, PinoLoggerOptions, FastifyLogFn, LogLevel } from './types/logger' | ||
// The RawRequestDefaultExpression, RawReplyDefaultExpression, and FastifyTypeProviderDefault parameters | ||
// should be narrowed further but those generic parameters are not passed to this FastifyServerOptions type | ||
// should be narrowed further but those generic parameters are not passed to this FastifyServerOptions type | ||
this: FastifyInstance<RawServer, RawRequestDefaultExpression<RawServer>, RawReplyDefaultExpression<RawServer>, Logger, FastifyTypeProviderDefault>, | ||
@@ -188,3 +188,3 @@ req: RawRequestDefaultExpression<RawServer> | ||
HTTPMethods, RawServerBase, RawRequestDefaultExpression, RawReplyDefaultExpression, RawServerDefault, ContextConfigDefault, RequestBodyDefault, RequestQuerystringDefault, RequestParamsDefault, RequestHeadersDefault, // './types/utils' | ||
DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onListenAsyncHookHandler, onListenHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, onRequestAbortHookHandler, onRequestAbortAsyncHookHandler, // './types/hooks' | ||
DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, onCloseAsyncHookHandler, onCloseHookHandler, onErrorAsyncHookHandler, onErrorHookHandler, onReadyAsyncHookHandler, onReadyHookHandler, onListenAsyncHookHandler, onListenHookHandler, onRegisterHookHandler, onRequestAsyncHookHandler, onRequestHookHandler, onResponseAsyncHookHandler, onResponseHookHandler, onRouteHookHandler, onSendAsyncHookHandler, onSendHookHandler, onTimeoutAsyncHookHandler, onTimeoutHookHandler, preHandlerAsyncHookHandler, preHandlerHookHandler, preParsingAsyncHookHandler, preParsingHookHandler, preSerializationAsyncHookHandler, preSerializationHookHandler, preValidationAsyncHookHandler, preValidationHookHandler, onRequestAbortHookHandler, onRequestAbortAsyncHookHandler, preCloseAsyncHookHandler, preCloseHookHandler, // './types/hooks' | ||
FastifyServerFactory, FastifyServerFactoryHandler, // './types/serverFactory' | ||
@@ -191,0 +191,0 @@ FastifyTypeProvider, FastifyTypeProviderDefault, // './types/type-provider' |
'use strict' | ||
const VERSION = '4.26.1' | ||
const VERSION = '4.26.2' | ||
@@ -74,3 +74,4 @@ const Avvio = require('avvio') | ||
FST_ERR_ROUTE_REWRITE_NOT_STR, | ||
FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN | ||
FST_ERR_SCHEMA_ERROR_FORMATTER_NOT_FN, | ||
FST_ERR_ERROR_HANDLER_NOT_FN | ||
} = errorCodes | ||
@@ -848,2 +849,6 @@ | ||
if (typeof func !== 'function') { | ||
throw new FST_ERR_ERROR_HANDLER_NOT_FN() | ||
} | ||
this[kErrorHandler] = buildErrorHandler(this[kErrorHandler], func.bind(this)) | ||
@@ -850,0 +855,0 @@ return this |
'use strict' | ||
const { AsyncResource } = require('node:async_hooks') | ||
const { Fifo } = require('toad-cache') | ||
const { FifoMap: Fifo } = require('toad-cache') | ||
const { safeParse: safeParseContentType, defaultContentType } = require('fast-content-type-parse') | ||
@@ -6,0 +6,0 @@ const secureJson = require('secure-json-parse') |
@@ -67,2 +67,8 @@ 'use strict' | ||
), | ||
FST_ERR_ERROR_HANDLER_NOT_FN: createError( | ||
'FST_ERR_ERROR_HANDLER_NOT_FN', | ||
'Error Handler must be a function', | ||
500, | ||
TypeError | ||
), | ||
@@ -69,0 +75,0 @@ /** |
{ | ||
"name": "fastify", | ||
"version": "4.26.1", | ||
"version": "4.26.2", | ||
"description": "Fast and low overhead web framework, for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "fastify.js", |
@@ -8,3 +8,3 @@ 'use strict' | ||
test('should expose 79 errors', t => { | ||
test('should expose 80 errors', t => { | ||
t.plan(1) | ||
@@ -18,7 +18,7 @@ const exportedKeys = Object.keys(errors) | ||
} | ||
t.equal(counter, 79) | ||
t.equal(counter, 80) | ||
}) | ||
test('ensure name and codes of Errors are identical', t => { | ||
t.plan(79) | ||
t.plan(80) | ||
const exportedKeys = Object.keys(errors) | ||
@@ -832,4 +832,14 @@ for (const key of exportedKeys) { | ||
test('FST_ERR_ERROR_HANDLER_NOT_FN', t => { | ||
t.plan(5) | ||
const error = new errors.FST_ERR_ERROR_HANDLER_NOT_FN() | ||
t.equal(error.name, 'FastifyError') | ||
t.equal(error.code, 'FST_ERR_ERROR_HANDLER_NOT_FN') | ||
t.equal(error.message, 'Error Handler must be a function') | ||
t.equal(error.statusCode, 500) | ||
t.ok(error instanceof TypeError) | ||
}) | ||
test('Ensure that all errors are in Errors.md TOC', t => { | ||
t.plan(79) | ||
t.plan(80) | ||
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8') | ||
@@ -846,3 +856,3 @@ | ||
test('Ensure that non-existing errors are not in Errors.md TOC', t => { | ||
t.plan(79) | ||
t.plan(80) | ||
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8') | ||
@@ -860,3 +870,3 @@ | ||
test('Ensure that all errors are in Errors.md documented', t => { | ||
t.plan(79) | ||
t.plan(80) | ||
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8') | ||
@@ -873,3 +883,3 @@ | ||
test('Ensure that non-existing errors are not in Errors.md documented', t => { | ||
t.plan(79) | ||
t.plan(80) | ||
const errorsMd = readFileSync(resolve(__dirname, '../../docs/Reference/Errors.md'), 'utf8') | ||
@@ -876,0 +886,0 @@ |
@@ -60,3 +60,3 @@ import fastify, { | ||
}) | ||
const lightMyRequestCallback: LightMyRequestCallback = (err: Error, response: LightMyRequestResponse) => { | ||
const lightMyRequestCallback: LightMyRequestCallback = (err: Error | undefined, response: LightMyRequestResponse | undefined) => { | ||
if (err) throw err | ||
@@ -63,0 +63,0 @@ } |
@@ -15,3 +15,6 @@ import { FastifyError } from '@fastify/error' | ||
RegisterOptions, | ||
RouteOptions | ||
RouteOptions, | ||
// preClose hook types should be exported correctly https://github.com/fastify/fastify/pull/5335 | ||
preCloseAsyncHookHandler, | ||
preCloseHookHandler | ||
} from '../../fastify' | ||
@@ -18,0 +21,0 @@ import { DoneFuncWithErrOrRes, HookHandlerDoneFunction, RequestPayload, preHandlerAsyncHookHandler } from '../../types/hooks' |
2421829
332
58042
22