@experteam-mx/ngx-services 20.8.6 → 20.9.1-dev3.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
- cacheTtl: 3000
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`.
@@ -1695,6 +1695,19 @@ class ApiCompaniesService {
1695
1695
  return this.http.delete(`${this.url}/employees/${id}`)
1696
1696
  .pipe(map(({ data }) => data));
1697
1697
  }
1698
+ /**
1699
+ * Exports the filtered employees list as a PDF file.
1700
+ *
1701
+ * @param {QueryParams} params - Query parameters used to filter the employees included in the export.
1702
+ * @return {Observable<HttpResponse<ArrayBuffer>>} An observable that emits the HTTP response containing the PDF as an ArrayBuffer.
1703
+ */
1704
+ getEmployeesExportPdf(params) {
1705
+ return this.http.get(`${this.url}/employees/exportPdf`, {
1706
+ params,
1707
+ observe: 'response',
1708
+ responseType: 'arraybuffer'
1709
+ });
1710
+ }
1698
1711
  /**
1699
1712
  * Retrieves the list of employees for a specified location based on provided query parameters.
1700
1713
  *
@@ -5833,23 +5846,40 @@ function apiTokenInterceptor(req, next) {
5833
5846
  return next(req);
5834
5847
  }
5835
5848
 
5836
- const DEFAULT_TTL = 10000; // ttl in ms
5837
5849
  const cache = new Map();
5838
5850
  const inFlightRequests = new Map();
5839
5851
  /**
5840
- * Interceptor function to handle HTTP caching for GET requests. It retrieves cached responses
5841
- * if available and valid; otherwise, it processes the request and caches the response for future use.
5852
+ * Resolves the TTL for a URL from the first matching `cacheRoutes` entry.
5853
+ * Returns `null` when there is no match or no routes configured.
5854
+ */
5855
+ function resolveCacheTtl(url, cacheRoutes) {
5856
+ if (!cacheRoutes?.length)
5857
+ return null;
5858
+ for (const route of cacheRoutes) {
5859
+ if (new RegExp(route.pattern).test(url)) {
5860
+ return route.ttl;
5861
+ }
5862
+ }
5863
+ return null;
5864
+ }
5865
+ /**
5866
+ * Interceptor function to handle opt-in HTTP caching for GET requests that match
5867
+ * a configured `cacheRoutes` pattern. Non-matching GETs pass through uncached.
5868
+ * Concurrent in-flight requests for the same key are deduplicated via `shareReplay`.
5842
5869
  *
5843
- * @param {HttpRequest<any>} req - The HTTP request object being intercepted.
5870
+ * @param {HttpRequest<unknown>} req - The HTTP request object being intercepted.
5844
5871
  * @param {HttpHandlerFn} next - The next HTTP handler function in the chain to process the request.
5845
- * @return {Observable<HttpEvent<any>>} An observable that emits the HTTP event, either from cache
5872
+ * @return {Observable<HttpEvent<unknown>>} An observable that emits the HTTP event, either from cache
5846
5873
  * or by invoking the next handler.
5847
5874
  */
5848
5875
  function httpCachingInterceptor(req, next) {
5849
- const { cacheTtl } = inject(ENVIRONMENT_TOKEN);
5876
+ const { cacheRoutes } = inject(ENVIRONMENT_TOKEN);
5850
5877
  if (req.method !== 'GET')
5851
5878
  return next(req);
5852
5879
  const key = req.urlWithParams;
5880
+ const routeTtl = resolveCacheTtl(key, cacheRoutes);
5881
+ if (routeTtl === null)
5882
+ return next(req);
5853
5883
  const cached = cache.get(key);
5854
5884
  if (cached) {
5855
5885
  const isExpired = Date.now() > cached.ttl;
@@ -5866,7 +5896,7 @@ function httpCachingInterceptor(req, next) {
5866
5896
  if (!(res instanceof HttpResponse)) {
5867
5897
  return;
5868
5898
  }
5869
- const ttl = Date.now() + (cacheTtl ?? DEFAULT_TTL);
5899
+ const ttl = Date.now() + routeTtl;
5870
5900
  cache.set(key, { res, ttl });
5871
5901
  }), finalize(() => {
5872
5902
  inFlightRequests.delete(key);