UNPKG

cache-kit

Version:

A simple caching layer for fetch requests — supports memory, browser (localStorage), and Node.js (filesystem) adapters with smart strategies.

84 lines (76 loc) 4.16 kB
/****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise, SuppressedError, Symbol, Iterator */ function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { var e = new Error(message); return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; }; const cachedFetch = (url, options, cacheOptions) => __awaiter(void 0, void 0, void 0, function* () { var _a; const methodName = (_a = options === null || options === void 0 ? void 0 : options.method) === null || _a === void 0 ? void 0 : _a.toLowerCase(); let response; const normalizedUrl = new URL(url).href; try { if ((!methodName || methodName === 'get') && !(options === null || options === void 0 ? void 0 : options.body)) { switch (cacheOptions.adapter) { case 'memory': { //Import memory adaptor here const { default: memoryCachedFetch } = yield import('./memory-BUmhYjW2.js'); response = yield memoryCachedFetch(normalizedUrl, options, cacheOptions); break; } case 'browser': { if (typeof window === 'undefined') { throw new Error('Browser adapter cannot be used in a Node.js environment.'); } //Import here const { default: browserCachedFetch } = yield import('./browser-CCDRMnUN.js'); response = yield browserCachedFetch(normalizedUrl, options, cacheOptions); break; } case 'node': { if (typeof window !== 'undefined') { throw new Error('Node adapter cannot be used in the browser.'); } //Import here const { default: nodeCachedFetch } = yield import('./node-CecXw4QL.js'); response = yield nodeCachedFetch(normalizedUrl, options, cacheOptions); break; } default: throw new Error(`Unknown adapter: ${cacheOptions.adapter}`); } } else { // If the request is not a GET request or doesn't have a body, we can fetch it. response = yield fetch(url, options); } } catch (error) { console.warn('⚠️ cachedFetch fallback due to error:', error); // Always fallback to a plain fetch in case of failure response = yield fetch(url, options); } return response; }); export { __awaiter as _, cachedFetch as c }; //# sourceMappingURL=index-2gRjNNLr.js.map