mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
13fdbec872
* chore: update changelog and update gitignore * chore: update PR and issue templates * chore: updated docs * chore: update all docs to match * chore: update both files to match * chore: remove un-needed yml * Update docs/fr/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/es/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/es/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/fr/pages/advanced/x-www-form-urlencoded-format.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/error-handling.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * Update docs/zh/pages/advanced/request-config.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
7.8 KiB
7.8 KiB
AGENTS.md
axios is a promise-based HTTP client for the browser and Node.js. The default instance is exported from lib/axios.js via index.js. Browser builds use the XHR or Fetch adapter; Node uses the HTTP/HTTPS adapter. Platform selection lives in lib/platform/.
This file is the canonical contributor guide for both human and AI agents working in this repo. .github/copilot-instructions.md is a thin stub that points back here — keep it in sync with the load-bearing safety rules below if you change them.
Setup And Safety
- Use
npm ci; repo.npmrcsetsignore-scripts=true, and CI also usesnpm ci --ignore-scripts. - Do not remove
ignore-scripts=true; if git hooks are needed after a fresh install, runnpm rebuild husky && npx huskyonce. - Adding or updating dependencies is security-sensitive;
package-lock.jsonis checked bylockfile-lintfor npm HTTPS hosts and integrity hashes. - Build/test/lint tools still execute dependency code despite
ignore-scripts; avoid unnecessary full builds when a focused check proves the change. - Do not add new runtime dependencies without discussion; the dependency surface is intentionally tiny.
Commands
- Build published artifacts:
npm run build(gulp cleardeletesdist/, then Rollup writes browser ESM/UMD/CJS and Node CJS bundles). - Lint source only:
npm run lint; focused lint:npx eslint lib/path/to/file.js. - Unit tests:
npm run test:vitest:unit; focused unit test:npm run test:vitest:unit -- tests/unit/path.test.js. - Browser tests need Playwright installed first (
npx playwright installlocally; CI usesnpx playwright install --with-deps); runnpm run test:vitest:browser:headlessfor CI parity. - Smoke/module compatibility suites test the packed package, not the source tree: run
npm run build,npm pack, install the tarball into the relevanttests/smoke/*ortests/module/*package, then run that suite's npm script. - CI order is install -> build -> Playwright install -> unit -> browser headless -> pack -> CJS/ESM module and smoke tests -> Bun/Deno smoke tests.
Package Shape
- Source is ESM (
type: module); public ESM entry isindex.js, which re-exports the default instance fromlib/axios.js. - Do not edit
dist/by hand; it is ignored and generated fromlib/by Rollup. - Runtime package exports are split by environment: browser/react-native map Node HTTP/platform files to browser/null replacements, while Node CJS ships as
dist/node/axios.cjs. - Keep public runtime exports,
index.d.ts(ESM types), andindex.d.cts(CJSexport = axiostypes) in sync for API changes. lib/env/data.jsis version-generated bygulp version; do not edit it for normal feature work.
Architecture Boundaries
lib/core/is axios domain logic: request dispatch, config merge, interceptors, headers, errors. Key classes:Axios(request dispatch + interceptor chains),AxiosError(standardized error codes),AxiosHeaders(case-insensitive header normalization),InterceptorManager(sync/async interceptor registration).lib/adapters/performs I/O; default adapter preference is['xhr', 'http', 'fetch'], with capability selection inlib/adapters/adapters.js. Detect by capability, not environment name.lib/platform/selects Node by default; browser builds rely on package/rollup aliasing tolib/platform/browser.lib/helpers/should stay generic and reusable outside axios; do not put axios-specific request lifecycle logic there.- New
lib/**/*.jsfiles should match existing source style: ESM imports with explicit.jsextensions,'use strict';where current library files use it, andAxiosErrorfor axios-originated failures.
Naming Conventions
- Classes: PascalCase (
Axios,AxiosError,InterceptorManager). - Functions: camelCase (
buildURL,mergeConfig,dispatchRequest). - Error codes: UPPER_SNAKE_CASE constants on
AxiosError(ERR_NETWORK,ETIMEDOUT). - Internal class slots:
Symbol-keyed (e.g.const $internals = Symbol('internals')inlib/core/AxiosHeaders.js) rather than underscore-prefixed properties.
Error Handling
- Throw
AxiosErrorfor axios-originated failures; never rawError. Pass(message, code, config, request, response)so consumers can introspect. - Wrap third-party errors with
AxiosError.from(error, code, config, request, response). - Canonical code list lives in
lib/core/AxiosError.js; current codes includeERR_BAD_OPTION_VALUE,ERR_BAD_OPTION,ECONNABORTED,ETIMEDOUT,ECONNREFUSED,ERR_NETWORK,ERR_FR_TOO_MANY_REDIRECTS,ERR_DEPRECATED,ERR_BAD_RESPONSE,ERR_BAD_REQUEST,ERR_CANCELED,ERR_NOT_SUPPORT,ERR_INVALID_URL,ERR_FORM_DATA_DEPTH_EXCEEDED. - Validate config options through the
validatorhelper; do not invent ad-hoc validation paths.
Interceptor Execution Order
- Request interceptors run last-registered-first (LIFO).
- Response interceptors run first-registered-first (FIFO).
- Both support
synchronous: true(avoids Promise wrapping when no async handler is in the chain) andrunWhen: (config) => booleanfor conditional execution. - Order matters for both behavior and tests; document it when adding new built-in interceptors.
Request Lifecycle
- User calls
axios()or a method alias. - Merge instance defaults with request config via
mergeConfig. - Run request interceptors (LIFO).
- Select adapter via
lib/adapters/adapters.jscapability check. - Apply
transformRequestfunctions. - Adapter performs the HTTP request.
- Apply
transformResponsefunctions. - Run response interceptors (FIFO).
- Resolve promise with
AxiosResponseor reject withAxiosError.
Cancellation
- Both
CancelToken(legacy) andAbortSignal(modern) are supported simultaneously; do not break either path. - Cancellation must work at any lifecycle stage, including mid-flight body reads.
- Always remove signal listeners on settlement or cancellation to prevent memory leaks.
Common Pitfalls
- Do not mutate config objects in-place; return new objects from merges/transforms.
- Do not assume browser- or Node-specific globals exist; capability-check first.
- Do not use
Function.prototype.binddirectly — uselib/helpers/bind.js, which forwardsargumentsviaapplyand is what the rest of the library relies on. - Do not throw raw
Errorfrom library code; useAxiosErrorwith an appropriate code (see Error Handling).
Tests
- Test layout is runtime-first:
tests/unit/**/*.test.js,tests/browser/**/*.browser.test.js,tests/smoke/esm/**/*.smoke.test.js,tests/smoke/cjs/**/*.smoke.test.cjs. - Use
tests/setup/server.jsfor local HTTP servers and cleanup withtry/finally; leaking servers causes Vitest hangs. - Keep CJS and ESM smoke coverage aligned when behavior is packaging/import related.
- Type compatibility is exercised through
tests/module/cjswith TypeScript 4.9 andtests/module/esmwith TypeScript 5.x; run the matching module suite for declaration changes. - Browser tests replace globals such as XHR; restore globals and reset spies in cleanup hooks.
Security-Sensitive Code
- For config reads that affect behavior, do not use prototype-walking reads (
in, destructuring, or directconfig.fooon untrusted config); guard with own-property checks as inutils.hasOwnProp/ localown()helpers. - New merge or object materialization code must continue filtering
__proto__,constructor, andprototype; regressions here are security bugs. - Changes touching URL construction, redirects, proxy/env handling, XSRF, socket paths, decompression limits, or adapters should consult
THREATMODEL.mdand add focused regression tests. - Keep
withXSRFTokencross-origin behavior explicit: onlytrueforces cross-origin XSRF header attachment. - Do not weaken
beforeRedirect, proxy, orsocketPathsafeguards without tests covering credential leakage or SSRF-style cases.