|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var lodash = require('lodash'); |
| 4 | + |
| 5 | +function createAction (type) { |
| 6 | + var payloadCreator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : lodash.identity; |
| 7 | + var metaCreator = arguments[2]; |
| 8 | + |
| 9 | + var hasPayload = !lodash.isUndefined(payloadCreator); |
| 10 | + var finalPayloadCreator = lodash.isFunction(payloadCreator) ? payloadCreator : lodash.identity; |
| 11 | + var hasMetaCreator = lodash.isFunction(metaCreator); |
| 12 | + |
| 13 | + var createAction = function createAction() { |
| 14 | + var payload = hasPayload ? finalPayloadCreator.apply(undefined, arguments) : payloadCreator; |
| 15 | + |
| 16 | + var action = { |
| 17 | + type: type, |
| 18 | + error: payload instanceof Error |
| 19 | + }; |
| 20 | + |
| 21 | + if (payload) { |
| 22 | + action.payload = payload; |
| 23 | + } |
| 24 | + |
| 25 | + if (hasMetaCreator) { |
| 26 | + action.meta = metaCreator.apply(undefined, arguments); |
| 27 | + } |
| 28 | + |
| 29 | + return action; |
| 30 | + }; |
| 31 | + |
| 32 | + return createAction; |
| 33 | +} |
| 34 | + |
| 35 | +var SuffixEnum = Object.freeze({ |
| 36 | + REQUESTED: 'REQUESTED', |
| 37 | + COMPLETED: 'COMPLETED', |
| 38 | + FAILED: 'FAILED' |
| 39 | +}); |
| 40 | + |
| 41 | +var defaultRTSuffixes = [SuffixEnum.REQUESTED, SuffixEnum.COMPLETED, SuffixEnum.FAILED]; |
| 42 | + |
| 43 | +var sharedConfig = { |
| 44 | + requestSuffixes: defaultRTSuffixes |
| 45 | +}; |
| 46 | + |
| 47 | +var createConfig = function createConfig(_ref) { |
| 48 | + var requestSuffixes = _ref.requestSuffixes; |
| 49 | + |
| 50 | + if (requestSuffixes) { |
| 51 | + try { |
| 52 | + sharedConfig.requestSuffixes = requestSuffixes; |
| 53 | + } catch (error) { |
| 54 | + console.warn('Request suffixes are already set somewhere in your code.'); |
| 55 | + } |
| 56 | + |
| 57 | + // This prevents direct access and modification of shared config object |
| 58 | + sharedConfig = Object.freeze(sharedConfig); |
| 59 | + |
| 60 | + return sharedConfig; |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +function createRequestTypes(base) { |
| 65 | + var res = {}; |
| 66 | + var suffixes = sharedConfig.requestSuffixes; |
| 67 | + |
| 68 | + suffixes.forEach(function (type) { |
| 69 | + res[type] = base + '_' + type; |
| 70 | + }); |
| 71 | + |
| 72 | + return res; |
| 73 | +} |
| 74 | + |
| 75 | +function createRequestActions (types) { |
| 76 | + var res = {}; |
| 77 | + |
| 78 | + Object.keys(types).forEach(function (type) { |
| 79 | + if (Object.prototype.hasOwnProperty.call(types, type)) { |
| 80 | + res[type.toLowerCase()] = createAction(types[type]); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + return res; |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = { |
| 88 | + createAction: createAction, |
| 89 | + createRequestTypes: createRequestTypes, |
| 90 | + createRequestActions: createRequestActions, |
| 91 | + createConfig: createConfig |
| 92 | +}; |
0 commit comments