How to use @tinkoff/utils - 10 common examples

To help you get started, we’ve selected a few @tinkoff/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github TinkoffCreditSystems / tinkoff-request / packages / plugin-log / src / log.ts View on Github external
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}}
 */
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-memory / src / memory.ts View on Github external
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,
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-deduplicate / src / deduplicate.spec.ts View on Github external
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();
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-batch / src / batch.spec.ts View on Github external
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();
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-batch / src / batch.spec.ts View on Github external
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();
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-circuit-breaker / src / circuit-breaker.spec.ts View on Github external
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));
    };
github TinkoffCreditSystems / tinkoff-request / packages / plugin-batch / src / batch.ts View on Github external
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] };
github TinkoffCreditSystems / tinkoff-request / packages / plugin-protocol-http / src / utils.ts View on Github external
const _getHeaders = (request: MakeRequestResult) => {
    return prop('headers', _getResponse(request));
};
github TinkoffCreditSystems / tinkoff-request / packages / cache-utils / src / getCacheKey.ts View on Github external
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;
};
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-memory / src / memory.ts View on Github external
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();
        },
    };