@npora/request 1.10.0 → 1.10.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 +23 -6
- package/dist/index.cjs +15 -3
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +15 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
# @npora/request
|
|
2
2
|
|
|
3
|
-
A TypeScript-first HTTP client built on the standard Fetch
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
A production-focused, TypeScript-first HTTP client built on the standard Fetch
|
|
4
|
+
API, with zero runtime dependencies.
|
|
5
|
+
|
|
6
|
+
Npora Request keeps the native Fetch model while adding the contracts and
|
|
7
|
+
resilience needed by real applications: inferred response types, runtime
|
|
8
|
+
validation, incremental streaming, stable errors, deterministic extension
|
|
9
|
+
ordering, retries, caching, concurrency control, circuit breaking,
|
|
10
|
+
authentication, and upload/download progress.
|
|
11
|
+
|
|
12
|
+
## Why Npora Request
|
|
13
|
+
|
|
14
|
+
- **Trust responses at runtime.** Standard Schema v1 validation checks and can
|
|
15
|
+
transform untrusted data while preserving full response and error metadata.
|
|
16
|
+
- **Stream without buffering everything.** SSE and NDJSON are exposed as lazy
|
|
17
|
+
async iterables with cancellation, timeout, and response-size enforcement.
|
|
18
|
+
- **Add resilience without hiding the transport.** Retry, cache, circuit
|
|
19
|
+
breaker, concurrency, authentication, logging, and progress are isolated,
|
|
20
|
+
composable plugins over the standard Fetch API.
|
|
21
|
+
- **Use one predictable contract everywhere.** Browsers, Web Workers, Node.js,
|
|
22
|
+
ESM, and CommonJS share the same typed lifecycle and stable error codes.
|
|
23
|
+
- **Ship less supply-chain risk.** The package has no runtime dependencies and
|
|
24
|
+
verifies public exports, browser behavior, security regressions, and size
|
|
25
|
+
budgets before release.
|
|
9
26
|
|
|
10
27
|
## Features
|
|
11
28
|
|
package/dist/index.cjs
CHANGED
|
@@ -1913,7 +1913,7 @@ var MemoryCacheStore = class {
|
|
|
1913
1913
|
get(key) {
|
|
1914
1914
|
const entry = this.entries.get(key);
|
|
1915
1915
|
if (!entry) return;
|
|
1916
|
-
if (
|
|
1916
|
+
if (isExpired(entry.expiresAt)) {
|
|
1917
1917
|
this.entries.delete(key);
|
|
1918
1918
|
return;
|
|
1919
1919
|
}
|
|
@@ -1965,10 +1965,11 @@ function cachePlugin(options = {}) {
|
|
|
1965
1965
|
context.hooks.onRequest(async (requestContext) => {
|
|
1966
1966
|
const cache = resolveExtensionConfig(requestContext.config, "cache");
|
|
1967
1967
|
if (!cache?.enabled || !isCacheableRequest(requestContext.config, methods)) return;
|
|
1968
|
+
normalizeCacheTtl(cache.ttl, requestContext.config);
|
|
1968
1969
|
const key = createCacheKey(requestContext.config, cache, varyHeaders);
|
|
1969
1970
|
const record = await readStore(store, key);
|
|
1970
1971
|
if (!active) return;
|
|
1971
|
-
if (record) if (
|
|
1972
|
+
if (record) if (isExpired(record.expiresAt)) await deleteStore(store, key);
|
|
1972
1973
|
else {
|
|
1973
1974
|
const cachedResponse = restoreCacheEntry(record, requestContext.config);
|
|
1974
1975
|
if (cachedResponse) {
|
|
@@ -2002,7 +2003,7 @@ function cachePlugin(options = {}) {
|
|
|
2002
2003
|
await deleteStore(store, key);
|
|
2003
2004
|
return;
|
|
2004
2005
|
}
|
|
2005
|
-
const ttl = cache.ttl
|
|
2006
|
+
const ttl = normalizeCacheTtl(cache.ttl, requestContext.config);
|
|
2006
2007
|
const record = createCacheEntry(requestContext.response, Date.now() + Math.max(0, ttl));
|
|
2007
2008
|
completedRecords.set(requestContext, record);
|
|
2008
2009
|
if (ttl <= 0 || !allowsPersistentCaching(requestContext.response)) {
|
|
@@ -2236,6 +2237,17 @@ function normalizeMaxEntries(value) {
|
|
|
2236
2237
|
if (!Number.isFinite(value)) return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2237
2238
|
return Math.max(0, Math.floor(value));
|
|
2238
2239
|
}
|
|
2240
|
+
function normalizeCacheTtl(value, config) {
|
|
2241
|
+
const ttl = value ?? 3e4;
|
|
2242
|
+
if (ttl !== Number.POSITIVE_INFINITY && (!Number.isFinite(ttl) || ttl < 0)) throw new RequestError("Cache ttl must be a non-negative finite number or Infinity", {
|
|
2243
|
+
code: "CONFIG_ERROR",
|
|
2244
|
+
config
|
|
2245
|
+
});
|
|
2246
|
+
return ttl;
|
|
2247
|
+
}
|
|
2248
|
+
function isExpired(expiresAt) {
|
|
2249
|
+
return Number.isNaN(expiresAt) || Date.now() > expiresAt;
|
|
2250
|
+
}
|
|
2239
2251
|
//#endregion
|
|
2240
2252
|
//#region src/plugins/circuitBreakerPlugin.ts
|
|
2241
2253
|
function circuitBreakerPlugin(options = {}) {
|
package/dist/index.d.cts
CHANGED
|
@@ -134,6 +134,12 @@ interface RetryEvent {
|
|
|
134
134
|
}
|
|
135
135
|
interface CacheOptions {
|
|
136
136
|
enabled?: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Time in milliseconds before a cached response expires.
|
|
139
|
+
* Use `0` to disable persistence or `Infinity` to retain it indefinitely.
|
|
140
|
+
*
|
|
141
|
+
* @default 30000
|
|
142
|
+
*/
|
|
137
143
|
ttl?: number;
|
|
138
144
|
key?: string;
|
|
139
145
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -134,6 +134,12 @@ interface RetryEvent {
|
|
|
134
134
|
}
|
|
135
135
|
interface CacheOptions {
|
|
136
136
|
enabled?: boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Time in milliseconds before a cached response expires.
|
|
139
|
+
* Use `0` to disable persistence or `Infinity` to retain it indefinitely.
|
|
140
|
+
*
|
|
141
|
+
* @default 30000
|
|
142
|
+
*/
|
|
137
143
|
ttl?: number;
|
|
138
144
|
key?: string;
|
|
139
145
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1912,7 +1912,7 @@ var MemoryCacheStore = class {
|
|
|
1912
1912
|
get(key) {
|
|
1913
1913
|
const entry = this.entries.get(key);
|
|
1914
1914
|
if (!entry) return;
|
|
1915
|
-
if (
|
|
1915
|
+
if (isExpired(entry.expiresAt)) {
|
|
1916
1916
|
this.entries.delete(key);
|
|
1917
1917
|
return;
|
|
1918
1918
|
}
|
|
@@ -1964,10 +1964,11 @@ function cachePlugin(options = {}) {
|
|
|
1964
1964
|
context.hooks.onRequest(async (requestContext) => {
|
|
1965
1965
|
const cache = resolveExtensionConfig(requestContext.config, "cache");
|
|
1966
1966
|
if (!cache?.enabled || !isCacheableRequest(requestContext.config, methods)) return;
|
|
1967
|
+
normalizeCacheTtl(cache.ttl, requestContext.config);
|
|
1967
1968
|
const key = createCacheKey(requestContext.config, cache, varyHeaders);
|
|
1968
1969
|
const record = await readStore(store, key);
|
|
1969
1970
|
if (!active) return;
|
|
1970
|
-
if (record) if (
|
|
1971
|
+
if (record) if (isExpired(record.expiresAt)) await deleteStore(store, key);
|
|
1971
1972
|
else {
|
|
1972
1973
|
const cachedResponse = restoreCacheEntry(record, requestContext.config);
|
|
1973
1974
|
if (cachedResponse) {
|
|
@@ -2001,7 +2002,7 @@ function cachePlugin(options = {}) {
|
|
|
2001
2002
|
await deleteStore(store, key);
|
|
2002
2003
|
return;
|
|
2003
2004
|
}
|
|
2004
|
-
const ttl = cache.ttl
|
|
2005
|
+
const ttl = normalizeCacheTtl(cache.ttl, requestContext.config);
|
|
2005
2006
|
const record = createCacheEntry(requestContext.response, Date.now() + Math.max(0, ttl));
|
|
2006
2007
|
completedRecords.set(requestContext, record);
|
|
2007
2008
|
if (ttl <= 0 || !allowsPersistentCaching(requestContext.response)) {
|
|
@@ -2235,6 +2236,17 @@ function normalizeMaxEntries(value) {
|
|
|
2235
2236
|
if (!Number.isFinite(value)) return value > 0 ? Number.POSITIVE_INFINITY : 0;
|
|
2236
2237
|
return Math.max(0, Math.floor(value));
|
|
2237
2238
|
}
|
|
2239
|
+
function normalizeCacheTtl(value, config) {
|
|
2240
|
+
const ttl = value ?? 3e4;
|
|
2241
|
+
if (ttl !== Number.POSITIVE_INFINITY && (!Number.isFinite(ttl) || ttl < 0)) throw new RequestError("Cache ttl must be a non-negative finite number or Infinity", {
|
|
2242
|
+
code: "CONFIG_ERROR",
|
|
2243
|
+
config
|
|
2244
|
+
});
|
|
2245
|
+
return ttl;
|
|
2246
|
+
}
|
|
2247
|
+
function isExpired(expiresAt) {
|
|
2248
|
+
return Number.isNaN(expiresAt) || Date.now() > expiresAt;
|
|
2249
|
+
}
|
|
2238
2250
|
//#endregion
|
|
2239
2251
|
//#region src/plugins/circuitBreakerPlugin.ts
|
|
2240
2252
|
function circuitBreakerPlugin(options = {}) {
|