Class: HttpAgentImpl
@ima/core.HttpAgentImpl
Implementation of the HttpAgent interface with internal caching of completed and ongoing HTTP requests and cookie storage.
Hierarchy
↳
HttpAgentImpl
Constructors
constructor
• new HttpAgentImpl(proxy
, cache
, cookie
, config
, Helper
)
Initializes the HTTP handler.
Parameters
Name | Type | Description |
---|---|---|
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. |
cookie | 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. |
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
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
_cookie
• 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
Name | Type |
---|---|
response | HttpAgentResponse <B > |
Returns
Defined in
packages/core/src/http/HttpAgentImpl.ts:541
_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
Name | Type | Description |
---|---|---|
value | V | The 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
Name | Type | Description |
---|---|---|
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.
Defined in
packages/core/src/http/HttpAgentImpl.ts:478
_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
Name | Type | Description |
---|---|---|
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
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?
, url
): HttpAgentRequestOptions
Prepares the provided request options object by filling in missing options with default values and adding extra options used internally.
Parameters
Name | Type | Description |
---|---|---|
options | Partial <HttpAgentRequestOptions > | Optional request options. |
url | string | - |
Returns
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
Name | Type | Description |
---|---|---|
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.
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
Name | Type | Description |
---|---|---|
response | HttpAgentResponse <B > | Server response. |
Returns
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
Name | Type | Description |
---|---|---|
method | string | HTTP method to use. |
url | string | The URL to which the request is sent. |
data | undefined | UnknownParameters | The data sent with the request. |
options | HttpAgentRequestOptions | Optional 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
Name | Type | Description |
---|---|---|
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.
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
Name | Type | Description |
---|---|---|
agentResponse | HttpAgentResponse <B > | The response of the server. |
Returns
void
Defined in
packages/core/src/http/HttpAgentImpl.ts:523
_setCookiesFromResponse
▸ _setCookiesFromResponse<B
>(agentResponse
): void
Sets all cookies from the Set-Cookie
response header to the
cookie storage.
Type parameters
Name |
---|
B |
Parameters
Name | Type | Description |
---|---|---|
agentResponse | HttpAgentResponse <B > | The response of the server. |
Returns
void
Defined in
packages/core/src/http/HttpAgentImpl.ts:501
clearDefaultHeaders
▸ clearDefaultHeaders(): HttpAgentImpl
Clears all configured default headers.
Returns
This HTTP agent.
Overrides
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
Name | Type |
---|---|
B | unknown |
Parameters
Name | Type | Description |
---|---|---|
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
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
Name | Type |
---|---|
B | unknown |
Parameters
Name | Type | Description |
---|---|---|
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
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
Name | Type | Description |
---|---|---|
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
Defined in
packages/core/src/http/HttpAgentImpl.ts:180
invalidateCache
▸ invalidateCache(method
, url
, data?
): void
Method invalidate cache for given params
Parameters
Name | Type |
---|---|
method | string |
url | string |
data? | UnknownParameters |
Returns
void
Overrides
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
Name | Type |
---|---|
B | unknown |
Parameters
Name | Type | Description |
---|---|---|
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
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
Name | Type |
---|---|
B | unknown |
Parameters
Name | Type | Description |
---|---|---|
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
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
Name | Type |
---|---|
B | unknown |
Parameters
Name | Type | Description |
---|---|---|
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
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
Name | Type | Description |
---|---|---|
header | 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 HTTP agent.