@mmstack/resource 21.3.2 → 21.4.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 +175 -56
- package/fesm2022/mmstack-resource.mjs.map +1 -1
- package/package.json +2 -2
- package/types/mmstack-resource.d.ts +74 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mmstack/resource",
|
|
3
|
-
"version": "21.
|
|
3
|
+
"version": "21.4.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"angular",
|
|
6
6
|
"signals",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"homepage": "https://github.com/mihajm/mmstack/blob/master/packages/resource",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@mmstack/primitives": "^21.0
|
|
21
|
+
"@mmstack/primitives": "^21.1.0",
|
|
22
22
|
"tslib": "^2.3.0"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { HttpResponse, HttpInterceptorFn, HttpRequest, HttpContext, HttpResourceRef, HttpHeaders, HttpResourceRequest, HttpResourceOptions } from '@angular/common/http';
|
|
2
|
-
import { Signal, Injector, Provider, WritableSignal, ValueEqualityFn } from '@angular/core';
|
|
2
|
+
import { Signal, Injector, Provider, ResourceRef, InjectionToken, WritableSignal, ValueEqualityFn } from '@angular/core';
|
|
3
|
+
import { RegisterOptions } from '@mmstack/primitives';
|
|
3
4
|
|
|
4
5
|
type StoredEntry<T> = Omit<CacheEntry<T>, 'timeout'>;
|
|
5
6
|
type CacheDB<T> = {
|
|
@@ -470,6 +471,42 @@ type RetryOptions = number | {
|
|
|
470
471
|
backoff?: number;
|
|
471
472
|
};
|
|
472
473
|
|
|
474
|
+
/**
|
|
475
|
+
* Auto-registration into the nearest transition scope, as a resource OPTION:
|
|
476
|
+
* - `true` — register for the pending indicator + hold-stale (does NOT block first paint);
|
|
477
|
+
* - `{ suspends: true }` — register as *suspending* (the boundary holds its placeholder until
|
|
478
|
+
* this resource has a value), i.e. full Suspense;
|
|
479
|
+
* - `{ suspends: false }` — same as `true`;
|
|
480
|
+
* - `false` / omitted — don't register.
|
|
481
|
+
*
|
|
482
|
+
* Defaultable via `provideResourceOptions` / `provideQueryResourceOptions` and overridable
|
|
483
|
+
* (including opting out with `false`) per call — so a dev can make "all queries participate in
|
|
484
|
+
* transitions" the default and turn it off for the odd one.
|
|
485
|
+
*/
|
|
486
|
+
type TransitionRegistration = boolean | RegisterOptions;
|
|
487
|
+
/** Options common to every resource kind (the base layer for the options-injection system). */
|
|
488
|
+
type CommonResourceOptions = {
|
|
489
|
+
/** Auto-registration into the nearest transition scope. */
|
|
490
|
+
readonly register?: TransitionRegistration;
|
|
491
|
+
/** Retry failed requests. */
|
|
492
|
+
readonly retry?: RetryOptions;
|
|
493
|
+
/** Configure a circuit breaker for the resource. */
|
|
494
|
+
readonly circuitBreaker?: CircuitBreakerOptions | true;
|
|
495
|
+
/** Trigger a request even when the request parameters are unchanged. @default false */
|
|
496
|
+
readonly triggerOnSameRequest?: boolean;
|
|
497
|
+
};
|
|
498
|
+
/** Layer 1: defaults that apply to ALL resource kinds. Type-specific providers inherit + override these. */
|
|
499
|
+
declare function provideResourceOptions(valueOrFn: CommonResourceOptions | (() => CommonResourceOptions)): Provider;
|
|
500
|
+
declare function injectResourceOptions(injector?: Injector): CommonResourceOptions;
|
|
501
|
+
/** Shared helper for the type-specific providers (query/mutation), so precedence is identical. */
|
|
502
|
+
declare function provideTypedResourceOptions<T>(token: InjectionToken<T>, valueOrFn: T | (() => T)): Provider;
|
|
503
|
+
/**
|
|
504
|
+
* Applies a resolved `register` option to a freshly-created resource — adds it to the nearest
|
|
505
|
+
* transition scope and removes it on destroy. Runs in the resource's injection context (or the
|
|
506
|
+
* provided `injector`), since registration needs `TRANSITION_SCOPE` + `DestroyRef`.
|
|
507
|
+
*/
|
|
508
|
+
declare function applyResourceRegistration(ref: ResourceRef<unknown>, register: TransitionRegistration | undefined, injector?: Injector): void;
|
|
509
|
+
|
|
473
510
|
/**
|
|
474
511
|
* Options for configuring caching behavior of a `queryResource`.
|
|
475
512
|
* - `true`: Enables caching with default settings.
|
|
@@ -530,7 +567,7 @@ type ResourceCacheOptions = true | {
|
|
|
530
567
|
* };
|
|
531
568
|
* ```
|
|
532
569
|
*/
|
|
533
|
-
type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult, TRaw> & {
|
|
570
|
+
type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult, TRaw> & CommonResourceOptions & {
|
|
534
571
|
/**
|
|
535
572
|
* Whether to keep the previous value of the resource while a refresh is in progress.
|
|
536
573
|
* Defaults to `false`. Also keeps status & headers while refreshing.
|
|
@@ -541,10 +578,6 @@ type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult
|
|
|
541
578
|
* refresh its data at the specified interval.
|
|
542
579
|
*/
|
|
543
580
|
refresh?: number;
|
|
544
|
-
/**
|
|
545
|
-
* Options for retrying failed requests.
|
|
546
|
-
*/
|
|
547
|
-
retry?: RetryOptions;
|
|
548
581
|
/**
|
|
549
582
|
* Called on every failed attempt, including each retry.
|
|
550
583
|
*
|
|
@@ -556,20 +589,20 @@ type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult
|
|
|
556
589
|
* "user actually needs to know" side effects (toasts, error reporting).
|
|
557
590
|
*/
|
|
558
591
|
onError?: (err: unknown, retryCount: number, isFinal: boolean) => void;
|
|
559
|
-
/**
|
|
560
|
-
* Options for configuring a circuit breaker for the resource.
|
|
561
|
-
*/
|
|
562
|
-
circuitBreaker?: CircuitBreakerOptions | true;
|
|
563
592
|
/**
|
|
564
593
|
* Options for enabling and configuring caching for the resource.
|
|
565
594
|
*/
|
|
566
595
|
cache?: ResourceCacheOptions;
|
|
567
596
|
/**
|
|
568
|
-
*
|
|
569
|
-
* @default false
|
|
597
|
+
* Comparison of request object
|
|
570
598
|
*/
|
|
571
|
-
|
|
599
|
+
equalRequest?: (a: HttpResourceRequest, b: HttpResourceRequest) => boolean;
|
|
572
600
|
};
|
|
601
|
+
/**
|
|
602
|
+
* Layer 2 (query): default options for every `queryResource`, inheriting + overriding the
|
|
603
|
+
* common defaults from `provideResourceOptions`. Per-call options override these in turn.
|
|
604
|
+
*/
|
|
605
|
+
declare function provideQueryResourceOptions(valueOrFn: Partial<QueryResourceOptions<any, any>> | (() => Partial<QueryResourceOptions<any, any>>)): Provider;
|
|
573
606
|
/**
|
|
574
607
|
* The reason a query resource is currently in the `disabled` state, or `null`
|
|
575
608
|
* if it is enabled. Useful for branching UI on cause (e.g. "offline" vs
|
|
@@ -588,6 +621,24 @@ type QueryResourceOptions<TResult, TRaw = TResult> = HttpResourceOptions<TResult
|
|
|
588
621
|
* ```
|
|
589
622
|
*/
|
|
590
623
|
type DisabledReason = 'offline' | 'circuit-open' | 'no-request';
|
|
624
|
+
/**
|
|
625
|
+
* Returned from a resource's request fn to PAUSE it: the resource holds its current value and last
|
|
626
|
+
* request (so it does not refetch on resume), and stops background work (no polling, no refetch
|
|
627
|
+
* while paused). Distinct from returning `undefined` (DISABLE), which drops the request — a
|
|
628
|
+
* disabled resource may refetch when re-enabled, a paused one resumes exactly where it left off.
|
|
629
|
+
*
|
|
630
|
+
* The request fn receives a {@link RequestContext} and can just return `ctx.paused`.
|
|
631
|
+
*/
|
|
632
|
+
declare const PAUSED: unique symbol;
|
|
633
|
+
/**
|
|
634
|
+
* Context passed to a resource's request fn. An object (not positional args) so it can grow
|
|
635
|
+
* without changing the call signature. Today it carries {@link PAUSED} so the fn can return it.
|
|
636
|
+
*/
|
|
637
|
+
type RequestContext = {
|
|
638
|
+
readonly paused: typeof PAUSED;
|
|
639
|
+
};
|
|
640
|
+
/** The request fn shape: build a request, or return `undefined` (disable) / `ctx.paused` (pause). */
|
|
641
|
+
type ResourceRequestFn = (ctx: RequestContext) => HttpResourceRequest | string | undefined | void | typeof PAUSED;
|
|
591
642
|
/**
|
|
592
643
|
* Represents a resource created by `queryResource`. Extends `HttpResourceRef`
|
|
593
644
|
* with `disabled` / `disabledReason` signals, writable `headers` / `statusCode`
|
|
@@ -656,7 +707,7 @@ type QueryResourceRef<TResult> = Omit<HttpResourceRef<TResult>, 'headers' | 'sta
|
|
|
656
707
|
* user.value(); // always User — never undefined, even before the first fetch resolves
|
|
657
708
|
* ```
|
|
658
709
|
*/
|
|
659
|
-
declare function queryResource<TResult, TRaw = TResult>(request:
|
|
710
|
+
declare function queryResource<TResult, TRaw = TResult>(request: ResourceRequestFn, options: QueryResourceOptions<TResult, TRaw> & {
|
|
660
711
|
defaultValue: NoInfer<TResult>;
|
|
661
712
|
}): QueryResourceRef<TResult>;
|
|
662
713
|
/**
|
|
@@ -689,7 +740,7 @@ declare function queryResource<TResult, TRaw = TResult>(request: () => HttpResou
|
|
|
689
740
|
* user.disabledReason(); // null while enabled; 'offline' / 'circuit-open' / 'no-request' otherwise
|
|
690
741
|
* ```
|
|
691
742
|
*/
|
|
692
|
-
declare function queryResource<TResult, TRaw = TResult>(request:
|
|
743
|
+
declare function queryResource<TResult, TRaw = TResult>(request: ResourceRequestFn, options?: QueryResourceOptions<TResult, TRaw>): QueryResourceRef<TResult | undefined>;
|
|
693
744
|
|
|
694
745
|
/**
|
|
695
746
|
* A reference to a manually triggered query resource. Extends
|
|
@@ -843,6 +894,11 @@ type MutationResourceOptions<TResult, TRaw = TResult, TMutation = TResult, TCTX
|
|
|
843
894
|
queue?: boolean;
|
|
844
895
|
equal?: ValueEqualityFn<TMutation>;
|
|
845
896
|
};
|
|
897
|
+
/**
|
|
898
|
+
* Layer 2 (mutation): default options for every `mutationResource`, inheriting + overriding the
|
|
899
|
+
* common defaults from `provideResourceOptions`. Per-call options override these in turn.
|
|
900
|
+
*/
|
|
901
|
+
declare function provideMutationResourceOptions(valueOrFn: Partial<MutationResourceOptions<any, any, any, any, any, any>> | (() => Partial<MutationResourceOptions<any, any, any, any, any, any>>)): Provider;
|
|
846
902
|
/**
|
|
847
903
|
* Represents a mutation resource created by `mutationResource`. Extends
|
|
848
904
|
* `QueryResourceRef` but strips methods that don't make sense for one-off
|
|
@@ -926,7 +982,7 @@ type MutationResourceRef<TResult, TMutation = TResult, TICTX = void> = Omit<Quer
|
|
|
926
982
|
* );
|
|
927
983
|
* ```
|
|
928
984
|
*/
|
|
929
|
-
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,
|
|
985
|
+
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>;
|
|
930
986
|
|
|
931
|
-
export { Cache, createCacheInterceptor, createCircuitBreaker, createDedupeRequestsInterceptor, injectQueryCache, manualQueryResource, mutationResource, noDedupe, provideCircuitBreakerDefaultOptions, provideQueryCache, queryResource };
|
|
932
|
-
export type { DisabledReason, ManualQueryResourceRef, MutationResourceOptions, MutationResourceRef, QueryResourceOptions, QueryResourceRef };
|
|
987
|
+
export { Cache, PAUSED, applyResourceRegistration, createCacheInterceptor, createCircuitBreaker, createDedupeRequestsInterceptor, injectQueryCache, injectResourceOptions, manualQueryResource, mutationResource, noDedupe, provideCircuitBreakerDefaultOptions, provideMutationResourceOptions, provideQueryCache, provideQueryResourceOptions, provideResourceOptions, provideTypedResourceOptions, queryResource };
|
|
988
|
+
export type { CommonResourceOptions, DisabledReason, ManualQueryResourceRef, MutationResourceOptions, MutationResourceRef, QueryResourceOptions, QueryResourceRef, RequestContext, ResourceRequestFn, TransitionRegistration };
|