@mmstack/resource 19.5.1 → 19.6.1

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/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './lib/manual-query';
2
2
  export * from './lib/mutation-resource';
3
+ export * from './lib/options';
3
4
  export * from './lib/query-resource';
4
5
  export * from './lib/util/public_api';
@@ -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, options?: MutationResourceOptions<TResult, TRaw, TMutation, TCTX, TICTX>): MutationResourceRef<TResult, TMutation, TICTX>;
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 {};
@@ -0,0 +1,37 @@
1
+ import { InjectionToken, type Injector, type Provider, type ResourceRef } from '@angular/core';
2
+ import { type CircuitBreakerOptions, type RetryOptions } from './util';
3
+ /**
4
+ * Auto-registration into the nearest transition scope, as a resource OPTION:
5
+ * - `'suspend'` — register as *suspending*: the boundary holds its placeholder until this
6
+ * resource has a value (full Suspense). The right choice for data the subtree can't render without;
7
+ * - `'indicator'` — register for the pending indicator + hold-stale only (does NOT block first
8
+ * paint). The right choice for in-region data: the boundary shows the held value with `aria-busy`;
9
+ * - `false` / omitted — don't register.
10
+ *
11
+ * Defaultable via `provideResourceOptions` / `provideQueryResourceOptions` and overridable
12
+ * (including opting out with `false`) per call — so a dev can make "all queries participate in
13
+ * transitions" the default and turn it off for the odd one.
14
+ */
15
+ export type TransitionRegistration = false | 'indicator' | 'suspend';
16
+ /** Options common to every resource kind (the base layer for the options-injection system). */
17
+ export type CommonResourceOptions = {
18
+ /** Auto-registration into the nearest transition scope. */
19
+ readonly register?: TransitionRegistration;
20
+ /** Retry failed requests. */
21
+ readonly retry?: RetryOptions;
22
+ /** Configure a circuit breaker for the resource. */
23
+ readonly circuitBreaker?: CircuitBreakerOptions | true;
24
+ /** Trigger a request even when the request parameters are unchanged. @default false */
25
+ readonly triggerOnSameRequest?: boolean;
26
+ };
27
+ /** Layer 1: defaults that apply to ALL resource kinds. Type-specific providers inherit + override these. */
28
+ export declare function provideResourceOptions(valueOrFn: CommonResourceOptions | (() => CommonResourceOptions)): Provider;
29
+ export declare function injectResourceOptions(injector?: Injector): CommonResourceOptions;
30
+ /** Shared helper for the type-specific providers (query/mutation), so precedence is identical. */
31
+ export declare function provideTypedResourceOptions<T>(token: InjectionToken<T>, valueOrFn: T | (() => T)): Provider;
32
+ /**
33
+ * Applies a resolved `register` option to a freshly-created resource — adds it to the nearest
34
+ * transition scope and removes it on destroy. Runs in the resource's injection context (or the
35
+ * provided `injector`), since registration needs `TRANSITION_SCOPE` + `DestroyRef`.
36
+ */
37
+ export declare function applyResourceRegistration(ref: ResourceRef<unknown>, register: TransitionRegistration | undefined, injector?: Injector): void;
@@ -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 { CircuitBreakerOptions, type RetryOptions } from './util';
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
- * Trigger a request every time the request function is triggered, even if the request parameters are the same.
100
- * @default false
91
+ * Comparison of request object
101
92
  */
102
- triggerOnSameRequest?: boolean;
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: () => HttpResourceRequest | string | undefined | void, options: QueryResourceOptions<TResult, TRaw> & {
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: () => HttpResourceRequest | string | undefined | void, options?: QueryResourceOptions<TResult, TRaw>): QueryResourceRef<TResult | undefined>;
237
+ export declare function queryResource<TResult, TRaw = TResult>(request: ResourceRequestFn, options?: QueryResourceOptions<TResult, TRaw>): QueryResourceRef<TResult | undefined>;
224
238
  export {};
@@ -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.5.1",
3
+ "version": "19.6.1",
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.3.0",
20
+ "@mmstack/primitives": "^19.4.0",
21
21
  "tslib": "^2.3.0"
22
22
  },
23
23
  "peerDependencies": {