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:72

Properties

_defaultHeaders

Protected _defaultHeaders: Map<string, string>

Defined in

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


_transformer

Protected _transformer: UrlTransformer

Defined in

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


_window

Protected _window: Window

Defined in

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

Methods

_composeRequestInit

_composeRequestInit(method, data, options): RequestInit

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

Parameters

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

Returns

RequestInit

A RequestInit object of the Fetch API.

Defined in

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


_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.
dataUnknownParametersThe 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:402


_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.
dataUnknownParametersThe 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:506


_convertObjectToFormData

Private _convertObjectToFormData(object): UnknownParameters | FormData

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

Parameters

NameTypeDescription
objectUnknownParametersThe object to be converted

Returns

UnknownParameters | FormData

Defined in

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


_convertObjectToQueryString

Private _convertObjectToQueryString(object): string

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

Parameters

NameTypeDescription
objectUnknownParametersThe object to be converted

Returns

string

Query string representation of the given object

Defined in

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


_createError

_createError(cause, requestParams, status, responseBody?): GenericError

Creates an error that represents a failed HTTP request.

Parameters

NameTypeDefault valueDescription
causeError | GenericErrorundefinedThe 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

The error representing a failed HTTP request.

Defined in

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


_getContentType

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

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

Parameters

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

Returns

null | string

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

Defined in

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


_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:322


_processError

_processError(fetchError, requestParams): GenericError

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

Parameters

NameTypeDescription
fetchErrorError | GenericErrorThe internal error to process.
requestParamsHttpProxyRequestParamsAn object representing the complete request parameters used to create and send the HTTP request.

Returns

GenericError

The error to provide to the calling API.

Defined in

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


_processResponse

_processResponse(requestParams, response, responseBody): HttpAgentResponse

Processes the response received from the server.

Parameters

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

Returns

HttpAgentResponse

The server's response along with all related metadata.

Defined in

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


_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.
data?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:527


_transformRequestBody

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

Formats request body according to request headers.

Parameters

NameTypeDescription
dataUnknownParametersThe data to be send with a request.
headersStringParametersHeaders object from options provided by the HTTP agent.

Returns

string | UnknownParameters | FormData

Defined in

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


clearDefaultHeaders

clearDefaultHeaders(): void

Clears all defaults headers sent with all requests.

Returns

void

Defined in

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


getErrorParams

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

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.

Parameters

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

Returns

HttpProxyErrorParams

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

Defined in

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


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:279


request

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

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

Parameters

NameTypeDescription
methodstringThe HTTP method to use.
urlstringThe URL to which the request should be made.
dataUnknownParametersThe 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<unknown>

A promise that resolves to the server response.

Defined in

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


setDefaultHeader

setDefaultHeader(header, value): void

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

void

Defined in

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