|
| 1 | +--- |
| 2 | +title: withResource() |
| 3 | +--- |
| 4 | + |
| 5 | +```typescript |
| 6 | +import { withResource } from '@angular-architects/ngrx-toolkit'; |
| 7 | +``` |
| 8 | + |
| 9 | +> **⚠️ Important Note**: This extension is very likely to land in NgRx once Angular's `Resource` enters developer preview. The `withResource` extension provides early access to this functionality and will be maintained for compatibility until the official NgRx implementation is available. |
| 10 | +
|
| 11 | +`withResource()` is a feature in NgRx SignalStore that connects Angular's Resource API with the store. |
| 12 | +The idea: you can use a store to directly manage async data (like loading from an API), and `withResource()` helps you wire that in. |
| 13 | + |
| 14 | +There are two flavors on how you can use it. |
| 15 | + |
| 16 | +**1. Single Resource flavor** |
| 17 | + |
| 18 | +- The Store implements the type `Resource<T>`. |
| 19 | +- That means the store itself exposes the standard resource properties and methods: |
| 20 | + - `value()`, `status()`, `error()`, `hasValue`, etc. |
| 21 | + |
| 22 | +**2. Named Resources flavor** |
| 23 | + |
| 24 | +- Instead of making the whole store act as a single resource, you can define multiple named resources within the same store. |
| 25 | +- Each resource gets its own name, which is used as a prefix for all its properties and methods. |
| 26 | +- For example, if you define a resource named `users`, the store will provide: |
| 27 | + - `usersValue()`, `usersStatus()`, `usersError()`, `usersHasValue()` |
| 28 | + |
| 29 | +For named resources, there’s an extra option: you can map them back into the Resource type. |
| 30 | +This is useful if you want to treat just that part as a “normal” Angular Resource again — for example, to pass it into a component that expects a `Resource<T>`. |
| 31 | + |
| 32 | +## Basic Usage |
| 33 | + |
| 34 | +The extension supports both single resource integration and multiple named resources, giving you flexibility in how you structure your async data management. |
| 35 | + |
| 36 | +### Single Resource |
| 37 | + |
| 38 | +```typescript |
| 39 | +import { withResource } from '@angular-architects/ngrx-toolkit'; |
| 40 | +import { signalStore, withState } from '@ngrx/signals'; |
| 41 | +import { httpResource } from '@angular/core'; |
| 42 | + |
| 43 | +export const UserStore = signalStore( |
| 44 | + withState({ userId: 1 }), |
| 45 | + withResource((state) => httpResource(() => `/user/${state.userId}`)), |
| 46 | +); |
| 47 | +``` |
| 48 | + |
| 49 | +The store now provides: |
| 50 | + |
| 51 | +- `value()`: The resource's current value |
| 52 | +- `status()`: The resource's current status |
| 53 | +- `error()`: Any error that occurred |
| 54 | +- `isLoading()`: Whether the resource is currently loading |
| 55 | +- `hasValue()`: Type guard to check if the resource has a value |
| 56 | +- `_reload()`: Method to reload the resource |
| 57 | + |
| 58 | +### Multiple Named Resources |
| 59 | + |
| 60 | +```typescript |
| 61 | +export const UserStore = signalStore( |
| 62 | + withState({ userId: undefined as number | undefined }), |
| 63 | + withResource(({ userId }) => ({ |
| 64 | + list: httpResource<User[]>(() => '/users', { defaultValue: [] }), |
| 65 | + detail: httpResource<User>(() => (userId === undefined ? undefined : `/user/${userId}`)), |
| 66 | + })), |
| 67 | +); |
| 68 | +``` |
| 69 | + |
| 70 | +With named resources, each resource gets prefixed properties: |
| 71 | + |
| 72 | +- `listValue()`, `detailValue()`: Resource values |
| 73 | +- `listStatus()`, `detailStatus()`: Resource statuses |
| 74 | +- `listError()`, `detailError()`: Resource errors |
| 75 | +- `listIsLoading()`, `detailIsLoading()`: Loading states |
| 76 | +- `listHasValue()`, `detailHasValue()`: Type guards |
| 77 | +- `_listReload()`, `_detailReload()`: Reload methods |
| 78 | + |
| 79 | +## Choosing Between Single and Multiple Resources |
| 80 | + |
| 81 | +- **Single resource:** use when your store works with just one data source. |
| 82 | +- **Named resources:** use when your store is larger and manages multiple entities or async operations. |
| 83 | + |
| 84 | +## Component Usage |
| 85 | + |
| 86 | +```typescript |
| 87 | +@Component({ |
| 88 | + selector: 'app-user-detail', |
| 89 | + template: ` |
| 90 | + @if (userStore.isLoading()) { |
| 91 | + <div>Loading...</div> |
| 92 | + } @else if (userStore.error()) { |
| 93 | + <p>An error has happened.</p> |
| 94 | + } @else if (userStore.hasValue()) { |
| 95 | + <h2>{{ userStore.value().name }}</h2> |
| 96 | + <p>{{ userStore.value().email }}</p> |
| 97 | + } |
| 98 | + `, |
| 99 | +}) |
| 100 | +export class UserDetail { |
| 101 | + protected readonly userStore = inject(UserStore); |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Resource Mapping |
| 106 | + |
| 107 | +For named resources, you can use the `mapToResource` utility to get a properly typed `Resource<T>`: |
| 108 | + |
| 109 | +```typescript |
| 110 | +import { mapToResource } from '@angular-architects/ngrx-toolkit'; |
| 111 | + |
| 112 | +const store = signalStore( |
| 113 | + withState({ userId: undefined as number | undefined }), |
| 114 | + withResource(({ userId }) => ({ |
| 115 | + user: httpResource<User>(() => (userId === undefined ? undefined : `/users/${userId}`)), |
| 116 | + })), |
| 117 | +); |
| 118 | + |
| 119 | +const userResource = mapToResource(store, 'user'); |
| 120 | +// userResource now satisfies Resource<User> |
| 121 | +``` |
0 commit comments