@mmstack/resource 19.0.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/LICENSE +21 -0
- package/README.md +111 -0
- package/fesm2022/mmstack-resource.mjs +1049 -0
- package/fesm2022/mmstack-resource.mjs.map +1 -0
- package/index.d.ts +1 -0
- package/lib/mutation-resource.d.ts +80 -0
- package/lib/public_api.d.ts +3 -0
- package/lib/query-resource.d.ts +92 -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/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 +9 -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/url-with-params.d.ts +2 -0
- package/package.json +42 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { 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 representing the current state of the circuit breaker.
|
|
19
|
+
*/
|
|
20
|
+
status: Signal<CircuitBreakerState>;
|
|
21
|
+
/**
|
|
22
|
+
* Signals a failure to the circuit breaker. This may cause the circuit breaker to open.
|
|
23
|
+
*/
|
|
24
|
+
fail: () => void;
|
|
25
|
+
/**
|
|
26
|
+
* Signals a success to the circuit breaker. This may cause the circuit breaker to close.
|
|
27
|
+
*/
|
|
28
|
+
success: () => void;
|
|
29
|
+
/**
|
|
30
|
+
* Attempts to transition the circuit breaker to the half-open state. This is typically used
|
|
31
|
+
* to test if the underlying issue has been resolved after the circuit breaker has been open.
|
|
32
|
+
*/
|
|
33
|
+
halfOpen: () => void;
|
|
34
|
+
/**
|
|
35
|
+
* Destroys the circuit breaker & initiates related cleanup
|
|
36
|
+
*/
|
|
37
|
+
destroy: () => void;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Options for creating a circuit breaker.
|
|
41
|
+
* - `false`: Disables circuit breaker functionality (always open).
|
|
42
|
+
* - true: Creates a new circuit breaker with default options.
|
|
43
|
+
* - `CircuitBreaker`: Provides an existing `CircuitBreaker` instance to use.
|
|
44
|
+
* - `{ treshold?: number; timeout?: number; }`: Creates a new circuit breaker with the specified options.
|
|
45
|
+
*/
|
|
46
|
+
export type CircuitBreakerOptions = false | CircuitBreaker | {
|
|
47
|
+
treshold?: number;
|
|
48
|
+
timeout?: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Creates a circuit breaker instance.
|
|
52
|
+
*
|
|
53
|
+
* @param options - Configuration options for the circuit breaker. Can be:
|
|
54
|
+
* - `undefined`: Creates a "no-op" circuit breaker that is always open (never trips).
|
|
55
|
+
* - `true`: Creates a circuit breaker with default settings (threshold: 5, timeout: 30000ms).
|
|
56
|
+
* - `CircuitBreaker`: Reuses an existing `CircuitBreaker` instance.
|
|
57
|
+
* - `{ threshold?: number; timeout?: number; }`: Creates a circuit breaker with the specified threshold and timeout.
|
|
58
|
+
*
|
|
59
|
+
* @returns A `CircuitBreaker` instance.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Create a circuit breaker with default settings:
|
|
63
|
+
* const breaker = createCircuitBreaker();
|
|
64
|
+
*
|
|
65
|
+
* // Create a circuit breaker with custom settings:
|
|
66
|
+
* const customBreaker = createCircuitBreaker({ threshold: 10, timeout: 60000 });
|
|
67
|
+
*
|
|
68
|
+
* // Share a single circuit breaker instance across multiple resources:
|
|
69
|
+
* const sharedBreaker = createCircuitBreaker();
|
|
70
|
+
* const resource1 = queryResource(..., { circuitBreaker: sharedBreaker });
|
|
71
|
+
* const resource2 = mutationResource(..., { circuitBreaker: sharedBreaker });
|
|
72
|
+
*/
|
|
73
|
+
export declare function createCircuitBreaker(opt?: CircuitBreakerOptions): CircuitBreaker;
|
|
74
|
+
export {};
|
|
@@ -0,0 +1,50 @@
|
|
|
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;
|
|
@@ -0,0 +1,3 @@
|
|
|
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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function hasSlowConnection(): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './cache';
|
|
2
|
+
export * from './circuit-breaker';
|
|
3
|
+
export * from './dedupe.interceptor';
|
|
4
|
+
export * from './equality';
|
|
5
|
+
export * from './has-slow-connection';
|
|
6
|
+
export * from './persist';
|
|
7
|
+
export * from './refresh';
|
|
8
|
+
export * from './retry-on-error';
|
|
9
|
+
export * from './url-with-params';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type HttpResourceRef } from '@angular/common/http';
|
|
2
|
+
import { type Signal, type ValueEqualityFn } from '@angular/core';
|
|
3
|
+
export declare function persistResourceValues<T>(resource: HttpResourceRef<T>, hasCachedValue: Signal<boolean>, persist?: boolean, equal?: ValueEqualityFn<T>): HttpResourceRef<T>;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mmstack/resource",
|
|
3
|
+
"version": "19.0.0",
|
|
4
|
+
"keywords": [
|
|
5
|
+
"angular",
|
|
6
|
+
"signals",
|
|
7
|
+
"http",
|
|
8
|
+
"resource",
|
|
9
|
+
"cache",
|
|
10
|
+
"mutation",
|
|
11
|
+
"rxjs"
|
|
12
|
+
],
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/mihajm/mmstack.git"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/mihajm/mmstack/blob/master/packages/resource",
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@angular/common": "~19.2.3",
|
|
21
|
+
"@angular/core": "~19.2.3",
|
|
22
|
+
"rxjs": "~7.8.2",
|
|
23
|
+
"uuid": "~11.1.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@mmstack/primitives": "*",
|
|
27
|
+
"@mmstack/object": "*",
|
|
28
|
+
"tslib": "^2.3.0"
|
|
29
|
+
},
|
|
30
|
+
"exports": {
|
|
31
|
+
".": {
|
|
32
|
+
"types": "./index.d.ts",
|
|
33
|
+
"default": "./fesm2022/mmstack-resource.mjs"
|
|
34
|
+
},
|
|
35
|
+
"./package.json": {
|
|
36
|
+
"default": "./package.json"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"sideEffects": false,
|
|
40
|
+
"module": "fesm2022/mmstack-resource.mjs",
|
|
41
|
+
"typings": "index.d.ts"
|
|
42
|
+
}
|