Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(codemod): add codemods for fastify v4 #1254

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"version": "1.0.2",
"private": false,
"name": "fastify/4/await-register-calls",
"engine": "jscodeshift",
"applicability": {
"from": [["fastify", ">=", "3.0.0"], ["fastify", "<", "4.0.0"]],
"to": [["fastify", ">=", "4.0.0"]]
},
"meta": {
"tags": ["fastify", "4", "migration"],
"git": "https://github.com/codemod-com/codemod/tree/main/packages/codemods/fastify/4/await-register-calls"
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/*.vue"]
}
2 changes: 2 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 Codemod Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
This codemod helps to improve error reporting in route definitions, route registration is now synchronous, so if you specify an onRoute hook in a plugin, you should now use await.register()

## What Changed

To improve error reporting in route definitions, route registration is now synchronous. As a result, if you specify an onRoute hook in a plugin you should now use await.register()


## Before

```jsx
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
});
done();
});
```

## After

```jsx
await fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
});
done();
});
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// biome-ignore lint/correctness/useHookAtTopLevel: <explanation>
fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
});
done();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
await fastify.register((instance, opts, done) => {
instance.addHook('onRoute', (routeOptions) => {
const { path, method } = routeOptions;
console.log({ path, method });
});
done();
});
15 changes: 15 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@codemod/fastify-4-await-register-calls",
"dependencies": {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"private": true

"devDependencies": {
"typescript": "^5.2.2",
"ts-node": "^10.9.1",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10",
"vitest": "^1.0.1",
"@vitest/coverage-v8": "^1.0.1"
},
"scripts": {},
"files": ["./README.md", "./.codemodrc.json", "./dist/index.cjs"],
"type": "module"
}
27 changes: 27 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { API, FileInfo, Options } from "jscodeshift";

export default function transform(file: FileInfo, api: API, options?: Options) {
const j = api.jscodeshift;
const root = j(file.source);
let dirtyFlag = false;

// Find all CallExpressions where the callee is fastify.register
root
.find(j.CallExpression, {
callee: {
object: { name: "fastify" },
property: { name: "register" },
},
})
.forEach((path) => {
// Check if the parent node is an AwaitExpression
const parent = path.parent.node;
if (!j.AwaitExpression.check(parent)) {
// Replace the CallExpression with an AwaitExpression
j(path).replaceWith(j.awaitExpression(path.node));
dirtyFlag = true;
}
});

return dirtyFlag ? root.toSource() : undefined;
}
24 changes: 24 additions & 0 deletions packages/codemods/fastify/4/await-register-calls/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"outDir": "./dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"module": "NodeNext",
"skipLibCheck": true,
"strict": true,
"target": "ES6",
"allowJs": true,
"noUncheckedIndexedAccess": true
},
"include": [
"./src/**/*.ts",
"./src/**/*.js",
"./test/**/*.ts",
"./test/**/*.js"
],
"exclude": ["node_modules", "./dist/**/*"],
"ts-node": {
"transpileOnly": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { configDefaults, defineConfig } from "vitest/config";

export default defineConfig({
test: {
include: [...configDefaults.include, "**/test/*.ts"],
},
});
24 changes: 24 additions & 0 deletions packages/codemods/fastify/4/migration-recipe/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"name": "fastify/4/migration-recipe",
"version": "1.0.2",
"private": false,
"engine": "recipe",
"names": [
"fastify/4/await-register-calls",
"fastify/4/expose-head-routes",
"fastify/4/remove-app-use",
"fastify/4/reply-raw-access",
"fastify/4/url-params-optional",
"fastify/4/wrap-routes-plugin"
],
"applicability": {
"from": [["fastify", ">=", "3.0.0"], ["fastify", "<", "4.0.0"]],
"to": [["fastify", ">=", "4.0.0"]]
},
"meta": {
"tags": ["fastify", "migration"],
"git": "https://github.com/codemod-com/codemod/tree/main/packages/codemods/fastify/4/migration-recipe"
},
"include": ["/.js", "/.jsx", "/.ts", "/.tsx", "/*.vue"]
}
10 changes: 10 additions & 0 deletions packages/codemods/fastify/4/migration-recipe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This recipe is a set of codemods that will help migrate to Fastify 4.

The recipe includes the following codemods:

- fastify/4/await-register-calls
- fastify/4/expose-head-routes
- fastify/4/remove-app-use
- fastify/4/reply-raw-access
- fastify/4/url-params-optional
- fastify/4/wrap-routes-plugin
5 changes: 5 additions & 0 deletions packages/codemods/fastify/4/migration-recipe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "@codemod/fastify-4-migration-recipe",
"files": ["./README.md", "./.codemodrc.json"],
"type": "module"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"private": true

16 changes: 16 additions & 0 deletions packages/codemods/fastify/4/remove-app-use/.codemodrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "https://codemod-utils.s3.us-west-1.amazonaws.com/configuration_schema.json",
"version": "1.0.2",
"private": false,
"name": "fastify/4/remove-app-use",
"engine": "jscodeshift",
"applicability": {
"from": [["fastify", ">=", "3.0.0"], ["fastify", "<", "4.0.0"]],
"to": [["fastify", ">=", "4.0.0"]]
},
"meta": {
"tags": ["fastify", "4", "migration"],
"git": "https://github.com/codemod-com/codemod/tree/main/packages/codemods/fastify/4/remove-app-use"
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/*.vue"]
}
2 changes: 2 additions & 0 deletions packages/codemods/fastify/4/remove-app-use/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
9 changes: 9 additions & 0 deletions packages/codemods/fastify/4/remove-app-use/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
The MIT License (MIT)

Copyright (c) 2024 Codemod Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 changes: 53 additions & 0 deletions packages/codemods/fastify/4/remove-app-use/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
This codemod removes app.use() and the use of middleware is no longer supported.

🚦 **Impact Level**: Minimal

## What Changed

With v4 of Fastify, app.use() has been removed and the use of middleware is no longer supported.

If you need to use middleware, use @fastify/middie or @fastify/express, which will continue to be maintained. However, it is strongly recommended that you migrate to Fastify's hooks.


## Before

```jsx
const fastify = require('fastify')();

fastify.use((req, res, next) => {
console.log('Middleware executed');
next();
});

fastify.get('/example', (req, res) => {
res.send('Hello, World!');
});

fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});

```

## After

```jsx
const fastify = require('fastify')();
const middie = require('@fastify/middie');

fastify.register(middie);

fastify.use((req, res, next) => {
console.log('Middleware executed');
next();
});

fastify.get('/example', (req, res) => {
res.send('Hello, World!');
});

fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fastify = require('fastify')();

fastify.use((req, res, next) => {
console.log('Middleware executed');
next();
});

fastify.get('/example', (req, res) => {
res.send('Hello, World!');
});

fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fastify = require('fastify')();
const middie = require('@fastify/middie');

fastify.register(middie);

fastify.use((req, res, next) => {
console.log('Middleware executed');
next();
});

fastify.get('/example', (req, res) => {
res.send('Hello, World!');
});

fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});


15 changes: 15 additions & 0 deletions packages/codemods/fastify/4/remove-app-use/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@codemod/fastify-4-remove-app-use",
"dependencies": {},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"private": true

"devDependencies": {
"typescript": "^5.2.2",
"ts-node": "^10.9.1",
"jscodeshift": "^0.15.1",
"@types/jscodeshift": "^0.11.10",
"vitest": "^1.0.1",
"@vitest/coverage-v8": "^1.0.1"
},
"scripts": {},
"files": ["./README.md", "./.codemodrc.json", "./dist/index.cjs"],
"type": "module"
}
Loading
Loading