A bundle containing NgRx Helper classes and functions to simplify the coding experience
Requires Angular 12.1+ with Ivy enabled and TypeScript 4.3+
npm install @ngrx/{store,effects,entity} @tomtomb/ngrx-toolkit
# or
yarn add @ngrx/{store,effects,entity} @tomtomb/ngrx-toolkit
You probably want to install the store devtools as well
npm install -D @ngrx/store-devtools
# or
yarn add -D @ngrx/store-devtools
For a full example have a look at the sandbox application. The toolkit can easily be integrated into an existing NgRx store.
These docs are based on version prior to 2.x. The new version included many breaking changes, so please refer to the sandbox app for now!
Creates an action group containing a
call
,success
andfailure
action.
Generic | Extends | Description | Default |
---|---|---|---|
Arguments | ArgumentsBase / null |
Arguments needed to perform the action | |
ResponseData | The response if the call was successfully performed | ||
ErrorResponse | Additional error data returned by the HttpErrorResponse error property | unknown |
IMPORTANT: The structure of the Arguments
type must be based on the ArgumentsBase
type.
export interface ArgumentsBase {
queryParams?: Record<string | number, unknown>;
params?: Record<string | number, unknown>;
body?: unknown;
sideUpdates?: Record<string, ArgumentsBase>;
}
Argument | Type | Description | Default |
---|---|---|---|
method | 'GET' / 'POST' / 'PUT' / 'PATCH' / 'DELETE' |
HTTP verb of the action | |
name | string |
The actions name | |
scope | string |
The actions scope | |
isUnique | boolean / undefined |
By default all action calls will be stored inside the store. If true , only one action call will be stored. This is useful for things like login actions |
false |
TypedActionObject<Arguments, ResponseData, ErrorResponse>
export const getFoo = createActionGroup<
{ queryParams: { id: string } }, // This object must follow the ArgumentsBase type
{ value: boolean },
{ additionalErrorData: string }
>({
method: 'GET',
name: 'Foo',
scope: 'Sandbox',
});
Creates an reducer based on provided TypedActionObjects.
WIP
Creates selector functions to be used by
FacadeBase
WIP
A abstract class for handling ngrx effects with ActionGroups
Argument | Type | Description | Default |
---|---|---|---|
actions | Actions |
The NgRx Actions Observable | |
featureService | class |
The class where the actual http calls are located |
These methods are all doing the same except for the used observable flattening operator.
RxJS Docs: switchMap, mergeMap, exhaustMap, concatMap
Generics should NOT be provided by the user. They are filled automatically.
Argument | Type | Description | Default |
---|---|---|---|
action | TypedActionObject |
The action group created via createActionGroup() |
|
serviceCall | method |
A reference to the actual http call | |
sideUpdates | object / undefined |
Additional user defined side effects. These actions will be called after the parent success action gets called | undefined |
@Injectable()
export class SandboxEffects extends EffectBase {
getFoo$ = this.onActionSwitchMap({
action: SandboxActions.getFoo,
serviceCall: this.featureService.getFoo,
});
constructor(
private actions$: Actions,
private featureService: SandboxService
) {
super(actions$, featureService);
}
}
A abstract class for dispatching actions and retrieving data from the store
WIP
A abstract class for making http calls with caching built-in
WIP