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
Name | Type | Description |
---|---|---|
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. |
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
Name | Type | Description |
---|---|---|
method | string | The HTTP method to use. |
data | undefined | UnknownParameters | The data to be send with a request. |
options | HttpAgentRequestOptions | Options provided by the HTTP agent. |
Returns
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
Name | Type | Description |
---|---|---|
method | string | The HTTP method to use. |
url | string | The URL to which the request should be sent. |
data | undefined | UnknownParameters | The data to send with the request. |
options | HttpAgentRequestOptions | Optional request options. |
Returns
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
Name | Type | Description |
---|---|---|
url | string | The URL to prepare for use with the fetch API. |
data | undefined | StringParameters | The 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
Name | Type |
---|---|
T | extends Record <string , string | Blob > |
Parameters
Name | Type | Description |
---|---|---|
object | undefined | T | The 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
Name | Type |
---|---|
T | extends Record <string , string | number | boolean > |
Parameters
Name | Type | Description |
---|---|---|
object | undefined | T | The 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
Name | Type | Default value | Description |
---|---|---|---|
cause | Error | GenericError <unknown > | undefined | The error's message. |
requestParams | HttpProxyRequestParams | undefined | An object representing the complete request parameters used to create and send the HTTP request. |
status | number | undefined | Server's response HTTP status code. |
responseBody | unknown | null | The 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
Name | Type | Description |
---|---|---|
method | string | The HTTP method to use. |
data | undefined | UnknownParameters | The data to be send with a request. |
headers | Record <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
Name | Type | Description |
---|---|---|
headers | Headers | The headers to convert. |
Returns
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
Name | Type | Description |
---|---|---|
fetchError | Error | GenericError <unknown > | The internal error to process. |
requestParams | HttpProxyRequestParams | An 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
Name | Type | Description |
---|---|---|
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
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
Name | Type | Description |
---|---|---|
method | string | The HTTP method. |
data | undefined | UnknownParameters | The 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
Name | Type | Description |
---|---|---|
data | undefined | UnknownParameters | The data to be send with a request. |
headers | Record <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
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
Name | Type |
---|---|
B | unknown |
Parameters
Name | Type | Description |
---|---|---|
method | string | The HTTP method used to make the request. |
url | string | The URL to which the request has been made. |
data | undefined | UnknownParameters | The data sent with the request. |
options | HttpAgentRequestOptions | Optional request options. |
status | number | The HTTP response status code send by the server. |
body | undefined | B | The body of HTTP error response (detailed information). |
cause | Error | The low-level cause error. |
Returns
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
Name | Type | Description |
---|---|---|
method | string | The HTTP method to use. |
url | string | The URL to which the request should be made. |
data | undefined | UnknownParameters | 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. |
options | HttpAgentRequestOptions | Optional 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
Name | Type | Description |
---|---|---|
header | string | A header name. |
value | string | A header value. |
Returns
this