@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
44 lines • 1.8 kB
JavaScript
import { __awaiter } from "tslib";
import memoize from 'memoizee';
import { rethrowDecorator, sleep } from '../../util';
import { jsonNormalizer } from '../../util/memoizer';
export 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 = memoize(func, this.options(functionLength));
const dec = rethrowDecorator(memo, (e, ...args) => __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 sleep(0);
}));
return dec;
}
options(length) {
return {
promise: true,
primitive: true,
maxAge: this.cacheTtlMs == CachedClientApi.NO_EXPIRATION
? undefined
: this.cacheTtlMs,
length,
normalizer: jsonNormalizer,
};
}
static normalizer(...args) {
return args
.map((arg) => JSON.stringify(arg))
.reduce((a, b) => a + b);
}
}
CachedClientApi.NO_EXPIRATION = -1;
//# sourceMappingURL=cache.js.map