- Spec files
npm-axios
Describes: pkg:npm/axios@0.30.x
- Description
- Promise based HTTP client for the browser and node.js
- Author
- tessl
- Last updated
index.md docs/
1# Axios23Axios is a Promise-based HTTP client library that works in both browsers and Node.js environments. It provides a consistent, feature-rich API for making HTTP requests with automatic JSON data transformation, request/response interceptors, request cancellation, timeout handling, and built-in protection against common security vulnerabilities like XSRF.45## Package Information67- **Package Name**: axios8- **Package Type**: npm9- **Language**: JavaScript (with TypeScript definitions)10- **Installation**: `npm install axios`1112## Core Imports1314```javascript15import axios from 'axios';16```1718For CommonJS:1920```javascript21const axios = require('axios');22```2324TypeScript imports for additional types:2526```typescript27import axios, {28AxiosRequestConfig,29AxiosResponse,30AxiosError,31AxiosInstance32} from 'axios';33```3435## Basic Usage3637```javascript38import axios from 'axios';3940// GET request41const response = await axios.get('https://api.example.com/users');42console.log(response.data);4344// POST request with data45const newUser = await axios.post('https://api.example.com/users', {46name: 'John Doe',47email: 'john@example.com'48});4950// Using request config object51const config = {52method: 'get',53url: 'https://api.example.com/users',54timeout: 5000,55headers: {56'Authorization': 'Bearer your-token'57}58};59const response = await axios(config);60```6162## Architecture6364Axios is built around several key components:6566- **Core Instance**: The main axios instance with default configuration and HTTP method aliases67- **Request/Response Cycle**: Configurable request and response transformation with interceptor support68- **Adapter System**: Platform-specific adapters (XHR for browsers, HTTP for Node.js) with automatic selection69- **Configuration System**: Hierarchical configuration merging from defaults, instance config, and request config70- **Error Handling**: Comprehensive error types with request/response context and status code validation71- **Cancellation**: Both modern AbortController and legacy CancelToken support for request cancellation7273## Capabilities7475### HTTP Client and Requests7677Core HTTP client functionality with method aliases, request configuration, and response handling. Supports all standard HTTP methods with automatic JSON handling and platform-specific optimizations.7879```javascript { .api }80// Main instance - callable function and object81axios(config: AxiosRequestConfig): Promise<AxiosResponse>;82axios(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;8384// HTTP method aliases85axios.get(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;86axios.post(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;87axios.put(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;88axios.patch(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;89axios.delete(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;90axios.head(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;91axios.options(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse>;9293// Form methods with automatic Content-Type94axios.postForm(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;95axios.putForm(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;96axios.patchForm(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse>;97```9899[HTTP Client and Requests](./http-client.md)100101### Request and Response Interceptors102103Middleware system for transforming requests before sending and responses before handling. Supports both synchronous and asynchronous interceptors with conditional execution and error handling.104105```javascript { .api }106// Request interceptors107axios.interceptors.request.use(108config => config,109error => Promise.reject(error)110);111112// Response interceptors113axios.interceptors.response.use(114response => response,115error => Promise.reject(error)116);117118interface AxiosInterceptorManager<V> {119use<T = V>(120onFulfilled?: (value: V) => T | Promise<T>,121onRejected?: (error: any) => any,122options?: AxiosInterceptorOptions123): number;124eject(id: number): void;125}126```127128[Interceptors](./interceptors.md)129130### Configuration and Defaults131132Comprehensive configuration system with default settings, instance-level configuration, and request-level overrides. Includes timeout handling, header management, and data transformation options.133134```javascript { .api }135// Default configuration access136axios.defaults: AxiosDefaults;137138// Key configuration options139interface AxiosRequestConfig {140url?: string;141method?: string;142baseURL?: string;143headers?: AxiosRequestHeaders;144params?: any;145data?: any;146timeout?: number;147auth?: AxiosBasicCredentials;148responseType?: ResponseType;149validateStatus?: (status: number) => boolean;150}151```152153[Configuration](./configuration.md)154155### Error Handling and Cancellation156157Comprehensive error handling with detailed error information and multiple cancellation mechanisms. Supports both modern AbortController and legacy CancelToken patterns for request cancellation.158159```javascript { .api }160// Error types161class AxiosError extends Error {162config?: AxiosRequestConfig;163code?: string;164request?: any;165response?: AxiosResponse;166isAxiosError: boolean;167status?: number;168}169170// Cancellation with AbortController (modern)171const controller = new AbortController();172axios.get('/api/data', { signal: controller.signal });173controller.abort();174175// Cancellation with CancelToken (legacy)176const source = axios.CancelToken.source();177axios.get('/api/data', { cancelToken: source.token });178source.cancel('Operation canceled');179```180181[Error Handling and Cancellation](./error-handling.md)182183### Utility Functions and Version Information184185Utility functions for error checking, data transformation, Promise handling, and URI building, plus access to version information.186187```javascript { .api }188// Instance creation and management189axios.create(config?: AxiosRequestConfig): AxiosInstance;190191// URI building192axios.getUri(config?: AxiosRequestConfig): string;193194// Type checking utilities195axios.isAxiosError(payload: any): boolean;196axios.isCancel(value: any): boolean;197198// Data conversion utilities199axios.toFormData(sourceObj: object, targetFormData?: FormData, options?: FormSerializerOptions): FormData;200axios.formToJSON(form: FormData | HTMLFormElement): object;201202// Promise utilities203axios.all<T>(values: Array<T | Promise<T>>): Promise<T[]>;204axios.spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;205206// Version information207axios.VERSION: string;208```209210## TypeScript Support211212Axios includes comprehensive TypeScript definitions with generic type support for request and response data:213214```typescript { .api }215interface AxiosResponse<T = any, D = any> {216data: T;217status: number;218statusText: string;219headers: AxiosResponseHeaders;220config: AxiosRequestConfig<D>;221request?: any;222}223224interface AxiosInstance {225<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D>): Promise<R>;226<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;227228get<T = any, R = AxiosResponse<T>, D = any>(url: string, config?: AxiosRequestConfig<D>): Promise<R>;229post<T = any, R = AxiosResponse<T>, D = any>(url: string, data?: D, config?: AxiosRequestConfig<D>): Promise<R>;230// ... other methods231}232```