@mmstack/resource 20.2.6 → 20.2.7
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 +44 -31
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/index.d.ts +578 -1
- package/package.json +4 -4
- package/lib/mutation-resource.d.ts +0 -88
- package/lib/public_api.d.ts +0 -3
- package/lib/query-resource.d.ts +0 -119
- package/lib/util/cache/cache.d.ts +0 -188
- package/lib/util/cache/cache.interceptor.d.ts +0 -42
- package/lib/util/cache/index.d.ts +0 -2
- package/lib/util/cache/public_api.d.ts +0 -2
- package/lib/util/catch-value-error.d.ts +0 -2
- package/lib/util/circuit-breaker.d.ts +0 -103
- package/lib/util/dedupe.interceptor.d.ts +0 -50
- package/lib/util/equality.d.ts +0 -3
- package/lib/util/has-slow-connection.d.ts +0 -1
- package/lib/util/index.d.ts +0 -11
- package/lib/util/persist.d.ts +0 -3
- package/lib/util/public_api.d.ts +0 -3
- package/lib/util/refresh.d.ts +0 -3
- package/lib/util/retry-on-error.d.ts +0 -6
- package/lib/util/to-resource-object.d.ts +0 -2
- package/lib/util/url-with-params.d.ts +0 -2
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { type HttpResourceRequest } from '@angular/common/http';
|
|
2
|
-
import { Signal, ValueEqualityFn } from '@angular/core';
|
|
3
|
-
import { type QueryResourceOptions, type QueryResourceRef } from './query-resource';
|
|
4
|
-
/**
|
|
5
|
-
* @internal
|
|
6
|
-
* Helper type for inferring the request body type based on the HTTP method.
|
|
7
|
-
*/
|
|
8
|
-
type NextRequest<TMethod extends HttpResourceRequest['method'], TMutation> = TMethod extends 'DELETE' | 'delete' ? Omit<HttpResourceRequest, 'body' | 'method'> & {
|
|
9
|
-
method: TMethod;
|
|
10
|
-
} : Omit<HttpResourceRequest, 'body' | 'method'> & {
|
|
11
|
-
body: TMutation;
|
|
12
|
-
method: TMethod;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Options for configuring a `mutationResource`.
|
|
16
|
-
*
|
|
17
|
-
* @typeParam TResult - The type of the expected result from the mutation.
|
|
18
|
-
* @typeParam TRaw - The raw response type from the HTTP request (defaults to TResult).
|
|
19
|
-
* @typeParam TCTX - The type of the context value returned by `onMutate`.
|
|
20
|
-
*/
|
|
21
|
-
export type MutationResourceOptions<TResult, TRaw = TResult, TMutation = TResult, TCTX = void, TICTX = TCTX> = Omit<QueryResourceOptions<TResult, TRaw>, 'equal' | 'onError' | 'keepPrevious' | 'refresh' | 'cache'> & {
|
|
22
|
-
/**
|
|
23
|
-
* A callback function that is called before the mutation request is made.
|
|
24
|
-
* @param value The value being mutated (the `body` of the request).
|
|
25
|
-
* @returns An optional context value that will be passed to the `onError`, `onSuccess`, and `onSettled` callbacks. This is useful for storing
|
|
26
|
-
* information needed during the mutation lifecycle, such as previous values for optimistic updates or rollback.
|
|
27
|
-
*/
|
|
28
|
-
onMutate?: (value: TMutation, initialCTX?: TICTX) => TCTX;
|
|
29
|
-
/**
|
|
30
|
-
* A callback function that is called if the mutation request fails.
|
|
31
|
-
* @param error The error that occurred.
|
|
32
|
-
* @param ctx The context value returned by the `onMutate` callback (or `undefined` if `onMutate` was not provided or returned `undefined`).
|
|
33
|
-
*/
|
|
34
|
-
onError?: (error: unknown, ctx: NoInfer<TCTX>) => void;
|
|
35
|
-
/**
|
|
36
|
-
* A callback function that is called if the mutation request succeeds.
|
|
37
|
-
* @param value The result of the mutation (the parsed response body).
|
|
38
|
-
* @param ctx The context value returned by the `onMutate` callback (or `undefined` if `onMutate` was not provided or returned `undefined`).
|
|
39
|
-
*/
|
|
40
|
-
onSuccess?: (value: NoInfer<TResult>, ctx: NoInfer<TCTX>) => void;
|
|
41
|
-
/**
|
|
42
|
-
* A callback function that is called when the mutation request settles (either succeeds or fails).
|
|
43
|
-
* @param ctx The context value returned by the `onMutate` callback (or `undefined` if `onMutate` was not provided or returned `undefined`).
|
|
44
|
-
*/
|
|
45
|
-
onSettled?: (ctx: NoInfer<TCTX>) => void;
|
|
46
|
-
equal?: ValueEqualityFn<TMutation>;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Represents a mutation resource created by `mutationResource`. Extends `QueryResourceRef`
|
|
50
|
-
* but removes methods that don't make sense for mutations (like `prefetch`, `value`, etc.).
|
|
51
|
-
*
|
|
52
|
-
* @typeParam TResult - The type of the expected result from the mutation.
|
|
53
|
-
*/
|
|
54
|
-
export type MutationResourceRef<TResult, TMutation = TResult, TICTX = void> = Omit<QueryResourceRef<TResult>, 'prefetch' | 'value' | 'hasValue' | 'set' | 'update'> & {
|
|
55
|
-
/**
|
|
56
|
-
* Executes the mutation.
|
|
57
|
-
*
|
|
58
|
-
* @param value The mutation value (usually the request body).
|
|
59
|
-
* @param ctx An optional initial context value that will be passed to the `onMutate` callback.
|
|
60
|
-
*/
|
|
61
|
-
mutate: (value: TMutation, ctx?: TICTX) => void;
|
|
62
|
-
/**
|
|
63
|
-
* A signal that holds the current mutation request, or `null` if no mutation is in progress.
|
|
64
|
-
* This can be useful for tracking the state of the mutation or for displaying loading indicators.
|
|
65
|
-
*/
|
|
66
|
-
current: Signal<TMutation | null>;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* Creates a resource for performing mutations (e.g., POST, PUT, PATCH, DELETE requests).
|
|
70
|
-
* Unlike `queryResource`, `mutationResource` is designed for one-off operations that change data.
|
|
71
|
-
* It does *not* cache responses and does not provide a `value` signal. Instead, it focuses on
|
|
72
|
-
* managing the mutation lifecycle (pending, error, success) and provides callbacks for handling
|
|
73
|
-
* these states.
|
|
74
|
-
*
|
|
75
|
-
* @param request A function that returns the base `HttpResourceRequest` to be made. This function is called reactively. The parameter is the mutation value provided by the `mutate` method.
|
|
76
|
-
* @param options Configuration options for the mutation resource. This includes callbacks
|
|
77
|
-
* for `onMutate`, `onError`, `onSuccess`, and `onSettled`.
|
|
78
|
-
* @typeParam TResult - The type of the expected result from the mutation.
|
|
79
|
-
* @typeParam TRaw - The raw response type from the HTTP request (defaults to TResult).
|
|
80
|
-
* @typeParam TMutation - The type of the mutation value (the request body).
|
|
81
|
-
* @typeParam TICTX - The type of the initial context value passed to `onMutate`.
|
|
82
|
-
* @typeParam TCTX - The type of the context value returned by `onMutate`.
|
|
83
|
-
* @typeParam TMethod - The HTTP method to be used for the mutation (defaults to `HttpResourceRequest['method']`).
|
|
84
|
-
* @returns A `MutationResourceRef` instance, which provides methods for triggering the mutation
|
|
85
|
-
* and observing its status.
|
|
86
|
-
*/
|
|
87
|
-
export declare function mutationResource<TResult, TRaw = TResult, TMutation = TResult, TCTX = void, TICTX = TCTX, TMethod extends HttpResourceRequest['method'] = HttpResourceRequest['method']>(request: (params: TMutation) => Omit<NextRequest<TMethod, TMutation>, 'body'> | undefined | void, options?: MutationResourceOptions<TResult, TRaw, TMutation, TCTX, TICTX>): MutationResourceRef<TResult, TMutation, TICTX>;
|
|
88
|
-
export {};
|
package/lib/public_api.d.ts
DELETED
package/lib/query-resource.d.ts
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { HttpHeaders, type HttpResourceOptions, type HttpResourceRef, type HttpResourceRequest } from '@angular/common/http';
|
|
2
|
-
import { Signal, WritableSignal } from '@angular/core';
|
|
3
|
-
import { CircuitBreakerOptions, type RetryOptions } from './util';
|
|
4
|
-
/**
|
|
5
|
-
* Options for configuring caching behavior of a `queryResource`.
|
|
6
|
-
* - `true`: Enables caching with default settings.
|
|
7
|
-
* - `{ ttl?: number; staleTime?: number; hash?: (req: HttpResourceRequest) => string; }`: Configures caching with custom settings.
|
|
8
|
-
*/
|
|
9
|
-
type ResourceCacheOptions = true | {
|
|
10
|
-
/**
|
|
11
|
-
* The Time To Live (TTL) for the cached data, in milliseconds. After this time, the cached data is
|
|
12
|
-
* considered expired and will be refetched.
|
|
13
|
-
*/
|
|
14
|
-
ttl?: number;
|
|
15
|
-
/**
|
|
16
|
-
* The duration, in milliseconds, during which stale data can be served while a revalidation request
|
|
17
|
-
* is made in the background.
|
|
18
|
-
*/
|
|
19
|
-
staleTime?: number;
|
|
20
|
-
/**
|
|
21
|
-
* A custom function to generate the cache key. Defaults to using the request URL with parameters.
|
|
22
|
-
* Provide a custom hash function if you need more control over how cache keys are generated,
|
|
23
|
-
* for instance, to ignore certain query parameters or to use request body for the cache key.
|
|
24
|
-
*/
|
|
25
|
-
hash?: (req: HttpResourceRequest) => string;
|
|
26
|
-
/**
|
|
27
|
-
* Whether to bust the browser cache by appending a unique query parameter to the request URL.
|
|
28
|
-
* This is useful for ensuring that the latest data is fetched from the server, bypassing any
|
|
29
|
-
* cached responses in the browser. The unique parameter is removed before calling the cache function, so it does not affect the cache key.
|
|
30
|
-
* @default false - By default, the resource will not bust the browser cache.
|
|
31
|
-
*/
|
|
32
|
-
bustBrowserCache?: boolean;
|
|
33
|
-
/**
|
|
34
|
-
* Whether to ignore the `Cache-Control` headers from the server when caching responses.
|
|
35
|
-
* If set to `true`, the resource will not respect any cache directives from the server,
|
|
36
|
-
* allowing you to control caching behavior entirely through the resource options.
|
|
37
|
-
* @default false - By default the resource will respect `Cache-Control` headers.
|
|
38
|
-
*/
|
|
39
|
-
ignoreCacheControl?: boolean;
|
|
40
|
-
};
|
|
41
|
-
/**
|
|
42
|
-
* Options for configuring a `queryResource`.
|
|
43
|
-
*/
|
|
44
|
-
export type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult, TRaw> & {
|
|
45
|
-
/**
|
|
46
|
-
* Whether to keep the previous value of the resource while a refresh is in progress.
|
|
47
|
-
* Defaults to `false`. Also keeps status & headers while refreshing.
|
|
48
|
-
*/
|
|
49
|
-
keepPrevious?: boolean;
|
|
50
|
-
/**
|
|
51
|
-
* The refresh interval, in milliseconds. If provided, the resource will automatically
|
|
52
|
-
* refresh its data at the specified interval.
|
|
53
|
-
*/
|
|
54
|
-
refresh?: number;
|
|
55
|
-
/**
|
|
56
|
-
* Options for retrying failed requests.
|
|
57
|
-
*/
|
|
58
|
-
retry?: RetryOptions;
|
|
59
|
-
/**
|
|
60
|
-
* An optional error handler callback. This function will be called whenever the
|
|
61
|
-
* underlying HTTP request fails. Useful for displaying toasts or other error messages.
|
|
62
|
-
*/
|
|
63
|
-
onError?: (err: unknown) => void;
|
|
64
|
-
/**
|
|
65
|
-
* Options for configuring a circuit breaker for the resource.
|
|
66
|
-
*/
|
|
67
|
-
circuitBreaker?: CircuitBreakerOptions | true;
|
|
68
|
-
/**
|
|
69
|
-
* Options for enabling and configuring caching for the resource.
|
|
70
|
-
*/
|
|
71
|
-
cache?: ResourceCacheOptions;
|
|
72
|
-
/**
|
|
73
|
-
* Trigger a request every time the request function is triggered, even if the request parameters are the same.
|
|
74
|
-
* @default false
|
|
75
|
-
*/
|
|
76
|
-
triggerOnSameRequest?: boolean;
|
|
77
|
-
};
|
|
78
|
-
/**
|
|
79
|
-
* Represents a resource created by `queryResource`. Extends `HttpResourceRef` with additional properties.
|
|
80
|
-
*/
|
|
81
|
-
export type QueryResourceRef<TResult> = Omit<HttpResourceRef<TResult>, 'headers' | 'statusCode'> & {
|
|
82
|
-
/**
|
|
83
|
-
* Linkedsignal of the response headers, when available.
|
|
84
|
-
*/
|
|
85
|
-
readonly headers: WritableSignal<HttpHeaders | undefined>;
|
|
86
|
-
/**
|
|
87
|
-
* Linkedsignal of the response status code, when available.
|
|
88
|
-
*/
|
|
89
|
-
readonly statusCode: WritableSignal<number | undefined>;
|
|
90
|
-
/**
|
|
91
|
-
* A signal indicating whether the resource is currently disabled (due to circuit breaker or undefined request).
|
|
92
|
-
*/
|
|
93
|
-
disabled: Signal<boolean>;
|
|
94
|
-
/**
|
|
95
|
-
* Prefetches data for the resource, populating the cache if caching is enabled. This can be
|
|
96
|
-
* used to proactively load data before it's needed. If a slow connection is detected, prefetching is skipped.
|
|
97
|
-
*
|
|
98
|
-
* @param req - Optional partial request parameters to use for the prefetch. This allows you
|
|
99
|
-
* to prefetch data with different parameters than the main resource request.
|
|
100
|
-
*/
|
|
101
|
-
prefetch: (req?: Partial<HttpResourceRequest>) => Promise<void>;
|
|
102
|
-
};
|
|
103
|
-
export declare function queryResource<TResult, TRaw = TResult>(request: () => HttpResourceRequest | undefined | void, options: QueryResourceOptions<TResult, TRaw> & {
|
|
104
|
-
defaultValue: NoInfer<TResult>;
|
|
105
|
-
}): QueryResourceRef<TResult>;
|
|
106
|
-
/**
|
|
107
|
-
* Creates an extended HTTP resource with features like caching, retries, refresh intervals,
|
|
108
|
-
* circuit breaker, and optimistic updates. Without additional options it is equivalent to simply calling `httpResource`.
|
|
109
|
-
*
|
|
110
|
-
* @param request A function that returns the `HttpResourceRequest` to be made. This function
|
|
111
|
-
* is called reactively, so the request can change over time. If the function
|
|
112
|
-
* returns `undefined`, the resource is considered "disabled" and no request will be made.
|
|
113
|
-
* @param options Configuration options for the resource. These options extend the basic
|
|
114
|
-
* `HttpResourceOptions` and add features like `keepPrevious`, `refresh`, `retry`,
|
|
115
|
-
* `onError`, `circuitBreaker`, and `cache`.
|
|
116
|
-
* @returns An `QueryResourceRef` instance, which extends the basic `HttpResourceRef` with additional features.
|
|
117
|
-
*/
|
|
118
|
-
export declare function queryResource<TResult, TRaw = TResult>(request: () => HttpResourceRequest | undefined | void, options?: QueryResourceOptions<TResult, TRaw>): QueryResourceRef<TResult | undefined>;
|
|
119
|
-
export {};
|
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import type { HttpResponse } from '@angular/common/http';
|
|
2
|
-
import { Injector, type Provider, type Signal } from '@angular/core';
|
|
3
|
-
/**
|
|
4
|
-
* Options for configuring the Least Recently Used (LRU) cache cleanup strategy.
|
|
5
|
-
* @internal
|
|
6
|
-
*/
|
|
7
|
-
type LRUCleanupType = {
|
|
8
|
-
type: 'lru';
|
|
9
|
-
/**
|
|
10
|
-
* How often to check for expired or excess entries, in milliseconds.
|
|
11
|
-
*/
|
|
12
|
-
checkInterval: number;
|
|
13
|
-
/**
|
|
14
|
-
* The maximum number of entries to keep in the cache. When the cache exceeds this size,
|
|
15
|
-
* the least recently used entries will be removed.
|
|
16
|
-
*/
|
|
17
|
-
maxSize: number;
|
|
18
|
-
};
|
|
19
|
-
/**
|
|
20
|
-
* Options for configuring the "oldest first" cache cleanup strategy.
|
|
21
|
-
* @internal
|
|
22
|
-
*/
|
|
23
|
-
type OldsetCleanupType = {
|
|
24
|
-
type: 'oldest';
|
|
25
|
-
/**
|
|
26
|
-
* How often to check for expired or excess entries, in milliseconds.
|
|
27
|
-
*/
|
|
28
|
-
checkInterval: number;
|
|
29
|
-
/**
|
|
30
|
-
* The maximum number of entries to keep in the cache. When the cache exceeds this size,
|
|
31
|
-
* the oldest entries will be removed.
|
|
32
|
-
*/
|
|
33
|
-
maxSize: number;
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Represents an entry in the cache.
|
|
37
|
-
* @internal
|
|
38
|
-
*/
|
|
39
|
-
export type CacheEntry<T> = {
|
|
40
|
-
value: T;
|
|
41
|
-
created: number;
|
|
42
|
-
stale: number;
|
|
43
|
-
useCount: number;
|
|
44
|
-
expiresAt: number;
|
|
45
|
-
timeout: ReturnType<typeof setTimeout>;
|
|
46
|
-
key: string;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Defines the types of cleanup strategies available for the cache.
|
|
50
|
-
* - `lru`: Least Recently Used. Removes the least recently accessed entries when the cache is full.
|
|
51
|
-
* - `oldest`: Removes the oldest entries when the cache is full.
|
|
52
|
-
*/
|
|
53
|
-
export type CleanupType = LRUCleanupType | OldsetCleanupType;
|
|
54
|
-
/**
|
|
55
|
-
* A generic cache implementation that stores data with time-to-live (TTL) and stale-while-revalidate capabilities.
|
|
56
|
-
*
|
|
57
|
-
* @typeParam T - The type of data to be stored in the cache.
|
|
58
|
-
*/
|
|
59
|
-
export declare class Cache<T> {
|
|
60
|
-
protected readonly ttl: number;
|
|
61
|
-
protected readonly staleTime: number;
|
|
62
|
-
private readonly internal;
|
|
63
|
-
private readonly cleanupOpt;
|
|
64
|
-
/**
|
|
65
|
-
* Creates a new `Cache` instance.
|
|
66
|
-
*
|
|
67
|
-
* @param ttl - The default Time To Live (TTL) for cache entries, in milliseconds. Defaults to one day.
|
|
68
|
-
* @param staleTime - The default duration, in milliseconds, during which a cache entry is considered
|
|
69
|
-
* stale but can still be used while revalidation occurs in the background. Defaults to 1 hour.
|
|
70
|
-
* @param cleanupOpt - Options for configuring the cache cleanup strategy. Defaults to LRU with a
|
|
71
|
-
* `maxSize` of 200 and a `checkInterval` of one hour.
|
|
72
|
-
*/
|
|
73
|
-
constructor(ttl?: number, staleTime?: number, cleanupOpt?: Partial<CleanupType>);
|
|
74
|
-
/** @internal */
|
|
75
|
-
private getInternal;
|
|
76
|
-
/**
|
|
77
|
-
* Retrieves a cache entry without affecting its usage count (for LRU). This is primarily
|
|
78
|
-
* for internal use or debugging.
|
|
79
|
-
* @internal
|
|
80
|
-
* @param key - The key of the entry to retrieve.
|
|
81
|
-
* @returns The cache entry, or `null` if not found or expired.
|
|
82
|
-
*/
|
|
83
|
-
getUntracked(key: string): (CacheEntry<T> & {
|
|
84
|
-
isStale: boolean;
|
|
85
|
-
}) | null;
|
|
86
|
-
/**
|
|
87
|
-
* Retrieves a cache entry as a signal.
|
|
88
|
-
*
|
|
89
|
-
* @param key - A function that returns the cache key. The key is a signal, allowing for dynamic keys. If the function returns null the value is also null.
|
|
90
|
-
* @returns A signal that holds the cache entry, or `null` if not found or expired. The signal
|
|
91
|
-
* updates whenever the cache entry changes (e.g., due to revalidation or expiration).
|
|
92
|
-
*/
|
|
93
|
-
get(key: () => string | null): Signal<(CacheEntry<T> & {
|
|
94
|
-
isStale: boolean;
|
|
95
|
-
}) | null>;
|
|
96
|
-
/**
|
|
97
|
-
* Retrieves a cache entry or an object with the key if not found.
|
|
98
|
-
*
|
|
99
|
-
* @param key - A function that returns the cache key. The key is a signal, allowing for dynamic keys. If the function returns null the value is also null.
|
|
100
|
-
* @returns A signal that holds the cache entry or an object with the key if not found. The signal
|
|
101
|
-
* updates whenever the cache entry changes (e.g., due to revalidation or expiration).
|
|
102
|
-
*/
|
|
103
|
-
getEntryOrKey(key: () => string | null): Signal<(CacheEntry<T> & {
|
|
104
|
-
isStale: boolean;
|
|
105
|
-
}) | string | null>;
|
|
106
|
-
/**
|
|
107
|
-
* Stores a value in the cache.
|
|
108
|
-
*
|
|
109
|
-
* @param key - The key under which to store the value.
|
|
110
|
-
* @param value - The value to store.
|
|
111
|
-
* @param staleTime - (Optional) The stale time for this entry, in milliseconds. Overrides the default `staleTime`.
|
|
112
|
-
* @param ttl - (Optional) The TTL for this entry, in milliseconds. Overrides the default `ttl`.
|
|
113
|
-
*/
|
|
114
|
-
store(key: string, value: T, staleTime?: number, ttl?: number): void;
|
|
115
|
-
/**
|
|
116
|
-
* Invalidates (removes) a cache entry.
|
|
117
|
-
*
|
|
118
|
-
* @param key - The key of the entry to invalidate.
|
|
119
|
-
*/
|
|
120
|
-
invalidate(key: string): void;
|
|
121
|
-
/** @internal */
|
|
122
|
-
private cleanup;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Options for configuring the cache.
|
|
126
|
-
*/
|
|
127
|
-
type CacheOptions = {
|
|
128
|
-
/**
|
|
129
|
-
* The default Time To Live (TTL) for cache entries, in milliseconds.
|
|
130
|
-
*/
|
|
131
|
-
ttl?: number;
|
|
132
|
-
/**
|
|
133
|
-
* The default duration, in milliseconds, during which a cache entry is considered
|
|
134
|
-
* stale but can still be used while revalidation occurs in the background.
|
|
135
|
-
*/
|
|
136
|
-
staleTime?: number;
|
|
137
|
-
/**
|
|
138
|
-
* Options for configuring the cache cleanup strategy.
|
|
139
|
-
*/
|
|
140
|
-
cleanup?: Partial<CleanupType>;
|
|
141
|
-
};
|
|
142
|
-
/**
|
|
143
|
-
* Provides the instance of the QueryCache for queryResource. This should probably be called
|
|
144
|
-
* in your application's root configuration, but can also be overriden with component/module providers.
|
|
145
|
-
*
|
|
146
|
-
* @param options - Optional configuration options for the cache.
|
|
147
|
-
* @returns An Angular `Provider` for the cache.
|
|
148
|
-
*
|
|
149
|
-
* @example
|
|
150
|
-
* // In your app.config.ts or AppModule providers:
|
|
151
|
-
*
|
|
152
|
-
* import { provideQueryCache } from './your-cache';
|
|
153
|
-
*
|
|
154
|
-
* export const appConfig: ApplicationConfig = {
|
|
155
|
-
* providers: [
|
|
156
|
-
* provideQueryCache({
|
|
157
|
-
* ttl: 60000, // Default TTL of 60 seconds
|
|
158
|
-
* staleTime: 30000, // Default staleTime of 30 seconds
|
|
159
|
-
* }),
|
|
160
|
-
* // ... other providers
|
|
161
|
-
* ]
|
|
162
|
-
* };
|
|
163
|
-
*/
|
|
164
|
-
export declare function provideQueryCache(opt?: CacheOptions): Provider;
|
|
165
|
-
/**
|
|
166
|
-
* Injects the `QueryCache` instance that is used within queryResource.
|
|
167
|
-
* Allows for direct modification of cached data, but is mostly meant for internal use.
|
|
168
|
-
*
|
|
169
|
-
* @param injector - (Optional) The injector to use. If not provided, the current
|
|
170
|
-
* injection context is used.
|
|
171
|
-
* @returns The `QueryCache` instance.
|
|
172
|
-
*
|
|
173
|
-
* @example
|
|
174
|
-
* // In your component or service:
|
|
175
|
-
*
|
|
176
|
-
* import { injectQueryCache } from './your-cache';
|
|
177
|
-
*
|
|
178
|
-
* constructor() {
|
|
179
|
-
* const cache = injectQueryCache();
|
|
180
|
-
*
|
|
181
|
-
* const myData = cache.get(() => 'my-data-key');
|
|
182
|
-
* if (myData() !== null) {
|
|
183
|
-
* // ... use cached data ...
|
|
184
|
-
* }
|
|
185
|
-
* }
|
|
186
|
-
*/
|
|
187
|
-
export declare function injectQueryCache<TRaw = unknown>(injector?: Injector): Cache<HttpResponse<TRaw>>;
|
|
188
|
-
export {};
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { HttpContext, type HttpInterceptorFn } from '@angular/common/http';
|
|
2
|
-
type CacheEntryOptions = {
|
|
3
|
-
key?: string;
|
|
4
|
-
ttl?: number;
|
|
5
|
-
staleTime?: number;
|
|
6
|
-
cache: boolean;
|
|
7
|
-
bustBrowserCache?: boolean;
|
|
8
|
-
ignoreCacheControl?: boolean;
|
|
9
|
-
parse?: (val: unknown) => unknown;
|
|
10
|
-
};
|
|
11
|
-
export declare function setCacheContext(ctx: HttpContext | undefined, opt: Omit<CacheEntryOptions, 'cache' | 'key'> & {
|
|
12
|
-
key: Required<CacheEntryOptions>['key'];
|
|
13
|
-
}): HttpContext;
|
|
14
|
-
/**
|
|
15
|
-
* Creates an `HttpInterceptorFn` that implements caching for HTTP requests. This interceptor
|
|
16
|
-
* checks for a caching configuration in the request's `HttpContext` (internally set by the queryResource).
|
|
17
|
-
* If caching is enabled, it attempts to retrieve responses from the cache. If a cached response
|
|
18
|
-
* is found and is not stale, it's returned directly. If the cached response is stale, it's returned,
|
|
19
|
-
* and a background revalidation request is made. If no cached response is found, the request
|
|
20
|
-
* is made to the server, and the response is cached according to the configured TTL and staleness.
|
|
21
|
-
* The interceptor also respects `Cache-Control` headers from the server.
|
|
22
|
-
*
|
|
23
|
-
* @param allowedMethods - An array of HTTP methods for which caching should be enabled.
|
|
24
|
-
* Defaults to `['GET', 'HEAD', 'OPTIONS']`.
|
|
25
|
-
*
|
|
26
|
-
* @returns An `HttpInterceptorFn` that implements the caching logic.
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* // In your app.config.ts or module providers:
|
|
30
|
-
*
|
|
31
|
-
* import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
32
|
-
* import { createCacheInterceptor } from '@mmstack/resource';
|
|
33
|
-
*
|
|
34
|
-
* export const appConfig: ApplicationConfig = {
|
|
35
|
-
* providers: [
|
|
36
|
-
* provideHttpClient(withInterceptors([createCacheInterceptor()])),
|
|
37
|
-
* // ... other providers
|
|
38
|
-
* ],
|
|
39
|
-
* };
|
|
40
|
-
*/
|
|
41
|
-
export declare function createCacheInterceptor(allowedMethods?: string[]): HttpInterceptorFn;
|
|
42
|
-
export {};
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { Injector, Provider, Signal } from '@angular/core';
|
|
2
|
-
/**
|
|
3
|
-
* Represents the possible states of a circuit breaker.
|
|
4
|
-
* - `CLOSED`: The circuit breaker is closed, and operations are allowed to proceed.
|
|
5
|
-
* - `OPEN`: The circuit breaker is open, and operations are blocked.
|
|
6
|
-
* - `HALF_OPEN`: The circuit breaker is in a half-open state, allowing a limited number of operations to test if the underlying issue is resolved.
|
|
7
|
-
*/
|
|
8
|
-
type CircuitBreakerState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
|
|
9
|
-
/**
|
|
10
|
-
* Represents a circuit breaker, which monitors operations and prevents failures from cascading.
|
|
11
|
-
*/
|
|
12
|
-
export type CircuitBreaker = {
|
|
13
|
-
/**
|
|
14
|
-
* A signal indicating whether the circuit breaker is currently closed (allowing operations).
|
|
15
|
-
*/
|
|
16
|
-
isClosed: Signal<boolean>;
|
|
17
|
-
/**
|
|
18
|
-
* A signal indicating whether the circuit breaker is either open or in a half-open state.
|
|
19
|
-
* This is useful for checking if operations are blocked.
|
|
20
|
-
* If the circuit breaker is open, operations should not proceed.
|
|
21
|
-
*/
|
|
22
|
-
isOpen: Signal<boolean>;
|
|
23
|
-
/**
|
|
24
|
-
* A signal representing the current state of the circuit breaker.
|
|
25
|
-
*/
|
|
26
|
-
status: Signal<CircuitBreakerState>;
|
|
27
|
-
/**
|
|
28
|
-
* Signals a failure to the circuit breaker. This may cause the circuit breaker to open.
|
|
29
|
-
*/
|
|
30
|
-
fail: (err?: Error) => void;
|
|
31
|
-
/**
|
|
32
|
-
* Signals a success to the circuit breaker. This may cause the circuit breaker to close.
|
|
33
|
-
*/
|
|
34
|
-
success: () => void;
|
|
35
|
-
/**
|
|
36
|
-
* Attempts to transition the circuit breaker to the half-open state. This is typically used
|
|
37
|
-
* to test if the underlying issue has been resolved after the circuit breaker has been open.
|
|
38
|
-
*/
|
|
39
|
-
halfOpen: () => void;
|
|
40
|
-
/**
|
|
41
|
-
* Destroys the circuit breaker & initiates related cleanup
|
|
42
|
-
*/
|
|
43
|
-
destroy: () => void;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Options for creating a circuit breaker.
|
|
47
|
-
*/
|
|
48
|
-
type CreateCircuitBreakerOptions = {
|
|
49
|
-
/**
|
|
50
|
-
* The number of failures that will cause the circuit breaker to open.
|
|
51
|
-
* @default 5
|
|
52
|
-
*/
|
|
53
|
-
treshold?: number;
|
|
54
|
-
/**
|
|
55
|
-
* The time in milliseconds after which the circuit breaker will reset and allow operations to proceed again.
|
|
56
|
-
* @default 30000 (30 seconds)
|
|
57
|
-
*/
|
|
58
|
-
timeout?: number;
|
|
59
|
-
/**
|
|
60
|
-
* A function that determines whether an error should cause the circuit breaker to increment the failure count.
|
|
61
|
-
* @default Always returns true
|
|
62
|
-
*/
|
|
63
|
-
shouldFail?: (err?: Error) => boolean;
|
|
64
|
-
/**
|
|
65
|
-
* A function that determines whether an error should cause the circuit breaker to be open forever.
|
|
66
|
-
* @default Always returns false
|
|
67
|
-
*/
|
|
68
|
-
shouldFailForever?: (err?: Error) => boolean;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Options for creating a circuit breaker.
|
|
72
|
-
* - `false`: Disables circuit breaker functionality (always open).
|
|
73
|
-
* - true: Creates a new circuit breaker with default options.
|
|
74
|
-
* - `CircuitBreaker`: Provides an existing `CircuitBreaker` instance to use.
|
|
75
|
-
* - `{ treshold?: number; timeout?: number; }`: Creates a new circuit breaker with the specified options.
|
|
76
|
-
*/
|
|
77
|
-
export type CircuitBreakerOptions = false | CircuitBreaker | CreateCircuitBreakerOptions;
|
|
78
|
-
export declare function provideCircuitBreakerDefaultOptions(options: CircuitBreakerOptions): Provider;
|
|
79
|
-
/**
|
|
80
|
-
* Creates a circuit breaker instance.
|
|
81
|
-
*
|
|
82
|
-
* @param options - Configuration options for the circuit breaker. Can be:
|
|
83
|
-
* - `undefined`: Creates a "no-op" circuit breaker that is always open (never trips).
|
|
84
|
-
* - `true`: Creates a circuit breaker with default settings (threshold: 5, timeout: 30000ms).
|
|
85
|
-
* - `CircuitBreaker`: Reuses an existing `CircuitBreaker` instance.
|
|
86
|
-
* - `{ threshold?: number; timeout?: number; }`: Creates a circuit breaker with the specified threshold and timeout.
|
|
87
|
-
*
|
|
88
|
-
* @returns A `CircuitBreaker` instance.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* // Create a circuit breaker with default settings:
|
|
92
|
-
* const breaker = createCircuitBreaker();
|
|
93
|
-
*
|
|
94
|
-
* // Create a circuit breaker with custom settings:
|
|
95
|
-
* const customBreaker = createCircuitBreaker({ threshold: 10, timeout: 60000 });
|
|
96
|
-
*
|
|
97
|
-
* // Share a single circuit breaker instance across multiple resources:
|
|
98
|
-
* const sharedBreaker = createCircuitBreaker();
|
|
99
|
-
* const resource1 = queryResource(..., { circuitBreaker: sharedBreaker });
|
|
100
|
-
* const resource2 = mutationResource(..., { circuitBreaker: sharedBreaker });
|
|
101
|
-
*/
|
|
102
|
-
export declare function createCircuitBreaker(opt?: CircuitBreakerOptions, injector?: Injector): CircuitBreaker;
|
|
103
|
-
export {};
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { HttpContext, HttpInterceptorFn } from '@angular/common/http';
|
|
2
|
-
/**
|
|
3
|
-
* Disables request deduplication for a specific HTTP request.
|
|
4
|
-
*
|
|
5
|
-
* @param ctx - The `HttpContext` to modify. If not provided, a new `HttpContext` is created.
|
|
6
|
-
* @returns The modified `HttpContext` with the `NO_DEDUPE` token set to `true`.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* // Disable deduplication for a specific POST request:
|
|
10
|
-
* const context = noDedupe();
|
|
11
|
-
* this.http.post('/api/data', payload, { context }).subscribe(...);
|
|
12
|
-
*
|
|
13
|
-
* // Disable deduplication, modifying an existing context:
|
|
14
|
-
* let context = new HttpContext();
|
|
15
|
-
* context = noDedupe(context);
|
|
16
|
-
* this.http.post('/api/data', payload, { context }).subscribe(...);
|
|
17
|
-
*/
|
|
18
|
-
export declare function noDedupe(ctx?: HttpContext): HttpContext;
|
|
19
|
-
/**
|
|
20
|
-
* Creates an `HttpInterceptorFn` that deduplicates identical HTTP requests.
|
|
21
|
-
* If multiple identical requests (same URL and parameters) are made concurrently,
|
|
22
|
-
* only the first request will be sent to the server. Subsequent requests will
|
|
23
|
-
* receive the response from the first request.
|
|
24
|
-
*
|
|
25
|
-
* @param allowed - An array of HTTP methods for which deduplication should be enabled.
|
|
26
|
-
* Defaults to `['GET', 'DELETE', 'HEAD', 'OPTIONS']`.
|
|
27
|
-
*
|
|
28
|
-
* @returns An `HttpInterceptorFn` that implements the request deduplication logic.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* // In your app.config.ts or module providers:
|
|
32
|
-
* import { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
33
|
-
* import { createDedupeRequestsInterceptor } from './your-dedupe-interceptor';
|
|
34
|
-
*
|
|
35
|
-
* export const appConfig: ApplicationConfig = {
|
|
36
|
-
* providers: [
|
|
37
|
-
* provideHttpClient(withInterceptors([createDedupeRequestsInterceptor()])),
|
|
38
|
-
* // ... other providers
|
|
39
|
-
* ],
|
|
40
|
-
* };
|
|
41
|
-
*
|
|
42
|
-
* // You can also specify which methods should be deduped
|
|
43
|
-
* export const appConfig: ApplicationConfig = {
|
|
44
|
-
* providers: [
|
|
45
|
-
* provideHttpClient(withInterceptors([createDedupeRequestsInterceptor(['GET'])])), // only dedupe GET calls
|
|
46
|
-
* // ... other providers
|
|
47
|
-
* ],
|
|
48
|
-
* };
|
|
49
|
-
*/
|
|
50
|
-
export declare function createDedupeRequestsInterceptor(allowed?: string[]): HttpInterceptorFn;
|
package/lib/util/equality.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { HttpResourceRequest } from '@angular/common/http';
|
|
2
|
-
import { type ValueEqualityFn } from '@angular/core';
|
|
3
|
-
export declare function createEqualRequest<TResult>(equalResult?: ValueEqualityFn<TResult>): (a: Partial<HttpResourceRequest> | undefined, b: Partial<HttpResourceRequest> | undefined) => boolean;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function hasSlowConnection(): boolean;
|
package/lib/util/index.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export * from './cache';
|
|
2
|
-
export * from './catch-value-error';
|
|
3
|
-
export * from './circuit-breaker';
|
|
4
|
-
export * from './dedupe.interceptor';
|
|
5
|
-
export * from './equality';
|
|
6
|
-
export * from './has-slow-connection';
|
|
7
|
-
export * from './persist';
|
|
8
|
-
export * from './refresh';
|
|
9
|
-
export * from './retry-on-error';
|
|
10
|
-
export * from './to-resource-object';
|
|
11
|
-
export * from './url-with-params';
|
package/lib/util/persist.d.ts
DELETED
package/lib/util/public_api.d.ts
DELETED
package/lib/util/refresh.d.ts
DELETED