@zairakai/js-http-client 1.0.15 → 1.0.16
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/LICENSE +21 -0
- package/README.md +22 -1
- package/dist/{chunk-SSH6WKEL.js → chunk-NWOTSCRW.js} +2 -2
- package/dist/index.cjs +8 -10
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +7 -9
- package/dist/{interceptors-Pv-t9Hbf.d.cts → interceptors-CDlYhrao.d.cts} +3 -2
- package/dist/{interceptors-Pv-t9Hbf.d.ts → interceptors-CDlYhrao.d.ts} +3 -2
- package/dist/interceptors.cjs +2 -2
- package/dist/interceptors.d.cts +1 -1
- package/dist/interceptors.d.ts +1 -1
- package/dist/interceptors.js +1 -1
- package/package.json +9 -7
- package/src/client.ts +6 -7
- package/src/interceptors.ts +7 -6
- package/src/request-tracker.ts +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Stanislas Poisson (Zairakai)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# JS HTTP Client
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
`@zairakai/js-http-client`
|
|
2
6
|
|
|
3
7
|
[![Main][pipeline-main-badge]][pipeline-main-link]
|
|
4
8
|
[![Develop][pipeline-develop-badge]][pipeline-develop-link]
|
|
@@ -12,6 +16,8 @@
|
|
|
12
16
|
[![Node.js][node-badge]][node]
|
|
13
17
|
[![ESLint][eslint-badge]][eslint]
|
|
14
18
|
[![Prettier][prettier-badge]][prettier]
|
|
19
|
+
[![Vitest][vitest-badge]][vitest]
|
|
20
|
+
[![Knip][knip-badge]][knip]
|
|
15
21
|
|
|
16
22
|
Axios-based HTTP client with request tracking, interceptor management, and Laravel CSRF support.
|
|
17
23
|
|
|
@@ -41,6 +47,17 @@ The documentation site has this guide and the reference of the API generated fro
|
|
|
41
47
|
npm install @zairakai/js-http-client
|
|
42
48
|
```
|
|
43
49
|
|
|
50
|
+
### In the browser
|
|
51
|
+
|
|
52
|
+
The library is made for the web: ESM and CommonJS builds, no Node.js built-ins, `"sideEffects": false`, and `axios` stays external (your bundler picks it up as a dependency). A bundler keeps only what you import (the tests of this repository check it with esbuild): a project that imports `createRequestTracker` alone gets a few hundred bytes, and the interceptors have their own entry point:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
import { createHttpClient } from '@zairakai/js-http-client'
|
|
56
|
+
import { createAuthInterceptor } from '@zairakai/js-http-client/interceptors'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
There is no minimum browser version.
|
|
60
|
+
|
|
44
61
|
---
|
|
45
62
|
|
|
46
63
|
## Usage
|
|
@@ -150,6 +167,10 @@ make test-all # vitest + bats + coverage
|
|
|
150
167
|
[eslint]: https://eslint.org
|
|
151
168
|
[prettier-badge]: https://img.shields.io/badge/formatter-prettier-F7B93E.svg?logo=prettier
|
|
152
169
|
[prettier]: https://prettier.io
|
|
170
|
+
[vitest-badge]: https://img.shields.io/badge/test-vitest-6E9F18.svg?logo=vitest
|
|
171
|
+
[vitest]: https://vitest.dev
|
|
172
|
+
[knip-badge]: https://img.shields.io/badge/dead%20code-knip-EC4899.svg
|
|
173
|
+
[knip]: https://knip.dev
|
|
153
174
|
[ecosystem]: https://gitlab.com/zairakai
|
|
154
175
|
[docs]: https://js-http-client-eb9079.gitlab.io
|
|
155
176
|
[docs-badge]: https://img.shields.io/badge/docs-online-blue
|
|
@@ -18,7 +18,7 @@ var createTrackingInterceptors = (tracker) => ({
|
|
|
18
18
|
}
|
|
19
19
|
});
|
|
20
20
|
var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
21
|
-
const token = "function"
|
|
21
|
+
const token = typeof tokenSource === "function" ? tokenSource() : tokenSource;
|
|
22
22
|
if (token) {
|
|
23
23
|
config.headers = config.headers ?? {};
|
|
24
24
|
config.headers["X-CSRF-TOKEN"] = token;
|
|
@@ -26,7 +26,7 @@ var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
|
26
26
|
return config;
|
|
27
27
|
};
|
|
28
28
|
var createAuthInterceptor = (tokenSource, type = "Bearer") => (config) => {
|
|
29
|
-
const token = "function"
|
|
29
|
+
const token = typeof tokenSource === "function" ? tokenSource() : tokenSource;
|
|
30
30
|
if (token) {
|
|
31
31
|
config.headers = config.headers ?? {};
|
|
32
32
|
config.headers["Authorization"] = `${type} ${token}`;
|
package/dist/index.cjs
CHANGED
|
@@ -68,7 +68,7 @@ var createTrackingInterceptors = (tracker) => ({
|
|
|
68
68
|
}
|
|
69
69
|
});
|
|
70
70
|
var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
71
|
-
const token = "function"
|
|
71
|
+
const token = typeof tokenSource === "function" ? tokenSource() : tokenSource;
|
|
72
72
|
if (token) {
|
|
73
73
|
config.headers = config.headers ?? {};
|
|
74
74
|
config.headers["X-CSRF-TOKEN"] = token;
|
|
@@ -76,7 +76,7 @@ var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
|
76
76
|
return config;
|
|
77
77
|
};
|
|
78
78
|
var createAuthInterceptor = (tokenSource, type = "Bearer") => (config) => {
|
|
79
|
-
const token = "function"
|
|
79
|
+
const token = typeof tokenSource === "function" ? tokenSource() : tokenSource;
|
|
80
80
|
if (token) {
|
|
81
81
|
config.headers = config.headers ?? {};
|
|
82
82
|
config.headers["Authorization"] = `${type} ${token}`;
|
|
@@ -125,7 +125,7 @@ var createRequestTracker = () => {
|
|
|
125
125
|
nbrInternal = Math.max(0, val);
|
|
126
126
|
},
|
|
127
127
|
get isActive() {
|
|
128
|
-
return
|
|
128
|
+
return nbrInternal > 0;
|
|
129
129
|
},
|
|
130
130
|
increment() {
|
|
131
131
|
nbrInternal++;
|
|
@@ -162,7 +162,7 @@ var createHttpClient = (options = {}) => {
|
|
|
162
162
|
withCredentials: config.withCredentials,
|
|
163
163
|
headers: config.headers
|
|
164
164
|
});
|
|
165
|
-
const tracker =
|
|
165
|
+
const tracker = config.trackRequests !== false ? createRequestTracker() : null;
|
|
166
166
|
if (tracker) {
|
|
167
167
|
const trackingInterceptors = createTrackingInterceptors(tracker);
|
|
168
168
|
client.interceptors.request.use(trackingInterceptors.request, trackingInterceptors.requestError);
|
|
@@ -177,21 +177,19 @@ var createHttpClient = (options = {}) => {
|
|
|
177
177
|
if (config.timeout !== DEFAULT_CONFIG.timeout) {
|
|
178
178
|
client.interceptors.request.use(createTimeoutInterceptor(config.timeout));
|
|
179
179
|
}
|
|
180
|
-
if (
|
|
180
|
+
if (config.enableErrorLogging !== false) {
|
|
181
181
|
client.interceptors.response.use((response) => response, createErrorLoggerInterceptor(config.logger));
|
|
182
182
|
}
|
|
183
|
-
if (config.retries &&
|
|
183
|
+
if (config.retries && config.retries > 0) {
|
|
184
184
|
client.interceptors.request.use((reqConfig) => {
|
|
185
|
-
;
|
|
186
|
-
reqConfig._axios = client;
|
|
187
|
-
return reqConfig;
|
|
185
|
+
return Object.assign(reqConfig, { _axios: client });
|
|
188
186
|
});
|
|
189
187
|
client.interceptors.response.use(
|
|
190
188
|
(response) => response,
|
|
191
189
|
createRetryInterceptor(config.retries, config.retryDelay, config.shouldRetry)
|
|
192
190
|
);
|
|
193
191
|
}
|
|
194
|
-
if (
|
|
192
|
+
if (config.laravel !== false) {
|
|
195
193
|
client.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
|
|
196
194
|
}
|
|
197
195
|
return {
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
export { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, Method as HttpMethod, default as axios } from 'axios';
|
|
3
|
-
import { R as RequestTracker, T as TokenSource, L as Logger, S as ShouldRetryFunction } from './interceptors-
|
|
4
|
-
export { a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, e as createRequestTracker, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors, i as globalRequestTracker } from './interceptors-
|
|
3
|
+
import { R as RequestTracker, T as TokenSource, L as Logger, S as ShouldRetryFunction } from './interceptors-CDlYhrao.cjs';
|
|
4
|
+
export { a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, e as createRequestTracker, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors, i as globalRequestTracker } from './interceptors-CDlYhrao.cjs';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* HTTP client factory with configurable options
|
|
@@ -77,4 +77,5 @@ declare const createLaravelClient: (options?: HttpClientOptions) => HttpClient;
|
|
|
77
77
|
*/
|
|
78
78
|
declare const createApiClient: (options?: HttpClientOptions) => HttpClient;
|
|
79
79
|
|
|
80
|
-
export {
|
|
80
|
+
export { Logger, RequestTracker, ShouldRetryFunction, TokenSource, createApiClient, createHttpClient, createLaravelClient };
|
|
81
|
+
export type { HttpClient, HttpClientOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
2
|
export { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, Method as HttpMethod, default as axios } from 'axios';
|
|
3
|
-
import { R as RequestTracker, T as TokenSource, L as Logger, S as ShouldRetryFunction } from './interceptors-
|
|
4
|
-
export { a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, e as createRequestTracker, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors, i as globalRequestTracker } from './interceptors-
|
|
3
|
+
import { R as RequestTracker, T as TokenSource, L as Logger, S as ShouldRetryFunction } from './interceptors-CDlYhrao.js';
|
|
4
|
+
export { a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, e as createRequestTracker, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors, i as globalRequestTracker } from './interceptors-CDlYhrao.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* HTTP client factory with configurable options
|
|
@@ -77,4 +77,5 @@ declare const createLaravelClient: (options?: HttpClientOptions) => HttpClient;
|
|
|
77
77
|
*/
|
|
78
78
|
declare const createApiClient: (options?: HttpClientOptions) => HttpClient;
|
|
79
79
|
|
|
80
|
-
export {
|
|
80
|
+
export { Logger, RequestTracker, ShouldRetryFunction, TokenSource, createApiClient, createHttpClient, createLaravelClient };
|
|
81
|
+
export type { HttpClient, HttpClientOptions };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
createRetryInterceptor,
|
|
6
6
|
createTimeoutInterceptor,
|
|
7
7
|
createTrackingInterceptors
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-NWOTSCRW.js";
|
|
9
9
|
|
|
10
10
|
// src/client.ts
|
|
11
11
|
import axios from "axios";
|
|
@@ -21,7 +21,7 @@ var createRequestTracker = () => {
|
|
|
21
21
|
nbrInternal = Math.max(0, val);
|
|
22
22
|
},
|
|
23
23
|
get isActive() {
|
|
24
|
-
return
|
|
24
|
+
return nbrInternal > 0;
|
|
25
25
|
},
|
|
26
26
|
increment() {
|
|
27
27
|
nbrInternal++;
|
|
@@ -58,7 +58,7 @@ var createHttpClient = (options = {}) => {
|
|
|
58
58
|
withCredentials: config.withCredentials,
|
|
59
59
|
headers: config.headers
|
|
60
60
|
});
|
|
61
|
-
const tracker =
|
|
61
|
+
const tracker = config.trackRequests !== false ? createRequestTracker() : null;
|
|
62
62
|
if (tracker) {
|
|
63
63
|
const trackingInterceptors = createTrackingInterceptors(tracker);
|
|
64
64
|
client.interceptors.request.use(trackingInterceptors.request, trackingInterceptors.requestError);
|
|
@@ -73,21 +73,19 @@ var createHttpClient = (options = {}) => {
|
|
|
73
73
|
if (config.timeout !== DEFAULT_CONFIG.timeout) {
|
|
74
74
|
client.interceptors.request.use(createTimeoutInterceptor(config.timeout));
|
|
75
75
|
}
|
|
76
|
-
if (
|
|
76
|
+
if (config.enableErrorLogging !== false) {
|
|
77
77
|
client.interceptors.response.use((response) => response, createErrorLoggerInterceptor(config.logger));
|
|
78
78
|
}
|
|
79
|
-
if (config.retries &&
|
|
79
|
+
if (config.retries && config.retries > 0) {
|
|
80
80
|
client.interceptors.request.use((reqConfig) => {
|
|
81
|
-
;
|
|
82
|
-
reqConfig._axios = client;
|
|
83
|
-
return reqConfig;
|
|
81
|
+
return Object.assign(reqConfig, { _axios: client });
|
|
84
82
|
});
|
|
85
83
|
client.interceptors.response.use(
|
|
86
84
|
(response) => response,
|
|
87
85
|
createRetryInterceptor(config.retries, config.retryDelay, config.shouldRetry)
|
|
88
86
|
);
|
|
89
87
|
}
|
|
90
|
-
if (
|
|
88
|
+
if (config.laravel !== false) {
|
|
91
89
|
client.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
|
|
92
90
|
}
|
|
93
91
|
return {
|
|
@@ -88,6 +88,7 @@ declare const createTimeoutInterceptor: (timeout: number) => (config: InternalAx
|
|
|
88
88
|
* @param shouldRetry - Function to determine if request should be retried
|
|
89
89
|
* @returns Response error interceptor
|
|
90
90
|
*/
|
|
91
|
-
declare const createRetryInterceptor: (retries?: number, delay?: number, shouldRetry?: ShouldRetryFunction) => (error: AxiosError) => Promise<
|
|
91
|
+
declare const createRetryInterceptor: (retries?: number, delay?: number, shouldRetry?: ShouldRetryFunction) => (error: AxiosError) => Promise<AxiosResponse>;
|
|
92
92
|
|
|
93
|
-
export {
|
|
93
|
+
export { createCSRFInterceptor as b, createAuthInterceptor as c, createErrorLoggerInterceptor as d, createRequestTracker as e, createRetryInterceptor as f, createTimeoutInterceptor as g, createTrackingInterceptors as h, globalRequestTracker as i };
|
|
94
|
+
export type { Logger as L, RequestTracker as R, ShouldRetryFunction as S, TokenSource as T, TrackingInterceptors as a };
|
|
@@ -88,6 +88,7 @@ declare const createTimeoutInterceptor: (timeout: number) => (config: InternalAx
|
|
|
88
88
|
* @param shouldRetry - Function to determine if request should be retried
|
|
89
89
|
* @returns Response error interceptor
|
|
90
90
|
*/
|
|
91
|
-
declare const createRetryInterceptor: (retries?: number, delay?: number, shouldRetry?: ShouldRetryFunction) => (error: AxiosError) => Promise<
|
|
91
|
+
declare const createRetryInterceptor: (retries?: number, delay?: number, shouldRetry?: ShouldRetryFunction) => (error: AxiosError) => Promise<AxiosResponse>;
|
|
92
92
|
|
|
93
|
-
export {
|
|
93
|
+
export { createCSRFInterceptor as b, createAuthInterceptor as c, createErrorLoggerInterceptor as d, createRequestTracker as e, createRetryInterceptor as f, createTimeoutInterceptor as g, createTrackingInterceptors as h, globalRequestTracker as i };
|
|
94
|
+
export type { Logger as L, RequestTracker as R, ShouldRetryFunction as S, TokenSource as T, TrackingInterceptors as a };
|
package/dist/interceptors.cjs
CHANGED
|
@@ -57,7 +57,7 @@ var createTrackingInterceptors = (tracker) => ({
|
|
|
57
57
|
}
|
|
58
58
|
});
|
|
59
59
|
var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
60
|
-
const token = "function"
|
|
60
|
+
const token = typeof tokenSource === "function" ? tokenSource() : tokenSource;
|
|
61
61
|
if (token) {
|
|
62
62
|
config.headers = config.headers ?? {};
|
|
63
63
|
config.headers["X-CSRF-TOKEN"] = token;
|
|
@@ -65,7 +65,7 @@ var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
|
65
65
|
return config;
|
|
66
66
|
};
|
|
67
67
|
var createAuthInterceptor = (tokenSource, type = "Bearer") => (config) => {
|
|
68
|
-
const token = "function"
|
|
68
|
+
const token = typeof tokenSource === "function" ? tokenSource() : tokenSource;
|
|
69
69
|
if (token) {
|
|
70
70
|
config.headers = config.headers ?? {};
|
|
71
71
|
config.headers["Authorization"] = `${type} ${token}`;
|
package/dist/interceptors.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import 'axios';
|
|
2
|
-
export { L as Logger, S as ShouldRetryFunction, T as TokenSource, a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors } from './interceptors-
|
|
2
|
+
export { L as Logger, S as ShouldRetryFunction, T as TokenSource, a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors } from './interceptors-CDlYhrao.cjs';
|
package/dist/interceptors.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import 'axios';
|
|
2
|
-
export { L as Logger, S as ShouldRetryFunction, T as TokenSource, a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors } from './interceptors-
|
|
2
|
+
export { L as Logger, S as ShouldRetryFunction, T as TokenSource, a as TrackingInterceptors, c as createAuthInterceptor, b as createCSRFInterceptor, d as createErrorLoggerInterceptor, f as createRetryInterceptor, g as createTimeoutInterceptor, h as createTrackingInterceptors } from './interceptors-CDlYhrao.js';
|
package/dist/interceptors.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zairakai/js-http-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.16",
|
|
4
4
|
"description": "Configurable HTTP client built on axios with request tracking, Laravel CSRF support, and interceptor management",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"http",
|
|
@@ -85,14 +85,15 @@
|
|
|
85
85
|
},
|
|
86
86
|
"devDependencies": {
|
|
87
87
|
"@types/node": "^26.6.6",
|
|
88
|
-
"@vitest/coverage-v8": "^5.0
|
|
89
|
-
"@zairakai/js-dev-tools": "^3.
|
|
90
|
-
"
|
|
88
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
89
|
+
"@zairakai/js-dev-tools": "^3.21.0",
|
|
90
|
+
"esbuild": "^0.28.1",
|
|
91
|
+
"knip": "^6.44.2",
|
|
91
92
|
"sort-package-json": "^4.1.0",
|
|
92
|
-
"tsup": "^
|
|
93
|
+
"tsup": "^9.0.0",
|
|
93
94
|
"typedoc": "^0.28.20",
|
|
94
95
|
"typescript": "^6.0.3",
|
|
95
|
-
"vitest": "^5.0
|
|
96
|
+
"vitest": "^5.1.0"
|
|
96
97
|
},
|
|
97
98
|
"peerDependenciesMeta": {
|
|
98
99
|
"axios": {
|
|
@@ -104,5 +105,6 @@
|
|
|
104
105
|
},
|
|
105
106
|
"publishConfig": {
|
|
106
107
|
"access": "public"
|
|
107
|
-
}
|
|
108
|
+
},
|
|
109
|
+
"sideEffects": false
|
|
108
110
|
}
|
package/src/client.ts
CHANGED
|
@@ -106,7 +106,7 @@ export const createHttpClient = (options: HttpClientOptions = {}): HttpClient =>
|
|
|
106
106
|
})
|
|
107
107
|
|
|
108
108
|
// Create request tracker if enabled
|
|
109
|
-
const tracker =
|
|
109
|
+
const tracker = config.trackRequests !== false ? createRequestTracker() : null
|
|
110
110
|
|
|
111
111
|
// Add request tracking interceptors
|
|
112
112
|
if (tracker) {
|
|
@@ -131,26 +131,25 @@ export const createHttpClient = (options: HttpClientOptions = {}): HttpClient =>
|
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
// Add error logging interceptor
|
|
134
|
-
if (
|
|
134
|
+
if (config.enableErrorLogging !== false) {
|
|
135
135
|
client.interceptors.response.use((response) => response, createErrorLoggerInterceptor(config.logger))
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
// Add retry interceptor
|
|
139
|
-
if (config.retries &&
|
|
139
|
+
if (config.retries && config.retries > 0) {
|
|
140
140
|
// Add the client instance to configs for retry handling
|
|
141
141
|
client.interceptors.request.use((reqConfig) => {
|
|
142
|
-
|
|
143
|
-
return reqConfig
|
|
142
|
+
return Object.assign(reqConfig, { _axios: client })
|
|
144
143
|
})
|
|
145
144
|
|
|
146
145
|
client.interceptors.response.use(
|
|
147
146
|
(response) => response,
|
|
148
|
-
createRetryInterceptor(config.retries, config.retryDelay, config.shouldRetry)
|
|
147
|
+
createRetryInterceptor(config.retries, config.retryDelay, config.shouldRetry),
|
|
149
148
|
)
|
|
150
149
|
}
|
|
151
150
|
|
|
152
151
|
// Add Laravel-style headers if not disabled
|
|
153
|
-
if (
|
|
152
|
+
if (config.laravel !== false) {
|
|
154
153
|
client.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
|
|
155
154
|
}
|
|
156
155
|
|
package/src/interceptors.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Common interceptors for HTTP clients
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
5
|
+
import axios, { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
6
6
|
import { RequestTracker } from './request-tracker.js'
|
|
7
7
|
|
|
8
8
|
/**
|
|
@@ -64,7 +64,7 @@ export const createTrackingInterceptors = (tracker: RequestTracker): TrackingInt
|
|
|
64
64
|
export const createCSRFInterceptor =
|
|
65
65
|
(tokenSource: TokenSource) =>
|
|
66
66
|
(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
|
|
67
|
-
const token = 'function'
|
|
67
|
+
const token = typeof tokenSource === 'function' ? tokenSource() : tokenSource
|
|
68
68
|
|
|
69
69
|
if (token) {
|
|
70
70
|
config.headers = config.headers ?? {}
|
|
@@ -83,7 +83,7 @@ export const createCSRFInterceptor =
|
|
|
83
83
|
export const createAuthInterceptor =
|
|
84
84
|
(tokenSource: TokenSource, type: string = 'Bearer') =>
|
|
85
85
|
(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
|
|
86
|
-
const token = 'function'
|
|
86
|
+
const token = typeof tokenSource === 'function' ? tokenSource() : tokenSource
|
|
87
87
|
|
|
88
88
|
if (token) {
|
|
89
89
|
config.headers = config.headers ?? {}
|
|
@@ -130,6 +130,7 @@ export const createTimeoutInterceptor =
|
|
|
130
130
|
*/
|
|
131
131
|
interface RetryConfig extends InternalAxiosRequestConfig {
|
|
132
132
|
__retryCount?: number
|
|
133
|
+
_axios?: AxiosInstance
|
|
133
134
|
}
|
|
134
135
|
|
|
135
136
|
/**
|
|
@@ -142,9 +143,9 @@ interface RetryConfig extends InternalAxiosRequestConfig {
|
|
|
142
143
|
export const createRetryInterceptor = (
|
|
143
144
|
retries: number = 3,
|
|
144
145
|
delay: number = 1000,
|
|
145
|
-
shouldRetry: ShouldRetryFunction = () => true
|
|
146
|
+
shouldRetry: ShouldRetryFunction = () => true,
|
|
146
147
|
) => {
|
|
147
|
-
return async (error: AxiosError): Promise<
|
|
148
|
+
return async (error: AxiosError): Promise<AxiosResponse> => {
|
|
148
149
|
const config = error.config as RetryConfig | undefined
|
|
149
150
|
|
|
150
151
|
if (!config || (config.__retryCount ?? 0) >= retries) {
|
|
@@ -161,7 +162,7 @@ export const createRetryInterceptor = (
|
|
|
161
162
|
|
|
162
163
|
// Try to use the instance from the error, otherwise fall back to axios
|
|
163
164
|
/* c8 ignore next */
|
|
164
|
-
const axiosInstance =
|
|
165
|
+
const axiosInstance: AxiosInstance = config._axios ?? axios
|
|
165
166
|
return axiosInstance.request(config)
|
|
166
167
|
}
|
|
167
168
|
}
|