@angular-architects/ngrx-toolkit 20.0.3 → 20.1.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.
@@ -1,6 +1,6 @@
1
1
  import * as i0 from '@angular/core';
2
2
  import { Injectable, InjectionToken, signal, effect, inject, PLATFORM_ID, computed, isSignal, untracked, isDevMode as isDevMode$1 } from '@angular/core';
3
- import { watchState, getState, signalStoreFeature, withMethods, withHooks, patchState as patchState$1, withState, withComputed, withProps } from '@ngrx/signals';
3
+ import { watchState, getState, signalStoreFeature, withMethods, withHooks, patchState as patchState$1, withState, withComputed, withProps, withLinkedState } from '@ngrx/signals';
4
4
  import { isPlatformBrowser, isPlatformServer } from '@angular/common';
5
5
  import { Subject } from 'rxjs';
6
6
  import { removeEntity, setAllEntities, updateEntity, addEntity } from '@ngrx/signals/entities';
@@ -1781,9 +1781,108 @@ function withFeatureFactory(factoryFn) {
1781
1781
  };
1782
1782
  }
1783
1783
 
1784
+ //** Types for `withResource` */
1785
+ function withResource(resourceFactory) {
1786
+ return (store) => {
1787
+ const resourceOrDictionary = resourceFactory({
1788
+ ...store.stateSignals,
1789
+ ...store.props,
1790
+ ...store.methods,
1791
+ });
1792
+ if (isResourceRef(resourceOrDictionary)) {
1793
+ return createUnnamedResource(resourceOrDictionary)(store);
1794
+ }
1795
+ else {
1796
+ return createNamedResource(resourceOrDictionary)(store);
1797
+ }
1798
+ };
1799
+ }
1800
+ function createUnnamedResource(resource) {
1801
+ function hasValue() {
1802
+ return resource.hasValue();
1803
+ }
1804
+ return signalStoreFeature(withLinkedState(() => ({ value: resource.value })), withProps(() => ({
1805
+ status: resource.status,
1806
+ error: resource.error,
1807
+ isLoading: resource.isLoading,
1808
+ })), withMethods(() => ({
1809
+ hasValue,
1810
+ _reload: () => resource.reload(),
1811
+ })));
1812
+ }
1813
+ function createNamedResource(dictionary) {
1814
+ const keys = Object.keys(dictionary);
1815
+ const state = keys.reduce((state, resourceName) => ({
1816
+ ...state,
1817
+ [`${resourceName}Value`]: dictionary[resourceName].value,
1818
+ }), {});
1819
+ const props = keys.reduce((props, resourceName) => ({
1820
+ ...props,
1821
+ [`${resourceName}Status`]: dictionary[resourceName].status,
1822
+ [`${resourceName}Error`]: dictionary[resourceName].error,
1823
+ [`${resourceName}IsLoading`]: dictionary[resourceName].isLoading,
1824
+ }), {});
1825
+ const methods = keys.reduce((methods, resourceName) => {
1826
+ return {
1827
+ ...methods,
1828
+ [`${resourceName}HasValue`]: () => dictionary[resourceName].hasValue(),
1829
+ [`_${resourceName}Reload`]: () => dictionary[resourceName].reload(),
1830
+ };
1831
+ }, {});
1832
+ return signalStoreFeature(withLinkedState(() => state), withProps(() => props), withMethods(() => methods));
1833
+ }
1834
+ function isResourceRef(value) {
1835
+ return (value !== null &&
1836
+ typeof value === 'object' &&
1837
+ 'value' in value &&
1838
+ isSignal(value.value) &&
1839
+ 'status' in value &&
1840
+ 'error' in value &&
1841
+ 'isLoading' in value &&
1842
+ 'hasValue' in value &&
1843
+ 'reload' in value);
1844
+ }
1845
+ //** Implementation of `mapToResource` */
1846
+ /**
1847
+ * @experimental
1848
+ * @description
1849
+ *
1850
+ * Maps a named resource to type `Resource<T>`.
1851
+ *
1852
+ * @usageNotes
1853
+ *
1854
+ * ```ts
1855
+ * const store = signalStore(
1856
+ * withState({ userId: undefined as number | undefined }),
1857
+ * withResource(({ userId }) => ({
1858
+ * user: httpResource<User[]>(() => '/users', { defaultValue: [] }),
1859
+ * }))
1860
+ * );
1861
+ * const userResource = mapToResource(store, 'user');
1862
+ * userResource satisfies Resource<User[]>;
1863
+ * ```
1864
+ *
1865
+ * @param store The store instance to map the resource to.
1866
+ * @param name The name of the resource to map.
1867
+ * @returns `ResourceRef<T>`
1868
+ */
1869
+ function mapToResource(store, name) {
1870
+ const resourceName = String(name);
1871
+ function hasValue() {
1872
+ return store[`${resourceName}HasValue`]();
1873
+ }
1874
+ return {
1875
+ value: store[`${resourceName}Value`],
1876
+ status: store[`${resourceName}Status`],
1877
+ error: store[`${resourceName}Error`],
1878
+ isLoading: store[`${resourceName}IsLoading`],
1879
+ hasValue,
1880
+ };
1881
+ }
1882
+
1784
1883
  /**
1785
1884
  * Generated bundle index. Do not edit.
1786
1885
  */
1787
1886
 
1788
- export { capitalize, createEffects, createPageArray, createReducer, deriveCallStateKeys, emptyFeature, firstPage, getCallStateKeys, getCollectionArray, getDataServiceKeys, getUndoRedoKeys, gotoPage, nextPage, noPayload, patchState, payload, previousPage, provideDevtoolsConfig, renameDevtoolsName, setError, setLoaded, setLoading, setMaxPageNavigationArrayItems, setPageSize, setResetState, updateState, withCallState, withConditional, withDataService, withDevToolsStub, withDevtools, withDisabledNameIndices, withFeatureFactory, withGlitchTracking, withImmutableState, withIndexedDB, withIndexedDB as withIndexeddb, withLocalStorage, withMapper, withPagination, withRedux, withReset, withSessionStorage, withStorageSync, withUndoRedo };
1887
+ export { capitalize, createEffects, createPageArray, createReducer, deriveCallStateKeys, emptyFeature, firstPage, getCallStateKeys, getCollectionArray, getDataServiceKeys, getUndoRedoKeys, gotoPage, mapToResource, nextPage, noPayload, patchState, payload, previousPage, provideDevtoolsConfig, renameDevtoolsName, setError, setLoaded, setLoading, setMaxPageNavigationArrayItems, setPageSize, setResetState, updateState, withCallState, withConditional, withDataService, withDevToolsStub, withDevtools, withDisabledNameIndices, withFeatureFactory, withGlitchTracking, withImmutableState, withIndexedDB, withIndexedDB as withIndexeddb, withLocalStorage, withMapper, withPagination, withRedux, withReset, withResource, withSessionStorage, withStorageSync, withUndoRedo };
1789
1888
  //# sourceMappingURL=angular-architects-ngrx-toolkit.mjs.map