Skip to content

FetchHttpClient

Class | Source Code

implements IHttpClient<RequestInit, Request, Response, FetchRawResponseBody>

Default implementation for an HTTP(S) client that uses the fetch API. Any kind of body can be given, but this implementation has a preference toward JSON. If no Content-Type is included on a request’s header, application/json is assumed and the body will be stringified accordingly. If the ‘Content-Type’ is included, the Accept header is auto-populated to prefer the same content type, with a secondary preference for anything.

When parsing the response’s body, the provided responseBodyParser is always preferred if supplied. If it’s not, the request’s Content-Type is observed. If it’s JSON, it’s parsed as JSON. Otherwise it’s parsed as text.

new FetchHttpClient(options: FetchHttpClientOptions)

Section titled “new FetchHttpClient(options: FetchHttpClientOptions)”

Triggered on a general network error. Does not occur on non-200 series responses.


onReceiveResponse: Event<ReceiveResponseHandler>

Section titled “onReceiveResponse: Event<ReceiveResponseHandler>”

Triggered when a response is received.


Triggered just before a request is sent.






defaultRequestOptions: undefined | Partial

Section titled “defaultRequestOptions: undefined | Partial”

Default options to include in all requests this client makes. Each option should be overrideable if the same option is defined in the provided options when making a request.




A default protocol and domain to use for the instance. It will be prepended to all request URIs if given during construction.

'https://my.site.com/'
'http://my.site.com'

delete(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>

Section titled “delete(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>”

Convenience function for sending a DELETE request.

A promise that resolves to response data. The properties of the returned object may be undefined if a fetch error occurred during processing. To globally address fetch errors made by this client, use the onFetchError event. To address errors for individual requests, use the allowThrow option on the request and handle it via try/catch.


get(uri: string, requestData?: Omit<BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>, “body”>): Promise<Partial<BasicHttpResponseData>>

Section titled “get(uri: string, requestData?: Omit<BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>, “body”>): Promise<Partial<BasicHttpResponseData>>”

Convenience function for sending a GET request.

A promise that resolves to response data. The properties of the returned object may be undefined if a fetch error occurred during processing. To globally address fetch errors made by this client, use the onFetchError event. To address errors for individual requests, use the allowThrow option on the request and handle it via try/catch.


makeRequest(method: string, uri: string, requestData: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>

Section titled “makeRequest(method: string, uri: string, requestData: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>”

Perform a request with the specified method. Useful if the convenience functions don’t provide the HTTP verb you need.

A promise that resolves to response data. The properties of the returned object may be undefined if a fetch error occurred during processing. To globally address fetch errors made by this client, use the onFetchError event. To address errors for individual requests, use the allowThrow option on the request and handle it via try/catch.


patch(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>

Section titled “patch(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>”

Convenience function for sending a PATCH request.

A promise that resolves to response data. The properties of the returned object may be undefined if a fetch error occurred during processing. To globally address fetch errors made by this client, use the onFetchError event. To address errors for individual requests, use the allowThrow option on the request and handle it via try/catch.


post(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>

Section titled “post(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>”

Convenience function for sending a POST request.

A promise that resolves to response data. The properties of the returned object may be undefined if a fetch error occurred during processing. To globally address fetch errors made by this client, use the onFetchError event. To address errors for individual requests, use the allowThrow option on the request and handle it via try/catch.


put(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>

Section titled “put(uri: string, requestData?: BasicHttpRequestData<FetchRawResponseBody, ParsedBodyType, RequestInit>): Promise<Partial<BasicHttpResponseData>>”

Convenience function for sending a PUT request.

A promise that resolves to response data. The properties of the returned object may be undefined if a fetch error occurred during processing. To globally address fetch errors made by this client, use the onFetchError event. To address errors for individual requests, use the allowThrow option on the request and handle it via try/catch.