axios 1.18.1 β 1.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +62 -0
- package/README.md +297 -5
- package/dist/axios.js +728 -170
- package/dist/axios.min.js +3 -3
- package/dist/axios.min.js.map +1 -1
- package/dist/browser/axios.cjs +871 -188
- package/dist/esm/axios.js +871 -188
- package/dist/esm/axios.min.js +2 -2
- package/dist/esm/axios.min.js.map +1 -1
- package/dist/node/axios.cjs +1037 -225
- package/index.d.cts +102 -74
- package/index.d.ts +100 -68
- package/lib/adapters/fetch.js +64 -11
- package/lib/adapters/http.js +140 -67
- package/lib/adapters/xhr.js +52 -3
- package/lib/core/Axios.js +43 -20
- package/lib/core/AxiosError.js +33 -2
- package/lib/core/AxiosHeaders.js +124 -1
- package/lib/core/InterceptorManager.js +107 -8
- package/lib/core/buildFullPath.js +41 -15
- package/lib/core/dispatchRequest.js +8 -2
- package/lib/core/mergeConfig.js +19 -4
- package/lib/core/methodList.js +17 -0
- package/lib/core/setFormDataHeaders.js +27 -0
- package/lib/defaults/index.js +2 -1
- package/lib/env/data.js +1 -1
- package/lib/helpers/Http2Sessions.js +2 -0
- package/lib/helpers/HttpStatusCode.js +12 -1
- package/lib/helpers/combineURLs.js +11 -3
- package/lib/helpers/composeSignals.js +12 -1
- package/lib/helpers/estimateDataURLDecodedBytes.js +115 -54
- package/lib/helpers/formDataToJSON.js +11 -5
- package/lib/helpers/fromDataURI.js +5 -4
- package/lib/helpers/normalizeURLForProtocolCheck.js +24 -0
- package/lib/helpers/parseHeaders.js +5 -3
- package/lib/helpers/progressEventReducer.js +4 -4
- package/lib/helpers/resolveConfig.js +5 -17
- package/lib/helpers/shouldBypassProxy.js +300 -5
- package/lib/helpers/throttle.js +3 -2
- package/lib/helpers/toFormData.js +17 -23
- package/lib/platform/node/classes/Buffer.js +11 -0
- package/lib/utils.js +151 -18
- package/package.json +21 -19
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## v1.19.0 β July 22, 2026
|
|
4
|
+
|
|
5
|
+
This release raises the form-data security floor, adds configuration and type-system capabilities, and fixes NO_PROXY matching, interceptor errors, progress reporting, and serialization edge cases.
|
|
6
|
+
|
|
7
|
+
## π Security Fixes
|
|
8
|
+
|
|
9
|
+
- Multipart Form Data: Raised the form-data dependency floor to ^4.0.6, preventing fresh installations from resolving versions affected by the CRLF injection vulnerability GHSA-hmw2-7cc7-3qxx (https://github.com/advisories/GHSA-hmw2-7cc7-3qxx). (#11028)
|
|
10
|
+
|
|
11
|
+
## π New Features
|
|
12
|
+
|
|
13
|
+
- Configuration Extensibility: Preserved own-enumerable symbol-keyed fields through mergeConfig and added a generic params type across public TypeScript declarations, responses, errors,
|
|
14
|
+
adapters, and serializers. (#11043, #11081)
|
|
15
|
+
- Header Parameter Parsing: Added the opt-in AxiosHeaders.parseParameters() parser for quote-aware, RFC-style HTTP parameter parsing while preserving legacy parsing behavior. (#11051)
|
|
16
|
+
- HTTP Status Codes: Added the missing Cloudflare 520 WebServerReturnsAnUnknownError status and matching ESM/CJS declarations. (#11067)
|
|
17
|
+
|
|
18
|
+
## π Bug Fixes
|
|
19
|
+
|
|
20
|
+
- Form Data Conversion: Limited formDataToJSON path splitting to dot and bracket notation, preserving literal punctuation in keys, and removed browser-facing Buffer.from usage from toFormData to avoid unnecessary polyfills. (#11006, #11018)
|
|
21
|
+
- Proxy Bypass: Canonicalized IPv4 shorthand, octal, and hexadecimal forms during NO_PROXY matching and honored * entries within comma- or space-separated bypass lists. (#11029, #11053)
|
|
22
|
+
- Cancellation: Propagated already-aborted input signals immediately when composing abort signals. (#11035)
|
|
23
|
+
- Header Handling: Preserved empty first values for duplicate singleton headers and made AxiosHeaders#getSetCookie() consistently return arrays for present values. (#11036, #11037)
|
|
24
|
+
- URL Handling: Included normalized, safely redacted offending URLs in malformed-protocol errors and removed repeated trailing slashes when combining base URLs. (#11008, #11038)
|
|
25
|
+
|
|
26
|
+
- Progress Events: Clamped malformed negative progress values to zero and ensured final Node.js download progress events are delivered before streamed responses close. (#11039, #11040)
|
|
27
|
+
- Error and JSON Serialization: Serialized Set values as arrays in JSON-compatible snapshots and synthesized useful AxiosError messages from otherwise-empty AggregateError instances. (#11044, #11059)
|
|
28
|
+
- Content-Length Enforcement: Corrected base64 data: URL size estimation so maxContentLength is enforced consistently by the HTTP and Fetch adapters. (#11061)
|
|
29
|
+
- Synchronous Interceptors: Prevented requests from being dispatched after synchronous request interceptors fail unless their paired rejection handler resolves successfully. (#11071)
|
|
30
|
+
|
|
31
|
+
## π§ Maintenance & Chores
|
|
32
|
+
|
|
33
|
+
- Dependencies: Updated development and test tooling, the docs fixture's Axios version, and GitHub Actions integrations including Checkout, Setup Node, Setup Deno, and Zizmor. (#11031, #11055, #11056, #11058, #11079, #11080, #11088, #11089, #11090)
|
|
34
|
+
- Build Outputs: Limited sourcemap generation to published minified bundles, removing broken map references from non-minified builds. (#11054)
|
|
35
|
+
- Form Data Internals: Centralized FormData header handling and made the Node.js adapter tolerate getHeaders() returning undefined under the content-only policy. (#11062)
|
|
36
|
+
- Developer Experience: Ignored common local AI-tooling directories and fixed a constant-reassignment crash when the development sandbox serves its root path. (#11032, #11073)
|
|
37
|
+
- Documentation: Updated sponsor information, clarified that baseURL is not a path-security boundary, scoped provenance claims to attested releases, and corrected the configuration-defaults documentation. (#11041, #11068, #11076, #11078)
|
|
38
|
+
- Publishing: Simplified v1 publishing to use the npm version bundled with Node.js 26 and updated package metadata for the 1.19.0 release. (#11083, #11095)
|
|
39
|
+
|
|
40
|
+
## π New Contributors
|
|
41
|
+
|
|
42
|
+
We are thrilled to welcome our new contributors. Thank you for helping improve Axios:
|
|
43
|
+
|
|
44
|
+
- @afonsojramos (#11028)
|
|
45
|
+
- @MahinAnowar (#11006)
|
|
46
|
+
- @yassertawfik4 (#11024)
|
|
47
|
+
- @AnandSundar (#11029)
|
|
48
|
+
- @lin-hongkuan (#11035)
|
|
49
|
+
- @Wali007-lab (#11054)
|
|
50
|
+
- @magicdawn (#11043)
|
|
51
|
+
- @andrewkernel (#11053)
|
|
52
|
+
- @Sagargupta16 (#11059)
|
|
53
|
+
- @Rpaudel379 (#11078)
|
|
54
|
+
- @kobihikri (#11076)
|
|
55
|
+
- @spokodev (#11061)
|
|
56
|
+
- @shaedrich (#11081)
|
|
57
|
+
- @QodeXcli (#11062)
|
|
58
|
+
- @akahoshi1421 (#11067)
|
|
59
|
+
- @TheHonoredOne914 (#11071)
|
|
60
|
+
- @Ahsan1Murtaza (#11073)
|
|
61
|
+
|
|
62
|
+
[Full Changelog](https://github.com/axios/axios/compare/v1.18.1...v1.19.0)
|
|
63
|
+
|
|
64
|
+
|
|
3
65
|
## v1.18.0 β June 13, 2026
|
|
4
66
|
|
|
5
67
|
This release hardens redirect and URL handling, improves the validateStatus configuration semantics, and includes updates to documentation, dependencies, and release metadata.
|
package/README.md
CHANGED
|
@@ -296,6 +296,128 @@
|
|
|
296
296
|
</p>
|
|
297
297
|
</td>
|
|
298
298
|
</tr>
|
|
299
|
+
<tr width="33.333333333333336%">
|
|
300
|
+
<td align="center" width="33.333333333333336%">
|
|
301
|
+
<a
|
|
302
|
+
href="https://www.jbo88b.com/vn/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
303
|
+
style="padding: 10px; display: inline-block"
|
|
304
|
+
target="_blank"
|
|
305
|
+
>
|
|
306
|
+
<img
|
|
307
|
+
width="71px"
|
|
308
|
+
height="70px"
|
|
309
|
+
src="https://images.opencollective.com/jbo-vietnam/3fc6159/avatar.png"
|
|
310
|
+
alt="JBO Vietnam"
|
|
311
|
+
/>
|
|
312
|
+
</a>
|
|
313
|
+
<p
|
|
314
|
+
align="center"
|
|
315
|
+
>
|
|
316
|
+
JBO Vietnam is a prominent online entertainment brand in Vietnam, offering sports betting, esports, online casino games, and a wide range of other exciting games.
|
|
317
|
+
</p>
|
|
318
|
+
<p align="center">
|
|
319
|
+
<a
|
|
320
|
+
href="https://www.jbo88b.com/vn/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
321
|
+
target="_blank"
|
|
322
|
+
><b>jbo88b.com</b></a
|
|
323
|
+
>
|
|
324
|
+
</p>
|
|
325
|
+
</td>
|
|
326
|
+
<td align="center" width="33.333333333333336%">
|
|
327
|
+
<a
|
|
328
|
+
href="https://www.jbo579.com/th/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
329
|
+
style="padding: 10px; display: inline-block"
|
|
330
|
+
target="_blank"
|
|
331
|
+
>
|
|
332
|
+
<img
|
|
333
|
+
width="71px"
|
|
334
|
+
height="70px"
|
|
335
|
+
src="https://images.opencollective.com/jbo-thailand/d17e84f/avatar.png"
|
|
336
|
+
alt="JBO Thailand"
|
|
337
|
+
/>
|
|
338
|
+
</a>
|
|
339
|
+
<p
|
|
340
|
+
align="center"
|
|
341
|
+
>
|
|
342
|
+
JBO Thailand is a prominent online entertainment brand in Thailand, offering sports betting, esports, online casino games, and a wide range of other exciting games.
|
|
343
|
+
</p>
|
|
344
|
+
<p align="center">
|
|
345
|
+
<a
|
|
346
|
+
href="https://www.jbo579.com/th/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
347
|
+
target="_blank"
|
|
348
|
+
><b>jbo88b.com</b></a
|
|
349
|
+
>
|
|
350
|
+
</p>
|
|
351
|
+
</td>
|
|
352
|
+
<td align="center" width="33.333333333333336%">
|
|
353
|
+
<a
|
|
354
|
+
href="https://kasyno.us.com/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
355
|
+
style="padding: 10px; display: inline-block"
|
|
356
|
+
target="_blank"
|
|
357
|
+
>
|
|
358
|
+
<img
|
|
359
|
+
width="71px"
|
|
360
|
+
height="70px"
|
|
361
|
+
src="https://images.opencollective.com/polskie-kasyno-online/c25e456/avatar.png"
|
|
362
|
+
alt="polskie kasyno online"
|
|
363
|
+
/>
|
|
364
|
+
</a>
|
|
365
|
+
<p
|
|
366
|
+
align="center"
|
|
367
|
+
>
|
|
368
|
+
Kasyno.us.com to niezaleΕΌny portal o tematyce kasyno online Polska, ktΓ³ry pomaga graczom znaleΕΊΔ bezpieczne i sprawdzone kasyna internetowe.
|
|
369
|
+
</p>
|
|
370
|
+
<p align="center">
|
|
371
|
+
<a
|
|
372
|
+
href="https://kasyno.us.com/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
373
|
+
target="_blank"
|
|
374
|
+
><b>kasyno.us.com</b></a
|
|
375
|
+
>
|
|
376
|
+
</p>
|
|
377
|
+
</td>
|
|
378
|
+
</tr>
|
|
379
|
+
<tr width="33.333333333333336%">
|
|
380
|
+
<td align="center" width="33.333333333333336%">
|
|
381
|
+
<a
|
|
382
|
+
href="https://www.scrapingbee.com/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
383
|
+
style="padding: 10px; display: inline-block"
|
|
384
|
+
target="_blank"
|
|
385
|
+
>
|
|
386
|
+
<img
|
|
387
|
+
width="90px"
|
|
388
|
+
height="90px"
|
|
389
|
+
src="https://images.opencollective.com/scrapingbee/6d5dc95/logo.png"
|
|
390
|
+
alt="ScrapingBee"
|
|
391
|
+
/>
|
|
392
|
+
</a>
|
|
393
|
+
<p
|
|
394
|
+
align="center"
|
|
395
|
+
>
|
|
396
|
+
Scrape websites with a powerful web scraping API without managing proxies, browsers, or anti-bot defenses.
|
|
397
|
+
</p>
|
|
398
|
+
<p align="center">
|
|
399
|
+
<a
|
|
400
|
+
href="https://www.scrapingbee.com/?utm_source=axios_docs_website&utm_medium=website&utm_campaign=axios_open_collective_sponsorship"
|
|
401
|
+
target="_blank"
|
|
402
|
+
><b>scrapingbee.com</b></a
|
|
403
|
+
>
|
|
404
|
+
</p>
|
|
405
|
+
</td>
|
|
406
|
+
<td align="center" width="33.333333333333336%">
|
|
407
|
+
<a
|
|
408
|
+
href="https://opencollective.com/axios/contribute"
|
|
409
|
+
target="_blank"
|
|
410
|
+
>π Become a sponsor</a
|
|
411
|
+
>
|
|
412
|
+
</td>
|
|
413
|
+
<td align="center" width="33.333333333333336%">
|
|
414
|
+
<a
|
|
415
|
+
href="https://opencollective.com/axios/contribute"
|
|
416
|
+
target="_blank"
|
|
417
|
+
>π Become a sponsor</a
|
|
418
|
+
>
|
|
419
|
+
</td>
|
|
420
|
+
</tr>
|
|
299
421
|
</table>
|
|
300
422
|
|
|
301
423
|
<!--<div>marker</div>-->
|
|
@@ -587,6 +709,7 @@ axios({
|
|
|
587
709
|
|
|
588
710
|
```js
|
|
589
711
|
// GET request for remote image in node.js
|
|
712
|
+
import fs from 'fs';
|
|
590
713
|
const response = await axios({
|
|
591
714
|
method: 'get',
|
|
592
715
|
url: 'https://bit.ly/2mTM3nY',
|
|
@@ -699,6 +822,11 @@ These config options are available for requests. Only `url` is required. Request
|
|
|
699
822
|
// Axios prepends `baseURL` to `url` unless `url` is absolute and `allowAbsoluteUrls` is set to true.
|
|
700
823
|
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
|
|
701
824
|
// to the methods of that instance.
|
|
825
|
+
// `baseURL` is not a security boundary. If `url` is attacker-controlled, validate it
|
|
826
|
+
// before passing it to axios. Relative URLs can contain `..` segments that resolve
|
|
827
|
+
// outside an intended path prefix after the final URL is parsed.
|
|
828
|
+
// HTTP(S) URLs must include `//` after the protocol. Values such as
|
|
829
|
+
// `https:example.com` and `https:/example.com` are rejected with ERR_INVALID_URL.
|
|
702
830
|
baseURL: 'https://some-domain.com/api/',
|
|
703
831
|
|
|
704
832
|
// `allowAbsoluteUrls` determines whether or not absolute URLs will override a configured `baseUrl`.
|
|
@@ -1307,6 +1435,40 @@ axios.interceptors.request.use(
|
|
|
1307
1435
|
);
|
|
1308
1436
|
```
|
|
1309
1437
|
|
|
1438
|
+
If a synchronous request interceptor throws, Axios calls that interceptor's paired rejection handler and stops running the remaining request interceptors. A rejection handler that returns normally, including one that returns `undefined` or a fulfilled Promise, treats the error as handled: Axios dispatches the request with the last valid config, and ignores the handler's return value. To block dispatch, omit the rejection handler or throw/return a rejected Promise from it. The final error then continues through response rejection interceptors.
|
|
1439
|
+
|
|
1440
|
+
Use a rejected Promise when validation must prevent the request:
|
|
1441
|
+
|
|
1442
|
+
```js
|
|
1443
|
+
axios.interceptors.request.use(
|
|
1444
|
+
function validate(config) {
|
|
1445
|
+
if (!config.headers.has('Authorization')) {
|
|
1446
|
+
throw new Error('Authorization is required');
|
|
1447
|
+
}
|
|
1448
|
+
return config;
|
|
1449
|
+
},
|
|
1450
|
+
function rejectInvalidRequest(error) {
|
|
1451
|
+
return Promise.reject(error);
|
|
1452
|
+
},
|
|
1453
|
+
{ synchronous: true }
|
|
1454
|
+
);
|
|
1455
|
+
```
|
|
1456
|
+
|
|
1457
|
+
A logging-only rejection handler can return normally to preserve the existing request-continuation behavior:
|
|
1458
|
+
|
|
1459
|
+
```js
|
|
1460
|
+
axios.interceptors.request.use(
|
|
1461
|
+
function prepare(config) {
|
|
1462
|
+
throw new Error('Optional preparation failed');
|
|
1463
|
+
},
|
|
1464
|
+
function logPreparationFailure(error) {
|
|
1465
|
+
console.warn(error);
|
|
1466
|
+
// Returning normally dispatches with the last valid config.
|
|
1467
|
+
},
|
|
1468
|
+
{ synchronous: true }
|
|
1469
|
+
);
|
|
1470
|
+
```
|
|
1471
|
+
|
|
1310
1472
|
If you want to execute a particular interceptor based on a runtime check,
|
|
1311
1473
|
you can add a `runWhen` function to the options object. The request interceptor will not run **if and only if** the return
|
|
1312
1474
|
of `runWhen` is `false`. Axios calls the function with the config
|
|
@@ -1374,7 +1536,7 @@ When a response is fulfilled and multiple response interceptors are registered:
|
|
|
1374
1536
|
- If a fulfillment interceptor throws, Axios skips the next fulfillment interceptor and calls the next rejection interceptor.
|
|
1375
1537
|
- After the error is caught, later fulfillment interceptors run again, just like in a promise chain.
|
|
1376
1538
|
|
|
1377
|
-
Read [the interceptor tests](./
|
|
1539
|
+
Read [the interceptor tests](./tests/browser/interceptors.browser.test.js) to see all this in code.
|
|
1378
1540
|
|
|
1379
1541
|
## Error types
|
|
1380
1542
|
|
|
@@ -1432,6 +1594,10 @@ axios.get('/user/12345').catch(function (error) {
|
|
|
1432
1594
|
});
|
|
1433
1595
|
```
|
|
1434
1596
|
|
|
1597
|
+
Axios rejects an `http:` or `https:` request `url` or `baseURL` that omits `//` after the protocol. For example, `https:example.com` and `https:/example.com` reject with `AxiosError` code `ERR_INVALID_URL` instead of being silently normalized. Pass a well-formed URL such as `https://example.com`.
|
|
1598
|
+
|
|
1599
|
+
The error identifies the malformed URL, for example `Invalid URL "https:example.com": missing "//" after protocol`. To keep logs useful without leaking secrets, Axios reports the control-character-normalized URL with credentials, query parameter values, and fragment contents redacted; the scheme, host, path, and query parameter names remain visible.
|
|
1600
|
+
|
|
1435
1601
|
Use `validateStatus` to override the default condition (`status >= 200 && status < 300`) and choose which HTTP status codes should reject.
|
|
1436
1602
|
|
|
1437
1603
|
```js
|
|
@@ -1867,6 +2033,24 @@ formData.append('users[1][surname]', 'Anderson');
|
|
|
1867
2033
|
formData.append('obj2{}', '[{"x":1}]');
|
|
1868
2034
|
```
|
|
1869
2035
|
|
|
2036
|
+
### Converting FormData to JSON
|
|
2037
|
+
|
|
2038
|
+
`axios.formToJSON()` converts `FormData` field names that use dot or bracket notation into nested objects and arrays. Only `.`, `[`, and `]` are structural separators. Other characters, including `-`, spaces, `+`, `*`, and `&`, remain part of the literal key.
|
|
2039
|
+
|
|
2040
|
+
```js
|
|
2041
|
+
const form = new FormData();
|
|
2042
|
+
form.append('user-name', 'johndoe');
|
|
2043
|
+
form.append('user.name', 'john');
|
|
2044
|
+
|
|
2045
|
+
console.log(axios.formToJSON(form));
|
|
2046
|
+
// {
|
|
2047
|
+
// 'user-name': 'johndoe',
|
|
2048
|
+
// user: { name: 'john' }
|
|
2049
|
+
// }
|
|
2050
|
+
```
|
|
2051
|
+
|
|
2052
|
+
`user[name]` also creates `{ user: { name: ... } }`, while `items[]` creates an array.
|
|
2053
|
+
|
|
1870
2054
|
Axios supports `postForm`, `putForm`, and `patchForm` as shortcuts for the matching HTTP methods with the `Content-Type` header preset to `multipart/form-data`.
|
|
1871
2055
|
|
|
1872
2056
|
## Posting files
|
|
@@ -1940,10 +2124,9 @@ submits this JSON object:
|
|
|
1940
2124
|
{
|
|
1941
2125
|
"foo": "1",
|
|
1942
2126
|
"deep": {
|
|
1943
|
-
"prop":
|
|
1944
|
-
"spaced": "3"
|
|
1945
|
-
}
|
|
2127
|
+
"prop": "2"
|
|
1946
2128
|
},
|
|
2129
|
+
"deep prop spaced": "3",
|
|
1947
2130
|
"baz": [
|
|
1948
2131
|
"4",
|
|
1949
2132
|
"5"
|
|
@@ -1954,6 +2137,8 @@ submits this JSON object:
|
|
|
1954
2137
|
}
|
|
1955
2138
|
```
|
|
1956
2139
|
|
|
2140
|
+
Only dot and bracket notation create nested paths when a form is converted to JSON. Spaces and other punctuation remain part of literal field names.
|
|
2141
|
+
|
|
1957
2142
|
Sending `Blobs`/`Files` as JSON (`base64`) is not currently supported.
|
|
1958
2143
|
|
|
1959
2144
|
## Progress capturing
|
|
@@ -2182,8 +2367,9 @@ Returns `this`.
|
|
|
2182
2367
|
### AxiosHeaders#get(header)
|
|
2183
2368
|
|
|
2184
2369
|
```
|
|
2185
|
-
get(headerName: string,
|
|
2370
|
+
get(headerName: string, parser: typeof AxiosHeaders.parseParameters): AxiosHeaderParameters;
|
|
2186
2371
|
get(headerName: string, parser: RegExp): RegExpExecArray | null;
|
|
2372
|
+
get(headerName: string, matcher?: true | AxiosHeaderParser): AxiosHeaderValue;
|
|
2187
2373
|
```
|
|
2188
2374
|
|
|
2189
2375
|
Returns the internal value of the header. It can take an extra argument to parse the header's value with `RegExp.exec`,
|
|
@@ -2203,6 +2389,15 @@ console.log(headers.get('Content-Type', true)); // parse key-value pairs from a
|
|
|
2203
2389
|
// boundary: 'Asrf456BGe4h'
|
|
2204
2390
|
// }
|
|
2205
2391
|
|
|
2392
|
+
const quotedHeaders = new AxiosHeaders({
|
|
2393
|
+
'Content-Type': 'multipart/form-data; boundary="a,b"',
|
|
2394
|
+
});
|
|
2395
|
+
|
|
2396
|
+
console.log({
|
|
2397
|
+
...quotedHeaders.get('Content-Type', AxiosHeaders.parseParameters),
|
|
2398
|
+
});
|
|
2399
|
+
// { boundary: 'a,b' }
|
|
2400
|
+
|
|
2206
2401
|
console.log(
|
|
2207
2402
|
headers.get('Content-Type', (value, name, headers) => {
|
|
2208
2403
|
return String(value).replace(/a/g, 'ZZZ');
|
|
@@ -2214,6 +2409,10 @@ console.log(headers.get('Content-Type', /boundary=(\w+)/)?.[0]);
|
|
|
2214
2409
|
// boundary=Asrf456BGe4h
|
|
2215
2410
|
```
|
|
2216
2411
|
|
|
2412
|
+
`AxiosHeaders.parseParameters` is the opt-in parser for normalized HTTP parameter values. It returns a null-prototype map, matches parameter names case-insensitively, removes quoted-string delimiters, decodes escaped quotes and backslashes, preserves commas and semicolons inside quoted values, and removes only HTTP optional whitespace around unquoted values. It omits the unsafe keys `__proto__`, `constructor`, and `prototype`.
|
|
2413
|
+
|
|
2414
|
+
Passing `true` remains the legacy tokenizer and keeps its existing output for backward compatibility.
|
|
2415
|
+
|
|
2217
2416
|
Returns the value of the header.
|
|
2218
2417
|
|
|
2219
2418
|
### AxiosHeaders#has(header, matcher?)
|
|
@@ -2498,6 +2697,99 @@ try {
|
|
|
2498
2697
|
}
|
|
2499
2698
|
```
|
|
2500
2699
|
|
|
2700
|
+
### Typing request data and query params
|
|
2701
|
+
|
|
2702
|
+
`AxiosRequestConfig<D = any, P = any>` uses `D` for the request body and `P` for query parameters. The params serializer receives the same `P`, and both types are preserved on the default response config:
|
|
2703
|
+
|
|
2704
|
+
```typescript
|
|
2705
|
+
import axios, {
|
|
2706
|
+
type AxiosPromise,
|
|
2707
|
+
type AxiosRequestConfig,
|
|
2708
|
+
type InternalAxiosRequestConfig,
|
|
2709
|
+
} from 'axios';
|
|
2710
|
+
|
|
2711
|
+
interface RequestBody {
|
|
2712
|
+
includeArchived: boolean;
|
|
2713
|
+
}
|
|
2714
|
+
|
|
2715
|
+
interface SearchParams {
|
|
2716
|
+
query: string;
|
|
2717
|
+
page?: number;
|
|
2718
|
+
}
|
|
2719
|
+
|
|
2720
|
+
interface SearchResponse {
|
|
2721
|
+
results: string[];
|
|
2722
|
+
}
|
|
2723
|
+
|
|
2724
|
+
const config: AxiosRequestConfig<RequestBody, SearchParams> = {
|
|
2725
|
+
data: { includeArchived: false },
|
|
2726
|
+
params: { query: 'axios', page: 1 },
|
|
2727
|
+
paramsSerializer: (params) => `${params.query}:${params.page ?? 1}`,
|
|
2728
|
+
};
|
|
2729
|
+
|
|
2730
|
+
const response = await axios.get('/search', config);
|
|
2731
|
+
response.config.data; // RequestBody | undefined
|
|
2732
|
+
response.config.params; // SearchParams | undefined
|
|
2733
|
+
|
|
2734
|
+
const invalidConfig: AxiosRequestConfig<RequestBody, SearchParams> = {
|
|
2735
|
+
// @ts-expect-error `query` must be a string
|
|
2736
|
+
params: { query: 123 },
|
|
2737
|
+
};
|
|
2738
|
+
|
|
2739
|
+
const adapter = (
|
|
2740
|
+
adapterConfig: InternalAxiosRequestConfig<RequestBody, SearchParams>
|
|
2741
|
+
): AxiosPromise<SearchResponse, RequestBody, SearchParams> =>
|
|
2742
|
+
Promise.resolve({
|
|
2743
|
+
data: { results: [] },
|
|
2744
|
+
status: 200,
|
|
2745
|
+
statusText: 'OK',
|
|
2746
|
+
headers: {},
|
|
2747
|
+
config: adapterConfig,
|
|
2748
|
+
});
|
|
2749
|
+
```
|
|
2750
|
+
|
|
2751
|
+
The request methods place `P` lastβ`<T, R, D, P>`βso the existing response data (`T`), custom response (`R`), and request data (`D`) positions do not change. Explicit custom response types still determine the resolved value. `RawAxiosRequestConfig`, `InternalAxiosRequestConfig`, defaults, `AxiosResponse`, `AxiosPromise<T, D, P>`, `AxiosError`, `CanceledError`, callable instances, adapters, and `mergeConfig()` also carry `P`. The default is `any` for backward compatibility.
|
|
2752
|
+
|
|
2753
|
+
Use all three public error generics when cancellation narrowing must retain the response, request body, and params types:
|
|
2754
|
+
|
|
2755
|
+
```typescript
|
|
2756
|
+
declare const error: unknown;
|
|
2757
|
+
|
|
2758
|
+
if (axios.isCancel<SearchResponse, RequestBody, SearchParams>(error)) {
|
|
2759
|
+
error.config?.data; // RequestBody | undefined
|
|
2760
|
+
error.config?.params; // SearchParams | undefined
|
|
2761
|
+
}
|
|
2762
|
+
```
|
|
2763
|
+
|
|
2764
|
+
### Symbol-keyed custom request config
|
|
2765
|
+
|
|
2766
|
+
Own enumerable symbol properties survive config merging, so module-augmented options can reach request interceptors and adapters:
|
|
2767
|
+
|
|
2768
|
+
```typescript
|
|
2769
|
+
import axios from 'axios';
|
|
2770
|
+
|
|
2771
|
+
export const someFlag: unique symbol = Symbol(
|
|
2772
|
+
'some flag used in request interceptor'
|
|
2773
|
+
);
|
|
2774
|
+
|
|
2775
|
+
declare module 'axios' {
|
|
2776
|
+
interface AxiosRequestConfig<D = any, P = any> {
|
|
2777
|
+
[someFlag]?: boolean;
|
|
2778
|
+
}
|
|
2779
|
+
}
|
|
2780
|
+
|
|
2781
|
+
axios.interceptors.request.use((config) => {
|
|
2782
|
+
if (config[someFlag]) {
|
|
2783
|
+
config.headers.set('X-Some-Flag', 'enabled');
|
|
2784
|
+
}
|
|
2785
|
+
return config;
|
|
2786
|
+
});
|
|
2787
|
+
|
|
2788
|
+
await axios.get('/users', { [someFlag]: true });
|
|
2789
|
+
```
|
|
2790
|
+
|
|
2791
|
+
Only own enumerable symbol properties are copied; inherited and non-enumerable symbol properties are not.
|
|
2792
|
+
|
|
2501
2793
|
Because axios publishes an ESM default export and a CJS `module.exports`, TypeScript has a few caveats.
|
|
2502
2794
|
The recommended setting is `"moduleResolution": "node16"`, which is implied by `"module": "node16"`. This requires TypeScript 4.7 or greater.
|
|
2503
2795
|
If you use ESM, your settings should be fine.
|