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 1 commit
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.
33 changes: 33 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,33 @@
This codemod helps 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 either:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is duplicated



🚦 **Impact Level**: Minimal

## 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
Member

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"],
},
});
16 changes: 16 additions & 0 deletions packages/codemods/fastify/4/expose-head-routes/.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/expose-head-routes",
"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/expose-head-routes"
},
"include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx", "**/*.vue"]
}
2 changes: 2 additions & 0 deletions packages/codemods/fastify/4/expose-head-routes/.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/expose-head-routes/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.
36 changes: 36 additions & 0 deletions packages/codemods/fastify/4/expose-head-routes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This codemod makes exposeHeadRoutes true by default

🚦 **Impact Level**: Minimal

## What Changed

Starting with v4, every GET route will create a sibling HEAD route. You can revert this behavior by setting exposeHeadRoutes: false in the server options.

## Before

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

fastify.get('/example', (request, reply) => {
reply.send({ message: 'Hello, World!' });
});

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

```

## After

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

fastify.get('/example', (request, reply) => {
reply.send({ message: 'Hello, World!' });
});
fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});

```
Copy link
Contributor

Choose a reason for hiding this comment

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

before and after look identical to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is manual change and we might not need a codemod for this. this is not required.

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

fastify.get('/example', (request, reply) => {
reply.send({ message: '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,8 @@
const fastify = require('fastify')();

fastify.get('/example', (request, reply) => {
reply.send({ message: 'Hello, World!' });
});
fastify.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
15 changes: 15 additions & 0 deletions packages/codemods/fastify/4/expose-head-routes/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@codemod/fastify-4-expose-head-routes",
"dependencies": {},
Copy link
Member

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/expose-head-routes/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 fastify.listen calls
root
.find(j.CallExpression, {
callee: {
object: { name: "fastify" },
property: { name: "listen" },
},
})
.forEach((path) => {
// Insert comment above the fastify.listen call
const comment = j.commentLine(
" A HEAD request to the /example endpoint will automatically respond with the same headers as the GET request.",
);
path.value.comments = path.value.comments || [];
path.value.comments.unshift(comment);
dirtyFlag = true;
});

return dirtyFlag ? root.toSource() : undefined;
}
24 changes: 24 additions & 0 deletions packages/codemods/fastify/4/expose-head-routes/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
Member

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.
Loading
Loading