function ApiError(status, statusText, response) { _classCallCheck(this, ApiError); _Error4.call(this); this.name = 'ApiError'; this.status = status; this.statusText = statusText; this.response = response; this.message = status + ' - ' + statusText; }
...
if (typeof failureType === 'string' || typeof failureType === 'symbol') {
failureType = { type: failureType };
}
failureType = _extends({
payload: function payload(action, state, res) {
return getJSON(res).then(function (json) {
return new _errors.ApiError(res.status, res.statusText, json);
});
}
}, failureType);
return [requestType, successType, failureType];
}
...
function InternalError(message) { _classCallCheck(this, InternalError); _Error2.call(this); this.name = 'InternalError'; this.message = message; }
...
context$1$0.next = 10;
break;
case 6:
context$1$0.prev = 6;
context$1$0.t0 = context$1$0['catch'](0);
descriptor.payload = new _errors.InternalError(context$1$0.t0.message);
descriptor.error = true;
case 10:
context$1$0.prev = 10;
context$1$0.next = 13;
return _regeneratorRuntime.awrap(typeof descriptor.meta === 'function' ? descriptor.meta.apply(descriptor, args) : descriptor
.meta);
...
function InvalidRSAA(validationErrors) { _classCallCheck(this, InvalidRSAA); _Error.call(this); this.name = 'InvalidRSAA'; this.message = 'Invalid RSAA'; this.validationErrors = validationErrors; }
...
_requestType = _callAPI.types[0];
if (_requestType && _requestType.type) {
_requestType = _requestType.type;
}
next({
type: _requestType,
payload: new _errors.InvalidRSAA(validationErrors),
error: true
});
}
return context$3$0.abrupt('return');
case 7:
callAPI = action[_CALL_API2['default']];
...
function RequestError(message) { _classCallCheck(this, RequestError); _Error3.call(this); this.name = 'RequestError'; this.message = message; }
...
break;
case 24:
context$3$0.prev = 24;
context$3$0.t0 = context$3$0['catch'](19);
context$3$0.next = 28;
return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, {
payload: new _errors.RequestError('[CALL_API].bailout function failed'),
error: true
}), [action, getState()]));
case 28:
context$3$0.t1 = context$3$0.sent;
return context$3$0.abrupt('return', next(context$3$0.t1));
...
function apiMiddleware(_ref) { var _this = this; var getState = _ref.getState; return function (next) { return function callee$2$0(action) { var validationErrors, _callAPI, _requestType, callAPI, endpoint, headers, method, body, credentials, bailout, types, _normalizeTypeDescriptors , requestType, successType, failureType, res; return _regeneratorRuntime.async(function callee$2$0$(context$3$0) { while (1) switch (context$3$0.prev = context$3$0.next) { case 0: if (_validation.isRSAA(action)) { context$3$0.next = 2; break; } return context$3$0.abrupt('return', next(action)); case 2: validationErrors = _validation.validateRSAA(action); if (!validationErrors.length) { context$3$0.next = 7; break; } _callAPI = action[_CALL_API2['default']]; if (_callAPI.types && Array.isArray(_callAPI.types)) { _requestType = _callAPI.types[0]; if (_requestType && _requestType.type) { _requestType = _requestType.type; } next({ type: _requestType, payload: new _errors.InvalidRSAA(validationErrors), error: true }); } return context$3$0.abrupt('return'); case 7: callAPI = action[_CALL_API2['default']]; endpoint = callAPI.endpoint; headers = callAPI.headers; method = callAPI.method; body = callAPI.body; credentials = callAPI.credentials; bailout = callAPI.bailout; types = callAPI.types; _normalizeTypeDescriptors = _util.normalizeTypeDescriptors(types); requestType = _normalizeTypeDescriptors[0]; successType = _normalizeTypeDescriptors[1]; failureType = _normalizeTypeDescriptors[2]; context$3$0.prev = 19; if (!(typeof bailout === 'boolean' && bailout || typeof bailout === 'function' && bailout(getState()))) { context$3$0.next = 22; break; } return context$3$0.abrupt('return'); case 22: context$3$0.next = 30; break; case 24: context$3$0.prev = 24; context$3$0.t0 = context$3$0['catch'](19); context$3$0.next = 28; return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, { payload: new _errors.RequestError('[CALL_API].bailout function failed'), error: true }), [action, getState()])); case 28: context$3$0.t1 = context$3$0.sent; return context$3$0.abrupt('return', next(context$3$0.t1)); case 30: if (!(typeof endpoint === 'function')) { context$3$0.next = 41; break; } context$3$0.prev = 31; endpoint = endpoint(getState()); context$3$0.next = 41; break; case 35: context$3$0.prev = 35; context$3$0.t2 = context$3$0['catch'](31); context$3$0.next = 39; return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, { payload: new _errors.RequestError('[CALL_API].endpoint function failed'), error: true }), [action, getState()])); case 39: context$3$0.t3 = context$3$0.sent; return context$3$0.abrupt('return', next(context$3$0.t3)); case 41: if (!(typeof headers === 'function')) { context$3$0.next = 52; break; } context$3$0.prev = 42; headers = headers(getState()); context$3$0.next = 52; break; case 46: context$3$0.prev = 46; context$3$0.t4 = context$3$0['catch'](42); context ...
n/a
function getJSON(res) { var contentType, emptyCodes; return _regeneratorRuntime.async(function getJSON$(context$1$0) { while (1) switch (context$1$0.prev = context$1$0.next) { case 0: contentType = res.headers.get('Content-Type'); emptyCodes = [204, 205]; if (!(! ~emptyCodes.indexOf(res.status) && contentType && ~contentType.indexOf('json'))) { context$1$0.next = 8; break; } context$1$0.next = 5; return _regeneratorRuntime.awrap(res.json()); case 5: return context$1$0.abrupt('return', context$1$0.sent); case 8: context$1$0.next = 10; return _regeneratorRuntime.awrap(_Promise.resolve()); case 10: return context$1$0.abrupt('return', context$1$0.sent); case 11: case 'end': return context$1$0.stop(); } }, null, this); }
n/a
function isRSAA(action) { return _lodashIsplainobject2['default'](action) && action.hasOwnProperty(_CALL_API2['default']); }
...
return function (next) {
return function callee$2$0(action) {
var validationErrors, _callAPI, _requestType, callAPI, endpoint, headers, method, body, credentials, bailout, types, _normalizeTypeDescriptors
, requestType, successType, failureType, res;
return _regeneratorRuntime.async(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
if (_validation.isRSAA(action)) {
context$3$0.next = 2;
break;
}
return context$3$0.abrupt('return', next(action));
case 2:
...
function isValidRSAA(action) { return !validateRSAA(action).length; }
n/a
function validateRSAA(action) { var validationErrors = []; var validCallAPIKeys = ['endpoint', 'method', 'body', 'headers', 'credentials', 'bailout', 'types']; var validMethods = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; var validCredentials = ['omit', 'same-origin', 'include']; if (!isRSAA(action)) { validationErrors.push('RSAAs must be plain JavaScript objects with a [CALL_API] property'); return validationErrors; } for (var key in action) { if (key !== [_CALL_API2['default']]) { validationErrors.push('Invalid root key: ' + key); } } var callAPI = action[_CALL_API2['default']]; if (!_lodashIsplainobject2['default'](callAPI)) { validationErrors.push('[CALL_API] property must be a plain JavaScript object'); } for (var key in callAPI) { if (! ~validCallAPIKeys.indexOf(key)) { validationErrors.push('Invalid [CALL_API] key: ' + key); } } var endpoint = callAPI.endpoint; var method = callAPI.method; var headers = callAPI.headers; var credentials = callAPI.credentials; var types = callAPI.types; var bailout = callAPI.bailout; if (typeof endpoint === 'undefined') { validationErrors.push('[CALL_API] must have an endpoint property'); } else if (typeof endpoint !== 'string' && typeof endpoint !== 'function') { validationErrors.push('[CALL_API].endpoint property must be a string or a function'); } if (typeof method === 'undefined') { validationErrors.push('[CALL_API] must have a method property'); } else if (typeof method !== 'string') { validationErrors.push('[CALL_API].method property must be a string'); } else if (! ~validMethods.indexOf(method.toUpperCase())) { validationErrors.push('Invalid [CALL_API].method: ' + method.toUpperCase()); } if (typeof headers !== 'undefined' && !_lodashIsplainobject2['default'](headers) && typeof headers !== 'function') { validationErrors.push('[CALL_API].headers property must be undefined, a plain JavaScript object, or a function'); } if (typeof credentials !== 'undefined') { if (typeof credentials !== 'string') { validationErrors.push('[CALL_API].credentials property must be undefined, or a string'); } else if (! ~validCredentials.indexOf(credentials)) { validationErrors.push('Invalid [CALL_API].credentials: ' + credentials); } } if (typeof bailout !== 'undefined' && typeof bailout !== 'boolean' && typeof bailout !== 'function') { validationErrors.push('[CALL_API].bailout property must be undefined, a boolean, or a function'); } if (typeof types === 'undefined') { validationErrors.push('[CALL_API] must have a types property'); } else if (!Array.isArray(types) || types.length !== 3) { validationErrors.push('[CALL_API].types property must be an array of length 3'); } else { var requestType = types[0]; var successType = types[1]; var failureType = types[2]; if (typeof requestType !== 'string' && typeof requestType !== 'symbol' && !isValidTypeDescriptor(requestType)) { validationErrors.push('Invalid request type'); } if (typeof successType !== 'string' && typeof successType !== 'symbol' && !isValidTypeDescriptor(successType)) { validationErrors.push('Invalid success type'); } if (typeof failureType !== 'string' && typeof failureType !== 'symbol' && !isValidTypeDescriptor(failureType)) { validationErrors.push('Invalid failure type'); } } return validationErrors; }
...
context$3$0.next = 2;
break;
}
return context$3$0.abrupt('return', next(action));
case 2:
validationErrors = _validation.validateRSAA(action);
if (!validationErrors.length) {
context$3$0.next = 7;
break;
}
_callAPI = action[_CALL_API2['default']];
...
function ApiError(status, statusText, response) { _classCallCheck(this, ApiError); _Error4.call(this); this.name = 'ApiError'; this.status = status; this.statusText = statusText; this.response = response; this.message = status + ' - ' + statusText; }
...
if (typeof failureType === 'string' || typeof failureType === 'symbol') {
failureType = { type: failureType };
}
failureType = _extends({
payload: function payload(action, state, res) {
return getJSON(res).then(function (json) {
return new _errors.ApiError(res.status, res.statusText, json);
});
}
}, failureType);
return [requestType, successType, failureType];
}
...
function InternalError(message) { _classCallCheck(this, InternalError); _Error2.call(this); this.name = 'InternalError'; this.message = message; }
...
context$1$0.next = 10;
break;
case 6:
context$1$0.prev = 6;
context$1$0.t0 = context$1$0['catch'](0);
descriptor.payload = new _errors.InternalError(context$1$0.t0.message);
descriptor.error = true;
case 10:
context$1$0.prev = 10;
context$1$0.next = 13;
return _regeneratorRuntime.awrap(typeof descriptor.meta === 'function' ? descriptor.meta.apply(descriptor, args) : descriptor
.meta);
...
function InvalidRSAA(validationErrors) { _classCallCheck(this, InvalidRSAA); _Error.call(this); this.name = 'InvalidRSAA'; this.message = 'Invalid RSAA'; this.validationErrors = validationErrors; }
...
_requestType = _callAPI.types[0];
if (_requestType && _requestType.type) {
_requestType = _requestType.type;
}
next({
type: _requestType,
payload: new _errors.InvalidRSAA(validationErrors),
error: true
});
}
return context$3$0.abrupt('return');
case 7:
callAPI = action[_CALL_API2['default']];
...
function RequestError(message) { _classCallCheck(this, RequestError); _Error3.call(this); this.name = 'RequestError'; this.message = message; }
...
break;
case 24:
context$3$0.prev = 24;
context$3$0.t0 = context$3$0['catch'](19);
context$3$0.next = 28;
return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, {
payload: new _errors.RequestError('[CALL_API].bailout function failed'),
error: true
}), [action, getState()]));
case 28:
context$3$0.t1 = context$3$0.sent;
return context$3$0.abrupt('return', next(context$3$0.t1));
...
function apiMiddleware(_ref) { var _this = this; var getState = _ref.getState; return function (next) { return function callee$2$0(action) { var validationErrors, _callAPI, _requestType, callAPI, endpoint, headers, method, body, credentials, bailout, types, _normalizeTypeDescriptors , requestType, successType, failureType, res; return _regeneratorRuntime.async(function callee$2$0$(context$3$0) { while (1) switch (context$3$0.prev = context$3$0.next) { case 0: if (_validation.isRSAA(action)) { context$3$0.next = 2; break; } return context$3$0.abrupt('return', next(action)); case 2: validationErrors = _validation.validateRSAA(action); if (!validationErrors.length) { context$3$0.next = 7; break; } _callAPI = action[_CALL_API2['default']]; if (_callAPI.types && Array.isArray(_callAPI.types)) { _requestType = _callAPI.types[0]; if (_requestType && _requestType.type) { _requestType = _requestType.type; } next({ type: _requestType, payload: new _errors.InvalidRSAA(validationErrors), error: true }); } return context$3$0.abrupt('return'); case 7: callAPI = action[_CALL_API2['default']]; endpoint = callAPI.endpoint; headers = callAPI.headers; method = callAPI.method; body = callAPI.body; credentials = callAPI.credentials; bailout = callAPI.bailout; types = callAPI.types; _normalizeTypeDescriptors = _util.normalizeTypeDescriptors(types); requestType = _normalizeTypeDescriptors[0]; successType = _normalizeTypeDescriptors[1]; failureType = _normalizeTypeDescriptors[2]; context$3$0.prev = 19; if (!(typeof bailout === 'boolean' && bailout || typeof bailout === 'function' && bailout(getState()))) { context$3$0.next = 22; break; } return context$3$0.abrupt('return'); case 22: context$3$0.next = 30; break; case 24: context$3$0.prev = 24; context$3$0.t0 = context$3$0['catch'](19); context$3$0.next = 28; return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, { payload: new _errors.RequestError('[CALL_API].bailout function failed'), error: true }), [action, getState()])); case 28: context$3$0.t1 = context$3$0.sent; return context$3$0.abrupt('return', next(context$3$0.t1)); case 30: if (!(typeof endpoint === 'function')) { context$3$0.next = 41; break; } context$3$0.prev = 31; endpoint = endpoint(getState()); context$3$0.next = 41; break; case 35: context$3$0.prev = 35; context$3$0.t2 = context$3$0['catch'](31); context$3$0.next = 39; return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, { payload: new _errors.RequestError('[CALL_API].endpoint function failed'), error: true }), [action, getState()])); case 39: context$3$0.t3 = context$3$0.sent; return context$3$0.abrupt('return', next(context$3$0.t3)); case 41: if (!(typeof headers === 'function')) { context$3$0.next = 52; break; } context$3$0.prev = 42; headers = headers(getState()); context$3$0.next = 52; break; case 46: context$3$0.prev = 46; context$3$0.t4 = context$3$0['catch'](42); context ...
n/a
function actionWith(descriptor, args) { return _regeneratorRuntime.async(function actionWith$(context$1$0) { while (1) switch (context$1$0.prev = context$1$0.next) { case 0: context$1$0.prev = 0; context$1$0.next = 3; return _regeneratorRuntime.awrap(typeof descriptor.payload === 'function' ? descriptor.payload.apply(descriptor, args) : descriptor.payload); case 3: descriptor.payload = context$1$0.sent; context$1$0.next = 10; break; case 6: context$1$0.prev = 6; context$1$0.t0 = context$1$0['catch'](0); descriptor.payload = new _errors.InternalError(context$1$0.t0.message); descriptor.error = true; case 10: context$1$0.prev = 10; context$1$0.next = 13; return _regeneratorRuntime.awrap(typeof descriptor.meta === 'function' ? descriptor.meta.apply(descriptor, args) : descriptor .meta); case 13: descriptor.meta = context$1$0.sent; context$1$0.next = 21; break; case 16: context$1$0.prev = 16; context$1$0.t1 = context$1$0['catch'](10); delete descriptor.meta; descriptor.payload = new _errors.InternalError(context$1$0.t1.message); descriptor.error = true; case 21: return context$1$0.abrupt('return', descriptor); case 22: case 'end': return context$1$0.stop(); } }, null, this, [[0, 6], [10, 16]]); }
...
context$3$0.next = 30;
break;
case 24:
context$3$0.prev = 24;
context$3$0.t0 = context$3$0['catch'](19);
context$3$0.next = 28;
return _regeneratorRuntime.awrap(_util.actionWith(_extends({}, requestType, {
payload: new _errors.RequestError('[CALL_API].bailout function failed'),
error: true
}), [action, getState()]));
case 28:
context$3$0.t1 = context$3$0.sent;
return context$3$0.abrupt('return', next(context$3$0.t1));
...
function getJSON(res) { var contentType, emptyCodes; return _regeneratorRuntime.async(function getJSON$(context$1$0) { while (1) switch (context$1$0.prev = context$1$0.next) { case 0: contentType = res.headers.get('Content-Type'); emptyCodes = [204, 205]; if (!(! ~emptyCodes.indexOf(res.status) && contentType && ~contentType.indexOf('json'))) { context$1$0.next = 8; break; } context$1$0.next = 5; return _regeneratorRuntime.awrap(res.json()); case 5: return context$1$0.abrupt('return', context$1$0.sent); case 8: context$1$0.next = 10; return _regeneratorRuntime.awrap(_Promise.resolve()); case 10: return context$1$0.abrupt('return', context$1$0.sent); case 11: case 'end': return context$1$0.stop(); } }, null, this); }
n/a
function normalizeTypeDescriptors(types) { var requestType = types[0]; var successType = types[1]; var failureType = types[2]; if (typeof requestType === 'string' || typeof requestType === 'symbol') { requestType = { type: requestType }; } if (typeof successType === 'string' || typeof successType === 'symbol') { successType = { type: successType }; } successType = _extends({ payload: function payload(action, state, res) { return getJSON(res); } }, successType); if (typeof failureType === 'string' || typeof failureType === 'symbol') { failureType = { type: failureType }; } failureType = _extends({ payload: function payload(action, state, res) { return getJSON(res).then(function (json) { return new _errors.ApiError(res.status, res.statusText, json); }); } }, failureType); return [requestType, successType, failureType]; }
...
endpoint = callAPI.endpoint;
headers = callAPI.headers;
method = callAPI.method;
body = callAPI.body;
credentials = callAPI.credentials;
bailout = callAPI.bailout;
types = callAPI.types;
_normalizeTypeDescriptors = _util.normalizeTypeDescriptors(types);
requestType = _normalizeTypeDescriptors[0];
successType = _normalizeTypeDescriptors[1];
failureType = _normalizeTypeDescriptors[2];
context$3$0.prev = 19;
if (!(typeof bailout === 'boolean' && bailout || typeof bailout === 'function' && bailout
(getState()))) {
context$3$0.next = 22;
...
function isRSAA(action) { return _lodashIsplainobject2['default'](action) && action.hasOwnProperty(_CALL_API2['default']); }
...
return function (next) {
return function callee$2$0(action) {
var validationErrors, _callAPI, _requestType, callAPI, endpoint, headers, method, body, credentials, bailout, types, _normalizeTypeDescriptors
, requestType, successType, failureType, res;
return _regeneratorRuntime.async(function callee$2$0$(context$3$0) {
while (1) switch (context$3$0.prev = context$3$0.next) {
case 0:
if (_validation.isRSAA(action)) {
context$3$0.next = 2;
break;
}
return context$3$0.abrupt('return', next(action));
case 2:
...
function isValidRSAA(action) { return !validateRSAA(action).length; }
n/a
function isValidTypeDescriptor(obj) { var validKeys = ['type', 'payload', 'meta']; if (!_lodashIsplainobject2['default'](obj)) { return false; } for (var key in obj) { if (! ~validKeys.indexOf(key)) { return false; } } if (!('type' in obj)) { return false; } else if (typeof obj.type !== 'string' && typeof obj.type !== 'symbol') { return false; } return true; }
n/a
function validateRSAA(action) { var validationErrors = []; var validCallAPIKeys = ['endpoint', 'method', 'body', 'headers', 'credentials', 'bailout', 'types']; var validMethods = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS']; var validCredentials = ['omit', 'same-origin', 'include']; if (!isRSAA(action)) { validationErrors.push('RSAAs must be plain JavaScript objects with a [CALL_API] property'); return validationErrors; } for (var key in action) { if (key !== [_CALL_API2['default']]) { validationErrors.push('Invalid root key: ' + key); } } var callAPI = action[_CALL_API2['default']]; if (!_lodashIsplainobject2['default'](callAPI)) { validationErrors.push('[CALL_API] property must be a plain JavaScript object'); } for (var key in callAPI) { if (! ~validCallAPIKeys.indexOf(key)) { validationErrors.push('Invalid [CALL_API] key: ' + key); } } var endpoint = callAPI.endpoint; var method = callAPI.method; var headers = callAPI.headers; var credentials = callAPI.credentials; var types = callAPI.types; var bailout = callAPI.bailout; if (typeof endpoint === 'undefined') { validationErrors.push('[CALL_API] must have an endpoint property'); } else if (typeof endpoint !== 'string' && typeof endpoint !== 'function') { validationErrors.push('[CALL_API].endpoint property must be a string or a function'); } if (typeof method === 'undefined') { validationErrors.push('[CALL_API] must have a method property'); } else if (typeof method !== 'string') { validationErrors.push('[CALL_API].method property must be a string'); } else if (! ~validMethods.indexOf(method.toUpperCase())) { validationErrors.push('Invalid [CALL_API].method: ' + method.toUpperCase()); } if (typeof headers !== 'undefined' && !_lodashIsplainobject2['default'](headers) && typeof headers !== 'function') { validationErrors.push('[CALL_API].headers property must be undefined, a plain JavaScript object, or a function'); } if (typeof credentials !== 'undefined') { if (typeof credentials !== 'string') { validationErrors.push('[CALL_API].credentials property must be undefined, or a string'); } else if (! ~validCredentials.indexOf(credentials)) { validationErrors.push('Invalid [CALL_API].credentials: ' + credentials); } } if (typeof bailout !== 'undefined' && typeof bailout !== 'boolean' && typeof bailout !== 'function') { validationErrors.push('[CALL_API].bailout property must be undefined, a boolean, or a function'); } if (typeof types === 'undefined') { validationErrors.push('[CALL_API] must have a types property'); } else if (!Array.isArray(types) || types.length !== 3) { validationErrors.push('[CALL_API].types property must be an array of length 3'); } else { var requestType = types[0]; var successType = types[1]; var failureType = types[2]; if (typeof requestType !== 'string' && typeof requestType !== 'symbol' && !isValidTypeDescriptor(requestType)) { validationErrors.push('Invalid request type'); } if (typeof successType !== 'string' && typeof successType !== 'symbol' && !isValidTypeDescriptor(successType)) { validationErrors.push('Invalid success type'); } if (typeof failureType !== 'string' && typeof failureType !== 'symbol' && !isValidTypeDescriptor(failureType)) { validationErrors.push('Invalid failure type'); } } return validationErrors; }
...
context$3$0.next = 2;
break;
}
return context$3$0.abrupt('return', next(action));
case 2:
validationErrors = _validation.validateRSAA(action);
if (!validationErrors.length) {
context$3$0.next = 7;
break;
}
_callAPI = action[_CALL_API2['default']];
...