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