@gooddata/api-client-bear
Version:
API Client for the GoodData platform
68 lines • 2.51 kB
JavaScript
// (C) 2023 GoodData Corporation
import { LRUCache } from "lru-cache";
import identity from "lodash/identity.js";
import { invariant } from "ts-invariant";
import { decoratedSdk } from "../decoratedModules/index.js";
import { cachedUser } from "./user.js";
import { cachedProject } from "./project.js";
import { cachingEnabled } from "./utils.js";
/**
* These are the recommended settings for the bear client caching.
*
* @remarks
* For more information on what the options mean see {@link CachingConfiguration}.
*
* @public
*/
export const RecommendedCachingConfiguration = {
enableCurrentProfileCaching: true,
maxProjectFeatureFlags: 1,
maxProjectPermissions: 1,
};
function assertPositiveOrUndefined(value, valueName) {
invariant(value === undefined || value > 0, `${valueName} to cache must be positive or undefined, got: ${value}`);
}
function cacheControl(ctx) {
const control = {
resetCurrentProfile: () => {
ctx.caches.currentProfile = undefined;
},
resetProjectFeatureFlags: () => {
ctx.caches.projectFeatureFlags?.clear();
},
resetAll: () => {
control.resetCurrentProfile();
control.resetProjectFeatureFlags();
},
};
return control;
}
export function withCaching(sdk, config) {
assertPositiveOrUndefined(config.maxProjectFeatureFlags, "maxProjectFeatureFlags");
assertPositiveOrUndefined(config.maxProjectPermissions, "maxProjectPermissions");
const currentProfileCaching = cachingEnabled(config.enableCurrentProfileCaching);
const projectFeatureFlags = cachingEnabled(config.maxProjectFeatureFlags);
const projectPermissions = cachingEnabled(config.maxProjectPermissions);
const ctx = {
caches: {
projectFeatureFlags: projectFeatureFlags
? new LRUCache({ max: config.maxProjectFeatureFlags })
: undefined,
projectPermissions: projectPermissions
? new LRUCache({ max: config.maxProjectPermissions })
: undefined,
currentProfile: undefined,
},
config,
};
const project = projectFeatureFlags || projectPermissions ? cachedProject(ctx) : identity;
const user = currentProfileCaching ? cachedUser(ctx) : identity;
if (config.onCacheReady) {
config.onCacheReady(cacheControl(ctx));
}
return decoratedSdk(sdk, {
project,
user,
});
}
//# sourceMappingURL=cachingClient.js.map