UNPKG

10.6 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
3 typeof define === 'function' && define.amd ? define(['exports'], factory) :
4 (global = global || self, factory(global.TypesafeActions = {}));
5}(this, (function (exports) { 'use strict';
6
7 function checkIsEmpty(arg) {
8 return arg == null;
9 }
10 function throwIsEmpty(argPosition) {
11 throw new Error("Argument " + argPosition + " is empty.");
12 }
13 function checkValidActionCreator(arg) {
14 return typeof arg === 'function' && 'getType' in arg;
15 }
16 function checkInvalidActionCreator(arg) {
17 return !checkValidActionCreator(arg);
18 }
19 function throwInvalidActionCreator(argPosition) {
20 throw new Error("Argument " + argPosition + " is invalid, it should be an action-creator instance from \"typesafe-actions\"");
21 }
22 function checkInvalidActionCreatorInArray(arg, idx) {
23 if (arg == null) {
24 throw new Error("Argument contains array with empty element at index " + idx);
25 } else if (arg.getType == null) {
26 throw new Error("Argument contains array with invalid element at index " + idx + ", it should be an action-creator instance from \"typesafe-actions\"");
27 }
28 }
29 function checkValidActionType(arg) {
30 return typeof arg === 'string' || typeof arg === 'symbol';
31 }
32 function checkInvalidActionType(arg) {
33 return !checkValidActionType(arg);
34 }
35 function throwInvalidActionType(argPosition) {
36 throw new Error("Argument " + argPosition + " is invalid, it should be an action type of type: string | symbol");
37 }
38 function checkInvalidActionTypeInArray(arg, idx) {
39 if (arg == null) {
40 throw new Error("Argument contains array with empty element at index " + idx);
41 } else if (typeof arg !== 'string' && typeof arg !== 'symbol') {
42 throw new Error("Argument contains array with invalid element at index " + idx + ", it should be of type: string | symbol");
43 }
44 }
45 function throwInvalidActionTypeOrActionCreator(argPosition) {
46 throw new Error("Argument " + argPosition + " is invalid, it should be an action-creator instance from \"typesafe-actions\" or action type of type: string | symbol");
47 }
48
49 function action(type, payload, meta, error) {
50 if (checkIsEmpty(type)) {
51 throwIsEmpty(1);
52 }
53
54 if (checkInvalidActionType(type)) {
55 throwInvalidActionCreator(1);
56 }
57
58 return {
59 type: type,
60 payload: payload,
61 meta: meta,
62 error: error
63 };
64 }
65
66 function createCustomAction(type, createHandler) {
67 if (checkIsEmpty(type)) {
68 throwIsEmpty(1);
69 }
70
71 if (checkInvalidActionType(type)) {
72 throwInvalidActionType(1);
73 }
74
75 var actionCreator = function actionCreator() {
76 var customProps = createHandler != null ? createHandler.apply(void 0, arguments) : undefined;
77 return Object.assign({
78 type: type
79 }, customProps);
80 };
81
82 var typeMeta = {
83 getType: function getType() {
84 return type;
85 },
86 toString: function toString() {
87 return type;
88 }
89 };
90 return Object.assign(actionCreator, typeMeta);
91 }
92
93 function createAction(type, payloadCreator, metaCreator) {
94 if (checkIsEmpty(type)) {
95 throwIsEmpty(1);
96 }
97
98 if (checkInvalidActionType(type)) {
99 throwInvalidActionType(1);
100 }
101
102 return function () {
103 return createCustomAction(type, function () {
104 var payload = arguments.length <= 0 ? undefined : arguments[0];
105 var meta = arguments.length <= 1 ? undefined : arguments[1];
106
107 if (payloadCreator != null || metaCreator != null) {
108 payload = payloadCreator != null ? payloadCreator.apply(void 0, arguments) : undefined;
109 meta = metaCreator != null ? metaCreator.apply(void 0, arguments) : undefined;
110 }
111
112 return Object.assign({}, payload !== undefined && {
113 payload: payload
114 }, {}, meta !== undefined && {
115 meta: meta
116 });
117 });
118 };
119 }
120
121 function throwInvalidAsyncActionArgument(argPosition) {
122 throw new Error("Argument " + argPosition + " is invalid, it should be an action type of \"string | symbol\" or a tuple of \"[string | symbol, Function, Function?]\"");
123 }
124 function createAsyncAction(requestArg, successArg, failureArg, cancelArg) {
125 var constructor = function constructor() {
126 var results = [requestArg, successArg, failureArg, cancelArg].map(function (arg, index) {
127 if (Array.isArray(arg)) {
128 return createAction(arg[0], arg[1], arg[2])();
129 } else if (typeof arg === 'string' || typeof arg === 'symbol') {
130 return createAction(arg)();
131 } else if (index < 3) {
132 throwInvalidAsyncActionArgument(index);
133 }
134 });
135 var request = results[0],
136 success = results[1],
137 failure = results[2],
138 cancel = results[3];
139 return {
140 request: request,
141 success: success,
142 failure: failure,
143 cancel: cancel
144 };
145 };
146
147 return constructor;
148 }
149
150 function getType(actionCreator) {
151 if (checkIsEmpty(actionCreator)) {
152 throwIsEmpty(1);
153 }
154
155 if (checkInvalidActionCreator(actionCreator)) {
156 throwInvalidActionCreator(1);
157 }
158
159 return actionCreator.getType();
160 }
161
162 function createReducer(initialState, initialHandlers) {
163 if (initialHandlers === void 0) {
164 initialHandlers = {};
165 }
166
167 var handlers = Object.assign({}, initialHandlers);
168
169 var rootReducer = function rootReducer(state, action) {
170 if (state === void 0) {
171 state = initialState;
172 }
173
174 if (handlers.hasOwnProperty(action.type)) {
175 var reducer = handlers[action.type];
176
177 if (typeof reducer !== 'function') {
178 throw Error("Reducer under \"" + action.type + "\" key is not a valid reducer");
179 }
180
181 return reducer(state, action);
182 } else {
183 return state;
184 }
185 };
186
187 var reducerHandler = function reducerHandler(singleOrMultipleCreatorsAndTypes, reducer) {
188 var creatorsAndTypes = Array.isArray(singleOrMultipleCreatorsAndTypes) ? singleOrMultipleCreatorsAndTypes : [singleOrMultipleCreatorsAndTypes];
189 var newHandlers = {};
190 creatorsAndTypes.map(function (acOrType, index) {
191 return checkValidActionCreator(acOrType) ? getType(acOrType) : checkValidActionType(acOrType) ? acOrType : throwInvalidActionTypeOrActionCreator(index + 1);
192 }).forEach(function (type) {
193 return newHandlers[type] = reducer;
194 });
195 return createReducer(initialState, Object.assign({}, handlers, {}, newHandlers));
196 };
197
198 var chainApi = Object.assign(rootReducer, {
199 handlers: Object.assign({}, handlers),
200 handleAction: reducerHandler,
201 handleType: reducerHandler
202 });
203 return chainApi;
204 }
205
206 function isOfType(actionTypeOrTypes, action) {
207 if (checkIsEmpty(actionTypeOrTypes)) {
208 throwIsEmpty(1);
209 }
210
211 var actionTypes = Array.isArray(actionTypeOrTypes) ? actionTypeOrTypes : [actionTypeOrTypes];
212 actionTypes.forEach(checkInvalidActionTypeInArray);
213
214 var assertFn = function assertFn(_action) {
215 return actionTypes.includes(_action.type);
216 };
217
218 if (action === undefined) {
219 return assertFn;
220 }
221
222 return assertFn(action);
223 }
224
225 function isActionOf(actionCreatorOrCreators, action) {
226 if (checkIsEmpty(actionCreatorOrCreators)) {
227 throwIsEmpty(1);
228 }
229
230 var actionCreators = Array.isArray(actionCreatorOrCreators) ? actionCreatorOrCreators : [actionCreatorOrCreators];
231 actionCreators.forEach(checkInvalidActionCreatorInArray);
232
233 var assertFn = function assertFn(_action) {
234 return actionCreators.some(function (actionCreator) {
235 return _action.type === actionCreator.getType();
236 });
237 };
238
239 if (action === undefined) {
240 return assertFn;
241 }
242
243 return assertFn(action);
244 }
245
246 function createAction$1(type, createHandler) {
247 var actionCreator = createHandler == null ? function () {
248 return action(type);
249 } : createHandler(action.bind(null, type));
250 return Object.assign(actionCreator, {
251 getType: function getType() {
252 return type;
253 },
254 toString: function toString() {
255 return type;
256 }
257 });
258 }
259
260 function createCustomAction$1(type, createHandler) {
261 if (checkIsEmpty(type)) {
262 throwIsEmpty(1);
263 }
264
265 if (checkInvalidActionType(type)) {
266 throwInvalidActionType(1);
267 }
268
269 var actionCreator = createHandler != null ? createHandler(type) : function () {
270 return {
271 type: type
272 };
273 };
274 return Object.assign(actionCreator, {
275 getType: function getType() {
276 return type;
277 },
278 toString: function toString() {
279 return type;
280 }
281 });
282 }
283
284 function createStandardAction(type) {
285 if (checkIsEmpty(type)) {
286 throwIsEmpty(1);
287 }
288
289 if (checkInvalidActionType(type)) {
290 throwInvalidActionType(1);
291 }
292
293 function constructor() {
294 return createCustomAction$1(type, function (_type) {
295 return function (payload, meta) {
296 return {
297 type: _type,
298 payload: payload,
299 meta: meta
300 };
301 };
302 });
303 }
304
305 function map(fn) {
306 return createCustomAction$1(type, function (_type) {
307 return function (payload, meta) {
308 return Object.assign(fn(payload, meta), {
309 type: _type
310 });
311 };
312 });
313 }
314
315 return Object.assign(constructor, {
316 map: map
317 });
318 }
319
320 var index = {
321 createAction: createAction$1,
322 createCustomAction: createCustomAction$1,
323 createStandardAction: createStandardAction
324 };
325
326 exports.action = action;
327 exports.createAction = createAction;
328 exports.createAsyncAction = createAsyncAction;
329 exports.createCustomAction = createCustomAction;
330 exports.createReducer = createReducer;
331 exports.deprecated = index;
332 exports.getType = getType;
333 exports.isActionOf = isActionOf;
334 exports.isOfType = isOfType;
335
336})));
337//# sourceMappingURL=typesafe-actions.umd.development.js.map