3 min read

This case study demonstrates the use of Codemod2.0 for migrating from Axios to Fetch. Codemod2.0 utilizes a combination of deterministic engines like ast-grep and large language models (LLMs), requiring access to an OpenAI API key (BYOM: bring your own model). This approach offers a semi-automated migration process that can be particularly beneficial for developers considering the shift from Axios to Fetch. There are alternative approaches, each with its own strengths and weaknesses, which you can learn more about here.

Why Migrate from Axios to Fetch?

With the Fetch API now supported by 97.25% of users' browsers globally, many developers are moving away from older libraries like Axios, similar to the transition from jQuery. Here are some key benefits of adopting Fetch:

  • Reduced Dependencies: Fetch is built into the browser, eliminating the need for external libraries.
  • Enhanced Error Handling: Fetch provides more consistent error handling mechanisms.
  • Cross-Browser Compatibility: Fetch works seamlessly across different browsers and server environments.
  • Smaller Bundle Sizes: Removing Axios can reduce the overall size of your web application bundle.

Axios to Fetch Migration Steps

Migrating from Axios to Fetch requires several steps for a smooth transition:

Step
Axios
Fetch
Basic Request Setup
Simplifies syntax for common use cases and automatically handles JSON data
Requires more configuration and manual handling of JSON data. You need to parse JSON responses explicitly
Handling Defaults
Allows setting default headers, base URLs, and timeouts globally
Requires manual setup for headers and other configurations in each request or by creating a wrapper function to handle these settings
Interceptors
Provides built-in support for request and response interceptors to modify requests or handle errors globally
Lacks built-in interceptor support, so you need to implement custom middleware or wrapper functions to achieve similar behavior
Error Handling
Automatically rejects promises for HTTP errors and provides detailed error messages
Requires manual checking of response status and rejection of promises. You need to write custom error-handling logic
Cancellation
Supports request cancellation through CancelTokens
Uses AbortController for request cancellation, which requires additional setup and management
Transforming Requests and Responses
Includes built-in methods for transforming requests and responses
Requires manual transformation, often necessitating additional parsing and processing logic
Instance Creation
Allows creating instances with custom configurations
Does not support instance creation natively, so you need to implement factory functions to achieve similar functionality

Manual migration can be tedious and error-prone, especially in large codebases. Codemod2.0 can automate much of this process, making it more efficient and less error-prone.

Migration Process with Codemod2.0

Identifying Axios Usages:

Using ast-grep, we can quickly locate most occurrences of Axios in the codebase. Here are some common Axios patterns (also, see ast-grep playground):

1import axios from 'axios'
2const response1 = await axios.get(url, options)
3const response2 = await axios.post(url, options).then(...)
4const response3 = await axios(options).then(...).catch(...)

Patterns to Match:

1const axiosPatterns = [
2 { pattern: "axios($$$_)" }, // axios()
3 { pattern: "axios.$_($$$)" }, // axios.get(...)
4 { pattern: "axios.$_($$$).$_($$$)" }, // axios.get(...).then(...)
5 { pattern: "axios.$_($$$).$_($$$).$_($$$)" }, // axios.get(...).then(...).catch(...)
6 { pattern: "axios.$_($$$).$_($$$).$_($$$).$_($$$)" }, // axios.get(...).then(...).catch(...).finally(...)
7];

Codemod Instructions:

Once we found all the occurrences, we can ask LLM to replace axios with fetch with specific instructions:

1const prompt = `
2 You are migrating from axios to fetch.
3 Ignore axios.create() and axios.all() as fetch doesn’t have these APIs.
4
5 Here is a general pattern to replace axios with fetch:
6 1. Replace axios.anyFunction(url) with fetch(url, options) and await it.
7 2. if response.ok is false, throw an error.
8 3. Get the response by calling response.json() or response.text() or response.blob() or response.arrayBuffer().
9 4. To be compatible with axios, you need to need to set result variable to { data: await response.json() }.
10 5. Infer the type of result variable from context and apply type to resulve variable { data: await response.json() as SomeType }.
11
12 Use AbortSignal to replace axios timeout option.
13 For example,
14 axios.get(url, { timeout: 5000 })
15 can be replaced with
16 fetch(url, { signal: AbortSignal.timeout(5000) })
17 `;

Results of Codemod2.0

After running the codemod on our own Codemod monorepo:

1npx codemod@latest axios/fetch --OPENAI_API_KEY=$OPENAI_API_KEY

Number of replacements: 16

Completely Automatic Migrations: 7/16 (43.75%)

  • LLM correctly inferred types: 5/16 (31.25%)

Migrations Requiring Small Fixes: 6/16 (37.5%)

  • Examples include adding types or fixing minor typos.

Migrations Requiring Large Manual Changes: 3/16 (18.75%)

Overall, 81.25% of cases were migrated either completely automatically or with minor fixes.

Conclusion

Codemod2.0 significantly reduces the effort required to migrate from Axios to Fetch, automating a substantial portion of the process. While some features specific to Axios, such as interceptors, instance creation, and request transformations, still require manual handling, the automated approach provided by Codemod2.0 proves to be highly effective for most common use cases. For larger commercial codebases needing more reliability and coverage, or for fully delegating the migration, our certified experts are available to assist.

For more information and to get started with your migration, please contact us.