UNPKG

markbello-iguazu-rest

Version:

A Redux REST caching library that follows Iguazu patterns

159 lines (137 loc) 6.45 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports.default = executeFetch; var _deepmerge = _interopRequireDefault(require("deepmerge")); var types = _interopRequireWildcard(require("../types")); var _url = require("../helpers/url"); var _config = _interopRequireDefault(require("../config")); var _asyncSideEffects = require("./asyncSideEffects"); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* * Copyright 2018 American Express Travel Related Services Company, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing * permissions and limitations under the License. */ function startsWith(string, target) { return String(string).slice(0, target.length) === target; } function extractDataFromResponse(res) { var contentType, isJson, body, status; return regeneratorRuntime.async(function extractDataFromResponse$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: contentType = res.headers.get('Content-Type'); isJson = startsWith(contentType, 'application/json'); _context.next = 4; return regeneratorRuntime.awrap(res[isJson ? 'json' : 'text']()); case 4: body = _context.sent; status = res.status; return _context.abrupt("return", res.ok ? Promise.resolve(body) : Promise.reject(Object.assign(new Error("".concat(res.statusText, " (").concat(res.url, ")")), { body: body, status: status }))); case 7: case "end": return _context.stop(); } } }); } var actionTypeMethodMap = { LOAD: 'GET', LOAD_COLLECTION: 'GET', CREATE: 'POST', UPDATE: 'PUT', DESTROY: 'DELETE', PATCH: 'PATCH' }; function getAsyncData(_ref) { var resource, id, opts, actionType, state, fetchClient, resources, defaultOpts, baseFetch, composeFetch, _resources$resource$f, url, resourceOpts, fetchOpts, fetchUrl, selectedFetchClient, composedFetchClient, res, rawData, transformData, data; return regeneratorRuntime.async(function getAsyncData$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: resource = _ref.resource, id = _ref.id, opts = _ref.opts, actionType = _ref.actionType, state = _ref.state, fetchClient = _ref.fetchClient; resources = _config.default.resources, defaultOpts = _config.default.defaultOpts, baseFetch = _config.default.baseFetch, composeFetch = _config.default.composeFetch; _resources$resource$f = resources[resource].fetch(id, actionType, state), url = _resources$resource$f.url, resourceOpts = _resources$resource$f.opts; fetchOpts = _deepmerge.default.all([{ method: actionTypeMethodMap[actionType] }, defaultOpts || {}, resourceOpts || {}, opts || {}]); fetchUrl = (0, _url.buildFetchUrl)({ url: url, id: id, opts: fetchOpts }); selectedFetchClient = fetchClient || baseFetch; composedFetchClient = composeFetch(selectedFetchClient); _context2.next = 9; return regeneratorRuntime.awrap(composedFetchClient(fetchUrl, fetchOpts)); case 9: res = _context2.sent; _context2.next = 12; return regeneratorRuntime.awrap(extractDataFromResponse(res)); case 12: rawData = _context2.sent; transformData = _config.default.resources[resource].transformData; data = transformData ? transformData(rawData, { id: id, opts: opts, actionType: actionType }) : rawData; return _context2.abrupt("return", data); case 16: case "end": return _context2.stop(); } } }); } function executeFetch(_ref2) { var resource = _ref2.resource, id = _ref2.id, opts = _ref2.opts, actionType = _ref2.actionType; return function (dispatch, getState) { var _ref3 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, fetchClient = _ref3.fetchClient; var promise = getAsyncData({ resource: resource, id: id, opts: opts, actionType: actionType, state: getState(), fetchClient: fetchClient }); dispatch({ type: types["".concat(actionType, "_STARTED")], resource: resource, id: id, opts: opts, promise: promise }); dispatch((0, _asyncSideEffects.waitAndDispatchFinished)(promise, { type: actionType, resource: resource, id: id, opts: opts })); return promise; }; }