Redux toolkit inspired utilities with rigid structure and good Typescript support. Uses createAction
from the toolkit.
Disclaimer: not suitable for actions with custom structure and complex payload
createApiRequestActions(types: ApiActionTypes)
- for API list requestscreateApiDetailRequestActions(types: ApiActionTypes)
- for API detail request
See implementation for more details.
- Define request action types object of type
ApiActionTypes
. - Use one of the utilities above to create object of predefined actions.
type UserRole = 'regular' | 'admin';
type User = {
id: number;
name: string;
role: UserRole;
};
const types: ApiActionTypes = {
REQUEST: 'FETCH_USERS_REQUEST',
SUCCESS: 'FETCH_USERS_SUCCESS',
FAILURE: 'FETCH_USERS_FAILURE',
}
const fetchUsersActions = createApiRequestActions<{ role?: UserRole }, User[], never, string>();