@mmstack/resource 19.5.1 → 19.6.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 +92 -2
- package/fesm2022/mmstack-resource.mjs +177 -58
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/index.d.ts +1 -0
- package/lib/mutation-resource.d.ts +7 -2
- package/lib/options.d.ts +38 -0
- package/lib/query-resource.d.ts +31 -17
- package/lib/util/refresh.d.ts +1 -1
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type HttpResourceRequest } from '@angular/common/http';
|
|
2
|
-
import { type Signal, type ValueEqualityFn } from '@angular/core';
|
|
2
|
+
import { type Provider, type Signal, type ValueEqualityFn } from '@angular/core';
|
|
3
3
|
import { type QueryResourceOptions, type QueryResourceRef } from './query-resource';
|
|
4
4
|
/**
|
|
5
5
|
* @internal
|
|
@@ -68,6 +68,11 @@ export type MutationResourceOptions<TResult, TRaw = TResult, TMutation = TResult
|
|
|
68
68
|
queue?: boolean;
|
|
69
69
|
equal?: ValueEqualityFn<TMutation>;
|
|
70
70
|
};
|
|
71
|
+
/**
|
|
72
|
+
* Layer 2 (mutation): default options for every `mutationResource`, inheriting + overriding the
|
|
73
|
+
* common defaults from `provideResourceOptions`. Per-call options override these in turn.
|
|
74
|
+
*/
|
|
75
|
+
export declare function provideMutationResourceOptions(valueOrFn: Partial<MutationResourceOptions<any, any, any, any, any, any>> | (() => Partial<MutationResourceOptions<any, any, any, any, any, any>>)): Provider;
|
|
71
76
|
/**
|
|
72
77
|
* Represents a mutation resource created by `mutationResource`. Extends
|
|
73
78
|
* `QueryResourceRef` but strips methods that don't make sense for one-off
|
|
@@ -151,5 +156,5 @@ export type MutationResourceRef<TResult, TMutation = TResult, TICTX = void> = Om
|
|
|
151
156
|
* );
|
|
152
157
|
* ```
|
|
153
158
|
*/
|
|
154
|
-
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,
|
|
159
|
+
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, options0?: MutationResourceOptions<TResult, TRaw, TMutation, TCTX, TICTX>): MutationResourceRef<TResult, TMutation, TICTX>;
|
|
155
160
|
export {};
|
package/lib/options.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { InjectionToken, type Injector, type Provider, type ResourceRef } from '@angular/core';
|
|
2
|
+
import { type RegisterOptions } from '@mmstack/primitives';
|
|
3
|
+
import { type CircuitBreakerOptions, type RetryOptions } from './util';
|
|
4
|
+
/**
|
|
5
|
+
* Auto-registration into the nearest transition scope, as a resource OPTION:
|
|
6
|
+
* - `true` — register for the pending indicator + hold-stale (does NOT block first paint);
|
|
7
|
+
* - `{ suspends: true }` — register as *suspending* (the boundary holds its placeholder until
|
|
8
|
+
* this resource has a value), i.e. full Suspense;
|
|
9
|
+
* - `{ suspends: false }` — same as `true`;
|
|
10
|
+
* - `false` / omitted — don't register.
|
|
11
|
+
*
|
|
12
|
+
* Defaultable via `provideResourceOptions` / `provideQueryResourceOptions` and overridable
|
|
13
|
+
* (including opting out with `false`) per call — so a dev can make "all queries participate in
|
|
14
|
+
* transitions" the default and turn it off for the odd one.
|
|
15
|
+
*/
|
|
16
|
+
export type TransitionRegistration = boolean | RegisterOptions;
|
|
17
|
+
/** Options common to every resource kind (the base layer for the options-injection system). */
|
|
18
|
+
export type CommonResourceOptions = {
|
|
19
|
+
/** Auto-registration into the nearest transition scope. */
|
|
20
|
+
readonly register?: TransitionRegistration;
|
|
21
|
+
/** Retry failed requests. */
|
|
22
|
+
readonly retry?: RetryOptions;
|
|
23
|
+
/** Configure a circuit breaker for the resource. */
|
|
24
|
+
readonly circuitBreaker?: CircuitBreakerOptions | true;
|
|
25
|
+
/** Trigger a request even when the request parameters are unchanged. @default false */
|
|
26
|
+
readonly triggerOnSameRequest?: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** Layer 1: defaults that apply to ALL resource kinds. Type-specific providers inherit + override these. */
|
|
29
|
+
export declare function provideResourceOptions(valueOrFn: CommonResourceOptions | (() => CommonResourceOptions)): Provider;
|
|
30
|
+
export declare function injectResourceOptions(injector?: Injector): CommonResourceOptions;
|
|
31
|
+
/** Shared helper for the type-specific providers (query/mutation), so precedence is identical. */
|
|
32
|
+
export declare function provideTypedResourceOptions<T>(token: InjectionToken<T>, valueOrFn: T | (() => T)): Provider;
|
|
33
|
+
/**
|
|
34
|
+
* Applies a resolved `register` option to a freshly-created resource — adds it to the nearest
|
|
35
|
+
* transition scope and removes it on destroy. Runs in the resource's injection context (or the
|
|
36
|
+
* provided `injector`), since registration needs `TRANSITION_SCOPE` + `DestroyRef`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function applyResourceRegistration(ref: ResourceRef<unknown>, register: TransitionRegistration | undefined, injector?: Injector): void;
|
package/lib/query-resource.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { HttpHeaders, type HttpResourceOptions, type HttpResourceRef, type HttpResourceRequest } from '@angular/common/http';
|
|
2
|
-
import { Signal, WritableSignal } from '@angular/core';
|
|
3
|
-
import {
|
|
1
|
+
import { type HttpHeaders, type HttpResourceOptions, type HttpResourceRef, type HttpResourceRequest } from '@angular/common/http';
|
|
2
|
+
import { type Provider, type Signal, type WritableSignal } from '@angular/core';
|
|
3
|
+
import { type CommonResourceOptions } from './options';
|
|
4
4
|
/**
|
|
5
5
|
* Options for configuring caching behavior of a `queryResource`.
|
|
6
6
|
* - `true`: Enables caching with default settings.
|
|
@@ -61,7 +61,7 @@ type ResourceCacheOptions = true | {
|
|
|
61
61
|
* };
|
|
62
62
|
* ```
|
|
63
63
|
*/
|
|
64
|
-
export type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult, TRaw> & {
|
|
64
|
+
export type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult, TRaw> & CommonResourceOptions & {
|
|
65
65
|
/**
|
|
66
66
|
* Whether to keep the previous value of the resource while a refresh is in progress.
|
|
67
67
|
* Defaults to `false`. Also keeps status & headers while refreshing.
|
|
@@ -72,10 +72,6 @@ export type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<
|
|
|
72
72
|
* refresh its data at the specified interval.
|
|
73
73
|
*/
|
|
74
74
|
refresh?: number;
|
|
75
|
-
/**
|
|
76
|
-
* Options for retrying failed requests.
|
|
77
|
-
*/
|
|
78
|
-
retry?: RetryOptions;
|
|
79
75
|
/**
|
|
80
76
|
* Called on every failed attempt, including each retry.
|
|
81
77
|
*
|
|
@@ -87,20 +83,20 @@ export type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<
|
|
|
87
83
|
* "user actually needs to know" side effects (toasts, error reporting).
|
|
88
84
|
*/
|
|
89
85
|
onError?: (err: unknown, retryCount: number, isFinal: boolean) => void;
|
|
90
|
-
/**
|
|
91
|
-
* Options for configuring a circuit breaker for the resource.
|
|
92
|
-
*/
|
|
93
|
-
circuitBreaker?: CircuitBreakerOptions | true;
|
|
94
86
|
/**
|
|
95
87
|
* Options for enabling and configuring caching for the resource.
|
|
96
88
|
*/
|
|
97
89
|
cache?: ResourceCacheOptions;
|
|
98
90
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @default false
|
|
91
|
+
* Comparison of request object
|
|
101
92
|
*/
|
|
102
|
-
|
|
93
|
+
equalRequest?: (a: HttpResourceRequest, b: HttpResourceRequest) => boolean;
|
|
103
94
|
};
|
|
95
|
+
/**
|
|
96
|
+
* Layer 2 (query): default options for every `queryResource`, inheriting + overriding the
|
|
97
|
+
* common defaults from `provideResourceOptions`. Per-call options override these in turn.
|
|
98
|
+
*/
|
|
99
|
+
export declare function provideQueryResourceOptions(valueOrFn: Partial<QueryResourceOptions<any, any>> | (() => Partial<QueryResourceOptions<any, any>>)): Provider;
|
|
104
100
|
/**
|
|
105
101
|
* The reason a query resource is currently in the `disabled` state, or `null`
|
|
106
102
|
* if it is enabled. Useful for branching UI on cause (e.g. "offline" vs
|
|
@@ -119,6 +115,24 @@ export type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<
|
|
|
119
115
|
* ```
|
|
120
116
|
*/
|
|
121
117
|
export type DisabledReason = 'offline' | 'circuit-open' | 'no-request';
|
|
118
|
+
/**
|
|
119
|
+
* Returned from a resource's request fn to PAUSE it: the resource holds its current value and last
|
|
120
|
+
* request (so it does not refetch on resume), and stops background work (no polling, no refetch
|
|
121
|
+
* while paused). Distinct from returning `undefined` (DISABLE), which drops the request — a
|
|
122
|
+
* disabled resource may refetch when re-enabled, a paused one resumes exactly where it left off.
|
|
123
|
+
*
|
|
124
|
+
* The request fn receives a {@link RequestContext} and can just return `ctx.paused`.
|
|
125
|
+
*/
|
|
126
|
+
export declare const PAUSED: unique symbol;
|
|
127
|
+
/**
|
|
128
|
+
* Context passed to a resource's request fn. An object (not positional args) so it can grow
|
|
129
|
+
* without changing the call signature. Today it carries {@link PAUSED} so the fn can return it.
|
|
130
|
+
*/
|
|
131
|
+
export type RequestContext = {
|
|
132
|
+
readonly paused: typeof PAUSED;
|
|
133
|
+
};
|
|
134
|
+
/** The request fn shape: build a request, or return `undefined` (disable) / `ctx.paused` (pause). */
|
|
135
|
+
export type ResourceRequestFn = (ctx: RequestContext) => HttpResourceRequest | string | undefined | void | typeof PAUSED;
|
|
122
136
|
/**
|
|
123
137
|
* Represents a resource created by `queryResource`. Extends `HttpResourceRef`
|
|
124
138
|
* with `disabled` / `disabledReason` signals, writable `headers` / `statusCode`
|
|
@@ -187,7 +201,7 @@ export type QueryResourceRef<TResult> = Omit<HttpResourceRef<TResult>, 'headers'
|
|
|
187
201
|
* user.value(); // always User — never undefined, even before the first fetch resolves
|
|
188
202
|
* ```
|
|
189
203
|
*/
|
|
190
|
-
export declare function queryResource<TResult, TRaw = TResult>(request:
|
|
204
|
+
export declare function queryResource<TResult, TRaw = TResult>(request: ResourceRequestFn, options: QueryResourceOptions<TResult, TRaw> & {
|
|
191
205
|
defaultValue: NoInfer<TResult>;
|
|
192
206
|
}): QueryResourceRef<TResult>;
|
|
193
207
|
/**
|
|
@@ -220,5 +234,5 @@ export declare function queryResource<TResult, TRaw = TResult>(request: () => Ht
|
|
|
220
234
|
* user.disabledReason(); // null while enabled; 'offline' / 'circuit-open' / 'no-request' otherwise
|
|
221
235
|
* ```
|
|
222
236
|
*/
|
|
223
|
-
export declare function queryResource<TResult, TRaw = TResult>(request:
|
|
237
|
+
export declare function queryResource<TResult, TRaw = TResult>(request: ResourceRequestFn, options?: QueryResourceOptions<TResult, TRaw>): QueryResourceRef<TResult | undefined>;
|
|
224
238
|
export {};
|
package/lib/util/refresh.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { HttpResourceRef } from '@angular/common/http';
|
|
2
2
|
import { DestroyRef } from '@angular/core';
|
|
3
|
-
export declare function refresh<T>(resource: HttpResourceRef<T>, destroyRef: DestroyRef, refresh?: number): HttpResourceRef<T>;
|
|
3
|
+
export declare function refresh<T>(resource: HttpResourceRef<T>, destroyRef: DestroyRef, refresh?: number, inactive?: () => boolean): HttpResourceRef<T>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mmstack/resource",
|
|
3
|
-
"version": "19.
|
|
3
|
+
"version": "19.6.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"angular",
|
|
6
6
|
"signals",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/mihajm/mmstack/blob/master/packages/resource",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@mmstack/primitives": "^19.
|
|
20
|
+
"@mmstack/primitives": "^19.4.0",
|
|
21
21
|
"tslib": "^2.3.0"
|
|
22
22
|
},
|
|
23
23
|
"peerDependencies": {
|