redux-promise-middleware
Advanced tools
Comparing version 4.4.2 to 5.0.0
@@ -1,19 +0,17 @@ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
import isPromise from './isPromise.js'; | ||
// The default async action types | ||
export var PENDING = 'PENDING'; | ||
export var FULFILLED = 'FULFILLED'; | ||
export var REJECTED = 'REJECTED'; | ||
var defaultTypes = [PENDING, FULFILLED, REJECTED]; | ||
/** | ||
* @function promiseMiddleware | ||
* @description | ||
* @returns {function} thunk | ||
* Function: promiseMiddleware | ||
* Description: The main promiseMiddleware accepts a configuration | ||
* object and returns the middleware. | ||
*/ | ||
@@ -23,4 +21,4 @@ export default function promiseMiddleware() { | ||
var promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypes; | ||
var promiseTypeSeparator = config.promiseTypeSeparator || '_'; | ||
var PROMISE_TYPE_SUFFIXES = config.promiseTypeSuffixes || defaultTypes; | ||
var PROMISE_TYPE_DELIMITER = config.promiseTypeDelimiter || '_'; | ||
@@ -33,6 +31,58 @@ return function (ref) { | ||
return function (action) { | ||
/** | ||
* Instantiate variables to hold: | ||
* (1) the promise | ||
* (2) the data for optimistic updates | ||
*/ | ||
var promise = void 0; | ||
var data = void 0; | ||
/** | ||
* There are multiple ways to dispatch a promise. The first step is to | ||
* determine if the promise is defined: | ||
* (a) explicitly (action.payload.promise is the promise) | ||
* (b) implicitly (action.payload is the promise) | ||
* (c) as an async function (returns a promise when called) | ||
* | ||
* If the promise is not defined in one of these three ways, we don't do | ||
* anything and move on to the next middleware in the middleware chain. | ||
*/ | ||
// Step 1a: Is there a payload? | ||
if (action.payload) { | ||
if (!isPromise(action.payload) && !isPromise(action.payload.promise)) { | ||
return next(action); | ||
var PAYLOAD = action.payload; | ||
// Step 1.1: Is the promise implicitly defined? | ||
if (isPromise(PAYLOAD)) { | ||
promise = PAYLOAD; | ||
} | ||
// Step 1.2: Is the promise explicitly defined? | ||
else if (isPromise(PAYLOAD.promise)) { | ||
promise = PAYLOAD.promise; | ||
data = PAYLOAD.data; | ||
} | ||
// Step 1.3: Is the promise returned by an async function? | ||
else if (typeof PAYLOAD === 'function' || typeof PAYLOAD.promise === 'function') { | ||
promise = PAYLOAD.promise ? PAYLOAD.promise() : PAYLOAD(); | ||
data = PAYLOAD.promise ? PAYLOAD.data : undefined; | ||
// Step 1.3.1: Is the return of action.payload a promise? | ||
if (!isPromise(promise)) { | ||
// If not, move on to the next middleware. | ||
return next(_extends({}, action, { | ||
payload: promise | ||
})); | ||
} | ||
} | ||
// Step 1.4: If there's no promise, move on to the next middleware. | ||
else { | ||
return next(action); | ||
} | ||
// Step 1b: If there's no payload, move on to the next middleware. | ||
} else { | ||
@@ -42,20 +92,42 @@ return next(action); | ||
// Deconstruct the properties of the original action object to constants | ||
var type = action.type, | ||
payload = action.payload, | ||
meta = action.meta; | ||
/** | ||
* Instantiate and define constants for: | ||
* (1) the action type | ||
* (2) the action meta | ||
*/ | ||
var TYPE = action.type; | ||
var META = action.meta; | ||
// Assign values for promise type suffixes | ||
/** | ||
* Instantiate and define constants for the action type suffixes. | ||
* These are appended to the end of the action type. | ||
*/ | ||
var _promiseTypeSuffixes = _slicedToArray(promiseTypeSuffixes, 3), | ||
_PENDING = _promiseTypeSuffixes[0], | ||
_FULFILLED = _promiseTypeSuffixes[1], | ||
_REJECTED = _promiseTypeSuffixes[2]; | ||
var _PROMISE_TYPE_SUFFIXE = _slicedToArray(PROMISE_TYPE_SUFFIXES, 3), | ||
_PENDING = _PROMISE_TYPE_SUFFIXE[0], | ||
_FULFILLED = _PROMISE_TYPE_SUFFIXE[1], | ||
_REJECTED = _PROMISE_TYPE_SUFFIXE[2]; | ||
/** | ||
* @function getAction | ||
* @description Utility function for creating a rejected or fulfilled | ||
* flux standard action object. | ||
* @param {boolean} Is the action rejected? | ||
* @returns {object} action | ||
* Function: getAction | ||
* Description: This function constructs and returns a rejected | ||
* or fulfilled action object. The action object is based off the Flux | ||
* Standard Action (FSA). | ||
* | ||
* Given an original action with the type FOO: | ||
* | ||
* The rejected object model will be: | ||
* { | ||
* error: true, | ||
* type: 'FOO_REJECTED', | ||
* payload: ..., | ||
* meta: ... (optional) | ||
* } | ||
* | ||
* The fulfilled object model will be: | ||
* { | ||
* type: 'FOO_FULFILLED', | ||
* payload: ..., | ||
* meta: ... (optional) | ||
* } | ||
*/ | ||
@@ -66,6 +138,8 @@ | ||
return _extends({ | ||
type: [type, isRejected ? _REJECTED : _FULFILLED].join(promiseTypeSeparator) | ||
// Concatentate the type string property. | ||
type: [TYPE, isRejected ? _REJECTED : _FULFILLED].join(PROMISE_TYPE_DELIMITER) | ||
}, newPayload === null || typeof newPayload === 'undefined' ? {} : { | ||
payload: newPayload | ||
}, meta !== undefined ? { meta: meta } : {}, isRejected ? { | ||
}, META !== undefined ? { meta: META } : {}, isRejected ? { | ||
error: true | ||
@@ -76,36 +150,9 @@ } : {}); | ||
/** | ||
* Assign values for promise and data variables. In the case the payload | ||
* is an object with a `promise` and `data` property, the values of those | ||
* properties will be used. In the case the payload is a promise, the | ||
* value of the payload will be used and data will be null. | ||
* Function: handleReject | ||
* Calls: getAction to construct the rejected action | ||
* Description: This function dispatches the rejected action and returns | ||
* the original Error object. Please note the developer is responsible | ||
* for constructing and throwing an Error object. The middleware does not | ||
* construct any Errors. | ||
*/ | ||
var promise = void 0; | ||
var data = void 0; | ||
if (!isPromise(action.payload) && _typeof(action.payload) === 'object') { | ||
promise = payload.promise; | ||
data = payload.data; | ||
} else { | ||
promise = payload; | ||
data = undefined; | ||
} | ||
/** | ||
* First, dispatch the pending action. This flux standard action object | ||
* describes the pending state of a promise and will include any data | ||
* (for optimistic updates) and/or meta from the original action. | ||
*/ | ||
next(_extends({ | ||
type: [type, _PENDING].join(promiseTypeSeparator) | ||
}, data !== undefined ? { payload: data } : {}, meta !== undefined ? { meta: meta } : {})); | ||
/* | ||
* @function handleReject | ||
* @description Dispatch the rejected action and return | ||
* an error object. The error object is the original error | ||
* that was thrown. The user of the library is responsible for | ||
* best practices in ensure that they are throwing an Error object. | ||
* @params reason The reason the promise was rejected | ||
* @returns {object} | ||
*/ | ||
var handleReject = function handleReject(reason) { | ||
@@ -118,9 +165,8 @@ var rejectedAction = getAction(reason, true); | ||
/* | ||
* @function handleFulfill | ||
* @description Dispatch the fulfilled action and | ||
* return the success object. The success object should | ||
/** | ||
* Function: handleFulfill | ||
* Calls: getAction to construct the fullfilled action | ||
* Description: This function dispatches the fulfilled action and | ||
* returns the success object. The success object should | ||
* contain the value and the dispatched action. | ||
* @param value The value the promise was resloved with | ||
* @returns {object} | ||
*/ | ||
@@ -137,30 +183,16 @@ var handleFulfill = function handleFulfill() { | ||
/** | ||
* Second, dispatch a rejected or fulfilled action. This flux standard | ||
* action object will describe the resolved state of the promise. In | ||
* the case of a rejected promise, it will include an `error` property. | ||
* | ||
* In order to allow proper chaining of actions using `then`, a new | ||
* promise is constructed and returned. This promise will resolve | ||
* with two properties: (1) the value (if fulfilled) or reason | ||
* (if rejected) and (2) the flux standard action. | ||
* | ||
* Rejected object: | ||
* { | ||
* reason: ... | ||
* action: { | ||
* error: true, | ||
* type: 'ACTION_REJECTED', | ||
* payload: ... | ||
* } | ||
* } | ||
* | ||
* Fulfilled object: | ||
* { | ||
* value: ... | ||
* action: { | ||
* type: 'ACTION_FULFILLED', | ||
* payload: ... | ||
* } | ||
* } | ||
* First, dispatch the pending action: | ||
* This object describes the pending state of a promise and will include | ||
* any data (for optimistic updates) and/or meta from the original action. | ||
*/ | ||
next(_extends({ | ||
// Concatentate the type string. | ||
type: [TYPE, _PENDING].join(PROMISE_TYPE_DELIMITER) | ||
}, data !== undefined ? { payload: data } : {}, META !== undefined ? { meta: META } : {})); | ||
/** | ||
* Second, dispatch a rejected or fulfilled action and move on to the | ||
* next middleware. | ||
*/ | ||
return promise.then(handleFulfill, handleReject); | ||
@@ -167,0 +199,0 @@ }; |
@@ -8,8 +8,6 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
exports.default = promiseMiddleware; | ||
@@ -23,12 +21,12 @@ | ||
// The default async action types | ||
var PENDING = exports.PENDING = 'PENDING'; | ||
var FULFILLED = exports.FULFILLED = 'FULFILLED'; | ||
var REJECTED = exports.REJECTED = 'REJECTED'; | ||
var defaultTypes = [PENDING, FULFILLED, REJECTED]; | ||
/** | ||
* @function promiseMiddleware | ||
* @description | ||
* @returns {function} thunk | ||
* Function: promiseMiddleware | ||
* Description: The main promiseMiddleware accepts a configuration | ||
* object and returns the middleware. | ||
*/ | ||
@@ -38,4 +36,4 @@ function promiseMiddleware() { | ||
var promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypes; | ||
var promiseTypeSeparator = config.promiseTypeSeparator || '_'; | ||
var PROMISE_TYPE_SUFFIXES = config.promiseTypeSuffixes || defaultTypes; | ||
var PROMISE_TYPE_DELIMITER = config.promiseTypeDelimiter || '_'; | ||
@@ -48,6 +46,58 @@ return function (ref) { | ||
return function (action) { | ||
/** | ||
* Instantiate variables to hold: | ||
* (1) the promise | ||
* (2) the data for optimistic updates | ||
*/ | ||
var promise = void 0; | ||
var data = void 0; | ||
/** | ||
* There are multiple ways to dispatch a promise. The first step is to | ||
* determine if the promise is defined: | ||
* (a) explicitly (action.payload.promise is the promise) | ||
* (b) implicitly (action.payload is the promise) | ||
* (c) as an async function (returns a promise when called) | ||
* | ||
* If the promise is not defined in one of these three ways, we don't do | ||
* anything and move on to the next middleware in the middleware chain. | ||
*/ | ||
// Step 1a: Is there a payload? | ||
if (action.payload) { | ||
if (!(0, _isPromise2.default)(action.payload) && !(0, _isPromise2.default)(action.payload.promise)) { | ||
return next(action); | ||
var PAYLOAD = action.payload; | ||
// Step 1.1: Is the promise implicitly defined? | ||
if ((0, _isPromise2.default)(PAYLOAD)) { | ||
promise = PAYLOAD; | ||
} | ||
// Step 1.2: Is the promise explicitly defined? | ||
else if ((0, _isPromise2.default)(PAYLOAD.promise)) { | ||
promise = PAYLOAD.promise; | ||
data = PAYLOAD.data; | ||
} | ||
// Step 1.3: Is the promise returned by an async function? | ||
else if (typeof PAYLOAD === 'function' || typeof PAYLOAD.promise === 'function') { | ||
promise = PAYLOAD.promise ? PAYLOAD.promise() : PAYLOAD(); | ||
data = PAYLOAD.promise ? PAYLOAD.data : undefined; | ||
// Step 1.3.1: Is the return of action.payload a promise? | ||
if (!(0, _isPromise2.default)(promise)) { | ||
// If not, move on to the next middleware. | ||
return next(_extends({}, action, { | ||
payload: promise | ||
})); | ||
} | ||
} | ||
// Step 1.4: If there's no promise, move on to the next middleware. | ||
else { | ||
return next(action); | ||
} | ||
// Step 1b: If there's no payload, move on to the next middleware. | ||
} else { | ||
@@ -57,20 +107,42 @@ return next(action); | ||
// Deconstruct the properties of the original action object to constants | ||
var type = action.type, | ||
payload = action.payload, | ||
meta = action.meta; | ||
/** | ||
* Instantiate and define constants for: | ||
* (1) the action type | ||
* (2) the action meta | ||
*/ | ||
var TYPE = action.type; | ||
var META = action.meta; | ||
// Assign values for promise type suffixes | ||
/** | ||
* Instantiate and define constants for the action type suffixes. | ||
* These are appended to the end of the action type. | ||
*/ | ||
var _promiseTypeSuffixes = _slicedToArray(promiseTypeSuffixes, 3), | ||
_PENDING = _promiseTypeSuffixes[0], | ||
_FULFILLED = _promiseTypeSuffixes[1], | ||
_REJECTED = _promiseTypeSuffixes[2]; | ||
var _PROMISE_TYPE_SUFFIXE = _slicedToArray(PROMISE_TYPE_SUFFIXES, 3), | ||
_PENDING = _PROMISE_TYPE_SUFFIXE[0], | ||
_FULFILLED = _PROMISE_TYPE_SUFFIXE[1], | ||
_REJECTED = _PROMISE_TYPE_SUFFIXE[2]; | ||
/** | ||
* @function getAction | ||
* @description Utility function for creating a rejected or fulfilled | ||
* flux standard action object. | ||
* @param {boolean} Is the action rejected? | ||
* @returns {object} action | ||
* Function: getAction | ||
* Description: This function constructs and returns a rejected | ||
* or fulfilled action object. The action object is based off the Flux | ||
* Standard Action (FSA). | ||
* | ||
* Given an original action with the type FOO: | ||
* | ||
* The rejected object model will be: | ||
* { | ||
* error: true, | ||
* type: 'FOO_REJECTED', | ||
* payload: ..., | ||
* meta: ... (optional) | ||
* } | ||
* | ||
* The fulfilled object model will be: | ||
* { | ||
* type: 'FOO_FULFILLED', | ||
* payload: ..., | ||
* meta: ... (optional) | ||
* } | ||
*/ | ||
@@ -81,6 +153,8 @@ | ||
return _extends({ | ||
type: [type, isRejected ? _REJECTED : _FULFILLED].join(promiseTypeSeparator) | ||
// Concatentate the type string property. | ||
type: [TYPE, isRejected ? _REJECTED : _FULFILLED].join(PROMISE_TYPE_DELIMITER) | ||
}, newPayload === null || typeof newPayload === 'undefined' ? {} : { | ||
payload: newPayload | ||
}, meta !== undefined ? { meta: meta } : {}, isRejected ? { | ||
}, META !== undefined ? { meta: META } : {}, isRejected ? { | ||
error: true | ||
@@ -91,36 +165,9 @@ } : {}); | ||
/** | ||
* Assign values for promise and data variables. In the case the payload | ||
* is an object with a `promise` and `data` property, the values of those | ||
* properties will be used. In the case the payload is a promise, the | ||
* value of the payload will be used and data will be null. | ||
* Function: handleReject | ||
* Calls: getAction to construct the rejected action | ||
* Description: This function dispatches the rejected action and returns | ||
* the original Error object. Please note the developer is responsible | ||
* for constructing and throwing an Error object. The middleware does not | ||
* construct any Errors. | ||
*/ | ||
var promise = void 0; | ||
var data = void 0; | ||
if (!(0, _isPromise2.default)(action.payload) && _typeof(action.payload) === 'object') { | ||
promise = payload.promise; | ||
data = payload.data; | ||
} else { | ||
promise = payload; | ||
data = undefined; | ||
} | ||
/** | ||
* First, dispatch the pending action. This flux standard action object | ||
* describes the pending state of a promise and will include any data | ||
* (for optimistic updates) and/or meta from the original action. | ||
*/ | ||
next(_extends({ | ||
type: [type, _PENDING].join(promiseTypeSeparator) | ||
}, data !== undefined ? { payload: data } : {}, meta !== undefined ? { meta: meta } : {})); | ||
/* | ||
* @function handleReject | ||
* @description Dispatch the rejected action and return | ||
* an error object. The error object is the original error | ||
* that was thrown. The user of the library is responsible for | ||
* best practices in ensure that they are throwing an Error object. | ||
* @params reason The reason the promise was rejected | ||
* @returns {object} | ||
*/ | ||
var handleReject = function handleReject(reason) { | ||
@@ -133,9 +180,8 @@ var rejectedAction = getAction(reason, true); | ||
/* | ||
* @function handleFulfill | ||
* @description Dispatch the fulfilled action and | ||
* return the success object. The success object should | ||
/** | ||
* Function: handleFulfill | ||
* Calls: getAction to construct the fullfilled action | ||
* Description: This function dispatches the fulfilled action and | ||
* returns the success object. The success object should | ||
* contain the value and the dispatched action. | ||
* @param value The value the promise was resloved with | ||
* @returns {object} | ||
*/ | ||
@@ -152,30 +198,16 @@ var handleFulfill = function handleFulfill() { | ||
/** | ||
* Second, dispatch a rejected or fulfilled action. This flux standard | ||
* action object will describe the resolved state of the promise. In | ||
* the case of a rejected promise, it will include an `error` property. | ||
* | ||
* In order to allow proper chaining of actions using `then`, a new | ||
* promise is constructed and returned. This promise will resolve | ||
* with two properties: (1) the value (if fulfilled) or reason | ||
* (if rejected) and (2) the flux standard action. | ||
* | ||
* Rejected object: | ||
* { | ||
* reason: ... | ||
* action: { | ||
* error: true, | ||
* type: 'ACTION_REJECTED', | ||
* payload: ... | ||
* } | ||
* } | ||
* | ||
* Fulfilled object: | ||
* { | ||
* value: ... | ||
* action: { | ||
* type: 'ACTION_FULFILLED', | ||
* payload: ... | ||
* } | ||
* } | ||
* First, dispatch the pending action: | ||
* This object describes the pending state of a promise and will include | ||
* any data (for optimistic updates) and/or meta from the original action. | ||
*/ | ||
next(_extends({ | ||
// Concatentate the type string. | ||
type: [TYPE, _PENDING].join(PROMISE_TYPE_DELIMITER) | ||
}, data !== undefined ? { payload: data } : {}, META !== undefined ? { meta: META } : {})); | ||
/** | ||
* Second, dispatch a rejected or fulfilled action and move on to the | ||
* next middleware. | ||
*/ | ||
return promise.then(handleFulfill, handleReject); | ||
@@ -182,0 +214,0 @@ }; |
@@ -113,8 +113,6 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
exports.default = promiseMiddleware; | ||
@@ -128,12 +126,12 @@ | ||
// The default async action types | ||
var PENDING = exports.PENDING = 'PENDING'; | ||
var FULFILLED = exports.FULFILLED = 'FULFILLED'; | ||
var REJECTED = exports.REJECTED = 'REJECTED'; | ||
var defaultTypes = [PENDING, FULFILLED, REJECTED]; | ||
/** | ||
* @function promiseMiddleware | ||
* @description | ||
* @returns {function} thunk | ||
* Function: promiseMiddleware | ||
* Description: The main promiseMiddleware accepts a configuration | ||
* object and returns the middleware. | ||
*/ | ||
@@ -143,4 +141,4 @@ function promiseMiddleware() { | ||
var promiseTypeSuffixes = config.promiseTypeSuffixes || defaultTypes; | ||
var promiseTypeSeparator = config.promiseTypeSeparator || '_'; | ||
var PROMISE_TYPE_SUFFIXES = config.promiseTypeSuffixes || defaultTypes; | ||
var PROMISE_TYPE_DELIMITER = config.promiseTypeDelimiter || '_'; | ||
@@ -153,6 +151,58 @@ return function (ref) { | ||
return function (action) { | ||
/** | ||
* Instantiate variables to hold: | ||
* (1) the promise | ||
* (2) the data for optimistic updates | ||
*/ | ||
var promise = void 0; | ||
var data = void 0; | ||
/** | ||
* There are multiple ways to dispatch a promise. The first step is to | ||
* determine if the promise is defined: | ||
* (a) explicitly (action.payload.promise is the promise) | ||
* (b) implicitly (action.payload is the promise) | ||
* (c) as an async function (returns a promise when called) | ||
* | ||
* If the promise is not defined in one of these three ways, we don't do | ||
* anything and move on to the next middleware in the middleware chain. | ||
*/ | ||
// Step 1a: Is there a payload? | ||
if (action.payload) { | ||
if (!(0, _isPromise2.default)(action.payload) && !(0, _isPromise2.default)(action.payload.promise)) { | ||
return next(action); | ||
var PAYLOAD = action.payload; | ||
// Step 1.1: Is the promise implicitly defined? | ||
if ((0, _isPromise2.default)(PAYLOAD)) { | ||
promise = PAYLOAD; | ||
} | ||
// Step 1.2: Is the promise explicitly defined? | ||
else if ((0, _isPromise2.default)(PAYLOAD.promise)) { | ||
promise = PAYLOAD.promise; | ||
data = PAYLOAD.data; | ||
} | ||
// Step 1.3: Is the promise returned by an async function? | ||
else if (typeof PAYLOAD === 'function' || typeof PAYLOAD.promise === 'function') { | ||
promise = PAYLOAD.promise ? PAYLOAD.promise() : PAYLOAD(); | ||
data = PAYLOAD.promise ? PAYLOAD.data : undefined; | ||
// Step 1.3.1: Is the return of action.payload a promise? | ||
if (!(0, _isPromise2.default)(promise)) { | ||
// If not, move on to the next middleware. | ||
return next(_extends({}, action, { | ||
payload: promise | ||
})); | ||
} | ||
} | ||
// Step 1.4: If there's no promise, move on to the next middleware. | ||
else { | ||
return next(action); | ||
} | ||
// Step 1b: If there's no payload, move on to the next middleware. | ||
} else { | ||
@@ -162,20 +212,42 @@ return next(action); | ||
// Deconstruct the properties of the original action object to constants | ||
var type = action.type, | ||
payload = action.payload, | ||
meta = action.meta; | ||
/** | ||
* Instantiate and define constants for: | ||
* (1) the action type | ||
* (2) the action meta | ||
*/ | ||
var TYPE = action.type; | ||
var META = action.meta; | ||
// Assign values for promise type suffixes | ||
/** | ||
* Instantiate and define constants for the action type suffixes. | ||
* These are appended to the end of the action type. | ||
*/ | ||
var _promiseTypeSuffixes = _slicedToArray(promiseTypeSuffixes, 3), | ||
_PENDING = _promiseTypeSuffixes[0], | ||
_FULFILLED = _promiseTypeSuffixes[1], | ||
_REJECTED = _promiseTypeSuffixes[2]; | ||
var _PROMISE_TYPE_SUFFIXE = _slicedToArray(PROMISE_TYPE_SUFFIXES, 3), | ||
_PENDING = _PROMISE_TYPE_SUFFIXE[0], | ||
_FULFILLED = _PROMISE_TYPE_SUFFIXE[1], | ||
_REJECTED = _PROMISE_TYPE_SUFFIXE[2]; | ||
/** | ||
* @function getAction | ||
* @description Utility function for creating a rejected or fulfilled | ||
* flux standard action object. | ||
* @param {boolean} Is the action rejected? | ||
* @returns {object} action | ||
* Function: getAction | ||
* Description: This function constructs and returns a rejected | ||
* or fulfilled action object. The action object is based off the Flux | ||
* Standard Action (FSA). | ||
* | ||
* Given an original action with the type FOO: | ||
* | ||
* The rejected object model will be: | ||
* { | ||
* error: true, | ||
* type: 'FOO_REJECTED', | ||
* payload: ..., | ||
* meta: ... (optional) | ||
* } | ||
* | ||
* The fulfilled object model will be: | ||
* { | ||
* type: 'FOO_FULFILLED', | ||
* payload: ..., | ||
* meta: ... (optional) | ||
* } | ||
*/ | ||
@@ -186,6 +258,8 @@ | ||
return _extends({ | ||
type: [type, isRejected ? _REJECTED : _FULFILLED].join(promiseTypeSeparator) | ||
// Concatentate the type string property. | ||
type: [TYPE, isRejected ? _REJECTED : _FULFILLED].join(PROMISE_TYPE_DELIMITER) | ||
}, newPayload === null || typeof newPayload === 'undefined' ? {} : { | ||
payload: newPayload | ||
}, meta !== undefined ? { meta: meta } : {}, isRejected ? { | ||
}, META !== undefined ? { meta: META } : {}, isRejected ? { | ||
error: true | ||
@@ -196,36 +270,9 @@ } : {}); | ||
/** | ||
* Assign values for promise and data variables. In the case the payload | ||
* is an object with a `promise` and `data` property, the values of those | ||
* properties will be used. In the case the payload is a promise, the | ||
* value of the payload will be used and data will be null. | ||
* Function: handleReject | ||
* Calls: getAction to construct the rejected action | ||
* Description: This function dispatches the rejected action and returns | ||
* the original Error object. Please note the developer is responsible | ||
* for constructing and throwing an Error object. The middleware does not | ||
* construct any Errors. | ||
*/ | ||
var promise = void 0; | ||
var data = void 0; | ||
if (!(0, _isPromise2.default)(action.payload) && _typeof(action.payload) === 'object') { | ||
promise = payload.promise; | ||
data = payload.data; | ||
} else { | ||
promise = payload; | ||
data = undefined; | ||
} | ||
/** | ||
* First, dispatch the pending action. This flux standard action object | ||
* describes the pending state of a promise and will include any data | ||
* (for optimistic updates) and/or meta from the original action. | ||
*/ | ||
next(_extends({ | ||
type: [type, _PENDING].join(promiseTypeSeparator) | ||
}, data !== undefined ? { payload: data } : {}, meta !== undefined ? { meta: meta } : {})); | ||
/* | ||
* @function handleReject | ||
* @description Dispatch the rejected action and return | ||
* an error object. The error object is the original error | ||
* that was thrown. The user of the library is responsible for | ||
* best practices in ensure that they are throwing an Error object. | ||
* @params reason The reason the promise was rejected | ||
* @returns {object} | ||
*/ | ||
var handleReject = function handleReject(reason) { | ||
@@ -238,9 +285,8 @@ var rejectedAction = getAction(reason, true); | ||
/* | ||
* @function handleFulfill | ||
* @description Dispatch the fulfilled action and | ||
* return the success object. The success object should | ||
/** | ||
* Function: handleFulfill | ||
* Calls: getAction to construct the fullfilled action | ||
* Description: This function dispatches the fulfilled action and | ||
* returns the success object. The success object should | ||
* contain the value and the dispatched action. | ||
* @param value The value the promise was resloved with | ||
* @returns {object} | ||
*/ | ||
@@ -257,30 +303,16 @@ var handleFulfill = function handleFulfill() { | ||
/** | ||
* Second, dispatch a rejected or fulfilled action. This flux standard | ||
* action object will describe the resolved state of the promise. In | ||
* the case of a rejected promise, it will include an `error` property. | ||
* | ||
* In order to allow proper chaining of actions using `then`, a new | ||
* promise is constructed and returned. This promise will resolve | ||
* with two properties: (1) the value (if fulfilled) or reason | ||
* (if rejected) and (2) the flux standard action. | ||
* | ||
* Rejected object: | ||
* { | ||
* reason: ... | ||
* action: { | ||
* error: true, | ||
* type: 'ACTION_REJECTED', | ||
* payload: ... | ||
* } | ||
* } | ||
* | ||
* Fulfilled object: | ||
* { | ||
* value: ... | ||
* action: { | ||
* type: 'ACTION_FULFILLED', | ||
* payload: ... | ||
* } | ||
* } | ||
* First, dispatch the pending action: | ||
* This object describes the pending state of a promise and will include | ||
* any data (for optimistic updates) and/or meta from the original action. | ||
*/ | ||
next(_extends({ | ||
// Concatentate the type string. | ||
type: [TYPE, _PENDING].join(PROMISE_TYPE_DELIMITER) | ||
}, data !== undefined ? { payload: data } : {}, META !== undefined ? { meta: META } : {})); | ||
/** | ||
* Second, dispatch a rejected or fulfilled action and move on to the | ||
* next middleware. | ||
*/ | ||
return promise.then(handleFulfill, handleReject); | ||
@@ -287,0 +319,0 @@ }; |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ReduxPromiseMiddleware=e():t.ReduxPromiseMiddleware=e()}(this,function(){return function(t){function e(r){if(o[r])return o[r].exports;var n=o[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,e),n.l=!0,n.exports}var o={};return e.m=t,e.c=o,e.i=function(t){return t},e.d=function(t,o,r){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=1)}([function(t,e,o){"use strict";function r(t){return null!==t&&"object"===(void 0===t?"undefined":n(t))&&(t&&"function"==typeof t.then)}Object.defineProperty(e,"__esModule",{value:!0});var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};e.default=r},function(t,e,o){"use strict";function r(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.promiseTypeSuffixes||y,o=t.promiseTypeSeparator||"_";return function(t){var r=t.dispatch;return function(t){return function(a){if(!a.payload)return t(a);if(!(0,f.default)(a.payload)&&!(0,f.default)(a.payload.promise))return t(a);var l=a.type,c=a.payload,p=a.meta,y=i(e,3),d=y[0],s=y[1],v=y[2],b=function(t,e){return u({type:[l,e?v:s].join(o)},null===t||void 0===t?{}:{payload:t},void 0!==p?{meta:p}:{},e?{error:!0}:{})},m=void 0,E=void 0;(0,f.default)(a.payload)||"object"!==n(a.payload)?(m=c,E=void 0):(m=c.promise,E=c.data),t(u({type:[l,d].join(o)},void 0!==E?{payload:E}:{},void 0!==p?{meta:p}:{}));var h=function(t){var e=b(t,!0);throw r(e),t},j=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,e=b(t,!1);return r(e),{value:t,action:e}};return m.then(j,h)}}}}Object.defineProperty(e,"__esModule",{value:!0}),e.REJECTED=e.FULFILLED=e.PENDING=void 0;var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&(t[r]=o[r])}return t},i=function(){function t(t,e){var o=[],r=!0,n=!1,u=void 0;try{for(var i,a=t[Symbol.iterator]();!(r=(i=a.next()).done)&&(o.push(i.value),!e||o.length!==e);r=!0);}catch(t){n=!0,u=t}finally{try{!r&&a.return&&a.return()}finally{if(n)throw u}}return o}return function(e,o){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return t(e,o);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}();e.default=r;var a=o(0),f=function(t){return t&&t.__esModule?t:{default:t}}(a),l=e.PENDING="PENDING",c=e.FULFILLED="FULFILLED",p=e.REJECTED="REJECTED",y=[l,c,p]}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.ReduxPromiseMiddleware=t():e.ReduxPromiseMiddleware=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var r={};return t.m=e,t.c=r,t.i=function(e){return e},t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(e,t,r){"use strict";function n(e){return null!==e&&"object"===(void 0===e?"undefined":o(e))&&(e&&"function"==typeof e.then)}Object.defineProperty(t,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};t.default=n},function(e,t,r){"use strict";function n(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.promiseTypeSuffixes||p,r=e.promiseTypeDelimiter||"_";return function(e){var n=e.dispatch;return function(e){return function(u){var a=void 0,l=void 0;if(!u.payload)return e(u);var c=u.payload;if((0,f.default)(c))a=c;else if((0,f.default)(c.promise))a=c.promise,l=c.data;else{if("function"!=typeof c&&"function"!=typeof c.promise)return e(u);if(a=c.promise?c.promise():c(),l=c.promise?c.data:void 0,!(0,f.default)(a))return e(i({},u,{payload:a}))}var p=u.type,d=u.meta,y=o(t,3),s=y[0],v=y[1],m=y[2],b=function(e,t){return i({type:[p,t?m:v].join(r)},null===e||void 0===e?{}:{payload:e},void 0!==d?{meta:d}:{},t?{error:!0}:{})},E=function(e){var t=b(e,!0);throw n(t),e},h=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=b(e,!1);return n(t),{value:e,action:t}};return e(i({type:[p,s].join(r)},void 0!==l?{payload:l}:{},void 0!==d?{meta:d}:{})),a.then(h,E)}}}}Object.defineProperty(t,"__esModule",{value:!0}),t.REJECTED=t.FULFILLED=t.PENDING=void 0;var o=function(){function e(e,t){var r=[],n=!0,o=!1,i=void 0;try{for(var u,f=e[Symbol.iterator]();!(n=(u=f.next()).done)&&(r.push(u.value),!t||r.length!==t);n=!0);}catch(e){o=!0,i=e}finally{try{!n&&f.return&&f.return()}finally{if(o)throw i}}return r}return function(t,r){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e};t.default=n;var u=r(0),f=function(e){return e&&e.__esModule?e:{default:e}}(u),a=t.PENDING="PENDING",l=t.FULFILLED="FULFILLED",c=t.REJECTED="REJECTED",p=[a,l,c]}])}); |
{ | ||
"name": "redux-promise-middleware", | ||
"description": "Redux middleware for handling promises and optimistic updates", | ||
"version": "4.4.2", | ||
"description": "Redux middleware for handling promises, async functions, and optimistic updates", | ||
"version": "5.0.0", | ||
"main": "dist/index.js", | ||
@@ -37,3 +37,3 @@ "module": "dist/es/index.js", | ||
"async", | ||
"example" | ||
"async functions" | ||
], | ||
@@ -84,2 +84,3 @@ "author": "Patrick Burtchaell <patrick@pburtchaell.com> (pburtchaell.com)", | ||
"sinon-chai": "^2.11.0", | ||
"prop-types": "^15.6.0", | ||
"webpack": "^2.6.1", | ||
@@ -86,0 +87,0 @@ "webpack-dev-middleware": "^1.10.2", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
213104
621
38
1