UNPKG

@knine-sdk/fetch

Version:
31 lines (24 loc) 1.25 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('./node_modules/tslib/tslib.es6.js'); var invariant = require('tiny-invariant'); var nodeFetch = require('node-fetch'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant); var nodeFetch__default = /*#__PURE__*/_interopDefaultLegacy(nodeFetch); const fetchWithFallbacks = (inputs, options = {}) => tslib_es6.__awaiter(void 0, void 0, void 0, function* () { invariant__default["default"](inputs.length > 0, 'Inputs are required'); const { fetch = nodeFetch__default["default"] } = options, init = tslib_es6.__rest(options, ["fetch"]); const [input, ...restInputs] = inputs; try { const response = yield fetch(input, init); invariant__default["default"](response === null || response === void 0 ? void 0 : response.ok, 'Request failed'); return response; } catch (error) { if (!restInputs.length) throw error; return yield fetchWithFallbacks(restInputs, options); } }); exports.fetchWithFallbacks = fetchWithFallbacks;