UNPKG

@botonic/plugin-contentful

Version:

## What Does This Plugin Do?

48 lines 2.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CachedClientApi = void 0; const tslib_1 = require("tslib"); const memoizee_1 = tslib_1.__importDefault(require("memoizee")); const util_1 = require("../../util"); const memoizer_1 = require("../../util/memoizer"); class CachedClientApi { constructor(client, cacheTtlMs = 10000, errorReport) { this.client = client; this.cacheTtlMs = cacheTtlMs; this.errorReport = errorReport; this.getAsset = this.memoize(client.getAsset.bind(client), 2); this.getAssets = this.memoize(client.getAssets.bind(client), 1); this.getEntries = this.memoize(client.getEntries.bind(client), 1); this.getEntry = this.memoize(client.getEntry.bind(client), 2); this.getContentType = this.memoize(client.getContentType.bind(client), 1); } memoize(func, functionLength) { const memo = (0, memoizee_1.default)(func, this.options(functionLength)); const dec = (0, util_1.rethrowDecorator)(memo, (e, ...args) => tslib_1.__awaiter(this, void 0, void 0, function* () { yield this.errorReport('Error calling Contentful API', String(func), args, e); // sleep required to ensure that after a failed invocation, the next one also always fails // https://github.com/medikoo/memoizee/issues/117 return (0, util_1.sleep)(0); })); return dec; } options(length) { return { promise: true, primitive: true, maxAge: this.cacheTtlMs == CachedClientApi.NO_EXPIRATION ? undefined : this.cacheTtlMs, length, normalizer: memoizer_1.jsonNormalizer, }; } static normalizer(...args) { return args .map((arg) => JSON.stringify(arg)) .reduce((a, b) => a + b); } } exports.CachedClientApi = CachedClientApi; CachedClientApi.NO_EXPIRATION = -1; //# sourceMappingURL=cache.js.map