Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
interface Logger {
info: LogFunction;
debug: LogFunction;
error: LogFunction;
}
const defaultLogger = (name: string): Logger => {
return {
info: (...args) => console.info(name, ...args), // tslint:disable-line:no-console
debug: (...args) => console.debug(name, ...args), // tslint:disable-line:no-console
error: (...args) => console.error(name, ...args), // tslint:disable-line:no-console
};
};
const getInfo = pick(['url', 'query', 'payload']);
/**
* Logs request events and timing
*
* requestParams:
* silent {boolean}
*
* metaInfo:
* log.start {number} - request start Date.now()
* log.end {number} - request end Date.now()
* log.duration {number} - request duration (end - start)
*
* @param name {string}
* @param logger {Function} - logger factory
* @return {{init: init, complete: complete, error: error}}
*/
init: (context, next, makeRequest) => {
const cacheKey = getCacheKeyUtil(context, getCacheKey);
if (lruCache.has(cacheKey)) {
context.updateExternalMeta(metaTypes.CACHE, {
memoryCache: true,
memoryCacheOutdated: false,
});
return next({
status: Status.COMPLETE,
response: lruCache.get(cacheKey),
});
}
const allowOutdated = propOr('memoryCacheAllowStale', allowStale, context.getRequest());
const outdated = allowOutdated && lruCache.peek(cacheKey);
if (outdated) {
const request = context.getRequest();
context.updateExternalMeta(metaTypes.CACHE, {
memoryCache: true,
memoryCacheOutdated: true,
});
lruCache.set(cacheKey, outdated, staleTtl); // remember outdated value, to prevent losing it
setTimeout(
() =>
makeRequest({
...request,
memoryCacheForce: true,
it('test plugin shouldExecute', () => {
const context = new Context();
expect(applyOrReturn([context], deduplicate({ shouldExecute: false }).shouldExecute)).toBeFalsy();
expect(applyOrReturn([context], deduplicate({ shouldExecute: true }).shouldExecute)).toBeTruthy();
context.setState({ request: { deduplicateCache: false, url: '' } });
expect(applyOrReturn([context], deduplicate({ shouldExecute: true }).shouldExecute)).toBeFalsy();
context.setState({ request: { deduplicateCache: true, url: '' } });
expect(applyOrReturn([context], deduplicate({ shouldExecute: true }).shouldExecute)).toBeTruthy();
});
it('test shouldExecute', () => {
let shouldExecute = batch({ makeGroupedRequest }).shouldExecute;
expect(applyOrReturn([context], shouldExecute)).toBeFalsy();
shouldExecute = batch({ makeGroupedRequest }).shouldExecute;
expect(applyOrReturn([context], shouldExecute)).toBeFalsy();
context.setState({
request: {
batchKey: '123',
},
});
expect(applyOrReturn([context], shouldExecute)).toBeTruthy();
});
it('test shouldExecute', () => {
let shouldExecute = batch({ makeGroupedRequest }).shouldExecute;
expect(applyOrReturn([context], shouldExecute)).toBeFalsy();
shouldExecute = batch({ makeGroupedRequest }).shouldExecute;
expect(applyOrReturn([context], shouldExecute)).toBeFalsy();
context.setState({
request: {
batchKey: '123',
},
});
expect(applyOrReturn([context], shouldExecute)).toBeTruthy();
});
const runFailure = (n: number, failure = true) => {
each((v, i) => {
plugin.init(context, next, makeRequest);
context.setState({ error: new Error(`error ${i}`) });
if (failure) {
plugin.error(context, next, makeRequest);
} else {
plugin.complete(context, next, makeRequest);
}
}, Array(n));
};
init: (context, next) => {
const request = context.getRequest();
const batchKey: string = prop('batchKey', request);
const batchTimeout = propOr('batchTimeout', timeout, request);
context.updateExternalMeta(BATCH, {
batched: true,
});
const running = batchRequests[batchKey];
if (running) {
running.requests.push(request);
running.nexts.push(next);
return;
}
batchRequests[batchKey] = { requests: [request], nexts: [next] };
const _getHeaders = (request: MakeRequestResult) => {
return prop('headers', _getResponse(request));
};
export default (context: Context, cacheKey = defaultCacheKey) => {
let key = prop('key', context.getInternalMeta(CACHE));
if (!key) {
key = cacheKey(context.getRequest());
context.updateInternalMeta(CACHE, {
key,
});
}
return key;
};
complete: (context, next) => {
const cacheKey = getCacheKeyUtil(context, getCacheKey);
const ttl = prop('memoryCacheTtl', context.getRequest());
lruCache.set(cacheKey, context.getResponse(), ttl);
context.updateExternalMeta(metaTypes.CACHE, {
memoryCacheBackground: prop('memoryCacheBackground', context.getRequest()),
});
next();
},
};