@experteam-mx/ngx-services 20.8.6 → 20.9.0
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
CHANGED
|
@@ -22,8 +22,12 @@ import { routes } from './app.routes'
|
|
|
22
22
|
|
|
23
23
|
const envs = {
|
|
24
24
|
...environment,
|
|
25
|
-
authCookie: 'token'
|
|
26
|
-
|
|
25
|
+
authCookie: 'token'
|
|
26
|
+
// cacheRoutes is opt-in; omit or leave empty until routes are registered:
|
|
27
|
+
// cacheRoutes: [
|
|
28
|
+
// { pattern: '/api/catalogs', ttl: 60_000 },
|
|
29
|
+
// { pattern: '/api/companies/.+/branches', ttl: 30_000 }
|
|
30
|
+
// ]
|
|
27
31
|
} as Environment
|
|
28
32
|
|
|
29
33
|
export const appConfig: ApplicationConfig = {
|
|
@@ -41,6 +45,8 @@ export const appConfig: ApplicationConfig = {
|
|
|
41
45
|
|
|
42
46
|
`provideNgxServices` registers `ENVIRONMENT_TOKEN`, which is required by the API services and some interceptors in this package.
|
|
43
47
|
|
|
48
|
+
`httpCachingInterceptor` caches only GET requests whose URL matches an entry in `cacheRoutes` (first matching RegExp pattern wins). With no routes configured, nothing is cached.
|
|
49
|
+
|
|
44
50
|
## NgModule compatibility
|
|
45
51
|
|
|
46
52
|
`NgxServicesModule.forRoot(environment)` is still available for compatibility, but it is deprecated for new standalone applications and will be removed in `20.2.0`.
|
|
@@ -5833,23 +5833,40 @@ function apiTokenInterceptor(req, next) {
|
|
|
5833
5833
|
return next(req);
|
|
5834
5834
|
}
|
|
5835
5835
|
|
|
5836
|
-
const DEFAULT_TTL = 10000; // ttl in ms
|
|
5837
5836
|
const cache = new Map();
|
|
5838
5837
|
const inFlightRequests = new Map();
|
|
5839
5838
|
/**
|
|
5840
|
-
*
|
|
5841
|
-
*
|
|
5839
|
+
* Resolves the TTL for a URL from the first matching `cacheRoutes` entry.
|
|
5840
|
+
* Returns `null` when there is no match or no routes configured.
|
|
5841
|
+
*/
|
|
5842
|
+
function resolveCacheTtl(url, cacheRoutes) {
|
|
5843
|
+
if (!cacheRoutes?.length)
|
|
5844
|
+
return null;
|
|
5845
|
+
for (const route of cacheRoutes) {
|
|
5846
|
+
if (new RegExp(route.pattern).test(url)) {
|
|
5847
|
+
return route.ttl;
|
|
5848
|
+
}
|
|
5849
|
+
}
|
|
5850
|
+
return null;
|
|
5851
|
+
}
|
|
5852
|
+
/**
|
|
5853
|
+
* Interceptor function to handle opt-in HTTP caching for GET requests that match
|
|
5854
|
+
* a configured `cacheRoutes` pattern. Non-matching GETs pass through uncached.
|
|
5855
|
+
* Concurrent in-flight requests for the same key are deduplicated via `shareReplay`.
|
|
5842
5856
|
*
|
|
5843
|
-
* @param {HttpRequest<
|
|
5857
|
+
* @param {HttpRequest<unknown>} req - The HTTP request object being intercepted.
|
|
5844
5858
|
* @param {HttpHandlerFn} next - The next HTTP handler function in the chain to process the request.
|
|
5845
|
-
* @return {Observable<HttpEvent<
|
|
5859
|
+
* @return {Observable<HttpEvent<unknown>>} An observable that emits the HTTP event, either from cache
|
|
5846
5860
|
* or by invoking the next handler.
|
|
5847
5861
|
*/
|
|
5848
5862
|
function httpCachingInterceptor(req, next) {
|
|
5849
|
-
const {
|
|
5863
|
+
const { cacheRoutes } = inject(ENVIRONMENT_TOKEN);
|
|
5850
5864
|
if (req.method !== 'GET')
|
|
5851
5865
|
return next(req);
|
|
5852
5866
|
const key = req.urlWithParams;
|
|
5867
|
+
const routeTtl = resolveCacheTtl(key, cacheRoutes);
|
|
5868
|
+
if (routeTtl === null)
|
|
5869
|
+
return next(req);
|
|
5853
5870
|
const cached = cache.get(key);
|
|
5854
5871
|
if (cached) {
|
|
5855
5872
|
const isExpired = Date.now() > cached.ttl;
|
|
@@ -5866,7 +5883,7 @@ function httpCachingInterceptor(req, next) {
|
|
|
5866
5883
|
if (!(res instanceof HttpResponse)) {
|
|
5867
5884
|
return;
|
|
5868
5885
|
}
|
|
5869
|
-
const ttl = Date.now() +
|
|
5886
|
+
const ttl = Date.now() + routeTtl;
|
|
5870
5887
|
cache.set(key, { res, ttl });
|
|
5871
5888
|
}), finalize(() => {
|
|
5872
5889
|
inFlightRequests.delete(key);
|