@zairakai/js-http-client 1.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 +139 -0
- package/dist/chunk-SSH6WKEL.js +74 -0
- package/dist/index.cjs +244 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.js +139 -0
- package/dist/interceptors-Pv-t9Hbf.d.cts +93 -0
- package/dist/interceptors-Pv-t9Hbf.d.ts +93 -0
- package/dist/interceptors.cjs +113 -0
- package/dist/interceptors.d.cts +2 -0
- package/dist/interceptors.d.ts +2 -0
- package/dist/interceptors.js +16 -0
- package/package.json +107 -0
- package/src/client.ts +207 -0
- package/src/index.ts +31 -0
- package/src/interceptors.ts +167 -0
- package/src/request-tracker.ts +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# @zairakai/js-http-client
|
|
2
|
+
|
|
3
|
+
[![Main][pipeline-main-badge]][pipeline-main-link]
|
|
4
|
+
[![Develop][pipeline-develop-badge]][pipeline-develop-link]
|
|
5
|
+
[![Coverage][coverage-badge]][coverage-link]
|
|
6
|
+
|
|
7
|
+
[![npm][npm-badge]][npm-link]
|
|
8
|
+
[![GitLab Release][gitlab-release-badge]][gitlab-release]
|
|
9
|
+
[![License][license-badge]][license]
|
|
10
|
+
|
|
11
|
+
[![Node.js][node-badge]][node]
|
|
12
|
+
[![ESLint][eslint-badge]][eslint]
|
|
13
|
+
[![Prettier][prettier-badge]][prettier]
|
|
14
|
+
|
|
15
|
+
Axios-based HTTP client with request tracking, interceptor management, and Laravel CSRF support.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
- **Three factory functions** — `createHttpClient`, `createApiClient`, `createLaravelClient`
|
|
22
|
+
- **Request tracking** — reactive `isLoading` and `requestCount` per client instance
|
|
23
|
+
- **Built-in interceptors** — CSRF token, auth bearer, retry with backoff, error logging, timeout
|
|
24
|
+
- **Laravel ready** — automatic `X-Requested-With` and CSRF headers
|
|
25
|
+
- **TypeScript first** — full type exports including `AxiosInstance`, `AxiosResponse`, `HttpMethod`
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
npm install @zairakai/js-http-client
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Usage
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { createHttpClient, createLaravelClient, createApiClient } from '@zairakai/js-http-client'
|
|
41
|
+
|
|
42
|
+
// Generic API client
|
|
43
|
+
const api = createHttpClient({
|
|
44
|
+
baseURL: 'https://api.example.com',
|
|
45
|
+
timeout: 5000,
|
|
46
|
+
authToken: () => localStorage.getItem('token'),
|
|
47
|
+
retries: 3,
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
const { data } = await api.get<User>('/users/1')
|
|
51
|
+
|
|
52
|
+
// Laravel full-stack client (CSRF + credentials + Laravel headers)
|
|
53
|
+
const laravelApi = createLaravelClient({ baseURL: '/api' })
|
|
54
|
+
await laravelApi.post('/users', { name: 'Alice' })
|
|
55
|
+
|
|
56
|
+
// Pure API client (no Laravel headers)
|
|
57
|
+
const publicApi = createApiClient({ baseURL: 'https://public.api.com' })
|
|
58
|
+
|
|
59
|
+
// Request tracking
|
|
60
|
+
console.log(api.isLoading) // true while requests are pending
|
|
61
|
+
console.log(api.requestCount) // number of active requests
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## API
|
|
67
|
+
|
|
68
|
+
### Factory functions
|
|
69
|
+
|
|
70
|
+
| Function | Description |
|
|
71
|
+
| - | - |
|
|
72
|
+
| `createHttpClient(options?)` | Base factory. Full control over all options. |
|
|
73
|
+
| `createLaravelClient(options?)` | Adds `withCredentials`, `X-Requested-With`, and CSRF support. |
|
|
74
|
+
| `createApiClient(options?)` | Clean JSON API client without Laravel-specific headers. |
|
|
75
|
+
|
|
76
|
+
### Options
|
|
77
|
+
|
|
78
|
+
| Option | Type | Default | Description |
|
|
79
|
+
| - | - | - | - |
|
|
80
|
+
| `baseURL` | `string` | `''` | Base URL for all requests |
|
|
81
|
+
| `timeout` | `number` | `10000` | Request timeout in ms |
|
|
82
|
+
| `authToken` | `string \| (() => string)` | — | Bearer token or token factory |
|
|
83
|
+
| `csrfToken` | `string \| (() => string)` | — | CSRF token or factory |
|
|
84
|
+
| `retries` | `number` | `0` | Number of retry attempts on failure |
|
|
85
|
+
| `retryDelay` | `number` | `1000` | Delay between retries in ms |
|
|
86
|
+
| `trackRequests` | `boolean` | `true` | Enable `isLoading` / `requestCount` |
|
|
87
|
+
| `logger` | `Logger` | — | Custom error logger |
|
|
88
|
+
|
|
89
|
+
### Standalone interceptors
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { createRetryInterceptor, createAuthInterceptor, createCSRFInterceptor } from '@zairakai/js-http-client'
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
---
|
|
96
|
+
|
|
97
|
+
## Development
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
make quality # eslint + prettier + knip + markdownlint
|
|
101
|
+
make quality-fix # auto-fix all fixable issues
|
|
102
|
+
make test # vitest
|
|
103
|
+
make test-all # vitest + bats + coverage
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Getting Help
|
|
109
|
+
|
|
110
|
+
[![License][license-badge]][license]
|
|
111
|
+
[![Security Policy][security-badge]][security]
|
|
112
|
+
[![Issues][issues-badge]][issues]
|
|
113
|
+
|
|
114
|
+
**Made with ❤️ by [Zairakai][ecosystem]**
|
|
115
|
+
|
|
116
|
+
<!-- Reference Links -->
|
|
117
|
+
[pipeline-main-badge]: https://gitlab.com/zairakai/npm-packages/js-http-client/badges/main/pipeline.svg?ignore_skipped=true&key_text=Main
|
|
118
|
+
[pipeline-main-link]: https://gitlab.com/zairakai/npm-packages/js-http-client/-/commits/main
|
|
119
|
+
[pipeline-develop-badge]: https://gitlab.com/zairakai/npm-packages/js-http-client/badges/develop/pipeline.svg?ignore_skipped=true&key_text=Develop
|
|
120
|
+
[pipeline-develop-link]: https://gitlab.com/zairakai/npm-packages/js-http-client/-/commits/develop
|
|
121
|
+
[coverage-badge]: https://gitlab.com/zairakai/npm-packages/js-http-client/badges/main/coverage.svg
|
|
122
|
+
[coverage-link]: https://gitlab.com/zairakai/npm-packages/js-http-client/-/pipelines?ref=main
|
|
123
|
+
[npm-badge]: https://img.shields.io/npm/v/@zairakai/js-http-client
|
|
124
|
+
[npm-link]: https://www.npmjs.com/package/@zairakai/js-http-client
|
|
125
|
+
[gitlab-release-badge]: https://img.shields.io/gitlab/v/release/zairakai/npm-packages/js-http-client?logo=gitlab
|
|
126
|
+
[gitlab-release]: https://gitlab.com/zairakai/npm-packages/js-http-client/-/releases
|
|
127
|
+
[license-badge]: https://img.shields.io/badge/license-MIT-blue.svg
|
|
128
|
+
[license]: ./LICENSE
|
|
129
|
+
[security-badge]: https://img.shields.io/badge/security-scanned-green.svg
|
|
130
|
+
[security]: ./SECURITY.md
|
|
131
|
+
[issues-badge]: https://img.shields.io/gitlab/issues/open-raw/zairakai%2Fnpm-packages%2Fhttp-client?logo=gitlab&label=Issues
|
|
132
|
+
[issues]: https://gitlab.com/zairakai/npm-packages/js-http-client/-/issues
|
|
133
|
+
[node-badge]: https://img.shields.io/badge/node.js-%3E%3D22-green.svg?logo=node.js
|
|
134
|
+
[node]: https://nodejs.org
|
|
135
|
+
[eslint-badge]: https://img.shields.io/badge/code%20style-eslint-4B32C3.svg?logo=eslint
|
|
136
|
+
[eslint]: https://eslint.org
|
|
137
|
+
[prettier-badge]: https://img.shields.io/badge/formatter-prettier-F7B93E.svg?logo=prettier
|
|
138
|
+
[prettier]: https://prettier.io
|
|
139
|
+
[ecosystem]: https://gitlab.com/zairakai
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/interceptors.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
var createTrackingInterceptors = (tracker) => ({
|
|
4
|
+
request: (config) => {
|
|
5
|
+
tracker.increment();
|
|
6
|
+
return config;
|
|
7
|
+
},
|
|
8
|
+
requestError: (error) => {
|
|
9
|
+
return Promise.reject(error);
|
|
10
|
+
},
|
|
11
|
+
response: (response) => {
|
|
12
|
+
tracker.decrement();
|
|
13
|
+
return response;
|
|
14
|
+
},
|
|
15
|
+
responseError: (error) => {
|
|
16
|
+
tracker.decrement();
|
|
17
|
+
return Promise.reject(error);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
21
|
+
const token = "function" === typeof tokenSource ? tokenSource() : tokenSource;
|
|
22
|
+
if (token) {
|
|
23
|
+
config.headers = config.headers ?? {};
|
|
24
|
+
config.headers["X-CSRF-TOKEN"] = token;
|
|
25
|
+
}
|
|
26
|
+
return config;
|
|
27
|
+
};
|
|
28
|
+
var createAuthInterceptor = (tokenSource, type = "Bearer") => (config) => {
|
|
29
|
+
const token = "function" === typeof tokenSource ? tokenSource() : tokenSource;
|
|
30
|
+
if (token) {
|
|
31
|
+
config.headers = config.headers ?? {};
|
|
32
|
+
config.headers["Authorization"] = `${type} ${token}`;
|
|
33
|
+
}
|
|
34
|
+
return config;
|
|
35
|
+
};
|
|
36
|
+
var createErrorLoggerInterceptor = (logger = console.error) => (error) => {
|
|
37
|
+
const logData = {
|
|
38
|
+
message: error.message,
|
|
39
|
+
status: error.response?.status,
|
|
40
|
+
statusText: error.response?.statusText,
|
|
41
|
+
url: error.config?.url,
|
|
42
|
+
method: error.config?.method?.toUpperCase()
|
|
43
|
+
};
|
|
44
|
+
logger("HTTP Error:", logData);
|
|
45
|
+
return Promise.reject(error);
|
|
46
|
+
};
|
|
47
|
+
var createTimeoutInterceptor = (timeout) => (config) => {
|
|
48
|
+
config.timeout = timeout;
|
|
49
|
+
return config;
|
|
50
|
+
};
|
|
51
|
+
var createRetryInterceptor = (retries = 3, delay = 1e3, shouldRetry = () => true) => {
|
|
52
|
+
return async (error) => {
|
|
53
|
+
const config = error.config;
|
|
54
|
+
if (!config || (config.__retryCount ?? 0) >= retries) {
|
|
55
|
+
return Promise.reject(error);
|
|
56
|
+
}
|
|
57
|
+
if (!shouldRetry(error)) {
|
|
58
|
+
return Promise.reject(error);
|
|
59
|
+
}
|
|
60
|
+
config.__retryCount = (config.__retryCount ?? 0) + 1;
|
|
61
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
62
|
+
const axiosInstance = config._axios ?? axios;
|
|
63
|
+
return axiosInstance.request(config);
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export {
|
|
68
|
+
createTrackingInterceptors,
|
|
69
|
+
createCSRFInterceptor,
|
|
70
|
+
createAuthInterceptor,
|
|
71
|
+
createErrorLoggerInterceptor,
|
|
72
|
+
createTimeoutInterceptor,
|
|
73
|
+
createRetryInterceptor
|
|
74
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
axios: () => import_axios3.default,
|
|
34
|
+
createApiClient: () => createApiClient,
|
|
35
|
+
createAuthInterceptor: () => createAuthInterceptor,
|
|
36
|
+
createCSRFInterceptor: () => createCSRFInterceptor,
|
|
37
|
+
createErrorLoggerInterceptor: () => createErrorLoggerInterceptor,
|
|
38
|
+
createHttpClient: () => createHttpClient,
|
|
39
|
+
createLaravelClient: () => createLaravelClient,
|
|
40
|
+
createRequestTracker: () => createRequestTracker,
|
|
41
|
+
createRetryInterceptor: () => createRetryInterceptor,
|
|
42
|
+
createTimeoutInterceptor: () => createTimeoutInterceptor,
|
|
43
|
+
createTrackingInterceptors: () => createTrackingInterceptors,
|
|
44
|
+
globalRequestTracker: () => globalRequestTracker
|
|
45
|
+
});
|
|
46
|
+
module.exports = __toCommonJS(src_exports);
|
|
47
|
+
|
|
48
|
+
// src/client.ts
|
|
49
|
+
var import_axios2 = __toESM(require("axios"), 1);
|
|
50
|
+
|
|
51
|
+
// src/interceptors.ts
|
|
52
|
+
var import_axios = __toESM(require("axios"), 1);
|
|
53
|
+
var createTrackingInterceptors = (tracker) => ({
|
|
54
|
+
request: (config) => {
|
|
55
|
+
tracker.increment();
|
|
56
|
+
return config;
|
|
57
|
+
},
|
|
58
|
+
requestError: (error) => {
|
|
59
|
+
return Promise.reject(error);
|
|
60
|
+
},
|
|
61
|
+
response: (response) => {
|
|
62
|
+
tracker.decrement();
|
|
63
|
+
return response;
|
|
64
|
+
},
|
|
65
|
+
responseError: (error) => {
|
|
66
|
+
tracker.decrement();
|
|
67
|
+
return Promise.reject(error);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
var createCSRFInterceptor = (tokenSource) => (config) => {
|
|
71
|
+
const token = "function" === typeof tokenSource ? tokenSource() : tokenSource;
|
|
72
|
+
if (token) {
|
|
73
|
+
config.headers = config.headers ?? {};
|
|
74
|
+
config.headers["X-CSRF-TOKEN"] = token;
|
|
75
|
+
}
|
|
76
|
+
return config;
|
|
77
|
+
};
|
|
78
|
+
var createAuthInterceptor = (tokenSource, type = "Bearer") => (config) => {
|
|
79
|
+
const token = "function" === typeof tokenSource ? tokenSource() : tokenSource;
|
|
80
|
+
if (token) {
|
|
81
|
+
config.headers = config.headers ?? {};
|
|
82
|
+
config.headers["Authorization"] = `${type} ${token}`;
|
|
83
|
+
}
|
|
84
|
+
return config;
|
|
85
|
+
};
|
|
86
|
+
var createErrorLoggerInterceptor = (logger = console.error) => (error) => {
|
|
87
|
+
const logData = {
|
|
88
|
+
message: error.message,
|
|
89
|
+
status: error.response?.status,
|
|
90
|
+
statusText: error.response?.statusText,
|
|
91
|
+
url: error.config?.url,
|
|
92
|
+
method: error.config?.method?.toUpperCase()
|
|
93
|
+
};
|
|
94
|
+
logger("HTTP Error:", logData);
|
|
95
|
+
return Promise.reject(error);
|
|
96
|
+
};
|
|
97
|
+
var createTimeoutInterceptor = (timeout) => (config) => {
|
|
98
|
+
config.timeout = timeout;
|
|
99
|
+
return config;
|
|
100
|
+
};
|
|
101
|
+
var createRetryInterceptor = (retries = 3, delay = 1e3, shouldRetry = () => true) => {
|
|
102
|
+
return async (error) => {
|
|
103
|
+
const config = error.config;
|
|
104
|
+
if (!config || (config.__retryCount ?? 0) >= retries) {
|
|
105
|
+
return Promise.reject(error);
|
|
106
|
+
}
|
|
107
|
+
if (!shouldRetry(error)) {
|
|
108
|
+
return Promise.reject(error);
|
|
109
|
+
}
|
|
110
|
+
config.__retryCount = (config.__retryCount ?? 0) + 1;
|
|
111
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
112
|
+
const axiosInstance = config._axios ?? import_axios.default;
|
|
113
|
+
return axiosInstance.request(config);
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
// src/request-tracker.ts
|
|
118
|
+
var createRequestTracker = () => {
|
|
119
|
+
let nbrInternal = 0;
|
|
120
|
+
return {
|
|
121
|
+
get count() {
|
|
122
|
+
return nbrInternal;
|
|
123
|
+
},
|
|
124
|
+
set count(val) {
|
|
125
|
+
nbrInternal = Math.max(0, val);
|
|
126
|
+
},
|
|
127
|
+
get isActive() {
|
|
128
|
+
return 0 < nbrInternal;
|
|
129
|
+
},
|
|
130
|
+
increment() {
|
|
131
|
+
nbrInternal++;
|
|
132
|
+
},
|
|
133
|
+
decrement() {
|
|
134
|
+
nbrInternal = Math.max(0, nbrInternal - 1);
|
|
135
|
+
},
|
|
136
|
+
reset() {
|
|
137
|
+
nbrInternal = 0;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
};
|
|
141
|
+
var globalRequestTracker = createRequestTracker();
|
|
142
|
+
|
|
143
|
+
// src/client.ts
|
|
144
|
+
var DEFAULT_CONFIG = {
|
|
145
|
+
baseURL: "",
|
|
146
|
+
timeout: 1e4,
|
|
147
|
+
withCredentials: false,
|
|
148
|
+
headers: {
|
|
149
|
+
"Content-Type": "application/json",
|
|
150
|
+
Accept: "application/json"
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var createHttpClient = (options = {}) => {
|
|
154
|
+
const config = { ...DEFAULT_CONFIG, ...options };
|
|
155
|
+
const client = import_axios2.default.create({
|
|
156
|
+
baseURL: config.baseURL,
|
|
157
|
+
timeout: config.timeout,
|
|
158
|
+
withCredentials: config.withCredentials,
|
|
159
|
+
headers: { ...DEFAULT_CONFIG.headers, ...config.headers }
|
|
160
|
+
});
|
|
161
|
+
const tracker = false !== config.trackRequests ? createRequestTracker() : null;
|
|
162
|
+
if (tracker) {
|
|
163
|
+
const trackingInterceptors = createTrackingInterceptors(tracker);
|
|
164
|
+
client.interceptors.request.use(trackingInterceptors.request, trackingInterceptors.requestError);
|
|
165
|
+
client.interceptors.response.use(trackingInterceptors.response, trackingInterceptors.responseError);
|
|
166
|
+
}
|
|
167
|
+
if (config.csrfToken) {
|
|
168
|
+
client.interceptors.request.use(createCSRFInterceptor(config.csrfToken));
|
|
169
|
+
}
|
|
170
|
+
if (config.authToken) {
|
|
171
|
+
client.interceptors.request.use(createAuthInterceptor(config.authToken, config.authType));
|
|
172
|
+
}
|
|
173
|
+
if (config.timeout !== DEFAULT_CONFIG.timeout) {
|
|
174
|
+
client.interceptors.request.use(createTimeoutInterceptor(config.timeout));
|
|
175
|
+
}
|
|
176
|
+
if (false !== config.enableErrorLogging) {
|
|
177
|
+
client.interceptors.response.use((response) => response, createErrorLoggerInterceptor(config.logger));
|
|
178
|
+
}
|
|
179
|
+
if (config.retries && 0 < config.retries) {
|
|
180
|
+
client.interceptors.request.use((reqConfig) => {
|
|
181
|
+
;
|
|
182
|
+
reqConfig._axios = client;
|
|
183
|
+
return reqConfig;
|
|
184
|
+
});
|
|
185
|
+
client.interceptors.response.use(
|
|
186
|
+
(response) => response,
|
|
187
|
+
createRetryInterceptor(config.retries, config.retryDelay, config.shouldRetry)
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (false !== config.laravel) {
|
|
191
|
+
client.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest";
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
client,
|
|
195
|
+
tracker,
|
|
196
|
+
// Convenience methods
|
|
197
|
+
get: (url, config2) => client.get(url, config2),
|
|
198
|
+
post: (url, data, config2) => client.post(url, data, config2),
|
|
199
|
+
put: (url, data, config2) => client.put(url, data, config2),
|
|
200
|
+
patch: (url, data, config2) => client.patch(url, data, config2),
|
|
201
|
+
delete: (url, config2) => client.delete(url, config2),
|
|
202
|
+
// Request status
|
|
203
|
+
get isLoading() {
|
|
204
|
+
return tracker ? tracker.isActive : false;
|
|
205
|
+
},
|
|
206
|
+
get requestCount() {
|
|
207
|
+
return tracker ? tracker.count : 0;
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
var createLaravelClient = (options = {}) => {
|
|
212
|
+
return createHttpClient({
|
|
213
|
+
withCredentials: true,
|
|
214
|
+
headers: {
|
|
215
|
+
"X-Requested-With": "XMLHttpRequest"
|
|
216
|
+
},
|
|
217
|
+
...options
|
|
218
|
+
});
|
|
219
|
+
};
|
|
220
|
+
var createApiClient = (options = {}) => {
|
|
221
|
+
return createHttpClient({
|
|
222
|
+
withCredentials: false,
|
|
223
|
+
laravel: false,
|
|
224
|
+
...options
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
// src/index.ts
|
|
229
|
+
var import_axios3 = __toESM(require("axios"), 1);
|
|
230
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
231
|
+
0 && (module.exports = {
|
|
232
|
+
axios,
|
|
233
|
+
createApiClient,
|
|
234
|
+
createAuthInterceptor,
|
|
235
|
+
createCSRFInterceptor,
|
|
236
|
+
createErrorLoggerInterceptor,
|
|
237
|
+
createHttpClient,
|
|
238
|
+
createLaravelClient,
|
|
239
|
+
createRequestTracker,
|
|
240
|
+
createRetryInterceptor,
|
|
241
|
+
createTimeoutInterceptor,
|
|
242
|
+
createTrackingInterceptors,
|
|
243
|
+
globalRequestTracker
|
|
244
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
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-Pv-t9Hbf.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-Pv-t9Hbf.cjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HTTP client factory with configurable options
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* HTTP client configuration options
|
|
12
|
+
*/
|
|
13
|
+
interface HttpClientOptions {
|
|
14
|
+
/** Base URL for requests */
|
|
15
|
+
baseURL?: string;
|
|
16
|
+
/** Default headers */
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
/** Include credentials */
|
|
19
|
+
withCredentials?: boolean;
|
|
20
|
+
/** Request timeout in milliseconds */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/** Enable request tracking */
|
|
23
|
+
trackRequests?: boolean;
|
|
24
|
+
/** CSRF token or getter function */
|
|
25
|
+
csrfToken?: TokenSource;
|
|
26
|
+
/** Auth token or getter function */
|
|
27
|
+
authToken?: TokenSource;
|
|
28
|
+
/** Auth token type (Bearer, Token, etc.) */
|
|
29
|
+
authType?: string;
|
|
30
|
+
/** Enable error logging */
|
|
31
|
+
enableErrorLogging?: boolean;
|
|
32
|
+
/** Custom logger function */
|
|
33
|
+
logger?: Logger;
|
|
34
|
+
/** Number of retries for failed requests */
|
|
35
|
+
retries?: number;
|
|
36
|
+
/** Delay between retries in milliseconds */
|
|
37
|
+
retryDelay?: number;
|
|
38
|
+
/** Custom retry logic */
|
|
39
|
+
shouldRetry?: ShouldRetryFunction;
|
|
40
|
+
/** Enable Laravel-specific features */
|
|
41
|
+
laravel?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* HTTP client instance with convenience methods
|
|
45
|
+
*/
|
|
46
|
+
interface HttpClient {
|
|
47
|
+
/** Axios instance */
|
|
48
|
+
readonly client: AxiosInstance;
|
|
49
|
+
/** Request tracker instance (null if tracking disabled) */
|
|
50
|
+
readonly tracker: RequestTracker | null;
|
|
51
|
+
/** Whether there are active requests */
|
|
52
|
+
readonly isLoading: boolean;
|
|
53
|
+
/** Current number of active requests */
|
|
54
|
+
readonly requestCount: number;
|
|
55
|
+
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
56
|
+
post<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
57
|
+
put<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
58
|
+
patch<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
59
|
+
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Creates a configured HTTP client with interceptors
|
|
63
|
+
* @param options - Configuration options
|
|
64
|
+
* @returns Configured axios instance with tracker
|
|
65
|
+
*/
|
|
66
|
+
declare const createHttpClient: (options?: HttpClientOptions) => HttpClient;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a Laravel-compatible HTTP client
|
|
69
|
+
* @param options - Configuration options
|
|
70
|
+
* @returns Configured HTTP client
|
|
71
|
+
*/
|
|
72
|
+
declare const createLaravelClient: (options?: HttpClientOptions) => HttpClient;
|
|
73
|
+
/**
|
|
74
|
+
* Creates an API client (typically for external APIs)
|
|
75
|
+
* @param options - Configuration options
|
|
76
|
+
* @returns Configured HTTP client
|
|
77
|
+
*/
|
|
78
|
+
declare const createApiClient: (options?: HttpClientOptions) => HttpClient;
|
|
79
|
+
|
|
80
|
+
export { type HttpClient, type HttpClientOptions, Logger, RequestTracker, ShouldRetryFunction, TokenSource, createApiClient, createHttpClient, createLaravelClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
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-Pv-t9Hbf.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-Pv-t9Hbf.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* HTTP client factory with configurable options
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* HTTP client configuration options
|
|
12
|
+
*/
|
|
13
|
+
interface HttpClientOptions {
|
|
14
|
+
/** Base URL for requests */
|
|
15
|
+
baseURL?: string;
|
|
16
|
+
/** Default headers */
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
/** Include credentials */
|
|
19
|
+
withCredentials?: boolean;
|
|
20
|
+
/** Request timeout in milliseconds */
|
|
21
|
+
timeout?: number;
|
|
22
|
+
/** Enable request tracking */
|
|
23
|
+
trackRequests?: boolean;
|
|
24
|
+
/** CSRF token or getter function */
|
|
25
|
+
csrfToken?: TokenSource;
|
|
26
|
+
/** Auth token or getter function */
|
|
27
|
+
authToken?: TokenSource;
|
|
28
|
+
/** Auth token type (Bearer, Token, etc.) */
|
|
29
|
+
authType?: string;
|
|
30
|
+
/** Enable error logging */
|
|
31
|
+
enableErrorLogging?: boolean;
|
|
32
|
+
/** Custom logger function */
|
|
33
|
+
logger?: Logger;
|
|
34
|
+
/** Number of retries for failed requests */
|
|
35
|
+
retries?: number;
|
|
36
|
+
/** Delay between retries in milliseconds */
|
|
37
|
+
retryDelay?: number;
|
|
38
|
+
/** Custom retry logic */
|
|
39
|
+
shouldRetry?: ShouldRetryFunction;
|
|
40
|
+
/** Enable Laravel-specific features */
|
|
41
|
+
laravel?: boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* HTTP client instance with convenience methods
|
|
45
|
+
*/
|
|
46
|
+
interface HttpClient {
|
|
47
|
+
/** Axios instance */
|
|
48
|
+
readonly client: AxiosInstance;
|
|
49
|
+
/** Request tracker instance (null if tracking disabled) */
|
|
50
|
+
readonly tracker: RequestTracker | null;
|
|
51
|
+
/** Whether there are active requests */
|
|
52
|
+
readonly isLoading: boolean;
|
|
53
|
+
/** Current number of active requests */
|
|
54
|
+
readonly requestCount: number;
|
|
55
|
+
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
56
|
+
post<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
57
|
+
put<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
58
|
+
patch<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
59
|
+
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Creates a configured HTTP client with interceptors
|
|
63
|
+
* @param options - Configuration options
|
|
64
|
+
* @returns Configured axios instance with tracker
|
|
65
|
+
*/
|
|
66
|
+
declare const createHttpClient: (options?: HttpClientOptions) => HttpClient;
|
|
67
|
+
/**
|
|
68
|
+
* Creates a Laravel-compatible HTTP client
|
|
69
|
+
* @param options - Configuration options
|
|
70
|
+
* @returns Configured HTTP client
|
|
71
|
+
*/
|
|
72
|
+
declare const createLaravelClient: (options?: HttpClientOptions) => HttpClient;
|
|
73
|
+
/**
|
|
74
|
+
* Creates an API client (typically for external APIs)
|
|
75
|
+
* @param options - Configuration options
|
|
76
|
+
* @returns Configured HTTP client
|
|
77
|
+
*/
|
|
78
|
+
declare const createApiClient: (options?: HttpClientOptions) => HttpClient;
|
|
79
|
+
|
|
80
|
+
export { type HttpClient, type HttpClientOptions, Logger, RequestTracker, ShouldRetryFunction, TokenSource, createApiClient, createHttpClient, createLaravelClient };
|