Skip to main content

http-client

Tramvai tokens used for integration and extending @tramvai/module-http-client

Tokens

import { Scope, createToken } from '@tinkoff/dippy';
import type { Agent, Dispatcher } from 'undici';
import type { BaseHttpClient, HttpClient, HttpClientInterceptor } from '@tramvai/http-client';
import type { TinkoffRequestOptions } from '@tramvai/tinkoff-request-http-client-adapter';

export type HttpClientFactoryOptions = TinkoffRequestOptions & { name: string };

/**
* @description
* Factory of HTTP clients with minimal base settings
*/
export const HTTP_CLIENT_FACTORY =
createToken<(options: HttpClientFactoryOptions) => HttpClient>('HTTP_CLIENT_FACTORY');

/**
* @description
* Default options for factory of HTTP clients
*/
export const DEFAULT_HTTP_CLIENT_FACTORY_OPTIONS = createToken<Partial<HttpClientFactoryOptions>>(
'DEFAULT_HTTP_CLIENT_FACTORY_OPTIONS'
);

/**
* @description
* Interceptors will be added to default options for factory of HTTP clients
*/
export const DEFAULT_HTTP_CLIENT_INTERCEPTORS = createToken<HttpClientInterceptor>(
'DEFAULT_HTTP_CLIENT_INTERCEPTORS',
{ multi: true }
);

/**
* @description
* Universal HTTP client for arbitrary requests
*/
export const HTTP_CLIENT = createToken<HttpClient>('HTTP_CLIENT');

/**
* @description
* Global HTTP and HTTPS agents for all fetch requests
* @see https://undici.nodejs.org/#/docs/api/Dispatcher.md
* @see https://undici.nodejs.org/#/docs/api/Agent.md
*/
export const HTTP_CLIENT_AGENT = createToken<{
http: Agent;
https: Agent;
}>('HTTP_CLIENT_AGENT', { scope: Scope.SINGLETON });

/**
* @description
* Global agent options
* @see https://undici.nodejs.org/#/docs/api/Agent?id=parameter-agentoptions
*/
export const HTTP_CLIENT_AGENT_OPTIONS = createToken<Agent.Options>('HTTP_CLIENT_AGENT_OPTIONS', {
scope: Scope.SINGLETON,
});

/**
* @description
* Interceptors for global agent
* @see https://undici.nodejs.org/#/docs/api/Dispatcher?id=dispatchercomposeinterceptors-interceptor
*/
export const HTTP_CLIENT_AGENT_INTERCEPTORS = createToken<Dispatcher.DispatcherComposeInterceptor>(
'HTTP_CLIENT_AGENT_INTERCEPTORS',
{
multi: true,
scope: Scope.SINGLETON,
}
);

/**
* @description
* List of HTTP headers that are proxied from app request to all of the backend API
*/
export const API_CLIENT_PASS_HEADERS = createToken<string | string[]>('apiClientPassHeaders', {
multi: true,
});

/**
* @description
* Internal api for app server.
* Uses the value of `APP_INFO_TOKEN` from di for constructing the request address
*/
export const PAPI_SERVICE = createToken<BaseHttpClient>('papi service');

/**
* @description
* Enable or disable circuit breaker
*/
export const DISABLE_CIRCUIT_BREAKER = createToken<boolean>('disable circuit breaker');