@npora/request 1.20.1 → 1.20.2
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 +9 -217
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -406,223 +406,15 @@ const user = await request.get<User>('/users/1', {
|
|
|
406
406
|
cache.clear()
|
|
407
407
|
```
|
|
408
408
|
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
```ts
|
|
414
|
-
const cache = cachePlugin({
|
|
415
|
-
store: sharedStore,
|
|
416
|
-
dedupe: true,
|
|
417
|
-
onEvent(event) {
|
|
418
|
-
metrics.increment(`cache.${event.type}`)
|
|
419
|
-
}
|
|
420
|
-
})
|
|
421
|
-
```
|
|
422
|
-
|
|
423
|
-
Use `cache.getStats()` for a snapshot of hits, misses, bypasses,
|
|
424
|
-
invalidations, deduplication, revalidation, stale recovery, and background
|
|
425
|
-
refresh outcomes.
|
|
426
|
-
`cache.resetStats()` resets only these counters; it does not clear entries.
|
|
427
|
-
Events contain only a decision type and timestamp, never URLs, cache keys, or
|
|
428
|
-
headers. Observer failures do not affect requests.
|
|
429
|
-
Failed manual or automatic invalidations are counted in
|
|
430
|
-
`invalidationErrors` and emit `invalidation-error` without exposing the error.
|
|
431
|
-
|
|
432
|
-
Seed or optimistically update parsed cache data without a network request:
|
|
433
|
-
|
|
434
|
-
```ts
|
|
435
|
-
await cache.set({ url: '/users/1' }, user, {
|
|
436
|
-
ttl: 30000,
|
|
437
|
-
tags: ['user:1']
|
|
438
|
-
})
|
|
439
|
-
|
|
440
|
-
await cache.update<User>({ url: '/users/1' }, current => ({
|
|
441
|
-
...current,
|
|
442
|
-
name: 'Updated locally'
|
|
443
|
-
}))
|
|
444
|
-
```
|
|
445
|
-
|
|
446
|
-
Return `undefined` from `update` to delete the entry. It returns `false` when
|
|
447
|
-
no entry exists. These operations use the same effective cache key as requests,
|
|
448
|
-
wait for overlapping asynchronous store operations, and prevent older
|
|
449
|
-
same-key responses from overwriting the new value.
|
|
450
|
-
|
|
451
|
-
Delete one entry without flushing unrelated cached responses:
|
|
452
|
-
|
|
453
|
-
```ts
|
|
454
|
-
await cache.delete({
|
|
455
|
-
url: '/users/1',
|
|
456
|
-
baseURL: '/api',
|
|
457
|
-
extensions: {
|
|
458
|
-
cache: { enabled: true }
|
|
459
|
-
}
|
|
460
|
-
})
|
|
461
|
-
```
|
|
462
|
-
|
|
463
|
-
Pass the effective request configuration used to create the entry, including
|
|
464
|
-
merged `baseURL`, query, response type, and representation headers. A custom
|
|
465
|
-
`extensions.cache.key` can be deleted without reproducing those dimensions.
|
|
466
|
-
|
|
467
|
-
Group related entries with bounded cache tags and invalidate them together:
|
|
468
|
-
|
|
469
|
-
```ts
|
|
470
|
-
await request.get('/users/1', {
|
|
471
|
-
extensions: {
|
|
472
|
-
cache: {
|
|
473
|
-
enabled: true,
|
|
474
|
-
tags: ['user:1', 'users']
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
})
|
|
478
|
-
|
|
479
|
-
await cache.invalidateTags('user:1')
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
The default memory store supports tags directly. Custom stores must implement
|
|
483
|
-
the optional `CacheStore.invalidateTags()` capability.
|
|
484
|
-
|
|
485
|
-
Persist JSON-compatible cached data across client instances with isolated
|
|
486
|
-
browser storage:
|
|
487
|
-
|
|
488
|
-
```ts
|
|
489
|
-
import { WebStorageCacheStore } from '@npora/request/plugins/cache'
|
|
490
|
-
|
|
491
|
-
const cache = cachePlugin({
|
|
492
|
-
store: new WebStorageCacheStore(sessionStorage, {
|
|
493
|
-
namespace: 'dashboard-v1',
|
|
494
|
-
maxEntries: 250
|
|
495
|
-
})
|
|
496
|
-
})
|
|
497
|
-
```
|
|
498
|
-
|
|
499
|
-
Use `localStorage` only when responses may safely survive browser restarts and
|
|
500
|
-
sign-out boundaries. The adapter never persists native `Response` objects;
|
|
501
|
-
`getResponse()` refetches when raw bytes are required.
|
|
502
|
-
|
|
503
|
-
For larger values and non-blocking storage, use IndexedDB instead:
|
|
504
|
-
|
|
505
|
-
```ts
|
|
506
|
-
import { IndexedDBCacheStore } from '@npora/request/plugins/cache'
|
|
507
|
-
|
|
508
|
-
const store = new IndexedDBCacheStore(indexedDB, {
|
|
509
|
-
databaseName: 'dashboard-cache',
|
|
510
|
-
namespace: 'account:1',
|
|
511
|
-
schemaVersion: 2,
|
|
512
|
-
maxEntries: 500,
|
|
513
|
-
maxBytes: 20 * 1024 * 1024,
|
|
514
|
-
shouldPersist: (entry, bytes) => (
|
|
515
|
-
entry.status === 200 && bytes < 2 * 1024 * 1024
|
|
516
|
-
),
|
|
517
|
-
onEvent: event => metrics.record('persistent-cache', event)
|
|
518
|
-
})
|
|
519
|
-
const cache = cachePlugin({ store })
|
|
520
|
-
```
|
|
521
|
-
|
|
522
|
-
IndexedDB uses structured cloning, preserving values such as `Blob`, `Date`,
|
|
523
|
-
`Map`, typed arrays, and `BigInt` while still omitting native `Response` bodies.
|
|
524
|
-
Increment the positive integer `schemaVersion` whenever the cached data shape
|
|
525
|
-
becomes incompatible. Older records are isolated and pruned automatically;
|
|
526
|
-
the default version `1` continues to read records created before this option
|
|
527
|
-
existed. Older clients preserve every higher-version record without validating
|
|
528
|
-
its envelope, including during writes, clearing, compaction, and quota
|
|
529
|
-
recovery, so rolling deployments remain safe when the persisted structure
|
|
530
|
-
changes completely.
|
|
531
|
-
`maxBytes` adds an approximate structured-clone byte budget alongside
|
|
532
|
-
`maxEntries`. Oversized entries are skipped and older entries are evicted by
|
|
533
|
-
LRU. Quota-exceeded writes remove the oldest half of the current schema cache
|
|
534
|
-
and retry once by default; set `quotaRecovery: false` to report the first quota
|
|
535
|
-
failure without recovery.
|
|
536
|
-
Use `await store.getUsage()` to inspect the current schema's aggregate entry
|
|
537
|
-
count and estimated bytes. The optional `onEvent` observer reports aggregated
|
|
538
|
-
eviction, recovery, cleanup, and oversized-entry rejection events without
|
|
539
|
-
exposing keys, namespaces, URLs, or response data. Observer failures never
|
|
540
|
-
affect cache operations.
|
|
541
|
-
Use `shouldPersist(entry, estimatedBytes)` for application-specific admission,
|
|
542
|
-
such as excluding sensitive payloads, one-hit responses, or large low-value
|
|
543
|
-
objects. It may return a boolean or promise. Returning `false` removes an older
|
|
544
|
-
same-key value and emits an aggregate `admission-policy` rejection; thrown
|
|
545
|
-
errors are reported to the caller and leave the old value unchanged.
|
|
546
|
-
Run bounded maintenance without loading the namespace into memory:
|
|
547
|
-
|
|
548
|
-
```ts
|
|
549
|
-
const result = await store.compact({
|
|
550
|
-
// Preserve seven days for stale-if-error recovery.
|
|
551
|
-
expiredBefore: Date.now() - 7 * 24 * 60 * 60 * 1000,
|
|
552
|
-
maxRemovals: 500
|
|
553
|
-
})
|
|
554
|
-
```
|
|
555
|
-
|
|
556
|
-
`compact()` removes current-schema entries beyond the chosen stale boundary,
|
|
557
|
-
reports aggregate scan/removal/byte counts, and emits privacy-safe `expired`
|
|
558
|
-
events. Repeat while `hasMore` is true when using `maxRemovals`. Usage
|
|
559
|
-
inspection now streams records through an IndexedDB cursor, avoiding a
|
|
560
|
-
namespace-sized result array.
|
|
561
|
-
|
|
562
|
-
Combine memory speed with persistence through a read-through, write-through
|
|
563
|
-
store:
|
|
564
|
-
|
|
565
|
-
```ts
|
|
566
|
-
const persistent = new IndexedDBCacheStore(indexedDB, {
|
|
567
|
-
namespace: 'account:1',
|
|
568
|
-
schemaVersion: 2
|
|
569
|
-
})
|
|
570
|
-
const channel = new BroadcastChannel('dashboard-cache:account:1:v1')
|
|
571
|
-
const store = new TieredCacheStore({
|
|
572
|
-
primary: new MemoryCacheStore({ maxEntries: 100 }),
|
|
573
|
-
secondary: persistent,
|
|
574
|
-
broadcast: { channel },
|
|
575
|
-
coordination: {
|
|
576
|
-
locks: navigator.locks,
|
|
577
|
-
namespace: 'dashboard-cache:account:1:v1'
|
|
578
|
-
}
|
|
579
|
-
})
|
|
580
|
-
const cache = cachePlugin({
|
|
581
|
-
store
|
|
582
|
-
})
|
|
583
|
-
```
|
|
584
|
-
|
|
585
|
-
Hot reads remain synchronous. Cold secondary hits are promoted to memory, and
|
|
586
|
-
writes reach the persistent tier before becoming visible in memory. The
|
|
587
|
-
optional channel evicts stale primary entries in other tabs without sending
|
|
588
|
-
URLs, headers, response data, or complete cache keys. Call `store.dispose()`
|
|
589
|
-
and `channel.close()` when the context is torn down. Optional Web Locks
|
|
590
|
-
coordination serializes same-key refreshes across tabs; waiters reread the
|
|
591
|
-
shared secondary cache instead of issuing a duplicate request. Omit
|
|
592
|
-
`coordination` where `navigator.locks` is unavailable.
|
|
593
|
-
Mutation requests can invalidate tags automatically after their final success:
|
|
594
|
-
|
|
595
|
-
```ts
|
|
596
|
-
await request.patch('/users/1', {
|
|
597
|
-
json: update,
|
|
598
|
-
extensions: {
|
|
599
|
-
cache: {
|
|
600
|
-
invalidateTags: ['user:1', 'users']
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
})
|
|
604
|
-
```
|
|
605
|
-
|
|
606
|
-
Final HTTP, Schema, interceptor, cancellation, and exhausted-retry failures do
|
|
607
|
-
not invalidate entries.
|
|
608
|
-
|
|
609
|
-
The default memory store keeps up to 1,000 entries using LRU eviction. Expired
|
|
610
|
-
entries with `ETag` or `Last-Modified` validators are conditionally revalidated;
|
|
611
|
-
set `maxEntries` to tune the bound, use `0` to disable storage, or use `Infinity`
|
|
612
|
-
for an explicitly unbounded store.
|
|
613
|
-
|
|
614
|
-
Request `Cache-Control: no-cache` or `max-age=0` to force validation, and use
|
|
615
|
-
`Cache-Control: no-store` to bypass cache reads, writes, and request sharing.
|
|
616
|
-
Response `stale-if-error` can serve an expired entry after network, timeout, or
|
|
617
|
-
5xx failure; `extensions.cache.staleIfError` sets an application limit in
|
|
618
|
-
milliseconds.
|
|
619
|
-
Response `stale-while-revalidate` can return stale data immediately while one
|
|
620
|
-
deduplicated refresh runs through the same client pipeline in the background.
|
|
409
|
+
See [cache workflows](docs/cache.md) for request sharing, cache updates, tag
|
|
410
|
+
invalidation, HTTP revalidation, stale recovery, persistent stores, and
|
|
411
|
+
coordination across browser tabs.
|
|
621
412
|
|
|
622
413
|
Built-in plugins:
|
|
623
414
|
|
|
624
415
|
- `retryPlugin()`
|
|
625
416
|
- `cachePlugin()`
|
|
417
|
+
- `memoryCachePlugin()`
|
|
626
418
|
- `circuitBreakerPlugin()`
|
|
627
419
|
- `concurrencyPlugin()`
|
|
628
420
|
- `rateLimitPlugin()`
|
|
@@ -863,12 +655,12 @@ for the complete matrix.
|
|
|
863
655
|
|
|
864
656
|
## Versioning
|
|
865
657
|
|
|
866
|
-
`@npora/request` follows Semantic Versioning.
|
|
867
|
-
|
|
868
|
-
|
|
658
|
+
`@npora/request` follows Semantic Versioning. Exports from every public
|
|
659
|
+
entrypoint listed in `package.json.exports`, their TypeScript declarations,
|
|
660
|
+
documented behavior, stable error codes, and plugin lifecycle are public API.
|
|
661
|
+
Breaking changes require a new major version.
|
|
869
662
|
|
|
870
|
-
Internal modules
|
|
871
|
-
API.
|
|
663
|
+
Internal modules outside those public entrypoints are not public API.
|
|
872
664
|
|
|
873
665
|
## License
|
|
874
666
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@npora/request",
|
|
3
|
-
"version": "1.20.
|
|
3
|
+
"version": "1.20.2",
|
|
4
4
|
"description": "A modern, TypeScript-first HTTP client powered by the Fetch API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"npora",
|
|
@@ -138,14 +138,14 @@
|
|
|
138
138
|
"@opentelemetry/sdk-trace-base": "^2.10.0",
|
|
139
139
|
"@playwright/test": "^1.61.1",
|
|
140
140
|
"@types/node": "^26.2.0",
|
|
141
|
-
"@vitest/coverage-v8": "^4.1.
|
|
141
|
+
"@vitest/coverage-v8": "^4.1.12",
|
|
142
142
|
"esbuild": "^0.28.2",
|
|
143
143
|
"tsdown": "^0.22.14",
|
|
144
144
|
"tsx": "^4.23.11",
|
|
145
145
|
"typescript": "^6.0.3",
|
|
146
146
|
"typescript-api": "npm:@typescript/typescript6@^6.0.2",
|
|
147
147
|
"typescript-next": "npm:typescript@^7.0.2",
|
|
148
|
-
"vitest": "^4.1.
|
|
148
|
+
"vitest": "^4.1.12"
|
|
149
149
|
},
|
|
150
150
|
"scripts": {
|
|
151
151
|
"dev": "tsdown --watch",
|
|
@@ -167,6 +167,7 @@
|
|
|
167
167
|
"test:coverage": "vitest run --coverage",
|
|
168
168
|
"test:run": "vitest run",
|
|
169
169
|
"test:security": "vitest run test/security.test.ts",
|
|
170
|
+
"test:migration:axios": "node --test benchmark/competitors/axios-migration.test.mjs",
|
|
170
171
|
"test:docs": "node test/docs.mjs",
|
|
171
172
|
"test:release-workflow": "node test/release-workflow.mjs",
|
|
172
173
|
"test:complexity": "node scripts/check-complexity.mjs $(find src -name '*.ts' ! -path '*/types/*' ! -name 'index.ts')",
|