Skip to main content

Class: HttpAgentImpl

@ima/core.HttpAgentImpl

Implementation of the HttpAgent interface with internal caching of completed and ongoing HTTP requests and cookie storage.

Hierarchy

Constructors

constructor

new HttpAgentImpl(proxy, cache, cookie, config, Helper)

Initializes the HTTP handler.

Parameters

NameTypeDescription
proxyHttpProxyThe low-level HTTP proxy for sending the HTTP requests.
cacheCache<HttpAgentResponse<unknown>>Cache to use for caching ongoing and completed requests.
cookieCookieStorageThe cookie storage to use internally.
configHttpAgentImplConfigConfiguration of the HTTP handler for the current application environment, specifying the various default request option values and cache option values.
Helper__moduleThe IMA.js helper methods.

Example

http
.get('url', { data: data }, {
ttl: 2000,
repeatRequest: 1,
withCredentials: true,
timeout: 2000,
accept: 'application/json',
language: 'en'
})
.then((response) => {
//resolve
}
.catch((error) => {
//catch
});

Example

http
.setDefaultHeader('Accept-Language', 'en')
.clearDefaultHeaders();

Overrides

HttpAgent.constructor

Defined in

packages/core/src/http/HttpAgentImpl.ts:69

Properties

_Helper

Protected _Helper: __module

Defined in

packages/core/src/http/HttpAgentImpl.ts:33


_cache

Protected _cache: Cache<HttpAgentResponse<unknown>>

Defined in

packages/core/src/http/HttpAgentImpl.ts:29


_cacheOptions

Protected _cacheOptions: HttpAgentImplCacheOptions

Defined in

packages/core/src/http/HttpAgentImpl.ts:31


Protected _cookie: CookieStorage

Defined in

packages/core/src/http/HttpAgentImpl.ts:30


_defaultRequestOptions

Protected _defaultRequestOptions: HttpAgentRequestOptions

Defined in

packages/core/src/http/HttpAgentImpl.ts:32


_internalCacheOfPromises

Protected _internalCacheOfPromises: Map<any, any>

Defined in

packages/core/src/http/HttpAgentImpl.ts:34


_proxy

Protected _proxy: HttpProxy

Defined in

packages/core/src/http/HttpAgentImpl.ts:28

Methods

_cleanResponse

_cleanResponse<B>(response): HttpAgentResponse<B>

Cleans cache response from data (abort controller, postProcessors), that cannot be persisted, before saving the data to the cache.

Type parameters

Name
B

Parameters

NameType
responseHttpAgentResponse<B>

Returns

HttpAgentResponse<B>

Defined in

packages/core/src/http/HttpAgentImpl.ts:532


_clone

_clone<V>(value): V

Attempts to clone the provided value, if possible. Values that cannot be cloned (e.g. promises) will be simply returned.

Type parameters

Name
V

Parameters

NameTypeDescription
valueVThe value to clone.

Returns

V

The created clone, or the provided value if the value cannot be cloned.

Defined in

packages/core/src/http/HttpAgentImpl.ts:221


_getCacheKeySuffix

_getCacheKeySuffix(method, url, data?): string

Generates cache key suffix for an HTTP request to the specified URL with the specified data.

Parameters

NameTypeDescription
methodstringThe HTTP method used by the request.
urlstringThe URL to which the request is sent.
data?UnknownParametersThe data sent with the request.

Returns

string

The suffix of a cache key to use for a request to the specified URL, carrying the specified data.

Defined in

packages/core/src/http/HttpAgentImpl.ts:475


_getCachedData

_getCachedData<B>(method, url, data?): null | Promise<HttpAgentResponse<B>>

Tests whether an ongoing or completed HTTP request for the specified URL and data is present in the internal cache and, if it is, the method returns a promise that resolves to the response body parsed as JSON.

The method returns null if no such request is present in the cache.

Type parameters

Name
B

Parameters

NameTypeDescription
methodstringThe HTTP method used by the request.
urlstringThe URL to which the request was made.
data?UnknownParametersThe data sent to the server with the request.

Returns

null | Promise<HttpAgentResponse<B>>

A promise that will resolve to the server response with the body parsed as JSON, or null if no such request is present in the cache.

Defined in

packages/core/src/http/HttpAgentImpl.ts:278


_prepareOptions

_prepareOptions(options?): HttpAgentRequestOptions

Prepares the provided request options object by filling in missing options with default values and adding extra options used internally.

Parameters

NameTypeDescription
optionsPartial<HttpAgentRequestOptions>Optional request options.

Returns

HttpAgentRequestOptions

Request options with set filled-in default values for missing fields, and extra options used internally.

Defined in

packages/core/src/http/HttpAgentImpl.ts:435


_proxyRejected

_proxyRejected<B>(error): Promise<HttpAgentResponse<B>>

Handles rejection of the HTTP request by the HTTP proxy. The method tests whether there are any remaining tries for the request, and if there are any, it attempts re-send the request.

The method rejects the internal request promise if there are no tries left.

Type parameters

Name
B

Parameters

NameTypeDescription
errorGenericError<HttpProxyErrorParams>The error provided by the HttpProxy, carrying the error parameters, such as the request url, data, method, options and other useful data.

Returns

Promise<HttpAgentResponse<B>>

A promise that will either resolve to a server's response (with the body parsed as JSON) if there are any tries left and the re-tried request succeeds, or rejects with an error containing details of the cause of the request's failure.

Defined in

packages/core/src/http/HttpAgentImpl.ts:398


_proxyResolved

_proxyResolved<B>(response): HttpAgentResponse<B>

Handles successful completion of an HTTP request by the HTTP proxy.

The method also updates the internal cookie storage with the cookies received from the server.

Type parameters

Name
B

Parameters

NameTypeDescription
responseHttpAgentResponse<B>Server response.

Returns

HttpAgentResponse<B>

The post-processed server response.

Defined in

packages/core/src/http/HttpAgentImpl.ts:343


_request

_request<B>(method, url, data, options): Promise<HttpAgentResponse<B>>

Sends a new HTTP request using the specified method to the specified url. The request will carry the provided data as query parameters if the HTTP method is GET, but the data will be sent as request body for any other request method.

Type parameters

Name
B

Parameters

NameTypeDescription
methodstringHTTP method to use.
urlstringThe URL to which the request is sent.
dataundefined | UnknownParametersThe data sent with the request.
optionsHttpAgentRequestOptionsOptional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response with the body parsed as JSON.

Defined in

packages/core/src/http/HttpAgentImpl.ts:314


_requestWithCheckCache

_requestWithCheckCache<B>(method, url, data?, options?): Promise<HttpAgentResponse<B>>

Check cache and if data isn’t available then make real request.

Type parameters

Name
B

Parameters

NameTypeDescription
methodstringThe HTTP method to use.
urlstringThe URL to which the request should be sent.
data?UnknownParametersThe data to send with the request.
options?Partial<HttpAgentRequestOptions>Optional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response with body parsed as JSON.

Defined in

packages/core/src/http/HttpAgentImpl.ts:243


_saveAgentResponseToCache

_saveAgentResponseToCache<B>(agentResponse): void

Saves the server response to the cache to be used as the result of the next request of the same properties.

Type parameters

Name
B

Parameters

NameTypeDescription
agentResponseHttpAgentResponse<B>The response of the server.

Returns

void

Defined in

packages/core/src/http/HttpAgentImpl.ts:514


_setCookiesFromResponse

_setCookiesFromResponse<B>(agentResponse): void

Sets all cookies from the Set-Cookie response header to the cookie storage.

Type parameters

Name
B

Parameters

NameTypeDescription
agentResponseHttpAgentResponse<B>The response of the server.

Returns

void

Defined in

packages/core/src/http/HttpAgentImpl.ts:498


clearDefaultHeaders

clearDefaultHeaders(): HttpAgentImpl

Clears all configured default headers.

Returns

HttpAgentImpl

This HTTP agent.

Overrides

HttpAgent.clearDefaultHeaders

Defined in

packages/core/src/http/HttpAgentImpl.ts:207


delete

delete<B>(url, data?, options?): Promise<HttpAgentResponse<B>>

Sends an HTTP DELETE request to the specified URL, sending the provided data as the request body. If an object is provided as the request data, the data will be JSON-encoded. Sending other primitive non-string values as the request body is not supported.

Type parameters

NameType
Bunknown

Parameters

NameTypeDescription
urlstringThe URL to which the request should be made.
data?UnknownParametersThe data to send to the server as the request body.
options?Partial<HttpAgentRequestOptions>Optional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response.

Overrides

HttpAgent.delete

Defined in

packages/core/src/http/HttpAgentImpl.ts:164


get

get<B>(url, data?, options?): Promise<HttpAgentResponse<B>>

Sends an HTTP GET request to the specified URL, sending the provided data as query parameters.

Type parameters

NameType
Bunknown

Parameters

NameTypeDescription
urlstringThe URL to which the request should be made.
data?UnknownParametersThe data to send to the server as query parameters.
options?Partial<HttpAgentRequestOptions>Optional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response.

Overrides

HttpAgent.get

Defined in

packages/core/src/http/HttpAgentImpl.ts:105


getCacheKey

getCacheKey(method, url, data?): string

Generates a cache key to use for identifying a request to the specified URL using the specified HTTP method, submitting the provided data.

Parameters

NameTypeDescription
methodstringThe HTTP method used by the request.
urlstringThe URL to which the request is sent.
data?UnknownParametersThe data associated with the request. These can be either the query parameters or request body data.

Returns

string

The key to use for identifying such a request in the cache.

Overrides

HttpAgent.getCacheKey

Defined in

packages/core/src/http/HttpAgentImpl.ts:180


invalidateCache

invalidateCache(method, url, data?): void

Method invalidate cache for given params

Parameters

NameType
methodstring
urlstring
data?UnknownParameters

Returns

void

Overrides

HttpAgent.invalidateCache

Defined in

packages/core/src/http/HttpAgentImpl.ts:189


patch

patch<B>(url, data?, options?): Promise<HttpAgentResponse<B>>

Sends an HTTP PATCH request to the specified URL, sending the provided data as the request body. If an object is provided as the request data, the data will be JSON-encoded. Sending other primitive non-string values as the request body is not supported.

Type parameters

NameType
Bunknown

Parameters

NameTypeDescription
urlstringThe URL to which the request should be made.
data?UnknownParametersThe data to send to the server as the request body.
options?Partial<HttpAgentRequestOptions>Optional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response.

Overrides

HttpAgent.patch

Defined in

packages/core/src/http/HttpAgentImpl.ts:148


post

post<B>(url, data?, options?): Promise<HttpAgentResponse<B>>

Sends an HTTP POST request to the specified URL, sending the provided data as the request body. If an object is provided as the request data, the data will be JSON-encoded. Sending other primitive non-string values as the request body is not supported.

Type parameters

NameType
Bunknown

Parameters

NameTypeDescription
urlstringThe URL to which the request should be made.
data?UnknownParametersThe data to send to the server as the request body.
options?Partial<HttpAgentRequestOptions>Optional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response.

Overrides

HttpAgent.post

Defined in

packages/core/src/http/HttpAgentImpl.ts:116


put

put<B>(url, data?, options?): Promise<HttpAgentResponse<B>>

Sends an HTTP PUT request to the specified URL, sending the provided data as the request body. If an object is provided as the request data, the data will be JSON-encoded. Sending other primitive non-string values as the request body is not supported.

Type parameters

NameType
Bunknown

Parameters

NameTypeDescription
urlstringThe URL to which the request should be made.
data?UnknownParametersThe data to send to the server as the request body.
options?Partial<HttpAgentRequestOptions>Optional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the response.

Overrides

HttpAgent.put

Defined in

packages/core/src/http/HttpAgentImpl.ts:132


setDefaultHeader

setDefaultHeader(header, value): HttpAgentImpl

Sets the specified header to be sent with every subsequent HTTP request, unless explicitly overridden by request options.

Parameters

NameTypeDescription
headerstringThe name of the header.
valuestringThe header value. To provide multiple values, separate them with commas (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2).

Returns

HttpAgentImpl

This HTTP agent.

Overrides

HttpAgent.setDefaultHeader

Defined in

packages/core/src/http/HttpAgentImpl.ts:198