@indra211/exios 1.0.0 → 2.0.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/README.md +54 -14
- package/dist/index.cjs +939 -395
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +74 -8
- package/dist/index.js +940 -395
- package/dist/index.node.cjs +2545 -0
- package/dist/index.node.js +2515 -0
- package/package.json +59 -4
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# exios
|
|
2
2
|
|
|
3
|
-
An axios-compatible HTTP client
|
|
3
|
+
An axios-compatible HTTP client with **exponential-backoff retry** and a **circuit breaker** built in. Like axios it uses **XHR in browsers** and **Node's `http`/`https` in Node**, with native `fetch` as a third adapter. Zero runtime dependencies, fully typed, ESM + CJS, runs in Node >= 20, modern browsers, Bun, Deno and edge runtimes.
|
|
4
4
|
|
|
5
5
|
Retry and the breaker are **on by default** for the default instance and for every `create()` instance, with safe defaults (idempotent methods only, `Retry-After` honoured, breaker outside the retry loop).
|
|
6
6
|
|
|
@@ -52,14 +52,59 @@ try {
|
|
|
52
52
|
|
|
53
53
|
The API surface matches axios: `exios(config)`, `exios(url, config)`, `exios.request`, `get/delete/head/options`, `post/put/patch`, `postForm/putForm/patchForm`, `create`, `defaults`, `interceptors`, `getUri`. Responses are `{ data, status, statusText, headers, config, request, meta }`.
|
|
54
54
|
|
|
55
|
+
## Adapters
|
|
56
|
+
|
|
57
|
+
| Adapter | Used by default in | Notes |
|
|
58
|
+
|---|---|---|
|
|
59
|
+
| `xhr` | browsers and web workers | Real upload and download progress everywhere. Ignores `fetchOptions`; always follows redirects (`redirect` is ignored). `responseType: 'stream'` is served by `fetch` (XHR cannot stream); a `ReadableStream` request body is read into memory first. |
|
|
60
|
+
| `http` | Node and Bun (Node build) | Node `http`/`https`: `proxy`, `httpAgent`/`httpsAgent`, `maxRedirects`, `beforeRedirect`, `socketPath`, `decompress`, `insecureHTTPParser`, Node `Readable` streams, upload and download progress. |
|
|
61
|
+
| `fetch` | Deno, Cloudflare Workers, edge runtimes, service workers | `fetchOptions`, `redirect`, custom `fetch`. Upload progress streams the body where the runtime supports request streams (Node, Deno, Bun, workers); in browser pages it reports one final event. |
|
|
62
|
+
|
|
63
|
+
The default is `adapter: ['xhr', 'http', 'fetch']`: the first one available wins. Force one with `adapter: 'fetch'` (or pass your own `(config) => Promise<response>` function). Asking for an adapter the runtime does not have rejects with `ERR_NOT_SUPPORT`. `package.json` export conditions pick the build automatically: `node` gets the Node build (with `http`); `browser`, `deno`, `workerd` and `edge-light` get the browser build (no `node:*` imports).
|
|
64
|
+
|
|
65
|
+
Retry, the circuit breaker, interceptors, timeouts, cancellation and error codes behave the same on every adapter. `response.request` is the `XMLHttpRequest`, `ClientRequest` or `Request` that was sent; on the `http` adapter `response.request.responseURL` is the final URL after redirects.
|
|
66
|
+
|
|
67
|
+
With `responseType: 'stream'`, `timeout` covers only getting the response, not reading the stream; aborting `signal` (or a `cancelToken`) after the response resolved still stops the stream with a `CanceledError`. A stream body from an attempt that is retried is cancelled.
|
|
68
|
+
|
|
69
|
+
### Node `http` options
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
await exios.get(url, {
|
|
73
|
+
proxy: { protocol: "http", host: "127.0.0.1", port: 8080, auth: { username: "u", password: "p" } },
|
|
74
|
+
httpsAgent: new https.Agent({ keepAlive: true }),
|
|
75
|
+
maxRedirects: 5, // default 21; 0 returns the 3xx response
|
|
76
|
+
beforeRedirect: (options, { statusCode, headers }) => {},
|
|
77
|
+
decompress: true, // gzip, deflate, br (default true)
|
|
78
|
+
socketPath: "/var/run/docker.sock",
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
- **Proxy:** `proxy` wins; `proxy: false` disables proxying; otherwise `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` (and lowercase variants) are used, like axios. HTTPS targets go through a `CONNECT` tunnel, using the TLS options (`ca`, `rejectUnauthorized`, ...) of your `httpsAgent`. An `https:` proxy (`protocol: 'https'` or `HTTPS_PROXY=https://...`) is reached over TLS. Aborts and timeouts also tear down a `CONNECT` still in progress.
|
|
83
|
+
- **Redirects:** 303, and 301/302 after a POST, become GET without a body; 307/308 resend the method and body (a one-shot stream body cannot be resent, so the 307/308 is returned). A redirect to another host or port, or from `https:` to `http:`, drops `Authorization`, `Cookie` and `Proxy-Authorization`. A redirect that would have to resend a one-shot stream body is returned instead of followed. Too many redirects reject with `ERR_FR_TOO_MANY_REDIRECTS` (never retried).
|
|
84
|
+
- **Network errors** keep `code: 'ERR_NETWORK'` (retry and the breaker depend on it); the Node error is in `error.cause`, so `error.cause.code` is `ECONNREFUSED`, `ECONNRESET`, `ENOTFOUND`, ... (axios puts that in `error.code` instead).
|
|
85
|
+
- **Bodies:** `FormData`, `Blob`, `URLSearchParams`, strings and typed arrays are buffered (so they are resent on retries and redirects, with an exact `Content-Length`); web `ReadableStream` and Node `Readable` bodies (including `fs` streams and the `form-data` package, whose `getHeaders()` boundary is used) are streamed and never retried. If a body stream fails, the request rejects with `ERR_BAD_REQUEST` (the stream's error is in `cause`), and an aborted or timed-out request destroys the body stream.
|
|
86
|
+
|
|
87
|
+
## Progress, XSRF, encodings and CancelToken
|
|
88
|
+
|
|
89
|
+
- **`onUploadProgress` / `onDownloadProgress`** on every adapter, axios event shape: `{ loaded, total, progress, bytes, rate, estimated, upload | download, event }`. With `decompress`, the download `total` is omitted (the `Content-Length` is the compressed size).
|
|
90
|
+
- **XSRF** (browsers): `xsrfCookieName` (default `'XSRF-TOKEN'`) is read from `document.cookie` and sent as `xsrfHeaderName` (default `'X-XSRF-TOKEN'`) when `withXSRFToken` is `true`, or when it is unset and the request is same-origin. `withXSRFToken: false` never sends it; a function decides per request. A header you set yourself is never overwritten.
|
|
91
|
+
- **`responseEncoding`** decodes text and JSON responses with any `TextDecoder` label (`'latin1'`, `'shift_jis'`, ...); default UTF-8. An unknown label rejects with `ERR_BAD_OPTION_VALUE` before any request is sent.
|
|
92
|
+
- **`CancelToken`** (deprecated in favour of `signal`, as in axios): `CancelToken.source()` → `{ token, cancel(message) }`, `new CancelToken(executor)`, `token.reason`, `token.throwIfRequested()`, `token.subscribe/unsubscribe`, `token.toAbortSignal()`. With both `signal` and `cancelToken`, whichever fires first cancels; cancelling also stops a retry backoff and is never counted by the breaker.
|
|
93
|
+
|
|
94
|
+
## Breaking changes since 1.0.0
|
|
95
|
+
|
|
96
|
+
- In Node the default transport is now Node `http`/`https` (it was `fetch`), so `responseType: 'stream'` returns a Node `Readable` instead of a web `ReadableStream`. Use `adapter: 'fetch'` to get 1.0.0 behaviour back.
|
|
97
|
+
- `response.request` / `error.request` are the adapter's request object (typed `any`), not always a `Request`.
|
|
98
|
+
- `responseEncoding` is now honoured (it was ignored).
|
|
99
|
+
|
|
55
100
|
## Migrating from axios
|
|
56
101
|
|
|
57
102
|
1. Swap the import: `import axios from 'axios'` becomes `import exios from '@indra211/exios'`. Named helpers: `isExiosError` (was `isAxiosError`), `isCancel`, `ExiosError`, `CanceledError`, `toFormData`, `mergeConfig`.
|
|
58
|
-
2.
|
|
59
|
-
3. Error codes are the axios strings (`ERR_BAD_REQUEST`, `ERR_BAD_RESPONSE`, `ERR_NETWORK`, `ECONNABORTED`, `ETIMEDOUT`, `ERR_CANCELED`, `ERR_BAD_OPTION_VALUE`) plus the new `ERR_CIRCUIT_OPEN`.
|
|
103
|
+
2. `CancelToken` works as in axios (and is deprecated there too); prefer `signal: controller.signal`.
|
|
104
|
+
3. Error codes are the axios strings (`ERR_BAD_REQUEST`, `ERR_BAD_RESPONSE`, `ERR_NETWORK`, `ECONNABORTED`, `ETIMEDOUT`, `ERR_CANCELED`, `ERR_BAD_OPTION_VALUE`, `ERR_NOT_SUPPORT`, `ERR_FR_TOO_MANY_REDIRECTS`) plus the new `ERR_CIRCUIT_OPEN`.
|
|
60
105
|
4. The number of attempts made is `response.meta.attempts` (and `error.meta.attempts`). A request rejected by an open circuit has `attempts: 0`.
|
|
61
|
-
5. `response.headers` is a plain lowercase-keyed object; `response.request` is the
|
|
62
|
-
6.
|
|
106
|
+
5. `response.headers` is a plain lowercase-keyed object; `response.request` is the adapter's request object (see Adapters).
|
|
107
|
+
6. Node transport options (`proxy`, agents, `maxRedirects`, `socketPath`, `decompress`, ...) work on the `http` adapter. The `fetch` adapter adds `fetch` (custom implementation), `fetchOptions` (`cache`, `credentials`, `mode`, `redirect`, `referrer`, `referrerPolicy`, `integrity`, `keepalive`, `priority`) and `redirect`. `fetchOptions.credentials` is used as given; `withCredentials: true` forces `'include'`, and the default is `'same-origin'`.
|
|
63
108
|
7. Any object body that is not `FormData`, `Blob`, `ArrayBuffer`/typed array, `ReadableStream` or `URLSearchParams` (including class instances and `Date`) is sent as JSON, like axios. Only plain objects and arrays are converted for `multipart/form-data` or `application/x-www-form-urlencoded`.
|
|
64
109
|
8. If you used `axios-retry` or `opossum`, remove them: use the `retry` and `breaker` options below.
|
|
65
110
|
|
|
@@ -130,18 +175,13 @@ Also:
|
|
|
130
175
|
- **2xx responses are never retried**, even if `shouldRetry` returns true.
|
|
131
176
|
- **Throwing request interceptors.** A synchronous request interceptor that throws routes to the next interceptor's `onRejected` (this differs from axios).
|
|
132
177
|
- **Hung half-open probes.** A half-open probe that hangs longer than `resetTimeoutMs` counts as a failure.
|
|
133
|
-
- **Never retried:** cancellations, `ERR_CIRCUIT_OPEN`, `ERR_BAD_OPTION_VALUE`, and requests whose body is a one-shot `ReadableStream
|
|
178
|
+
- **Never retried:** cancellations, `ERR_CIRCUIT_OPEN`, `ERR_BAD_OPTION_VALUE`, `ERR_NOT_SUPPORT`, `ERR_FR_TOO_MANY_REDIRECTS`, and requests whose body is a one-shot stream (web `ReadableStream` or Node `Readable`).
|
|
134
179
|
|
|
135
180
|
## Not supported
|
|
136
181
|
|
|
137
|
-
-
|
|
138
|
-
-
|
|
139
|
-
-
|
|
140
|
-
- **`responseEncoding`:** ignored; text responses are always decoded as UTF-8.
|
|
141
|
-
- **XSRF** cookie/header handling.
|
|
142
|
-
- **Adapters:** there is a single transport, `fetch`. Inject a custom implementation with the `fetch` option.
|
|
143
|
-
- **`CancelToken`:** use `AbortSignal`.
|
|
144
|
-
- **Distributed or persisted breaker state.**
|
|
182
|
+
- `maxRate` (bandwidth limits), `lookup` / `family` DNS options, HTTP/2 on the `http` adapter.
|
|
183
|
+
- `redirect: 'manual' | 'error'` and `fetchOptions` on the `xhr` adapter (browsers' XHR always follows redirects); use `adapter: 'fetch'` when you need them.
|
|
184
|
+
- Distributed or persisted breaker state.
|
|
145
185
|
|
|
146
186
|
## License
|
|
147
187
|
|