-
Notifications
You must be signed in to change notification settings - Fork 36
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
arshcodemod
wants to merge
6
commits into
codemod-com:main
Choose a base branch
from
arshcodemod:feat-fastify-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a5d3f7b
feat(codemod): add codemods for fastify v4
arshcodemod 287a326
feat(codemod): manual tempering needed
arshcodemod ccb440b
feat(codemod):needs manual intervention
arshcodemod 7a5f07f
feat(codemod): minor fix
arshcodemod 9f55c31
fix(codemod): remove-app-use
arshcodemod affc29e
update reply-raw-access codemod
arshcodemod File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
packages/codemods/fastify/4/await-register-calls/.codemodrc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
packages/codemods/fastify/4/await-register-calls/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
``` |
8 changes: 8 additions & 0 deletions
8
packages/codemods/fastify/4/await-register-calls/__testfixtures__/fixture1.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
7 changes: 7 additions & 0 deletions
7
packages/codemods/fastify/4/await-register-calls/__testfixtures__/fixture1.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
packages/codemods/fastify/4/await-register-calls/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@codemod/fastify-4-await-register-calls", | ||
"dependencies": {}, | ||
"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
27
packages/codemods/fastify/4/await-register-calls/src/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
packages/codemods/fastify/4/await-register-calls/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/codemods/fastify/4/await-register-calls/vitest.config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
packages/codemods/fastify/4/migration-recipe/.codemodrc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
16 changes: 16 additions & 0 deletions
16
packages/codemods/fastify/4/remove-app-use/.codemodrc.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
``` |
14 changes: 14 additions & 0 deletions
14
packages/codemods/fastify/4/remove-app-use/__testfixtures__/fixture1.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |
19 changes: 19 additions & 0 deletions
19
packages/codemods/fastify/4/remove-app-use/__testfixtures__/fixture1.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "@codemod/fastify-4-remove-app-use", | ||
"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" | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.