Skip to main content

Class: HttpProxy

@ima/core.HttpProxy

Middleware proxy between HttpAgent implementations and the Fetch API, providing a Promise-oriented API for sending requests.

Constructors

constructor

new HttpProxy(transformer, window)

Initializes the HTTP proxy.

Parameters

NameTypeDescription
transformerUrlTransformerA transformer of URLs to which requests are made.
windowWindowHelper for manipulating the global object window regardless of the client/server-side environment.

Defined in

packages/core/src/http/HttpProxy.ts:76

Properties

_defaultHeaders

Protected _defaultHeaders: Map<string, string>

Defined in

packages/core/src/http/HttpProxy.ts:66


_transformer

Protected _transformer: UrlTransformer

Defined in

packages/core/src/http/HttpProxy.ts:64


_window

Protected _window: Window

Defined in

packages/core/src/http/HttpProxy.ts:65

Methods

_composeRequestInit

_composeRequestInit(method, data, options): ImaRequestInit

Composes an init object, which can be used as a second argument of window.fetch method.

Parameters

NameTypeDescription
methodstringThe HTTP method to use.
dataundefined | UnknownParametersThe data to be send with a request.
optionsHttpAgentRequestOptionsOptions provided by the HTTP agent.

Returns

ImaRequestInit

An ImaRequestInit object (extended from RequestInit of the Fetch API).

Defined in

packages/core/src/http/HttpProxy.ts:459


_composeRequestParams

_composeRequestParams(method, url, data, options): HttpProxyRequestParams

Composes an object representing the HTTP request parameters from the provided arguments.

Parameters

NameTypeDescription
methodstringThe HTTP method to use.
urlstringThe URL to which the request should be sent.
dataundefined | UnknownParametersThe data to send with the request.
optionsHttpAgentRequestOptionsOptional request options.

Returns

HttpProxyRequestParams

An object representing the complete request parameters used to create and send the HTTP request.

Defined in

packages/core/src/http/HttpProxy.ts:433


_composeRequestUrl

_composeRequestUrl(url, data): string

Transforms the provided URL using the current URL transformer and adds the provided data to the URL's query string.

Parameters

NameTypeDescription
urlstringThe URL to prepare for use with the fetch API.
dataundefined | StringParametersThe data to be attached to the query string.

Returns

string

The transformed URL with the provided data attached to its query string.

Defined in

packages/core/src/http/HttpProxy.ts:540


_convertObjectToFormData

Private _convertObjectToFormData<T>(object): undefined | FormData | T

Converts given data to FormData object. If FormData object is not supported by the browser the original object is returned.

Type parameters

NameType
Textends Record<string, string | Blob>

Parameters

NameTypeDescription
objectundefined | TThe object to be converted

Returns

undefined | FormData | T

Defined in

packages/core/src/http/HttpProxy.ts:631


_convertObjectToQueryString

Private _convertObjectToQueryString<T>(object): undefined | string

Returns query string representation of the data parameter. (Returned string does not contain ? at the beginning)

Type parameters

NameType
Textends Record<string, string | number | boolean>

Parameters

NameTypeDescription
objectundefined | TThe object to be converted

Returns

undefined | string

Query string representation of the given object

Defined in

packages/core/src/http/HttpProxy.ts:605


_createError

_createError(cause, requestParams, status, responseBody?): GenericError<unknown>

Creates an error that represents a failed HTTP request.

Parameters

NameTypeDefault valueDescription
causeError | GenericError<unknown>undefinedThe error's message.
requestParamsHttpProxyRequestParamsundefinedAn object representing the complete request parameters used to create and send the HTTP request.
statusnumberundefinedServer's response HTTP status code.
responseBodyunknownnullThe body of the server's response, if any.

Returns

GenericError<unknown>

The error representing a failed HTTP request.

Defined in

packages/core/src/http/HttpProxy.ts:401


_getContentType

_getContentType(method, data, headers): null | string

Gets a Content-Type header value for defined method, data and options.

Parameters

NameTypeDescription
methodstringThe HTTP method to use.
dataundefined | UnknownParametersThe data to be send with a request.
headersRecord<string, string>-

Returns

null | string

A Content-Type header value, null for requests with no body.

Defined in

packages/core/src/http/HttpProxy.ts:512


_headersToPlainObject

_headersToPlainObject(headers): StringParameters

Converts the provided Fetch API's Headers object to a plain object.

Parameters

NameTypeDescription
headersHeadersThe headers to convert.

Returns

StringParameters

Converted headers.

Defined in

packages/core/src/http/HttpProxy.ts:355


_processError

_processError(fetchError, requestParams): GenericError<unknown>

Processes the provided Fetch API or internal error and creates an error to expose to the calling API.

Parameters

NameTypeDescription
fetchErrorError | GenericError<unknown>The internal error to process.
requestParamsHttpProxyRequestParamsAn object representing the complete request parameters used to create and send the HTTP request.

Returns

GenericError<unknown>

The error to provide to the calling API.

Defined in

packages/core/src/http/HttpProxy.ts:375


_processResponse

_processResponse<B>(requestParams, response, responseBody): HttpAgentResponse<B>

Processes the response received from the server.

Type parameters

Name
B

Parameters

NameTypeDescription
requestParamsHttpProxyRequestParamsThe original request's parameters.
responseResponseThe Fetch API's Response object representing the server's response.
responseBodyBThe server's response body.

Returns

HttpAgentResponse<B>

The server's response along with all related metadata.

Defined in

packages/core/src/http/HttpProxy.ts:326


_shouldRequestHaveBody

_shouldRequestHaveBody(method, data): boolean

Checks if a request should have a body (GET and HEAD requests don't have a body).

Parameters

NameTypeDescription
methodstringThe HTTP method.
dataundefined | UnknownParametersThe data to be send with a request.

Returns

boolean

true if a request has a body, otherwise false.

Defined in

packages/core/src/http/HttpProxy.ts:561


_transformRequestBody

Private _transformRequestBody(data, headers): undefined | string | UnknownParameters | FormData

Formats request body according to request headers.

Parameters

NameTypeDescription
dataundefined | UnknownParametersThe data to be send with a request.
headersRecord<string, string>Headers object from options provided by the HTTP agent.

Returns

undefined | string | UnknownParameters | FormData

Defined in

packages/core/src/http/HttpProxy.ts:581


clearDefaultHeaders

clearDefaultHeaders(): HttpProxy

Clears all defaults headers sent with all requests.

Returns

HttpProxy

this

Defined in

packages/core/src/http/HttpProxy.ts:228


getErrorParams

getErrorParams<B>(method, url, data, options, status, body, cause): HttpProxyErrorParams<B>

Gets an object that describes a failed HTTP request, providing information about both the failure reported by the server and how the request has been sent to the server.

Type parameters

NameType
Bunknown

Parameters

NameTypeDescription
methodstringThe HTTP method used to make the request.
urlstringThe URL to which the request has been made.
dataundefined | UnknownParametersThe data sent with the request.
optionsHttpAgentRequestOptionsOptional request options.
statusnumberThe HTTP response status code send by the server.
bodyundefined | BThe body of HTTP error response (detailed information).
causeErrorThe low-level cause error.

Returns

HttpProxyErrorParams<B>

An object containing both the details of the error and the request that lead to it.

Defined in

packages/core/src/http/HttpProxy.ts:251


haveToSetCookiesManually

haveToSetCookiesManually(): boolean

Returns true if cookies have to be processed manually by setting Cookie HTTP header on requests and parsing the Set-Cookie HTTP response header.

The result of this method depends on the current application environment, the client-side usually handles cookie processing automatically, leading this method returning false.

At the client-side, the method tests whether the client has cookies enabled (which results in cookies being automatically processed by the browser), and returns true or false accordingly.

true if cookies are not processed automatically by the environment and have to be handled manually by parsing response headers and setting request headers, otherwise false.

Returns

boolean

Defined in

packages/core/src/http/HttpProxy.ts:311


request

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

Executes a HTTP request to the specified URL using the specified HTTP method, carrying the provided data.

Type parameters

Name
B

Parameters

NameTypeDescription
methodstringThe HTTP method to use.
urlstringThe URL to which the request should be made.
dataundefined | UnknownParametersThe data to be send to the server. The data will be included as query parameters if the request method is GET or HEAD, and as a request body for any other request method.
optionsHttpAgentRequestOptionsOptional request options.

Returns

Promise<HttpAgentResponse<B>>

A promise that resolves to the server response.

Defined in

packages/core/src/http/HttpProxy.ts:109


setDefaultHeader

setDefaultHeader(header, value): HttpProxy

Sets the specified default HTTP header. The header will be sent with all subsequent HTTP requests unless reconfigured using this method, overridden by request options, or cleared by clearDefaultHeaders method.

Parameters

NameTypeDescription
headerstringA header name.
valuestringA header value.

Returns

HttpProxy

this

Defined in

packages/core/src/http/HttpProxy.ts:217