@mmstack/resource 20.0.2 → 20.0.4
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/fesm2022/mmstack-resource.mjs +33 -18
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/index.d.ts +1 -520
- package/lib/mutation-resource.d.ts +92 -0
- package/lib/public_api.d.ts +3 -0
- package/lib/query-resource.d.ts +97 -0
- package/lib/util/cache/cache.d.ts +177 -0
- package/lib/util/cache/cache.interceptor.d.ts +39 -0
- package/lib/util/cache/index.d.ts +2 -0
- package/lib/util/cache/public_api.d.ts +2 -0
- package/lib/util/catch-value-error.d.ts +2 -0
- package/lib/util/circuit-breaker.d.ts +74 -0
- package/lib/util/dedupe.interceptor.d.ts +50 -0
- package/lib/util/equality.d.ts +3 -0
- package/lib/util/has-slow-connection.d.ts +1 -0
- package/lib/util/index.d.ts +11 -0
- package/lib/util/persist.d.ts +3 -0
- package/lib/util/public_api.d.ts +3 -0
- package/lib/util/refresh.d.ts +3 -0
- package/lib/util/retry-on-error.d.ts +6 -0
- package/lib/util/to-resource-object.d.ts +2 -0
- package/lib/util/url-with-params.d.ts +2 -0
- package/package.json +1 -1
|
@@ -322,30 +322,25 @@ function parseCacheControlHeader(req) {
|
|
|
322
322
|
};
|
|
323
323
|
return directives;
|
|
324
324
|
}
|
|
325
|
-
function resolveTimings(cacheControl,
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
ttl,
|
|
329
|
-
};
|
|
325
|
+
function resolveTimings(cacheControl, optStaleTime, optTTL) {
|
|
326
|
+
let staleTime = optStaleTime;
|
|
327
|
+
let ttl = optTTL;
|
|
330
328
|
if (cacheControl.immutable)
|
|
331
329
|
return {
|
|
332
330
|
staleTime: Infinity,
|
|
333
331
|
ttl: Infinity,
|
|
334
332
|
};
|
|
333
|
+
if (cacheControl.maxAge !== null)
|
|
334
|
+
ttl = cacheControl.maxAge * 1000;
|
|
335
|
+
if (cacheControl.staleWhileRevalidate !== null)
|
|
336
|
+
staleTime = cacheControl.staleWhileRevalidate * 1000;
|
|
335
337
|
// if no-cache is set, we must always revalidate
|
|
336
338
|
if (cacheControl.noCache || cacheControl.mustRevalidate)
|
|
337
|
-
|
|
338
|
-
if (
|
|
339
|
-
|
|
340
|
-
if (cacheControl.maxAge !== null)
|
|
341
|
-
timings.ttl = cacheControl.maxAge * 1000;
|
|
342
|
-
// if stale-while-revalidate is set, we must revalidate after that time at the latest, but we can still serve the stale data
|
|
343
|
-
if (cacheControl.staleWhileRevalidate !== null) {
|
|
344
|
-
const ms = cacheControl.staleWhileRevalidate * 1000;
|
|
345
|
-
if (timings.staleTime === undefined || timings.staleTime > ms)
|
|
346
|
-
timings.staleTime = ms;
|
|
339
|
+
staleTime = 0;
|
|
340
|
+
if (ttl !== undefined && staleTime !== undefined && ttl < staleTime) {
|
|
341
|
+
staleTime = ttl;
|
|
347
342
|
}
|
|
348
|
-
return
|
|
343
|
+
return { staleTime, ttl };
|
|
349
344
|
}
|
|
350
345
|
/**
|
|
351
346
|
* Creates an `HttpInterceptorFn` that implements caching for HTTP requests. This interceptor
|
|
@@ -403,6 +398,8 @@ function createCacheInterceptor(allowedMethods = ['GET', 'HEAD', 'OPTIONS']) {
|
|
|
403
398
|
if (cacheControl.noStore)
|
|
404
399
|
return;
|
|
405
400
|
const { staleTime, ttl } = resolveTimings(cacheControl, opt.staleTime, opt.ttl);
|
|
401
|
+
if (opt.ttl === 0)
|
|
402
|
+
return; // no point
|
|
406
403
|
cache.store(key, event, staleTime, ttl);
|
|
407
404
|
}
|
|
408
405
|
}), map((event) => {
|
|
@@ -784,6 +781,24 @@ function retryOnError(res, opt) {
|
|
|
784
781
|
};
|
|
785
782
|
}
|
|
786
783
|
|
|
784
|
+
function toResourceObject(res) {
|
|
785
|
+
return {
|
|
786
|
+
asReadonly: () => res.asReadonly(),
|
|
787
|
+
destroy: () => res.destroy(),
|
|
788
|
+
error: res.error,
|
|
789
|
+
headers: res.headers,
|
|
790
|
+
isLoading: res.isLoading,
|
|
791
|
+
progress: res.progress,
|
|
792
|
+
status: res.status,
|
|
793
|
+
statusCode: res.statusCode,
|
|
794
|
+
value: res.value,
|
|
795
|
+
reload: () => res.reload(),
|
|
796
|
+
hasValue: (() => res.hasValue()),
|
|
797
|
+
set: (v) => res.set(v),
|
|
798
|
+
update: (v) => res.update(v),
|
|
799
|
+
};
|
|
800
|
+
}
|
|
801
|
+
|
|
787
802
|
function normalizeParams(params) {
|
|
788
803
|
if (params instanceof HttpParams)
|
|
789
804
|
return params.toString();
|
|
@@ -849,11 +864,11 @@ function queryResource(request, options) {
|
|
|
849
864
|
};
|
|
850
865
|
})
|
|
851
866
|
: stableRequest;
|
|
852
|
-
let resource = httpResource(cachedRequest, {
|
|
867
|
+
let resource = toResourceObject(httpResource(cachedRequest, {
|
|
853
868
|
...options,
|
|
854
869
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
855
870
|
parse: options?.parse, // Not my favorite thing to do, but here it is completely safe.
|
|
856
|
-
});
|
|
871
|
+
}));
|
|
857
872
|
resource = catchValueError(resource, options?.defaultValue);
|
|
858
873
|
// get full HttpResonse from Cache
|
|
859
874
|
const cachedEvent = cache.get(cacheKey);
|