@mmstack/resource 19.6.2 → 19.6.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/lib/options.d.ts CHANGED
@@ -1,5 +1,59 @@
1
1
  import { InjectionToken, type Injector, type Provider, type ResourceRef } from '@angular/core';
2
2
  import { type CircuitBreakerOptions, type RetryOptions } from './util';
3
+ import { type HttpResourceRequest } from '@angular/common/http';
4
+ /**
5
+ * Options for enabling and configuring caching for a resource.
6
+ *
7
+ * - `true`: Enables caching with default settings.
8
+ * - `{ ttl?: number; staleTime?: number; hash?: (req: HttpResourceRequest) => string; }`: Configures caching with custom settings.
9
+ */
10
+ export type ResourceCacheOptions = true | {
11
+ /**
12
+ * The time-to-live for the cached value in milliseconds.
13
+ * After this time, the value is removed from the cache entirely.
14
+ * Defaults to 5 minutes (`300_000`).
15
+ */
16
+ ttl?: number;
17
+ /**
18
+ * The time in milliseconds during which the cached value is considered "fresh".
19
+ * If a request is made within this time, the cached value is returned immediately without a background fetch.
20
+ * Defaults to 0 (always stale, triggering a background fetch).
21
+ */
22
+ staleTime?: number;
23
+ /**
24
+ * A custom function to generate the cache key from the HTTP request.
25
+ * By default, it hashes the URL, method, headers (specified by `varyHeaders`), and body.
26
+ */
27
+ hash?: (req: HttpResourceRequest) => string;
28
+ /**
29
+ * A list of header names to include in the default cache key generation.
30
+ * Ignored if a custom `hash` function is provided.
31
+ *
32
+ * Note: still call `cache.clear()` on logout — the previous user's entries are
33
+ * unreachable under the new key but linger until their TTL.
34
+ */
35
+ varyHeaders?: string[];
36
+ /**
37
+ * Whether to bust the browser cache by appending a unique query parameter to the request URL.
38
+ * This is useful for ensuring that the latest data is fetched from the server, bypassing any
39
+ * cached responses in the browser. The unique parameter is removed before calling the cache function, so it does not affect the cache key.
40
+ * @default false - By default, the resource will not bust the browser cache.
41
+ */
42
+ bustBrowserCache?: boolean;
43
+ /**
44
+ * Whether to ignore the `Cache-Control` headers from the server when caching responses.
45
+ * If set to `true`, the resource will not respect any cache directives from the server,
46
+ * allowing you to control caching behavior entirely through the resource options.
47
+ * @default false - By default the resource will respect `Cache-Control` headers.
48
+ */
49
+ ignoreCacheControl?: boolean;
50
+ /**
51
+ * If true, it saves the cached responses to an indexedDb table, making it available across
52
+ * tabs, sessions and reloads..only valid JSON responses can be persisted (so no Blobs, formData, ArrayBuffers etc.)
53
+ * @default false
54
+ */
55
+ persist?: boolean;
56
+ };
3
57
  /**
4
58
  * Auto-registration into the nearest transition scope, as a resource OPTION:
5
59
  * - `'suspend'` — register as *suspending*: the boundary holds its placeholder until this
@@ -1,62 +1,9 @@
1
1
  import { type HttpHeaders, type HttpResourceOptions, type HttpResourceRef, type HttpResourceRequest } from '@angular/common/http';
2
2
  import { type Provider, type Signal, type WritableSignal } from '@angular/core';
3
3
  import { type PauseOption } from '@mmstack/primitives';
4
- import { type CommonResourceOptions } from './options';
4
+ import { type CommonResourceOptions, type ResourceCacheOptions } from './options';
5
5
  import { type RefreshOptions } from './util';
6
6
  export { type RefreshOptions } from './util';
7
- /**
8
- * Options for configuring caching behavior of a `queryResource`.
9
- * - `true`: Enables caching with default settings.
10
- * - `{ ttl?: number; staleTime?: number; hash?: (req: HttpResourceRequest) => string; }`: Configures caching with custom settings.
11
- */
12
- type ResourceCacheOptions = true | {
13
- /**
14
- * The Time To Live (TTL) for the cached data, in milliseconds. After this time, the cached data is
15
- * considered expired and will be refetched.
16
- */
17
- ttl?: number;
18
- /**
19
- * The duration, in milliseconds, during which stale data can be served while a revalidation request
20
- * is made in the background.
21
- */
22
- staleTime?: number;
23
- /**
24
- * A custom function to generate the cache key. Defaults to using the request URL with parameters.
25
- * Provide a custom hash function if you need more control over how cache keys are generated,
26
- * for instance, to ignore certain query parameters or to use request body for the cache key.
27
- */
28
- hash?: (req: HttpResourceRequest) => string;
29
- /**
30
- * Whether to bust the browser cache by appending a unique query parameter to the request URL.
31
- * This is useful for ensuring that the latest data is fetched from the server, bypassing any
32
- * cached responses in the browser. The unique parameter is removed before calling the cache function, so it does not affect the cache key.
33
- * @default false - By default, the resource will not bust the browser cache.
34
- */
35
- bustBrowserCache?: boolean;
36
- /**
37
- * Whether to ignore the `Cache-Control` headers from the server when caching responses.
38
- * If set to `true`, the resource will not respect any cache directives from the server,
39
- * allowing you to control caching behavior entirely through the resource options.
40
- * @default false - By default the resource will respect `Cache-Control` headers.
41
- */
42
- ignoreCacheControl?: boolean;
43
- /**
44
- * Whether to persist the cache entry in the local DB instance.
45
- * @default false - By default, the cache entry is not persisted.
46
- */
47
- persist?: boolean;
48
- /**
49
- * Request headers whose values should partition the cache key — e.g.
50
- * `['Authorization']` gives each user their own entries, `['Accept-Language']`
51
- * separates per-language responses. Header values are one-way digested into the
52
- * key (never embedded raw), so secrets don't end up in persisted/broadcast keys.
53
- * Ignored when a custom `hash` function is provided (it owns the key entirely).
54
- *
55
- * Note: still call `cache.clear()` on logout — the previous user's entries are
56
- * unreachable under the new key but linger until their TTL.
57
- */
58
- varyHeaders?: string[];
59
- };
60
7
  /**
61
8
  * Options for configuring a `queryResource`. Extends Angular's
62
9
  * `HttpResourceOptions` with caching, retries, refresh intervals, circuit
@@ -5,6 +5,7 @@ export * from './dedupe-interceptor';
5
5
  export * from './equality';
6
6
  export * from './has-slow-connection';
7
7
  export * from './hash-request';
8
+ export * from './merge-options';
8
9
  export * from './persist';
9
10
  export * from './refresh';
10
11
  export * from './retry-on-error';
@@ -0,0 +1,24 @@
1
+ import type { CircuitBreakerOptions } from './circuit-breaker';
2
+ import type { RefreshOptions } from './refresh';
3
+ import type { RetryOptions } from './retry-on-error';
4
+ import type { ResourceCacheOptions } from '../options';
5
+ /**
6
+ * Deep merges multiple circuit breaker options.
7
+ * The latter options override the former.
8
+ */
9
+ export declare function mergeCircuitBreakerOptions(global?: CircuitBreakerOptions | true, query?: CircuitBreakerOptions | true, local?: CircuitBreakerOptions | true): CircuitBreakerOptions | true | undefined;
10
+ /**
11
+ * Deep merges multiple retry options.
12
+ * The latter options override the former.
13
+ */
14
+ export declare function mergeRetryOptions(global?: RetryOptions | number, query?: RetryOptions | number, local?: RetryOptions | number): RetryOptions | number | undefined;
15
+ /**
16
+ * Deep merges multiple cache options.
17
+ * The latter options override the former.
18
+ */
19
+ export declare function mergeCacheOptions(query?: ResourceCacheOptions, local?: ResourceCacheOptions): ResourceCacheOptions | undefined;
20
+ /**
21
+ * Deep merges multiple refresh options.
22
+ * The latter options override the former.
23
+ */
24
+ export declare function mergeRefreshOptions(query?: RefreshOptions | number, local?: RefreshOptions | number): RefreshOptions | number | undefined;
@@ -1,3 +1,4 @@
1
1
  export * from './cache/public_api';
2
2
  export { createCircuitBreaker, provideCircuitBreakerDefaultOptions, } from './circuit-breaker';
3
3
  export { createDedupeRequestsInterceptor, noDedupe, } from './dedupe-interceptor';
4
+ export { hashRequest } from './hash-request';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mmstack/resource",
3
- "version": "19.6.2",
3
+ "version": "19.6.4",
4
4
  "keywords": [
5
5
  "angular",
6
6
  "signals",