UNPKG

nuxt-isomorphic-fetch

Version:

Isomorphic access to server-side API in Nuxt.js asyncData

298 lines (245 loc) 11 kB
module.exports = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = "/"; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 2); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { module.exports = __webpack_require__(6); /***/ }), /* 1 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "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; }; /* harmony default export */ __webpack_exports__["a"] = (function (path, body, opts) { if ((typeof path === "undefined" ? "undefined" : _typeof(path)) === "object") { opts = Object.assign({}, path); } else { opts = Object.assign({}, opts, { path: path, body: body }); } if (!opts.path) { throw new Error("fetch: path required."); } if (!opts.method) { opts.method = opts.body === undefined ? 'GET' : 'POST'; } return opts; }); /***/ }), /* 2 */ /***/ (function(module, exports, __webpack_require__) { module.exports = __webpack_require__(3); /***/ }), /* 3 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__nuxt__ = __webpack_require__(4); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__fetch__ = __webpack_require__(5); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_2__koa__ = __webpack_require__(7); /* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "nuxt", function() { return __WEBPACK_IMPORTED_MODULE_0__nuxt__["a"]; }); /* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "fetch", function() { return __WEBPACK_IMPORTED_MODULE_1__fetch__["a"]; }); /* harmony reexport (binding) */ __webpack_require__.d(__webpack_exports__, "koa", function() { return __WEBPACK_IMPORTED_MODULE_2__koa__["a"]; }); /***/ }), /* 4 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; /* harmony export (immutable) */ __webpack_exports__["a"] = fetcher; function fetcher(opts) { var fetch = opts.client; fetch.isomorphic = function (context) { for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } if (context.isServer) { return opts.server.apply(opts, [context].concat(args)); } else { return opts.client.apply(opts, args); } }; return fetch; } /***/ }), /* 5 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; /* harmony export (immutable) */ __webpack_exports__["a"] = fetcher; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator__ = __webpack_require__(0); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator__); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__parseArgs__ = __webpack_require__(1); function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function fetcher(opts) { return function () { var _ref = _asyncToGenerator( /*#__PURE__*/__WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator___default.a.mark(function _callee() { var fetchOpts, path, windowFetchOpts, res, _args = arguments; return __WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator___default.a.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: fetchOpts = __WEBPACK_IMPORTED_MODULE_1__parseArgs__["a" /* default */].apply(undefined, _args); path = (opts.prefix || "") + fetchOpts.path; windowFetchOpts = Object.assign({}, opts.opts, { method: fetchOpts.method }); if (fetchOpts.body !== undefined && fetchOpts.body !== null) { windowFetchOpts.body = JSON.stringify(fetchOpts.body); windowFetchOpts.headers = { 'Content-Type': 'application/json' }; } _context.next = 6; return window.fetch(path, windowFetchOpts); case 6: res = _context.sent; if (res.ok) { _context.next = 9; break; } throw new Error(res); case 9: _context.next = 11; return res.json(); case 11: return _context.abrupt('return', _context.sent); case 12: case 'end': return _context.stop(); } } }, _callee, this); })); function fetch() { return _ref.apply(this, arguments); } return fetch; }(); } /***/ }), /* 6 */ /***/ (function(module, exports) { module.exports = require("regenerator-runtime"); /***/ }), /* 7 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; /* harmony export (immutable) */ __webpack_exports__["a"] = fetcher; /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator__ = __webpack_require__(0); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator__); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__parseArgs__ = __webpack_require__(1); function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function fetcher(opts) { return function () { var _ref = _asyncToGenerator( /*#__PURE__*/__WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator___default.a.mark(function _callee(context) { for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } var fetchOpts, koaCtx; return __WEBPACK_IMPORTED_MODULE_0__Users_semenov_work_github_nuxt_isomorphic_fetch_node_modules_babel_runtime_regenerator___default.a.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: fetchOpts = __WEBPACK_IMPORTED_MODULE_1__parseArgs__["a" /* default */].apply(undefined, _toConsumableArray(args)); koaCtx = { method: fetchOpts.method, path: (opts.prefix || "") + fetchOpts.path, request: { body: fetchOpts.body }, disableBodyParser: true }; _context.next = 4; return opts.middleware(koaCtx, function () {}); case 4: if (!koaCtx.status) { koaCtx.status = koaCtx.body !== undefined ? 200 : 404; } if (!(koaCtx.status != 200)) { _context.next = 7; break; } throw new Error("Isomorphic Koa fetch returned HTTP status " + koaCtx.status + "."); case 7: return _context.abrupt("return", koaCtx.body); case 8: case "end": return _context.stop(); } } }, _callee, this); })); function fetch(_x) { return _ref.apply(this, arguments); } return fetch; }(); } /***/ }) /******/ ]); //# sourceMappingURL=main.map