@molecule/app-http-axios 1.0.0 → 1.0.1
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 +393 -0
- package/package.json +4 -3
package/README.md
ADDED
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:50:59.135Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/app-http-axios
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
Axios HTTP client provider for `@molecule/app-http`.
|
|
16
|
+
|
|
17
|
+
This package provides an Axios-based implementation of the molecule HttpClient interface,
|
|
18
|
+
allowing you to use molecule's HTTP abstractions with Axios.
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { createAxiosClient } from '@molecule/app-http-axios'
|
|
24
|
+
import { setClient } from '@molecule/app-http'
|
|
25
|
+
|
|
26
|
+
const client = createAxiosClient({
|
|
27
|
+
baseURL: 'https://api.example.com',
|
|
28
|
+
timeout: 10000,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
setClient(client)
|
|
32
|
+
|
|
33
|
+
// Now use via `@molecule/app-http`
|
|
34
|
+
import { get, post } from '@molecule/app-http'
|
|
35
|
+
const users = await get('/users')
|
|
36
|
+
const newUser = await post('/users', { name: 'John' })
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Type
|
|
40
|
+
|
|
41
|
+
`provider`
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @molecule/app-http-axios @molecule/app-http axios
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## API
|
|
50
|
+
|
|
51
|
+
### Interfaces
|
|
52
|
+
|
|
53
|
+
#### `AxiosHttpClientConfig`
|
|
54
|
+
|
|
55
|
+
Axios-specific configuration.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
interface AxiosHttpClientConfig {
|
|
59
|
+
/**
|
|
60
|
+
* Base URL for all requests.
|
|
61
|
+
*/
|
|
62
|
+
baseURL?: string
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Default headers for all requests.
|
|
66
|
+
*/
|
|
67
|
+
defaultHeaders?: Record<string, string>
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Default timeout in milliseconds.
|
|
71
|
+
*/
|
|
72
|
+
timeout?: number
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Whether to include credentials by default.
|
|
76
|
+
*/
|
|
77
|
+
withCredentials?: boolean
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Existing Axios instance to wrap (optional).
|
|
81
|
+
*/
|
|
82
|
+
instance?: AxiosInstance
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Additional Axios-specific config.
|
|
86
|
+
*/
|
|
87
|
+
axiosConfig?: AxiosRequestConfig
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
#### `FullRequestConfig`
|
|
92
|
+
|
|
93
|
+
Full request configuration including method and URL.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
interface FullRequestConfig extends RequestConfig {
|
|
97
|
+
/**
|
|
98
|
+
* HTTP method.
|
|
99
|
+
*/
|
|
100
|
+
method: HttpMethod
|
|
101
|
+
/**
|
|
102
|
+
* Request URL (can be relative or absolute).
|
|
103
|
+
*/
|
|
104
|
+
url: string
|
|
105
|
+
/**
|
|
106
|
+
* Request body data.
|
|
107
|
+
*/
|
|
108
|
+
data?: unknown
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
#### `HttpClient`
|
|
113
|
+
|
|
114
|
+
HTTP client interface.
|
|
115
|
+
|
|
116
|
+
All HTTP providers must implement this interface.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
interface HttpClient {
|
|
120
|
+
/**
|
|
121
|
+
* Base URL for all requests.
|
|
122
|
+
*/
|
|
123
|
+
baseURL: string
|
|
124
|
+
/**
|
|
125
|
+
* Default headers for all requests.
|
|
126
|
+
*/
|
|
127
|
+
defaultHeaders: Record<string, string>
|
|
128
|
+
/**
|
|
129
|
+
* Makes a generic HTTP request.
|
|
130
|
+
*/
|
|
131
|
+
request<T = unknown>(config: FullRequestConfig): Promise<HttpResponse<T>>
|
|
132
|
+
/**
|
|
133
|
+
* Makes a GET request.
|
|
134
|
+
*/
|
|
135
|
+
get<T = unknown>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
136
|
+
/**
|
|
137
|
+
* Makes a POST request.
|
|
138
|
+
*/
|
|
139
|
+
post<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
140
|
+
/**
|
|
141
|
+
* Makes a PUT request.
|
|
142
|
+
*/
|
|
143
|
+
put<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
144
|
+
/**
|
|
145
|
+
* Makes a PATCH request.
|
|
146
|
+
*/
|
|
147
|
+
patch<T = unknown>(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
148
|
+
/**
|
|
149
|
+
* Makes a DELETE request.
|
|
150
|
+
*/
|
|
151
|
+
delete<T = unknown>(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
152
|
+
/**
|
|
153
|
+
* Adds a request interceptor.
|
|
154
|
+
* Returns a function to remove the interceptor.
|
|
155
|
+
*/
|
|
156
|
+
addRequestInterceptor(interceptor: RequestInterceptor): () => void
|
|
157
|
+
/**
|
|
158
|
+
* Adds a response interceptor.
|
|
159
|
+
* Returns a function to remove the interceptor.
|
|
160
|
+
*/
|
|
161
|
+
addResponseInterceptor(interceptor: ResponseInterceptor): () => void
|
|
162
|
+
/**
|
|
163
|
+
* Adds an error interceptor.
|
|
164
|
+
* Returns a function to remove the interceptor.
|
|
165
|
+
*/
|
|
166
|
+
addErrorInterceptor(interceptor: ErrorInterceptor): () => void
|
|
167
|
+
/**
|
|
168
|
+
* Sets the authorization token.
|
|
169
|
+
*/
|
|
170
|
+
setAuthToken(token: string | null): void
|
|
171
|
+
/**
|
|
172
|
+
* Returns the current authorization token, or `null` if not set.
|
|
173
|
+
*/
|
|
174
|
+
getAuthToken(): string | null
|
|
175
|
+
/**
|
|
176
|
+
* Registers a handler for authentication errors (401).
|
|
177
|
+
*
|
|
178
|
+
* @returns An unsubscribe function.
|
|
179
|
+
*/
|
|
180
|
+
onAuthError(handler: () => void): () => void
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### `HttpClientConfig`
|
|
185
|
+
|
|
186
|
+
HTTP client configuration.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
interface HttpClientConfig {
|
|
190
|
+
/**
|
|
191
|
+
* Base URL for all requests.
|
|
192
|
+
*/
|
|
193
|
+
baseURL?: string
|
|
194
|
+
/**
|
|
195
|
+
* Default headers for all requests.
|
|
196
|
+
*/
|
|
197
|
+
defaultHeaders?: Record<string, string>
|
|
198
|
+
/**
|
|
199
|
+
* Default timeout in milliseconds.
|
|
200
|
+
*/
|
|
201
|
+
timeout?: number
|
|
202
|
+
/**
|
|
203
|
+
* Whether to include credentials by default.
|
|
204
|
+
*/
|
|
205
|
+
withCredentials?: boolean
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### `HttpResponse`
|
|
210
|
+
|
|
211
|
+
Parsed HTTP response with status code, headers, and typed body data.
|
|
212
|
+
|
|
213
|
+
```typescript
|
|
214
|
+
interface HttpResponse<T = unknown> {
|
|
215
|
+
/**
|
|
216
|
+
* Response data.
|
|
217
|
+
*/
|
|
218
|
+
data: T
|
|
219
|
+
/**
|
|
220
|
+
* HTTP status code.
|
|
221
|
+
*/
|
|
222
|
+
status: number
|
|
223
|
+
/**
|
|
224
|
+
* HTTP status text.
|
|
225
|
+
*/
|
|
226
|
+
statusText: string
|
|
227
|
+
/**
|
|
228
|
+
* Response headers.
|
|
229
|
+
*/
|
|
230
|
+
headers: Record<string, string>
|
|
231
|
+
/**
|
|
232
|
+
* Original request config.
|
|
233
|
+
*/
|
|
234
|
+
config: FullRequestConfig
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
#### `RequestConfig`
|
|
239
|
+
|
|
240
|
+
HTTP request options (headers, query params, timeout, credentials, response type, abort signal).
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
interface RequestConfig {
|
|
244
|
+
/**
|
|
245
|
+
* Request headers.
|
|
246
|
+
*/
|
|
247
|
+
headers?: Record<string, string>
|
|
248
|
+
/**
|
|
249
|
+
* Query parameters.
|
|
250
|
+
*/
|
|
251
|
+
params?: Record<string, string | number | boolean | undefined>
|
|
252
|
+
/**
|
|
253
|
+
* Request timeout in milliseconds.
|
|
254
|
+
*/
|
|
255
|
+
timeout?: number
|
|
256
|
+
/**
|
|
257
|
+
* Whether to include credentials (cookies).
|
|
258
|
+
*/
|
|
259
|
+
withCredentials?: boolean
|
|
260
|
+
/**
|
|
261
|
+
* Response type.
|
|
262
|
+
*/
|
|
263
|
+
responseType?: 'json' | 'text' | 'blob' | 'arraybuffer'
|
|
264
|
+
/**
|
|
265
|
+
* Abort signal for cancellation.
|
|
266
|
+
*/
|
|
267
|
+
signal?: AbortSignal
|
|
268
|
+
/**
|
|
269
|
+
* Request body data.
|
|
270
|
+
*/
|
|
271
|
+
data?: unknown
|
|
272
|
+
/**
|
|
273
|
+
* Custom request options (implementation-specific).
|
|
274
|
+
*/
|
|
275
|
+
options?: Record<string, unknown>
|
|
276
|
+
}
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Types
|
|
280
|
+
|
|
281
|
+
#### `ErrorInterceptor`
|
|
282
|
+
|
|
283
|
+
Intercepts HTTP errors to transform, retry, or rethrow them.
|
|
284
|
+
Throwing from this interceptor propagates the error to the caller.
|
|
285
|
+
|
|
286
|
+
```typescript
|
|
287
|
+
type ErrorInterceptor = (error: HttpError) => HttpError | Promise<HttpError> | never
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
#### `HttpMethod`
|
|
291
|
+
|
|
292
|
+
HTTP request method.
|
|
293
|
+
|
|
294
|
+
```typescript
|
|
295
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
#### `RequestInterceptor`
|
|
299
|
+
|
|
300
|
+
Intercepts outgoing requests to modify headers, URL, or body
|
|
301
|
+
before the request is sent.
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
type RequestInterceptor = (
|
|
305
|
+
config: FullRequestConfig,
|
|
306
|
+
) => FullRequestConfig | Promise<FullRequestConfig>
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
#### `ResponseInterceptor`
|
|
310
|
+
|
|
311
|
+
Intercepts incoming responses to transform data, check status,
|
|
312
|
+
or perform side effects before the response reaches the caller.
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
type ResponseInterceptor<T = unknown> = (
|
|
316
|
+
response: HttpResponse<T>,
|
|
317
|
+
) => HttpResponse<T> | Promise<HttpResponse<T>>
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### Classes
|
|
321
|
+
|
|
322
|
+
#### `HttpError`
|
|
323
|
+
|
|
324
|
+
HTTP error with response details.
|
|
325
|
+
|
|
326
|
+
### Functions
|
|
327
|
+
|
|
328
|
+
#### `createAxiosClient(config)`
|
|
329
|
+
|
|
330
|
+
Creates an Axios-based HTTP client implementing the molecule `HttpClient` interface.
|
|
331
|
+
Supports request/response/error interceptors, auth token management, and 401 error handlers.
|
|
332
|
+
|
|
333
|
+
```typescript
|
|
334
|
+
function createAxiosClient(config?: AxiosHttpClientConfig): HttpClient
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
- `config` — Axios client configuration (base URL, headers, timeout, credentials, or an existing Axios instance).
|
|
338
|
+
|
|
339
|
+
**Returns:** An `HttpClient` backed by Axios with interceptor and auth token support.
|
|
340
|
+
|
|
341
|
+
### Constants
|
|
342
|
+
|
|
343
|
+
#### `provider`
|
|
344
|
+
|
|
345
|
+
Default Axios-based HTTP client created with default options (no base URL, 30s timeout).
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
const provider: HttpClient
|
|
349
|
+
```
|
|
350
|
+
|
|
351
|
+
## Core Interface
|
|
352
|
+
|
|
353
|
+
Implements `@molecule/app-http` interface.
|
|
354
|
+
|
|
355
|
+
## Bond Wiring
|
|
356
|
+
|
|
357
|
+
Setup function to register this provider with the core interface:
|
|
358
|
+
|
|
359
|
+
```typescript
|
|
360
|
+
import { setClient } from '@molecule/app-http'
|
|
361
|
+
import { provider } from '@molecule/app-http-axios'
|
|
362
|
+
|
|
363
|
+
export function setupHttpAxios(): void {
|
|
364
|
+
setClient(provider)
|
|
365
|
+
}
|
|
366
|
+
```
|
|
367
|
+
|
|
368
|
+
## Injection Notes
|
|
369
|
+
|
|
370
|
+
### Requirements
|
|
371
|
+
|
|
372
|
+
Peer dependencies:
|
|
373
|
+
|
|
374
|
+
- `@molecule/app-http` ^1.0.1
|
|
375
|
+
- `axios` ^1.6.0
|
|
376
|
+
|
|
377
|
+
### Runtime Dependencies
|
|
378
|
+
|
|
379
|
+
- `@molecule/app-http`
|
|
380
|
+
- `axios`
|
|
381
|
+
|
|
382
|
+
- **Call `setClient(...)` at startup, BEFORE any `get`/`post` from
|
|
383
|
+
`@molecule/app-http` runs.** The core's `getClient()` lazily bonds its own
|
|
384
|
+
fetch-based fallback on first use — requests issued before `setClient()`
|
|
385
|
+
silently bypass the Axios client (no interceptors, no auth token).
|
|
386
|
+
- `get()`/`post()`/… resolve to `HttpResponse<T>` — read `.data` for the
|
|
387
|
+
body (`const { data: users } = await get('/users')`).
|
|
388
|
+
- The exported `provider` constant is a pre-built default client (no baseURL,
|
|
389
|
+
30 s timeout); prefer `createAxiosClient({...})` so baseURL/credentials are
|
|
390
|
+
explicit.
|
|
391
|
+
- `instance`/`axiosConfig` accept raw Axios options — anything the app sets
|
|
392
|
+
there couples it to Axios; keep app code on the `@molecule/app-http`
|
|
393
|
+
interface so the bond stays swappable.
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/app-http-axios",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Axios HTTP client provider for @molecule/app-http",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
8
|
"files": [
|
|
9
|
-
"dist"
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
10
11
|
],
|
|
11
12
|
"scripts": {
|
|
12
13
|
"build": "tsc",
|
|
@@ -20,7 +21,7 @@
|
|
|
20
21
|
"node": ">=20"
|
|
21
22
|
},
|
|
22
23
|
"peerDependencies": {
|
|
23
|
-
"@molecule/app-http": "^1.0.
|
|
24
|
+
"@molecule/app-http": "^1.0.1",
|
|
24
25
|
"axios": "^1.6.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|