Skip to main content

Class: HttpProxy

Defined in: packages/core/src/http/HttpProxy.ts:63

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

Constructors​

Constructor​

new HttpProxy(transformer, window): HttpProxy

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

Initializes the HTTP proxy.

Parameters​

transformer​

UrlTransformer

A transformer of URLs to which requests are made.

window​

Window

Helper for manipulating the global object window regardless of the client/server-side environment.

Returns​

HttpProxy

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

Defined in: packages/core/src/http/HttpProxy.ts:466

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

Parameters​

method​

string

The HTTP method to use.

data​

The data to be send with a request.

UnknownParameters | undefined

options​

HttpAgentRequestOptions

Options provided by the HTTP agent.

Returns​

ImaRequestInit

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


_composeRequestParams()​

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

Defined in: packages/core/src/http/HttpProxy.ts:440

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

Parameters​

method​

string

The HTTP method to use.

url​

string

The URL to which the request should be sent.

data​

The data to send with the request.

UnknownParameters | undefined

options​

HttpAgentRequestOptions

Optional request options.

Returns​

HttpProxyRequestParams

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


_composeRequestUrl()​

_composeRequestUrl(url, data): string

Defined in: packages/core/src/http/HttpProxy.ts:547

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

Parameters​

url​

string

The URL to prepare for use with the fetch API.

data​

The data to be attached to the query string.

StringParameters | undefined

Returns​

string

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


_createError()​

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

Defined in: packages/core/src/http/HttpProxy.ts:408

Creates an error that represents a failed HTTP request.

Parameters​

cause​

The error's message.

Error | GenericError<unknown>

requestParams​

HttpProxyRequestParams

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

status​

number

Server's response HTTP status code.

responseBody​

unknown = null

The body of the server's response, if any.

Returns​

GenericError

The error representing a failed HTTP request.


_getContentType()​

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

Defined in: packages/core/src/http/HttpProxy.ts:519

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

Parameters​

method​

string

The HTTP method to use.

data​

The data to be send with a request.

UnknownParameters | undefined

headers​

Record<string, string>

Returns​

string | null

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


_headersToPlainObject()​

_headersToPlainObject(headers): StringParameters

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

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

Parameters​

headers​

Headers

The headers to convert.

Returns​

StringParameters

Converted headers.


_processError()​

_processError(fetchError, requestParams): GenericError

Defined in: packages/core/src/http/HttpProxy.ts:382

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

Parameters​

fetchError​

The internal error to process.

Error | GenericError<unknown>

requestParams​

HttpProxyRequestParams

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

Returns​

GenericError

The error to provide to the calling API.


_processResponse()​

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

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

Processes the response received from the server.

Type Parameters​

B​

B

Parameters​

requestParams​

HttpProxyRequestParams

The original request's parameters.

response​

Response

The Fetch API's Response object representing the server's response.

responseBody​

B

The server's response body.

Returns​

HttpAgentResponse<B>

The server's response along with all related metadata.


_shouldRequestHaveBody()​

_shouldRequestHaveBody(method, data): boolean

Defined in: packages/core/src/http/HttpProxy.ts:568

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

Parameters​

method​

string

The HTTP method.

data​

The data to be send with a request.

UnknownParameters | undefined

Returns​

boolean

true if a request has a body, otherwise false.


clearDefaultHeaders()​

clearDefaultHeaders(): this

Defined in: packages/core/src/http/HttpProxy.ts:235

Clears all defaults headers sent with all requests.

Returns​

this

this


getErrorParams()​

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

Defined in: packages/core/src/http/HttpProxy.ts:258

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​

B​

B = unknown

Parameters​

method​

string

The HTTP method used to make the request.

url​

string

The URL to which the request has been made.

data​

The data sent with the request.

UnknownParameters | undefined

options​

HttpAgentRequestOptions

Optional request options.

status​

number

The HTTP response status code send by the server.

body​

The body of HTTP error response (detailed information).

B | undefined

cause​

Error

The low-level cause error.

Returns​

HttpProxyErrorParams<B>

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


haveToSetCookiesManually()​

haveToSetCookiesManually(): boolean

Defined in: packages/core/src/http/HttpProxy.ts:318

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


request()​

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

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

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

Type Parameters​

B​

B

Parameters​

method​

string

The HTTP method to use.

url​

string

The URL to which the request should be made.

data​

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

UnknownParameters | undefined

options​

HttpAgentRequestOptions

Optional request options.

Returns​

Promise<HttpAgentResponse<B>>

A promise that resolves to the server response.


setDefaultHeader()​

setDefaultHeader(header, value): this

Defined in: packages/core/src/http/HttpProxy.ts:224

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 HttpProxy#clearDefaultHeaders method.

Parameters​

string

A header name.

value​

string

A header value.

Returns​

this

this