@molecule/app-http 1.1.0 → 1.1.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 +153 -136
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@ AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
|
3
3
|
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
4
|
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
5
|
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
-
Generated: 2026-10-
|
|
6
|
+
Generated: 2026-10-16T00:35:00.541Z
|
|
7
7
|
-->
|
|
8
8
|
|
|
9
9
|
# @molecule/app-http
|
|
@@ -49,29 +49,6 @@ npm install @molecule/app-http @molecule/app-bond @molecule/app-i18n @molecule/a
|
|
|
49
49
|
|
|
50
50
|
### Interfaces
|
|
51
51
|
|
|
52
|
-
#### `FullRequestConfig`
|
|
53
|
-
|
|
54
|
-
Full request configuration including method and URL.
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
interface FullRequestConfig extends RequestConfig {
|
|
58
|
-
/**
|
|
59
|
-
* HTTP method.
|
|
60
|
-
*/
|
|
61
|
-
method: HttpMethod
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Request URL (can be relative or absolute).
|
|
65
|
-
*/
|
|
66
|
-
url: string
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Request body data.
|
|
70
|
-
*/
|
|
71
|
-
data?: unknown
|
|
72
|
-
}
|
|
73
|
-
```
|
|
74
|
-
|
|
75
52
|
#### `HttpClient`
|
|
76
53
|
|
|
77
54
|
HTTP client interface.
|
|
@@ -192,39 +169,6 @@ interface HttpClientConfig {
|
|
|
192
169
|
}
|
|
193
170
|
```
|
|
194
171
|
|
|
195
|
-
#### `HttpResponse`
|
|
196
|
-
|
|
197
|
-
Parsed HTTP response with status code, headers, and typed body data.
|
|
198
|
-
|
|
199
|
-
```typescript
|
|
200
|
-
interface HttpResponse<T = unknown> {
|
|
201
|
-
/**
|
|
202
|
-
* Response data.
|
|
203
|
-
*/
|
|
204
|
-
data: T
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* HTTP status code.
|
|
208
|
-
*/
|
|
209
|
-
status: number
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* HTTP status text.
|
|
213
|
-
*/
|
|
214
|
-
statusText: string
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Response headers.
|
|
218
|
-
*/
|
|
219
|
-
headers: Record<string, string>
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Original request config.
|
|
223
|
-
*/
|
|
224
|
-
config: FullRequestConfig
|
|
225
|
-
}
|
|
226
|
-
```
|
|
227
|
-
|
|
228
172
|
#### `RequestConfig`
|
|
229
173
|
|
|
230
174
|
HTTP request options (headers, query params, timeout, credentials, response type, abort signal).
|
|
@@ -273,25 +217,64 @@ interface RequestConfig {
|
|
|
273
217
|
}
|
|
274
218
|
```
|
|
275
219
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
#### `ErrorInterceptor`
|
|
220
|
+
#### `FullRequestConfig`
|
|
279
221
|
|
|
280
|
-
|
|
281
|
-
Throwing from this interceptor propagates the error to the caller.
|
|
222
|
+
Full request configuration including method and URL.
|
|
282
223
|
|
|
283
224
|
```typescript
|
|
284
|
-
|
|
225
|
+
interface FullRequestConfig extends RequestConfig {
|
|
226
|
+
/**
|
|
227
|
+
* HTTP method.
|
|
228
|
+
*/
|
|
229
|
+
method: HttpMethod
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Request URL (can be relative or absolute).
|
|
233
|
+
*/
|
|
234
|
+
url: string
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Request body data.
|
|
238
|
+
*/
|
|
239
|
+
data?: unknown
|
|
240
|
+
}
|
|
285
241
|
```
|
|
286
242
|
|
|
287
|
-
#### `
|
|
243
|
+
#### `HttpResponse`
|
|
288
244
|
|
|
289
|
-
HTTP
|
|
245
|
+
Parsed HTTP response with status code, headers, and typed body data.
|
|
290
246
|
|
|
291
247
|
```typescript
|
|
292
|
-
|
|
248
|
+
interface HttpResponse<T = unknown> {
|
|
249
|
+
/**
|
|
250
|
+
* Response data.
|
|
251
|
+
*/
|
|
252
|
+
data: T
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* HTTP status code.
|
|
256
|
+
*/
|
|
257
|
+
status: number
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* HTTP status text.
|
|
261
|
+
*/
|
|
262
|
+
statusText: string
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Response headers.
|
|
266
|
+
*/
|
|
267
|
+
headers: Record<string, string>
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Original request config.
|
|
271
|
+
*/
|
|
272
|
+
config: FullRequestConfig
|
|
273
|
+
}
|
|
293
274
|
```
|
|
294
275
|
|
|
276
|
+
### Types
|
|
277
|
+
|
|
295
278
|
#### `RequestInterceptor`
|
|
296
279
|
|
|
297
280
|
Intercepts outgoing requests to modify headers, URL, or body
|
|
@@ -303,6 +286,8 @@ type RequestInterceptor = (
|
|
|
303
286
|
) => FullRequestConfig | Promise<FullRequestConfig>
|
|
304
287
|
```
|
|
305
288
|
|
|
289
|
+
- `config` — The full request configuration to transform.
|
|
290
|
+
|
|
306
291
|
#### `ResponseInterceptor`
|
|
307
292
|
|
|
308
293
|
Intercepts incoming responses to transform data, check status,
|
|
@@ -314,12 +299,44 @@ type ResponseInterceptor<T = unknown> = (
|
|
|
314
299
|
) => HttpResponse<T> | Promise<HttpResponse<T>>
|
|
315
300
|
```
|
|
316
301
|
|
|
302
|
+
#### `ErrorInterceptor`
|
|
303
|
+
|
|
304
|
+
Intercepts HTTP errors to transform, retry, or rethrow them.
|
|
305
|
+
Throwing from this interceptor propagates the error to the caller.
|
|
306
|
+
|
|
307
|
+
```typescript
|
|
308
|
+
type ErrorInterceptor = (error: HttpError) => HttpError | Promise<HttpError> | never
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
#### `HttpMethod`
|
|
312
|
+
|
|
313
|
+
HTTP request method.
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS'
|
|
317
|
+
```
|
|
318
|
+
|
|
317
319
|
### Classes
|
|
318
320
|
|
|
319
321
|
#### `HttpError`
|
|
320
322
|
|
|
321
323
|
HTTP error with response details.
|
|
322
324
|
|
|
325
|
+
```typescript
|
|
326
|
+
class HttpError extends Error {
|
|
327
|
+
constructor(
|
|
328
|
+
message: string,
|
|
329
|
+
public status: number,
|
|
330
|
+
public response?: HttpResponse,
|
|
331
|
+
public config?: FullRequestConfig,
|
|
332
|
+
public errorKey?: string,
|
|
333
|
+
) {
|
|
334
|
+
super(message)
|
|
335
|
+
this.name = 'HttpError'
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
323
340
|
### Functions
|
|
324
341
|
|
|
325
342
|
#### `createFetchClient(config)`
|
|
@@ -338,31 +355,71 @@ function createFetchClient(config?: HttpClientConfig): HttpClient
|
|
|
338
355
|
|
|
339
356
|
**Returns:** A fully configured `HttpClient` instance.
|
|
340
357
|
|
|
341
|
-
#### `
|
|
358
|
+
#### `unwrapList(res)`
|
|
342
359
|
|
|
343
|
-
|
|
360
|
+
Normalize an unknown response body into a typed array.
|
|
361
|
+
|
|
362
|
+
Accepts:
|
|
363
|
+
|
|
364
|
+
- a bare array → returned as-is (cast)
|
|
365
|
+
- `{ data: T[] }` envelope → the inner array
|
|
366
|
+
- `HttpResponse<T[]>` (i.e. `{ data: T[], status, ... }`) → the inner array
|
|
367
|
+
- `HttpResponse<{ data: T[] }>` (the response of an envelope-returning
|
|
368
|
+
endpoint as it arrives from `@molecule/app-http`'s `HttpClient`) → the
|
|
369
|
+
doubly-nested inner array
|
|
370
|
+
- `HttpResponse<{ data: T[], total, limit, offset }>` (a RICH pagination
|
|
371
|
+
envelope from a pre-built `@molecule/api-resource-*` list endpoint) → the
|
|
372
|
+
inner `data` array (the numeric `total`/`limit`/`offset` make the shape
|
|
373
|
+
unambiguous, so callers can pass the whole HttpResponse and still get the rows)
|
|
374
|
+
- anything else → `[]`
|
|
375
|
+
|
|
376
|
+
Callers commonly pass either the raw JSON body (e.g. from `fetch().then(r =>
|
|
377
|
+
r.json())`) or the `HttpResponse` returned by `useHttpClient().get(...)`.
|
|
378
|
+
Both shapes are handled here so pages don't have to remember to call
|
|
379
|
+
`unwrapList(res.data)` vs `unwrapList(res)`.
|
|
344
380
|
|
|
345
381
|
```typescript
|
|
346
|
-
function
|
|
382
|
+
function unwrapList(res: unknown): T[]
|
|
347
383
|
```
|
|
348
384
|
|
|
349
|
-
- `
|
|
350
|
-
- `config` — Optional request configuration.
|
|
385
|
+
- `res` — Raw response body OR an `HttpResponse` envelope from `@molecule/app-http`.
|
|
351
386
|
|
|
352
|
-
**Returns:**
|
|
387
|
+
**Returns:** A typed array `T[]`; never `null`/`undefined`.
|
|
353
388
|
|
|
354
|
-
#### `
|
|
389
|
+
#### `unwrapSingle(res)`
|
|
355
390
|
|
|
356
|
-
|
|
391
|
+
Normalize an unknown response body into a single typed resource.
|
|
392
|
+
|
|
393
|
+
Accepts:
|
|
394
|
+
|
|
395
|
+
- a non-empty plain object → returned as-is (cast)
|
|
396
|
+
- `{ data: T }` envelope → the inner value
|
|
397
|
+
- `{ data: null }`, `{ data: undefined }`, `{ data: [] }`, or
|
|
398
|
+
`{ data: {} }` (mock-server's no-match shape) → `null`
|
|
399
|
+
- an empty object `{}` → `null`
|
|
400
|
+
- arrays, primitives, `null`, `undefined` → `null`
|
|
401
|
+
|
|
402
|
+
The "envelope contains an array → null" branch handles the case
|
|
403
|
+
where the mock server returns `[]` for unmatched endpoints but the
|
|
404
|
+
caller expects a single resource.
|
|
357
405
|
|
|
358
406
|
```typescript
|
|
359
|
-
function
|
|
407
|
+
function unwrapSingle(res: unknown): T | null
|
|
360
408
|
```
|
|
361
409
|
|
|
362
|
-
- `
|
|
363
|
-
- `config` — Optional request configuration (headers, params, timeout).
|
|
410
|
+
- `res` — Raw response body (e.g. `HttpResponse.data` from `@molecule/app-http`).
|
|
364
411
|
|
|
365
|
-
**Returns:** The
|
|
412
|
+
**Returns:** The typed resource `T`, or `null` when the response shape indicates "no resource" (including the various empty envelopes above).
|
|
413
|
+
|
|
414
|
+
#### `setClient(client)`
|
|
415
|
+
|
|
416
|
+
Registers an HTTP client as the active singleton.
|
|
417
|
+
|
|
418
|
+
```typescript
|
|
419
|
+
function setClient(client: HttpClient): void
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
- `client` — The HTTP client implementation to bond.
|
|
366
423
|
|
|
367
424
|
#### `getClient()`
|
|
368
425
|
|
|
@@ -375,17 +432,16 @@ function getClient(): HttpClient
|
|
|
375
432
|
|
|
376
433
|
**Returns:** The active HTTP client instance.
|
|
377
434
|
|
|
378
|
-
#### `
|
|
435
|
+
#### `get(url, config)`
|
|
379
436
|
|
|
380
|
-
Makes a
|
|
437
|
+
Makes a GET request using the bonded HTTP client.
|
|
381
438
|
|
|
382
439
|
```typescript
|
|
383
|
-
function
|
|
440
|
+
function get(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
384
441
|
```
|
|
385
442
|
|
|
386
|
-
- `url` — The request URL.
|
|
387
|
-
- `
|
|
388
|
-
- `config` — Optional request configuration.
|
|
443
|
+
- `url` — The request URL (relative to baseURL if configured).
|
|
444
|
+
- `config` — Optional request configuration (headers, params, timeout).
|
|
389
445
|
|
|
390
446
|
**Returns:** The HTTP response with typed data.
|
|
391
447
|
|
|
@@ -417,71 +473,32 @@ function put(url: string, data?: unknown, config?: RequestConfig): Promise<HttpR
|
|
|
417
473
|
|
|
418
474
|
**Returns:** The HTTP response with typed data.
|
|
419
475
|
|
|
420
|
-
#### `
|
|
421
|
-
|
|
422
|
-
Registers an HTTP client as the active singleton.
|
|
423
|
-
|
|
424
|
-
```typescript
|
|
425
|
-
function setClient(client: HttpClient): void
|
|
426
|
-
```
|
|
427
|
-
|
|
428
|
-
- `client` — The HTTP client implementation to bond.
|
|
429
|
-
|
|
430
|
-
#### `unwrapList(res)`
|
|
431
|
-
|
|
432
|
-
Normalize an unknown response body into a typed array.
|
|
433
|
-
|
|
434
|
-
Accepts:
|
|
435
|
-
|
|
436
|
-
- a bare array → returned as-is (cast)
|
|
437
|
-
- `{ data: T[] }` envelope → the inner array
|
|
438
|
-
- `HttpResponse<T[]>` (i.e. `{ data: T[], status, ... }`) → the inner array
|
|
439
|
-
- `HttpResponse<{ data: T[] }>` (the response of an envelope-returning
|
|
440
|
-
endpoint as it arrives from `@molecule/app-http`'s `HttpClient`) → the
|
|
441
|
-
doubly-nested inner array
|
|
442
|
-
- `HttpResponse<{ data: T[], total, limit, offset }>` (a RICH pagination
|
|
443
|
-
envelope from a pre-built `@molecule/api-resource-*` list endpoint) → the
|
|
444
|
-
inner `data` array (the numeric `total`/`limit`/`offset` make the shape
|
|
445
|
-
unambiguous, so callers can pass the whole HttpResponse and still get the rows)
|
|
446
|
-
- anything else → `[]`
|
|
476
|
+
#### `patch(url, data, config)`
|
|
447
477
|
|
|
448
|
-
|
|
449
|
-
r.json())`) or the `HttpResponse` returned by `useHttpClient().get(...)`.
|
|
450
|
-
Both shapes are handled here so pages don't have to remember to call
|
|
451
|
-
`unwrapList(res.data)` vs `unwrapList(res)`.
|
|
478
|
+
Makes a PATCH request using the bonded HTTP client.
|
|
452
479
|
|
|
453
480
|
```typescript
|
|
454
|
-
function
|
|
481
|
+
function patch(url: string, data?: unknown, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
455
482
|
```
|
|
456
483
|
|
|
457
|
-
- `
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
#### `unwrapSingle(res)`
|
|
462
|
-
|
|
463
|
-
Normalize an unknown response body into a single typed resource.
|
|
484
|
+
- `url` — The request URL.
|
|
485
|
+
- `data` — The request body (partial update).
|
|
486
|
+
- `config` — Optional request configuration.
|
|
464
487
|
|
|
465
|
-
|
|
488
|
+
**Returns:** The HTTP response with typed data.
|
|
466
489
|
|
|
467
|
-
|
|
468
|
-
- `{ data: T }` envelope → the inner value
|
|
469
|
-
- `{ data: null }`, `{ data: undefined }`, `{ data: [] }`, or
|
|
470
|
-
`{ data: {} }` (mock-server's no-match shape) → `null`
|
|
471
|
-
- an empty object `{}` → `null`
|
|
472
|
-
- arrays, primitives, `null`, `undefined` → `null`
|
|
490
|
+
#### `del(url, config)`
|
|
473
491
|
|
|
474
|
-
|
|
475
|
-
where the mock server returns `[]` for unmatched endpoints but the
|
|
476
|
-
caller expects a single resource.
|
|
492
|
+
Makes a DELETE request using the bonded HTTP client.
|
|
477
493
|
|
|
478
494
|
```typescript
|
|
479
|
-
function
|
|
495
|
+
function del(url: string, config?: RequestConfig): Promise<HttpResponse<T>>
|
|
480
496
|
```
|
|
481
497
|
|
|
482
|
-
- `
|
|
498
|
+
- `url` — The request URL.
|
|
499
|
+
- `config` — Optional request configuration.
|
|
483
500
|
|
|
484
|
-
**Returns:** The
|
|
501
|
+
**Returns:** The HTTP response with typed data.
|
|
485
502
|
|
|
486
503
|
### Constants
|
|
487
504
|
|