-
Notifications
You must be signed in to change notification settings - Fork 42
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
a5d3f7b
287a326
ccb440b
7a5f07f
9f55c31
affc29e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
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. |
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: | ||
|
||
|
||
🚦 **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(); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@codemod/fastify-4-await-register-calls", | ||
"dependencies": {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} |
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; | ||
} |
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"], | ||
}, | ||
}); |
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"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
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. |
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'); | ||
}); | ||
|
||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. before and after look identical to me There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@codemod/fastify-4-expose-head-routes", | ||
"dependencies": {}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
} |
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; | ||
} |
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"], | ||
}, | ||
}); |
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"] | ||
} |
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 |
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" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "private": true |
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"] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
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. |
There was a problem hiding this comment.
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