Codemod Registry
Explore community-led codemods to migrate, optimize, and transform your codebase.
Ready to contribute?
Build your own codemod and share it with the community.
Explore community-led codemods to migrate, optimize, and transform your codebase.
Build your own codemod and share it with the community.
Explore community-led codemods to migrate, optimize, and transform your codebase.
This codemod updates `fastify.register` to use `return` instead of `done`, reflecting changes in Fastify v5 for asynchronous plugin registration. ### Before ```ts fastify.register(async function(instance, opts, done) { done(); }); ``` ### After ```ts fastify.register(async function(instance, opts) { return; }); ``` ---
This recipe provides a set of codemods designed to assist with migrating to Fastify 5. Each codemod addresses specific changes and enhancements introduced in Fastify 5. ### Included Codemods - **`fastify/5/req-params-hasOwnProperty-to-objectHasOwn`**: Converts `req.params.hasOwnProperty` to `Object.hasOwn`. - **`fastify/5/listen-arg-transformation`**: Updates the transformation of arguments passed to `listen`. - **`fastify/5/replace-hardcoded-url-in-hasroute`**: Replaces hardcoded URLs in `hasRoute` checks. - **`fastify/5/replace-reply-sent-with-hijack`**: Replaces `reply.sent` with `reply.hijack`. - **`fastify/5/add-expose-head-routes-option`**: Adds the `exposeHeadRoutes` option where necessary. - **`fastify/5/decorate-request-to-getter-method`**: Converts request decorators to getter methods. - **`fastify/5/route-schema-enhancement`**: Enhances route schemas with new features. - **`fastify/5/req-connection-to-socket`**: Converts `req.connection` to `req.socket`. - **`fastify/5/getResponseTime-to-elapsedTime`**: Renames `getResponseTime` to `elapsedTime`. - **`fastify/5/redirect-arg-order`**: Updates the argument order for `redirect`. - **`fastify/5/make-reply-trailer-async`**: Converts `reply.trailer` to an async method. - **`fastify/5/remove-done-callback`**: Changes `plugin.register` from `done` callback to `return`. - **`fastify/5/rename-logger-to-logger-instance`**: Renames `logger` to `loggerInstance`. These codemods will help streamline your migration process and ensure compatibility with Fastify 5. ---
This codemod updates the Fastify logger configuration by renaming the `logger` option to `loggerInstance`, in line with Fastify v5 changes. ### Before ```ts const logger = require('pino')(); const fastify = require('fastify')({ logger, }); ``` ### After ```ts const loggerInstance = require('pino')(); const fastify = require('fastify')({ loggerInstance, }); ``` --- This example shows how the codemod modifies the logger option to `loggerInstance`, aligning with the updated configuration practices in Fastify v5.
This codemod updates the `reply.trailer` method to use an `async` function for handling trailers, aligning with Fastify v5's support for asynchronous operations in trailers. ### Before ```ts fastify.get('/route', (req, reply) => { reply.trailer('ETag', function(reply, payload) { return 'custom-etag'; }); reply.send(''); }); ``` ### After ```ts fastify.get('/route', (req, reply) => { reply.trailer('ETag', async function(reply, payload) { return 'custom-etag'; }); reply.send(''); }); ``` ---
This codemod updates `reply.redirect` by placing the status code as the second argument, as per Fastify v5 conventions. ### Before ```ts reply.redirect(301, '/new-route'); ``` ### After ```ts reply.redirect('/new-route', 301); ``` ---
This codemod converts `reply.getResponseTime()` to `reply.elapsedTime`, reflecting changes in Fastify v5 for retrieving response time. ### Before ```ts fastify.get('/route', (req, reply) => { console.log(reply.getResponseTime()); return { hello: 'world' }; }); ``` ### After ```ts fastify.get('/route', (req, reply) => { console.log(reply.elapsedTime); return { hello: 'world' }; }); ``` ---
This codemod updates references from `req.connection` to `req.socket`, reflecting changes in Fastify v5's request handling. ### Before ```ts fastify.get('/route', (req, reply) => { console.log(req.connection.remoteAddress); return { hello: 'world' }; }); ``` ### After ```ts fastify.get('/route', (req, reply) => { console.log(req.socket.remoteAddress); return { hello: 'world' }; }); ``` ---
This codemod enhances the schema definition for query strings by converting it to the full object schema format, adding properties and required fields. ### Before ```ts fastify.get( '/route', { schema: { querystring: { name: { type: 'string' }, }, }, }, (req, reply) => { reply.send({ hello: req.query.name }); }, ); ``` ### After ```ts fastify.get( '/route', { schema: { querystring: { type: 'object', properties: { name: { type: 'string' }, }, required: ['name'], }, }, }, (req, reply) => { reply.send({ hello: req.query.name }); }, ); ``` --- This illustrates how the codemod updates query string schema definitions in Fastify v5 to the more detailed object schema format, ensuring proper validation.
This codemod transforms `fastify.decorateRequest` with static objects into the new Fastify v5 pattern using a getter function. ### Before ```ts fastify.decorateRequest('myObject', { hello: 'world' }); ``` ### After ```ts fastify.decorateRequest('myObject', { getter() { return { hello: 'world' }; }, }); ``` --- This shows the transition from decorating requests with static objects to using a getter method in Fastify v5, which allows for more dynamic and flexible request decorations.
This codemod introduces the `exposeHeadRoutes: false` option to avoid automatic `HEAD` route exposure, while keeping manually defined `HEAD` routes intact. ### Before ```ts fastify.get('/route', {}, (req, reply) => { reply.send({ hello: 'world' }); }); fastify.head('/route', (req, reply) => { // ... }); ``` ### After ```ts fastify.get( '/route', { exposeHeadRoutes: false, }, (req, reply) => { reply.send({ hello: 'world' }); }, ); fastify.head('/route', (req, reply) => { // ... }); ``` ---
This codemod turns `reply.sent = true` into `reply.hijack()`, updating to the new Fastify v5 method for handling manual responses. ### Before ```ts fastify.get('/route', (req, reply) => { reply.sent = true; reply.raw.end('hello'); }); ``` ### After ```ts fastify.get('/route', (req, reply) => { reply.hijack(); reply.raw.end('hello'); }); ``` ---
This codemod highlights the shift in how routes with dynamic parameters should be referenced in `fastify.hasRoute` in Fastify v5. ### Before ```ts fastify.get('/example/:file(^\\d+).png', function(request, reply) {}); console.log( fastify.hasRoute({ method: 'GET', url: '/example/12345.png', }), ); ``` ### After ```ts fastify.get('/example/:file(^\\d+).png', function(request, reply) {}); console.log( fastify.hasRoute({ method: 'GET', url: '/example/:file(^\\d+).png', }), ); ``` ---
Build your own codemod and share it with the community.