Skip to main content

Class: HttpAgentImpl

Defined in: packages/core/src/http/HttpAgentImpl.ts:36

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

Extends​

Constructors​

Constructor​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:78

Initializes the HTTP handler.

Parameters​

proxy​

HttpProxy

The low-level HTTP proxy for sending the HTTP requests.

cache​

Cache<HttpAgentResponse<unknown>>

Cache to use for caching ongoing and completed requests.

CookieStorage

The cookie storage to use internally.

config​

HttpAgentImplConfig

Configuration of the HTTP handler for the current application environment, specifying the various default request option values and cache option values.

Helper​

__module

The IMA.js helper methods.

Returns​

HttpAgentImpl

Examples​

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

Overrides​

HttpAgent.constructor

Properties​

_cache​

protected _cache: Cache<ErrorCacheData | HttpAgentResponse<unknown>>

Defined in: packages/core/src/http/HttpAgentImpl.ts:38


_cacheOptions​

protected _cacheOptions: HttpAgentImplCacheOptions

Defined in: packages/core/src/http/HttpAgentImpl.ts:40


protected _cookie: CookieStorage

Defined in: packages/core/src/http/HttpAgentImpl.ts:39


_defaultRequestOptions​

protected _defaultRequestOptions: HttpAgentRequestOptions

Defined in: packages/core/src/http/HttpAgentImpl.ts:41


_Helper​

protected _Helper: __module

Defined in: packages/core/src/http/HttpAgentImpl.ts:42


_internalCacheOfPromises​

protected _internalCacheOfPromises: Map<any, any>

Defined in: packages/core/src/http/HttpAgentImpl.ts:43


_proxy​

protected _proxy: HttpProxy

Defined in: packages/core/src/http/HttpAgentImpl.ts:37

Methods​

_cleanErrorParams()​

_cleanErrorParams(params): HttpProxyErrorParams

Defined in: packages/core/src/http/HttpAgentImpl.ts:613

Create a copy of response errorParams without AbortController, AbortController signal, postProcessors and error cause Response. Setting errorParams with AbortController or signal or postProcessors or error cause response into cache would result in crashing.

Parameters​

params​

HttpProxyErrorParams

the error params.

Returns​

HttpProxyErrorParams

Pure copy of errorParams without non-persistent data.


_cleanResponse()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:576

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

Type Parameters​

B​

B

Parameters​

response​

HttpAgentResponse<B>

Returns​

HttpAgentResponse<B>


_clone()​

_clone<V>(value): V

Defined in: packages/core/src/http/HttpAgentImpl.ts:230

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

Type Parameters​

V​

V

Parameters​

value​

V

The value to clone.

Returns​

V

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


_getCachedData()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:287

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​

B​

B

Parameters​

method​

string

The HTTP method used by the request.

url​

string

The URL to which the request was made.

data?​

UnknownParameters

The data sent to the server with the request.

Returns​

Promise<HttpAgentResponse<B>> | null

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.


_getCacheKeySuffix()​

_getCacheKeySuffix(method, url, data?): string

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

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

Parameters​

method​

string

The HTTP method used by the request.

url​

string

The URL to which the request is sent.

data?​

UnknownParameters

The 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.


_prepareOptions()​

_prepareOptions(options, url): HttpAgentRequestOptions

Defined in: packages/core/src/http/HttpAgentImpl.ts:471

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

Parameters​

options​

Partial<HttpAgentRequestOptions> = {}

Optional request options.

url​

string

Returns​

HttpAgentRequestOptions

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


_proxyRejected()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:417

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​

B​

B

Parameters​

error​

GenericError<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.


_proxyResolved()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:362

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​

B​

B

Parameters​

response​

HttpAgentResponse<B>

Server response.

Returns​

HttpAgentResponse<B>

The post-processed server response.


_request()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:333

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​

B​

B

Parameters​

method​

string

HTTP method to use.

url​

string

The URL to which the request is sent.

data​

The data sent with the request.

UnknownParameters | undefined

options​

HttpAgentRequestOptions

Optional request options.

Returns​

Promise<HttpAgentResponse<B>>

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


_requestWithCheckCache()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:252

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

Type Parameters​

B​

B

Parameters​

method​

string

The HTTP method to use.

url​

string

The URL to which the request should be sent.

data?​

UnknownParameters

The 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.


_saveAgentResponseToCache()​

_saveAgentResponseToCache<B>(agentResponse): void

Defined in: packages/core/src/http/HttpAgentImpl.ts:558

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

Type Parameters​

B​

B

Parameters​

agentResponse​

HttpAgentResponse<B>

The response of the server.

Returns​

void


_setCookiesFromResponse()​

_setCookiesFromResponse<B>(agentResponse): void

Defined in: packages/core/src/http/HttpAgentImpl.ts:537

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

Type Parameters​

B​

B

Parameters​

agentResponse​

HttpAgentResponse<B>

The response of the server.

Returns​

void


clearDefaultHeaders()​

clearDefaultHeaders(): this

Defined in: packages/core/src/http/HttpAgentImpl.ts:216

Clears all configured default headers.

Returns​

this

This HTTP agent.

Overrides​

HttpAgent.clearDefaultHeaders


delete()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:173

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​

B​

B = unknown

Parameters​

url​

string

The URL to which the request should be made.

data?​

UnknownParameters

The 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


get()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:114

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

Type Parameters​

B​

B = unknown

Parameters​

url​

string

The URL to which the request should be made.

data?​

UnknownParameters

The 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


getCacheKey()​

getCacheKey(method, url, data?): string

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

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

Parameters​

method​

string

The HTTP method used by the request.

url​

string

The URL to which the request is sent.

data?​

UnknownParameters

The 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


invalidateCache()​

invalidateCache(method, url, data?): void

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

Method invalidate cache for given params

Parameters​

method​

string

url​

string

data?​

UnknownParameters

Returns​

void

Overrides​

HttpAgent.invalidateCache


patch()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:157

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​

B​

B = unknown

Parameters​

url​

string

The URL to which the request should be made.

data?​

UnknownParameters

The 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


post()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:125

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​

B​

B = unknown

Parameters​

url​

string

The URL to which the request should be made.

data?​

UnknownParameters

The 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


put()​

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

Defined in: packages/core/src/http/HttpAgentImpl.ts:141

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​

B​

B = unknown

Parameters​

url​

string

The URL to which the request should be made.

data?​

UnknownParameters

The 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


setDefaultHeader()​

setDefaultHeader(header, value): this

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

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

Parameters​

string

The name of the header.

value​

string

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

Returns​

this

This HTTP agent.

Overrides​

HttpAgent.setDefaultHeader