@knine-sdk/fetch
Version:
Fetchers for Knine projects.
22 lines (19 loc) • 817 B
JavaScript
import { __awaiter, __rest } from './node_modules/tslib/tslib.es6.js';
import invariant from 'tiny-invariant';
import nodeFetch from 'node-fetch';
const fetchWithFallbacks = (inputs, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
invariant(inputs.length > 0, 'Inputs are required');
const { fetch = nodeFetch } = options, init = __rest(options, ["fetch"]);
const [input, ...restInputs] = inputs;
try {
const response = yield fetch(input, init);
invariant(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);
}
});
export { fetchWithFallbacks };