UNPKG

with-simple-caching

Version:

A wrapper that makes it simple to add caching to any function

27 lines 1.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCacheFromCacheOptionOrFromForKeyArgs = void 0; const type_fns_1 = require("type-fns"); const BadRequestError_1 = require("../../utils/errors/BadRequestError"); const withExtendableCachingAsync_1 = require("../wrappers/withExtendableCachingAsync"); const getCacheFromCacheOption_1 = require("./getCacheFromCacheOption"); /** * a function which is capable of grabbing the cache from arguments to the `invalidate` or `update` commands, supporting both the case when invoked with `forInput` and when invoked with `forKey` */ const getCacheFromCacheOptionOrFromForKeyArgs = ({ args, options, trigger, }) => { // if the args have the forInput property, then we can grab the cache like normal if ((0, withExtendableCachingAsync_1.hasForInputProperty)(args)) return (0, getCacheFromCacheOption_1.getCacheFromCacheOption)({ forInput: args.forInput, cacheOption: options.cache, }); // otherwise, if the cache was explicitly declared, then use it if (!(0, type_fns_1.isAFunction)(options.cache)) return options.cache; // otherwise, we require the cache to have ben defined as an input to this method, when using invalidate by input, since we expect to grab the cache off of the input if (!args.cache) throw new BadRequestError_1.BadRequestError(`could not find the cache to ${trigger.toLowerCase()} in. ${trigger.toLowerCase()} was called forKey but the cache for this method was defined as a function which retrieves the cache from the input. therefore, since there is no input accessible, the cache should have been explicitly passed in on ${trigger.toLowerCase()}`, { args }); return args.cache; }; exports.getCacheFromCacheOptionOrFromForKeyArgs = getCacheFromCacheOptionOrFromForKeyArgs; //# sourceMappingURL=getCacheFromCacheOptionOrFromForKeyArgs.js.map