-
-
Notifications
You must be signed in to change notification settings - Fork 31
feat(axios-to-whatwg-fetch): introduce
#269
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
Open
AugustinMauroy
wants to merge
38
commits into
main
Choose a base branch
from
feat/axios-to-whatwg-fetch
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.
+1,223
−6
Open
Changes from all commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
6f94b7a
wip
brunocroh e51e1cb
wip
brunocroh b74ed68
wip
brunocroh 320dd82
wip
brunocroh 84c09b7
wip
brunocroh 9bfe594
wip
brunocroh a08cf23
wip
brunocroh 335b801
wip
brunocroh 398aa8e
wip
brunocroh 85325e9
Merge branch 'main' into feat/axios-to-whatwg-fetch
AugustinMauroy 667a821
Update
AugustinMauroy c828c60
add jsdoc
AugustinMauroy 8a5629a
write readme
AugustinMauroy ca6dc94
test: correct white space
AugustinMauroy 8cf7ab6
update dep
AugustinMauroy 691931d
wip
AugustinMauroy 4eb9d44
Update package-lock.json
AugustinMauroy 7064c88
feat(`axios-to-whatwg-fetch`): add support request
AugustinMauroy 560db6d
WIP
AugustinMauroy faf6ac2
update
AugustinMauroy 03a36d7
include tsx
AugustinMauroy 69fb048
Update workflow.ts
AugustinMauroy 88e6b69
Merge branch 'main' into feat/axios-to-whatwg-fetch
AugustinMauroy d712823
add removing dep part
AugustinMauroy e3ca8f5
Update package-lock.json
AugustinMauroy eb0f5e3
fix: yaml formatting
AugustinMauroy 13893a1
don't use EOL ?
AugustinMauroy bddf181
fix ?
AugustinMauroy 71d6cdc
simplify
AugustinMauroy 2d056a5
Update recipes/axios-to-whatwg-fetch/src/workflow.ts
AugustinMauroy 8a8b987
Update recipes/axios-to-whatwg-fetch/src/workflow.ts
AugustinMauroy 85beb77
Update recipes/axios-to-whatwg-fetch/src/workflow.ts
AugustinMauroy 8f11004
Update recipes/axios-to-whatwg-fetch/src/workflow.ts
AugustinMauroy 890c10f
WIP
AugustinMauroy 2b8a759
fix formating in output
AugustinMauroy c4a29b2
wip
AugustinMauroy baa2632
WIP
AugustinMauroy 631d0da
Merge branch 'main' into feat/axios-to-whatwg-fetch
AugustinMauroy 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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,142 @@ | ||
| # Axios to WHATWG Fetch Codemod | ||
|
|
||
| ## Description | ||
|
|
||
| This codemod transforms code using Axios to leverage the WHATWG Fetch API, which is now natively available in Node.js. By replacing Axios with Fetch, you can reduce dependencies, mitigate risks, and improve performance. | ||
|
|
||
| ## Supported Transformations | ||
|
|
||
| The codemod supports the following Axios methods and converts them to their Fetch equivalents: | ||
|
|
||
| - `axios.request(config)` | ||
| - `axios.get(url[, config])` | ||
| - `axios.delete(url[, config])` | ||
| - `axios.head(url[, config])` | ||
| - `axios.options(url[, config])` | ||
| - `axios.post(url[, data[, config]])` | ||
| - `axios.put(url[, data[, config]])` | ||
| - `axios.patch(url[, data[, config]])` | ||
| - `axios.postForm(url[, data[, config]])` | ||
| - `axios.putForm(url[, data[, config]])` | ||
| - `axios.patchForm(url[, data[, config]])` | ||
| - `axios.request(config)` | ||
|
|
||
| ### Examples | ||
|
|
||
| #### GET Request | ||
|
|
||
| ```diff | ||
| const base = 'https://dummyjson.com/todos'; | ||
|
|
||
| - const all = await axios.get(base); | ||
| + const all = await fetch(base).then(async (res) => Object.assign(res, { data: await res.json() })).catch(() => null); | ||
| console.log('\nGET /todos ->', all.status); | ||
| console.log(`Preview: ${all.data.todos.length} todos`); | ||
| ``` | ||
|
|
||
| #### POST Request | ||
|
|
||
| ```diff | ||
| const base = 'https://dummyjson.com/todos'; | ||
|
|
||
| - const created = await axios.post( | ||
| - `${base}/add`, { | ||
| - todo: 'Use DummyJSON in the project', | ||
| - completed: false, | ||
| - userId: 5, | ||
| - }, { | ||
| - headers: { 'Content-Type': 'application/json' } | ||
| - } | ||
| - ); | ||
| + const created = await fetch(`${base}/add`, { | ||
| + method: 'POST', | ||
| + headers: { 'Content-Type': 'application/json' }, | ||
| + body: JSON.stringify({ | ||
| + todo: 'Use DummyJSON in the project', | ||
| + completed: false, | ||
| + userId: 5, | ||
| + }), | ||
| + }).then(async (res) => Object.assign(res, { data: await res.json() })); | ||
| console.log('\nPOST /todos/add ->', created.status); | ||
| console.log('Preview:', created.data?.id ? `created id ${created.data.id}` : JSON.stringify(created.data).slice(0,200)); | ||
| ``` | ||
|
|
||
| #### POST Form Request | ||
|
|
||
| ```diff | ||
| const formEndpoint = '/submit'; | ||
|
|
||
| - const created = await axios.postForm(formEndpoint, { | ||
| - title: 'Form Demo', | ||
| - completed: false, | ||
| - }); | ||
| + const created = await fetch(formEndpoint, { | ||
| + method: 'POST', | ||
| + body: new URLSearchParams({ | ||
| + title: 'Form Demo', | ||
| + completed: false, | ||
| + }), | ||
| + }).then(async (res) => Object.assign(res, { data: await res.json() })); | ||
| console.log('Preview:', created.data); | ||
| ``` | ||
|
|
||
| #### PUT Request | ||
|
|
||
| ```diff | ||
| const base = 'https://dummyjson.com/todos'; | ||
|
|
||
| - const updatedPut = await axios.put( | ||
| - `${base}/1`, | ||
| - { completed: false }, | ||
| - { headers: { 'Content-Type': 'application/json' } } | ||
| - ); | ||
| + const updatedPut = await fetch(`${base}/1`, { | ||
| + method: 'PUT', | ||
| + headers: { 'Content-Type': 'application/json' }, | ||
| + body: JSON.stringify({ completed: false }), | ||
| + }).then(async (res) => Object.assign(res, { data: await res.json() })); | ||
| console.log('\nPUT /todos/1 ->', updatedPut.status); | ||
| console.log('Preview:', updatedPut.data?.completed !== undefined ? `completed=${updatedPut.data.completed}` : JSON.stringify(updatedPut.data).slice(0,200)); | ||
| ``` | ||
|
|
||
| #### DELETE Request | ||
|
|
||
| ```diff | ||
| const base = 'https://dummyjson.com/todos'; | ||
|
|
||
| - const deleted = await axios.delete(`${base}/1`); | ||
| + const deleted = await fetch(`${base}/1`, { method: 'DELETE' }) | ||
| + .then(async (res) => Object.assign(res, { data: await res.json() })); | ||
| console.log('\nDELETE /todos/1 ->', deleted.status); | ||
| console.log('Preview:', deleted.data ? JSON.stringify(deleted.data).slice(0,200) : typeof deleted.data); | ||
| ``` | ||
|
|
||
| #### `request` axios Method | ||
|
|
||
| ```diff | ||
| const base = 'https://dummyjson.com/todos'; | ||
|
|
||
| - const customRequest = await axios.request({ | ||
| - url: `${base}/1`, | ||
| - method: 'PATCH', | ||
| - headers: { 'Content-Type': 'application/json' }, | ||
| - data: { completed: true }, | ||
| - }); | ||
| + const customRequest = await fetch(`${base}/1`, { | ||
| + method: 'PATCH', | ||
| + headers: { 'Content-Type': 'application/json' }, | ||
| + body: JSON.stringify({ completed: true }), | ||
| + }).then(async (res) => Object.assign(res, { data: await res.json() })); | ||
| console.log('\nPATCH /todos/1 ->', customRequest.status); | ||
| console.log('Preview:', customRequest.data?.completed !== undefined ? `completed=${customRequest.data.completed}` : JSON.stringify(customRequest.data).slice(0,200)); | ||
| ``` | ||
|
|
||
| ## Unsupported APIs | ||
|
|
||
| The codemod does not yet cover Axios features outside of direct request helpers, such as interceptors, cancel tokens, or instance configuration from `axios.create()`. | ||
|
|
||
| ## References | ||
|
|
||
| - [Fetch Spec](https://fetch.spec.whatwg.org) | ||
| - [Axios Documentation](https://axios-http.com) | ||
| - [Node.js Documentation](https://nodejs.org/docs/latest/api/globals.html#fetch) |
This file contains hidden or 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,26 @@ | ||
| schema_version: "1.0" | ||
| name: "@nodejs/axios-to-whatwg-fetch" | ||
| version: 1.0.0 | ||
| description: Replace `axios` with `fetch` | ||
| author: Bruno Rodrigues | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public | ||
|
|
||
| # needed for removing dependencies | ||
| capabilities: | ||
| - fs | ||
| - child_process |
This file contains hidden or 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,25 @@ | ||
| { | ||
| "name": "@nodejs/axios-to-whatwg-fetch", | ||
| "version": "1.0.0", | ||
| "description": "Replace `axios` with `fetch`", | ||
| "type": "module", | ||
| "scripts": { | ||
| "test": "npx codemod jssg test -l tsx ./src/workflow.ts ./" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/nodejs/userland-migrations.git", | ||
| "directory": "recipes/axios-to-whatwg-fetch", | ||
| "bugs": "https://github.com/nodejs/userland-migrations/issues" | ||
| }, | ||
| "author": "Bruno Rodrigues", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/nodejs/userland-migrations/blob/main/recipes/axios-to-whatwg-fetch/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.0.9" | ||
| }, | ||
| "dependencies": { | ||
| "@nodejs/codemod-utils": "*", | ||
| "dedent": "^1.7.0" | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| import removeDependencies from '@nodejs/codemod-utils/remove-dependencies'; | ||
|
|
||
| /** | ||
| * Remove `chalk` and `@types/chalk` dependencies from package.json | ||
| */ | ||
| export default function removeAxiosDependencies(): string | null { | ||
| return removeDependencies(['axios']); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.