3.4.1 • Published 2 years ago

@tomtomb/ngrx-toolkit v3.4.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

NgRx Toolkit

A bundle containing NgRx Helper classes and functions to simplify the coding experience

NPM version License Conventional Commits Open in Visual Studio Code

Installation

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

Getting started

For a full example have a look at the sandbox application. The toolkit can easily be integrated into an existing NgRx store.

API

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!

createActionGroup<Arguments, ResponseData, ErrorResponse>({method, name, scope, isUnique})

Creates an action group containing a call, success and failure action.

Generics

GenericExtendsDescriptionDefault
ArgumentsArgumentsBase / nullArguments needed to perform the action
ResponseDataThe response if the call was successfully performed
ErrorResponseAdditional error data returned by the HttpErrorResponse error propertyunknown

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>;
}

Arguments

ArgumentTypeDescriptionDefault
method'GET' / 'POST' / 'PUT' / 'PATCH' / 'DELETE'HTTP verb of the action
namestringThe actions name
scopestringThe actions scope
isUniqueboolean / undefinedBy 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 actionsfalse

Returns

TypedActionObject<Arguments, ResponseData, ErrorResponse>

Example

export const getFoo = createActionGroup<
  { queryParams: { id: string } }, // This object must follow the ArgumentsBase type
  { value: boolean },
  { additionalErrorData: string }
>({
  method: 'GET',
  name: 'Foo',
  scope: 'Sandbox',
});

View the sandbox code


createStoreSlice({actions})

Creates an reducer based on provided TypedActionObjects.

WIP

View the sandbox code


createEntitySelectors({getState, storeSlice})

Creates selector functions to be used by FacadeBase

WIP

View the sandbox code


EffectBase (class)

A abstract class for handling ngrx effects with ActionGroups

constructor(actions, featureService)

Arguments

ArgumentTypeDescriptionDefault
actionsActionsThe NgRx Actions Observable
featureServiceclassThe class where the actual http calls are located

onActionSwitchMap(action, serviceCall, sideUpdates)

onActionMergeMap(action, serviceCall, sideUpdates)

onActionExhaustMap(action, serviceCall, sideUpdates)

onActionConcatMap(action, serviceCall, sideUpdates)

These methods are all doing the same except for the used observable flattening operator.

RxJS Docs: switchMap, mergeMap, exhaustMap, concatMap

Generics

Generics should NOT be provided by the user. They are filled automatically.

Arguments

ArgumentTypeDescriptionDefault
actionTypedActionObjectThe action group created via createActionGroup()
serviceCallmethodA reference to the actual http call
sideUpdatesobject / undefinedAdditional user defined side effects. These actions will be called after the parent success action gets calledundefined

Example

@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);
  }
}

View the sandbox code


FacadeBase (class)

A abstract class for dispatching actions and retrieving data from the store

WIP

View the sandbox code


ServiceBase (class)

A abstract class for making http calls with caching built-in

WIP

View the sandbox code


3.4.0

2 years ago

3.4.1

2 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.0

3 years ago

3.1.4

3 years ago

3.1.3

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.11.0

3 years ago

2.10.0

3 years ago

2.9.1

3 years ago

2.9.0

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.1

3 years ago

2.4.1

3 years ago

2.3.2

3 years ago

2.4.0

3 years ago

2.3.1

3 years ago

2.4.2

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.10.1

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.0

3 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago