|
| 1 | +# V3 Migration Guide |
| 2 | + |
| 3 | +This guide is aimed to help migration from Fastify v2 to v3. |
| 4 | + |
| 5 | +Before beginning please ensure that any deprecation warnings from v2 are fixed as we have removed all deprecated features and they will no longer work after upgrading. ([#1750](https://github.com/fastify/fastify/pull/1750)) |
| 6 | + |
| 7 | +## Breaking changes |
| 8 | + |
| 9 | +### Changed middleware support ([#2014](https://github.com/fastify/fastify/pull/2014)) |
| 10 | + |
| 11 | +From Fastify v3, middleware support does not come out of the box with the framework itself. |
| 12 | + |
| 13 | +If you use Express middleware in your application, please install and register the [`fastify-express`](https://github.com/fastify/fastify-express) or [`middie`](https://github.com/fastify/middie) plugin before doing so. |
| 14 | + |
| 15 | +**v2:** |
| 16 | + |
| 17 | +```js |
| 18 | +fastify.use(require('cors')()); |
| 19 | +``` |
| 20 | + |
| 21 | +**v3:** |
| 22 | + |
| 23 | +```js |
| 24 | +await fastify.register(require('fastify-express')); |
| 25 | +fastify.use(require('cors')()); |
| 26 | +``` |
| 27 | + |
| 28 | +### Changed logging serialization ([#2017](https://github.com/fastify/fastify/pull/2017)) |
| 29 | + |
| 30 | +We have updated our logging [Serializers](https://github.com/fastify/fastify/blob/master/docs/Logging.md) to now receive Fastify [`Request`](https://github.com/fastify/fastify/blob/master/docs/Request.md) and [`Reply`](https://github.com/fastify/fastify/blob/master/docs/Reply.md) objects instead of native ones. |
| 31 | + |
| 32 | +If you have created custom serializers they will need updating if they expect properties that aren't exposed by the Fastify objects themselves. |
| 33 | + |
| 34 | +**v2:** |
| 35 | + |
| 36 | +```js |
| 37 | +const fastify = require('fastify')({ |
| 38 | + logger: { |
| 39 | + serializers: { |
| 40 | + res(res) { |
| 41 | + return { |
| 42 | + statusCode: res.statusCode, |
| 43 | + customProp: res.customProp |
| 44 | + }; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +}); |
| 49 | +``` |
| 50 | + |
| 51 | +**v3:** |
| 52 | + |
| 53 | +```js |
| 54 | +const fastify = require('fastify')({ |
| 55 | + logger: { |
| 56 | + serializers: { |
| 57 | + res(reply) { |
| 58 | + return { |
| 59 | + statusCode: reply.statusCode, // No change required |
| 60 | + customProp: reply.raw.customProp // Log custom property from res object |
| 61 | + }; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +}); |
| 66 | +``` |
| 67 | + |
| 68 | +### Changed schema substitution ([#2023](https://github.com/fastify/fastify/pull/2023)) |
| 69 | + |
| 70 | +We have dropped support for non-standard `replace-way` shared schema substitution and replaced it with standard compliant JSON Schema `$ref` based substitution. To better understand this change read [Validation and Serialization in Fastify v3](https://dev.to/eomm/validation-and-serialization-in-fastify-v3-2e8l). |
| 71 | + |
| 72 | +**v2:** |
| 73 | + |
| 74 | +```js |
| 75 | +const schema = { |
| 76 | + body: 'schemaId#' |
| 77 | +}; |
| 78 | +fastify.route({ method, url, schema, handler }); |
| 79 | +``` |
| 80 | + |
| 81 | +**v3:** |
| 82 | + |
| 83 | +```js |
| 84 | +const schema = { |
| 85 | + body: { |
| 86 | + $ref: 'schemaId#' |
| 87 | + } |
| 88 | +}; |
| 89 | +fastify.route({ method, url, schema, handler }); |
| 90 | +``` |
| 91 | + |
| 92 | +### Changed schema validation options ([#2023](https://github.com/fastify/fastify/pull/2023)) |
| 93 | + |
| 94 | +We have replaced `setSchemaCompiler` and `setSchemaResolver` options with `setValidatorCompiler` to enable future tooling improvements. To deepen this change [read the article](https://dev.to/eomm/validation-and-serialization-in-fastify-v3-2e8l). |
| 95 | + |
| 96 | +**v2:** |
| 97 | + |
| 98 | +```js |
| 99 | +const fastify = Fastify(); |
| 100 | +const ajv = new AJV(); |
| 101 | +ajv.addSchema(schemaA); |
| 102 | +ajv.addSchema(schemaB); |
| 103 | + |
| 104 | +fastify.setSchemaCompiler(schema => ajv.compile(schema)); |
| 105 | +fastify.setSchemaResolver(ref => ajv.getSchema(ref).schema); |
| 106 | +``` |
| 107 | + |
| 108 | +**v3:** |
| 109 | + |
| 110 | +```js |
| 111 | +const fastify = Fastify(); |
| 112 | +const ajv = new AJV(); |
| 113 | +ajv.addSchema(schemaA); |
| 114 | +ajv.addSchema(schemaB); |
| 115 | + |
| 116 | +fastify.setValidatorCompiler(({ schema, method, url, httpPart }) => |
| 117 | + ajv.compile(schema) |
| 118 | +); |
| 119 | +``` |
| 120 | + |
| 121 | +### Changed hooks behaviour ([#2004](https://github.com/fastify/fastify/pull/2004)) |
| 122 | + |
| 123 | +From Fastify v3, the behavior of `onRoute` and `onRegister` hooks will change slightly in order to support hook encapsulation. |
| 124 | + |
| 125 | +- `onRoute` - The hook will be called asynchronously, in v1/v2 it's called as soon as a route is registered. This means that if you want to use it, you should register this hook as soon as possible in your code. |
| 126 | +- `onRegister` - Same as the onRoute hook, the only difference is that now the very first call will no longer be the framework itself, but the first registered plugin |
| 127 | + |
| 128 | +### Changed TypeScript support |
| 129 | + |
| 130 | +The type system was changed in Fastify version 3. The new type system introduces generic constraining and defaulting, plus a new way to define schema types such as a request body, querystring, and more! |
| 131 | + |
| 132 | +**v2:** |
| 133 | + |
| 134 | +```ts |
| 135 | +interface PingQuerystring { |
| 136 | + foo?: number; |
| 137 | +} |
| 138 | + |
| 139 | +interface PingParams { |
| 140 | + bar?: string; |
| 141 | +} |
| 142 | + |
| 143 | +interface PingHeaders { |
| 144 | + a?: string; |
| 145 | +} |
| 146 | + |
| 147 | +interface PingBody { |
| 148 | + baz?: string; |
| 149 | +} |
| 150 | + |
| 151 | +server.get<PingQuerystring, PingParams, PingHeaders, PingBody>( |
| 152 | + '/ping/:bar', |
| 153 | + opts, |
| 154 | + (request, reply) => { |
| 155 | + console.log(request.query); // This is of type `PingQuerystring` |
| 156 | + console.log(request.params); // This is of type `PingParams` |
| 157 | + console.log(request.headers); // This is of type `PingHeaders` |
| 158 | + console.log(request.body); // This is of type `PingBody` |
| 159 | + } |
| 160 | +); |
| 161 | +``` |
| 162 | + |
| 163 | +**v3:** |
| 164 | + |
| 165 | +```ts |
| 166 | +server.get<{ |
| 167 | + Querystring: PingQuerystring; |
| 168 | + Params: PingParams; |
| 169 | + Headers: PingHeaders; |
| 170 | + Body: PingBody; |
| 171 | +}>('/ping/:bar', opts, async (request, reply) => { |
| 172 | + console.log(request.query); // This is of type `PingQuerystring` |
| 173 | + console.log(request.params); // This is of type `PingParams` |
| 174 | + console.log(request.headers); // This is of type `PingHeaders` |
| 175 | + console.log(request.body); // This is of type `PingBody` |
| 176 | +}); |
| 177 | +``` |
| 178 | + |
| 179 | +## Further additions and improvements |
| 180 | + |
| 181 | +- Hooks now have consistent context irregardless of how they are registered ([#2005](https://github.com/fastify/fastify/pull/2005)) |
| 182 | +- Deprecated `request.req` and `reply.res` for [`request.raw`](https://github.com/fastify/fastify/blob/master/docs/Request.md) and [`reply.raw`](https://github.com/fastify/fastify/blob/master/docs/Reply.md) ([#2008](https://github.com/fastify/fastify/pull/2008)) |
| 183 | +- Removed `modifyCoreObjects` option ([#2015](https://github.com/fastify/fastify/pull/2015)) |
| 184 | +- Added [`connectionTimeout`](https://github.com/fastify/fastify/blob/master/docs/Server.md#factory-connection-timeout) option ([#2086](https://github.com/fastify/fastify/pull/2086)) |
| 185 | +- Added [`keepAliveTimeout`](https://github.com/fastify/fastify/blob/master/docs/Server.md#factory-keep-alive-timeout) option ([#2086](https://github.com/fastify/fastify/pull/2086)) |
| 186 | +- Added async-await support for [plugins](https://github.com/fastify/fastify/blob/master/docs/Plugins.md#async-await) ([#2093](https://github.com/fastify/fastify/pull/2093)) |
0 commit comments