Source Code – Axios

Time:2022-8-5

Axios

content

  1. main idea
  2. interceptor
  3. Cancel
  4. Promise: Axios' use of Promises is quite extreme

    • Use the then chain to concatenate your ownrequest interceptor => ask => response interceptorchain, and then execute in an orderly manner
    • Use the asynchronous idea of ​​Promise, separate the resolve method, and provide the ability to actively cancel the request to the outside world

Source Code - Axios

other

  1. Packaging of xhr/http: headers(Authorization: btoa/atob), buildFullPath, onloadend/onabort/onerror/ontimeout/progress
  2. enhanceError: Usually js has less abnormal error information, and Axios itself encapsulates more information for isAxiosError errors. You can get more HTTP error information through toJSON
  3. Utility functions: small and beautiful, named
  4. Conversion of request and response body: We do not need to deal with the Content-Type of the request body and the data format of the return body in most scenarios, but also allow developers to customize
  5. AbortController: Provides developers with a custom controller that blocks requests, similar to fetch(url, { signal })

Original illustration, reprinted remarks.