UNPKG

2.05 kBJavaScriptView Raw
1/// <reference path='../typings/index.d.ts' />
2import { endsWith, startsWith } from "lodash";
3
4export const PREFIX = "@API";
5export const REQUEST = "REQUEST";
6export const SUCCESS = "SUCCESS";
7export const FAILURE = "FAILURE";
8export const CLEAR = "CLEAR";
9export const REFRESH = "REFRESH";
10
11/**
12 * make api action types
13 * @param {string} base base name
14 */
15export const makeTypes = key => ({
16 request: `${PREFIX}/${key}/${REQUEST}`,
17 success: `${PREFIX}/${key}/${SUCCESS}`,
18 failure: `${PREFIX}/${key}/${FAILURE}`,
19 clear: `${PREFIX}/${key}/${CLEAR}`,
20 refresh: `${PREFIX}/${key}/${REFRESH}`,
21});
22
23export const successOf = key => `${PREFIX}/${key}/${SUCCESS}`;
24export const requestOf = key => `${PREFIX}/${key}/${REQUEST}`;
25export const failureOf = key => `${PREFIX}/${key}/${FAILURE}`;
26export const clearOf = key => `${PREFIX}/${key}/${CLEAR}`;
27export const refreshOf = key => `${PREFIX}/${key}/${REFRESH}`;
28
29/**
30 * action is api action
31 * @param {import("@36node/redux-api").Action} action action
32 */
33export function isApi(action) {
34 const { key, type } = action;
35 return key && startsWith(type, PREFIX);
36}
37
38/**
39 * Is request action
40 * @param {import("@36node/redux-api").Action} action action
41 */
42export function isRequest(action) {
43 return isApi(action) && endsWith(action.type, REQUEST);
44}
45
46/**
47 * Is success action
48 * @param {import("@36node/redux-api").Action} action action
49 */
50export function isSuccess(action) {
51 return isApi(action) && endsWith(action.type, SUCCESS);
52}
53
54/**
55 * Is failure action
56 * @param {import("@36node/redux-api").Action} action action
57 */
58export function isFailure(action) {
59 return isApi(action) && endsWith(action.type, FAILURE);
60}
61
62/**
63 * Is clear action
64 * @param {import("@36node/redux-api").Action} action action
65 */
66export function isClear(action) {
67 return isApi(action) && endsWith(action.type, CLEAR);
68}
69
70/**
71 * Is refresh action
72 * @param {import("@36node/redux-api").Action} action action
73 */
74export function isRefresh(action) {
75 return isApi(action) && endsWith(action.type, REFRESH);
76}