Axios Documentation

repository·master·Indexed 41 days ago

https://github.com/axios/axios-docs

Official documentation for Axios, covering API usage, request method aliases, and request cancellation. Includes guides on using AbortController (introduced in v0.22.0) and the deprecated CancelToken API, as well as technical details for building the documentation site using the inert static site generator and Docker.

Tokens
27.4K
Snippets
100
Records
134
Agent score
47%

What's inside Axios

  1. What is Axios?

    master

    Axios is a promise-based HTTP client that works in both the browser and Node.js (isomorphic).

    • In the browser, it uses XMLHttpRequest.
    • In Node.js, it uses the native http module.

    Key features include:

    • Intercepting requests and responses.
    • Automatic data transformation (JSON, Multipart/FormData, and URL-encoded).
    • Request/Response data transformation.
    • Request cancellation and timeouts.
    • Automatic JSON handling in responses.
    • Support for XSRF protection on the client side.
    • Progress tracking for uploads and downloads.
  2. Axios core features

    master

    Axios provides several advanced HTTP capabilities out of the box:

    • Promise API Support: Built on top of Promises for asynchronous flow control.
    • Interceptors: Ability to intercept and modify requests and responses.
    • Data Transformation: Automatic serialization and transformation of request and response data.
    • Automatic Serialization: Supports application/json, multipart/form-data, and application/x-www-form-urlencoded.
    • Request Management: Supports canceling requests and setting timeouts.
    • Query Parameters: Handles serialization of query parameters, including support for nested entries.
    • Progress Tracking: Captures upload/download progress in both browsers and Node.js (including speed rate and remaining time).
    • Security: Includes client-side support for protecting against XSRF.
    • Node.js Specifics: Supports setting bandwidth limits and uses the native http module.
  3. Difference between timeout and cancellation

    master

    It is important to distinguish between response timeouts and connection cancellations:

    1. timeout property: Handles response timeouts. It defines how long the client waits for a response before timing out.
    2. signal (AbortController) or cancelToken: Handles connection timeouts/cancellations. This allows you to proactively terminate a request (e.g., if the network is unreachable or the user cancels the action) to prevent the request from hanging indefinitely in the stack.

    For complete coverage, you should combine timeout with an early cancellation method like signal.

  4. How the documentation translation architecture works

    master

    The documentation translation system is based on configuration files and localized markdown files.

    • Configuration Files: Each language requires a {language-shortcut}.lang.js file (e.g., en.lang.js).
    • Content Files: The actual translated content resides in posts/{language-shortcut}/*.md (e.g., posts/en/).
    • Language Codes: You must use the ISO 639-1 two-letter code for the {language-shortcut} (e.g., zh for Traditional Chinese, de for German).
  5. Structure of a translation

    master

    A translation in the Axios documentation repository consists of two main parts:

    1. A configuration file: {language-shortcut}.lang.js (e.g., en.lang.js or de.lang.js).
    2. Translated document files: Located in posts/{language-shortcut}/*.md (e.g., posts/en or posts/de).

    Note: {language-shortcut} must be the 2-letter ISO 639-1 code for your language.

  6. Understand Axios Semver behavior

    master
    Until Axios reaches version 1.0, minor version updates (e.g., moving from 0.5.x to 0.6.0) may introduce breaking changes. While 0.5.1 and 0.5.4 are expected to share the same API, a minor version bump in the 0.x.x range does not guarantee backward compatibility.
  7. Use automatic multipart serialization

    master

    Starting from v0.27.0, Axios automatically serializes plain objects into FormData if the Content-Type header is set to multipart/form-data. This allows you to pass nested objects and files directly in the request body without manually constructing a FormData instance.

    import axios from 'axios';
    
    axios.post('https://httpbin.org/post', {
      user: {
        name: 'Dmitriy'
      },
      file: fs.createReadStream('/foo/bar.jpg')
    }, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }).then(({data})=> console.log(data));
  8. Automatic object serialization to FormData

    master

    Starting from version v0.27.0, Axios supports automatic serialization of objects into multipart/form-data if you explicitly set the Content-Type header to multipart/form-data in your request config.

    This allows you to pass nested objects and file streams directly in the request body.

    import axios from 'axios';
    
    axios.post('https://httpbin.org/post', {
      user: {
        name: 'Dmitriy'
      },
      file: fs.createReadStream('/foo/bar.jpg')
    }, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }).then(({data})=> console.log(data));
  9. Key features of Axios

    master

    Axios provides several advanced capabilities for handling HTTP requests:

    • Generates XMLHttpRequest for browsers.
    • Generates http requests for Node.js.
    • Supports the Promise API.
    • Interceptors: Ability to intercept requests and responses.
    • Data Transformation: Ability to transform request and response data.
    • Request Cancellation: Support for canceling requests.
    • Automatic JSON Transformation: Automatically converts JSON data.
    • XSRF Protection: Client-side support to prevent Cross-Site Request Forgery.
  10. Understand the axios-docs Docker Architecture

    master

    The project uses a multi-stage Docker build to optimize for both development and production environments:

    1. Stage 1: Builder (node:20-alpine): Installs dependencies and builds the static site into /app/public.
    2. Stage 2: Production (nginx:alpine): Copies the static files from the builder stage and serves them on port 8080. This stage is optimized for security (non-root user, read-only filesystem) and performance (Gzip, cache headers).
    3. Stage 3: Development (node:20-alpine): Uses http-server on port 8080 and includes volume mounts to allow for live editing and hot reloading.
    ┌─────────────────────────────────────────────────────────┐
    │                    Multi-Stage Build                     
    ├─────────────────────────────────────────────────────────┤
    │  Stage 1: Builder                                        
    │  ├── node:20-alpine                                      
    │  ├── Install dependencies                                
    │  └── Build static site → /app/public                     
    ├─────────────────────────────────────────────────────────┤
    │  Stage 2: Production                                     
    │  ├── nginx:alpine                                        
    │  ├── Copy static files from builder                      
    │  └── Serve on port 8080                                  
    ├─────────────────────────────────────────────────────────┤
    │  Stage 3: Development                                    
    │  ├── node:20-alpine                                      
    │  ├── Volume mounts for live editing                      
    │  └── http-server on port 8080                            
    └─────────────────────────────────────────────────────────┘