Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/symfa-inc/angular-action-creator
https://github.com/symfa-inc/angular-action-creator
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/symfa-inc/angular-action-creator
- Owner: Symfa-Inc
- Created: 2021-08-05T05:21:36.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-09-27T06:51:45.000Z (5 months ago)
- Last Synced: 2024-12-23T01:04:42.653Z (about 2 months ago)
- Language: TypeScript
- Size: 25.4 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# angular-action-creator
Action creator for ngrx store.
## Usage
### Basic Action
```ts
import { ActionCreator } from '@aiscom-llc/angular-action-creator';const actionCreator = new NgRxAction('Auth/API'); // Actions prefix
// Specify payload shape as generic type argument.
const somethingHappened = actionCreator.createAction<{foo: string}>('SOMETHING_HAPPENED');// Get action creator type.
console.log(somethingHappened.type); // [Auth/API] SOMETHING_HAPPENED// Create action.
const action = somethingHappened({foo: 'bar'});
console.log(action); // {type: '[Auth/API] SOMETHING_HAPPENED', foo: 'bar'}
```### Async Action
Async Action Creators are objects with properties `started`, `success` and
`failed` whose values are action creators.```ts
import { ActionCreator } from '@aiscom-llc/angular-action-creator';const actionCreator = new NgRxAction('Auth/API'); // Actions type prefix
// specify parameters and result shapes as generic type arguments
const doSomething =
actionCreator.createAsyncAction<{ foo: string }, // parameter type
{ bar: number }, // success type
{ code: number } // error type (not required, ErrorState by default)
>('DO_SOMETHING');console.log(doSomething.started({ foo: 'lol' }));
// {type: '[Auth/API] DO_SOMETHING_STARTED', foo: 'lol'}console.log(doSomething.success({
params: { foo: 'lol' },
result: { bar: 42 },
}));
// {type: '[Auth/API] DO_SOMETHING_SUCCESS',
// params: {foo: 'lol'},
// result: {bar: 42},
// }console.log(doSomething.failed({
params: { foo: 'lol' },
error: { code: 42 },
}));
// {type: '[Auth/API] DO_SOMETHING_FAILED',
// params: {foo: 'lol'},
// error: {code: 42},
// }
//----------------------------------------
//Using destructuring assignment syntax
const {
started: doSomethingStarted,
success: doSomethingSuccess,
failed: doSomethingFailed,
} = doSomething;doSomethingStarted({ foo: 'lol' });
doSomethingSuccess({
params: { foo: 'lol' },
result: { bar: 42 },
});doSomethingFailed({
params: { foo: 'lol' },
error: { code: 42 },
});```