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 |
Members
-
_defaultHeaders :Map.<string, string>
-
The default HTTP headers to include with the HTTP requests, unless overridden.
Type:
- Map.<string, string>
-
_transformer :UrlTransformer
-
A transformer of URLs to which requests are made.
Type:
-
_window :Window
-
Helper for manipulating the global object
window
regardless of the client/server-side environment.Type:
Methods
-
_composeRequestInit(method, data, options)
-
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
Object.<string, (boolean|number|string|Date)> The data to be send with a request.
options
HttpAgent~RequestOptions Options provided by the HTTP agent.
Returns:
A
RequestInit
object of the Fetch API.- Type
- RequestInit
-
_composeRequestParams(method, url, data, options)
-
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
Object.<string, (boolean|number|string|Date)> The data to send with the request.
options
HttpAgent~RequestOptions Optional request options.
Returns:
An object representing the complete request parameters used to create and send the HTTP request.
-
_composeRequestUrl(url, data)
-
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
Object.<string, (boolean|number|string|Date)> The data to be attached to the query string.
Returns:
The transformed URL with the provided data attached to its query string.
- Type
- string
-
_createError(cause, requestParams, status, responseBody)
-
Creates an error that represents a failed HTTP request.
Parameters:
Name Type Default Description cause
Error The error's message.
requestParams
HttpProxy~RequestParams An object representing the complete request parameters used to create and send the HTTP request.
status
number Server's response HTTP status code.
responseBody
* null The body of the server's response, if any.
Returns:
The error representing a failed HTTP request.
- Type
- GenericError
-
_getContentType(method, data, options)
-
Gets a
Content-Type
header value for defined method, data and options.Parameters:
Name Type Description method
string The HTTP method to use.
data
Object.<string, (boolean|number|string|Date)> The data to be send with a request.
options
HttpAgent~RequestOptions Options provided by the HTTP agent.
Returns:
A
Content-Type
header value.- Type
- string
-
_getFetchApi()
-
Returns window.fetch compatible API to use, depending on the method being used at the server (polyfill) or client (native/polyfill) side.
Returns:
An implementation of the Fetch API to use.
- Type
- function
-
_headersToPlainObject(headers)
-
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.
- Type
- Object.<string, string>
-
_processError(fetchError, requestParams)
-
Processes the provided Fetch API or internal error and creates an error to expose to the calling API.
Parameters:
Name Type Description fetchError
Error The internal error to process.
requestParams
HttpProxy~RequestParams An object representing the complete request parameters used to create and send the HTTP request.
Returns:
The error to provide to the calling API.
- Type
- GenericError
-
_processResponse(requestParams, response, responseBody)
-
Processes the response received from the server.
Parameters:
Name Type Description requestParams
HttpProxy~RequestParams The original request's parameters.
response
Response The Fetch API's
Response
object representing the server's response.responseBody
* The server's response body.
Returns:
The server's response along with all related metadata.
- Type
- HttpAgent~Response
-
_shouldRequestHaveBody(method, data)
-
Checks if a request should have a body (
GET
andHEAD
requests don't have a body).Parameters:
Name Type Description method
string The HTTP method.
data
Object.<string, (boolean|number|string|Date)> The data to be send with a request.
Returns:
true
if a request has a body, otherwisefalse
.- Type
- boolean
-
clearDefaultHeaders()
-
Clears all defaults headers sent with all requests.
-
getErrorParams(method, url, data, options, status, body, cause)
-
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:
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
Object.<string, (boolean|number|string|Date)> The data sent with the request.
options
HttpAgent~RequestOptions Optional request options.
status
number The HTTP response status code send by the server.
body
object 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.
-
haveToSetCookiesManually()
-
Returns
true
if cookies have to be processed manually by settingCookie
HTTP header on requests and parsing theSet-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
orfalse
accordingly.Returns:
true
if cookies are not processed automatically by the environment and have to be handled manually by parsing response headers and setting request headers, otherwisefalse
.- Type
- boolean
-
request(method, url, data [, options])
-
Executes a HTTP request to the specified URL using the specified HTTP method, carrying the provided data.
Parameters:
Name Type Argument Description method
string The HTTP method to use.
url
string The URL to which the request should be made.
data
Object.<string, (boolean|number|string|Date)> The data to be send to the server. The data will be included as query parameters if the request method is
GET
orHEAD
, and as a request body for any other request method.options
HttpAgent~RequestOptions <optional>
Optional request options.
Returns:
A promise that resolves to the server response.
- Type
- Promise.<HttpAgent~Response>
-
setDefaultHeader(header, value)
-
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:
Name Type Description header
string A header name.
value
string A header value.
Type Definitions
-
ErrorParams
-
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:
- Object
Properties:
Name Type Description errorName
string An error name.
status
number The HTTP response status code send by the server.
body
object The body of HTTP error response (detailed information).
cause
Error The low-level cause error.
params
HttpProxy~RequestParams An object representing the complete request parameters used to create and send the HTTP request.
-
RequestParams
-
An object representing the complete request parameters used to create and send the HTTP request.
Type:
- Object
Properties:
Name Type Description method
string The HTTP method.
url
string The original URL to which to make the request.
transformedUrl
string The actual URL to which to make the request, created by applying the URL transformer to the original URL.
data
Object.<string, (boolean|number|string|Date)> The request data, sent as query or body.
options
HttpAgent~RequestOptions The high-level request options provided by the HTTP agent.