How to use the @tinkoff/request-core.Context function in @tinkoff/request-core

To help you get started, we’ve selected a few @tinkoff/request-core 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-validate / src / validate.spec.ts View on Github external
import { Context, Status } from '@tinkoff/request-core';
import { VALIDATE } from './constants';
import validate from './validate';

const validator = ({ response }) => {
    if (response.error) {
        return new Error(response.error);
    }
};

const errorValidator = ({ error }) => {
    return !!error.valid;
};

const plugin = validate({ validator, errorValidator });
const context = new Context();

context.setState = jest.fn(context.setState.bind(context));
context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));

describe('plugins/validate/validate', () => {
    beforeEach(() => {
        // @ts-ignore
        context.setState.mockClear();
        // @ts-ignore
        context.updateExternalMeta.mockClear();
    });

    it('if validator returns undefined plugin should not return any state or call next callback', () => {
        context.setState({ response: { a: 1 } });
        (context.setState as jest.Mock).mockClear();
        const next = jest.fn();
github TinkoffCreditSystems / tinkoff-request / packages / plugin-protocol-http / src / http.spec.ts View on Github external
headers: {
                        'Content-type': 'application/json;',
                    },
                },
            })
        );

        fetch.mockResponse(mockResponse);

        class MockedAgent {
            requests() {}
            destroy() {}
        }

        http({ agent: { http: new MockedAgent() as any, https: new MockedAgent() as any } }).init(
            new Context({ request: { url: 'http://test.com/api' } }),
            next,
            null
        );

        await new Promise((res) => {
            next.mockImplementation(res);
        });

        expect(mockResponse).toBeCalled();
        expect(next).toHaveBeenLastCalledWith({
            response,
            status: Status.COMPLETE,
        });
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-fallback / src / fallback.spec.ts View on Github external
it('tries return from cache on error, but persist cache errors', () => {
        const request = { url: 'test[123]//pf' };
        const error = new Error('123');
        const plugin = fallback({ getCacheKey, shouldExecute: true });
        const context = new Context({ request, error });
        const next = jest.fn();

        mockPersistentCache.get.mockImplementation((_, cb) => cb(error));
        context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));

        plugin.error(context, next, null);
        expect(getCacheKey).toHaveBeenLastCalledWith(request);
        expect(mockPersistentCache.put).not.toHaveBeenCalled();
        expect(mockPersistentCache.get).toHaveBeenLastCalledWith(encodeURIComponent(request.url), expect.any(Function));
        expect(context.updateExternalMeta).not.toHaveBeenLastCalledWith(metaTypes.CACHE, { fromFallback: true });
        expect(next).toHaveBeenLastCalledWith();
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-fallback / src / fallback.spec.ts View on Github external
it('if shouldFallback returns false, do not use cache', () => {
        const request = { url: 'test[123]//pf' };
        const error = new Error('123');
        const shouldFallback = jest.fn(() => false);
        const plugin = fallback({ getCacheKey, shouldFallback, shouldExecute: true });
        const context = new Context({ request, error });
        const next = jest.fn();

        context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));

        plugin.error(context, next, null);
        expect(shouldFallback).toHaveBeenCalledWith(context.getState());
        expect(getCacheKey).not.toHaveBeenCalled();
        expect(mockPersistentCache.put).not.toHaveBeenCalled();
        expect(mockPersistentCache.get).not.toHaveBeenCalled();
        expect(context.updateExternalMeta).not.toHaveBeenCalled();
        expect(next).toHaveBeenLastCalledWith();
    });
});
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-memory / src / memory.spec.ts View on Github external
import { metaTypes } from '@tinkoff/request-cache-utils';
import memoryCache from './memory';

const mockLru = {
    get: jest.fn(),
    set: jest.fn(),
    has: jest.fn(),
    peek: jest.fn(),
};

jest.mock('lru-cache', () => () => mockLru);

const getCacheKey = jest.fn((req) => req.url);
const plugin = memoryCache({ getCacheKey });
const next = jest.fn();
const context = new Context({ request: { url: 'test' } });

context.updateMeta = jest.fn(context.updateMeta.bind(context));

describe('plugins/cache/memory', () => {
    beforeEach(() => {
        mockLru.get.mockClear();
        mockLru.set.mockClear();
        mockLru.has.mockClear();
        mockLru.peek.mockClear();
        next.mockClear();
        context.setState({ meta: {} });
    });

    it('init, no cache value', () => {
        mockLru.has.mockImplementation(() => false);
        plugin.init(context, next, null);
github TinkoffCreditSystems / tinkoff-request / packages / plugin-transform-url / src / url.spec.ts View on Github external
it('baseUrl is not passed', () => {
        const request = { url: 'test12313123/weawe' };
        const context = new Context({ request });

        plugin.init(context, next, null);

        expect(transform).toHaveBeenCalledWith({
            ...request,
            baseUrl,
        });
        expect(next).toHaveBeenCalledWith({
            request: {
                ...request,
                url: baseUrl + request.url,
            },
        });
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-fallback / src / fallback.spec.ts View on Github external
it('save to cache on complete', () => {
        const request = { url: 'test[123]//pf' };
        const response = { a: 1, b: 2 };
        const plugin = fallback({ getCacheKey, shouldExecute: true });
        const context = new Context({ request, response });

        plugin.complete(context, next, null);
        expect(getCacheKey).toHaveBeenLastCalledWith(request);
        expect(mockPersistentCache.put).toHaveBeenLastCalledWith(
            encodeURIComponent(request.url),
            response,
            expect.any(Function)
        );
        expect(mockPersistentCache.get).not.toHaveBeenCalled();
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-cache-etag / src / etag.spec.ts View on Github external
import etagCache from './etag';
import { ETAG } from './constants';

const mockLru = {
    get: jest.fn(),
    set: jest.fn(),
    has: jest.fn(),
    peek: jest.fn(),
};

jest.mock('lru-cache', () => () => mockLru);

const getCacheKey = jest.fn((req) => req.url);
const plugin = etagCache({ getCacheKey });
const next = jest.fn();
const context = new Context({ request: { url: 'test' } });

context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));
context.updateInternalMeta = jest.fn(context.updateInternalMeta.bind(context));

describe('plugins/cache/etag', () => {
    beforeEach(() => {
        mockLru.get.mockClear();
        mockLru.set.mockClear();
        mockLru.has.mockClear();
        mockLru.peek.mockClear();
        next.mockClear();
        (context as any).internalMeta = {};
        (context.updateExternalMeta as any).mockClear();
        (context.updateInternalMeta as any).mockClear();
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-circuit-breaker / src / circuit-breaker.spec.ts View on Github external
beforeEach(() => {
        next.mockClear();

        context = new Context({ request: { url: 'test' } });
        plugin = circuitBreaker({
            failureThreshold: 5,
            failureTimeout: 100,
            openTimeout: 200,
            halfOpenThreshold: 3,
        });
    });
github TinkoffCreditSystems / tinkoff-request / packages / plugin-batch / src / batch.spec.ts View on Github external
beforeEach(() => {
        context = new Context();
        context.updateExternalMeta = jest.fn(context.updateExternalMeta.bind(context));
    });

@tinkoff/request-core

Request library extendable by plugins

Apache-2.0
Latest version published 5 months ago

Package Health Score

45 / 100
Full package analysis