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.
Constructors
Section titled “Constructors”new FetchHttpClient(options: FetchHttpClientOptions)
Section titled “new FetchHttpClient(options: FetchHttpClientOptions)”Properties
Section titled “Properties”protected static URI_REGEX: RegExp
Section titled “protected static URI_REGEX: RegExp”onError: Event
Section titled “onError: Event”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.
onSendRequest: Event<SendRequestHandler>
Section titled “onSendRequest: Event<SendRequestHandler>”Triggered just before a request is sent.
protected _defaultRequestOptions: Object
Section titled “protected _defaultRequestOptions: Object”protected _host: string
Section titled “protected _host: string”protected _path: string
Section titled “protected _path: string”protected _protocol: HttpProtocol
Section titled “protected _protocol: HttpProtocol”protected _rateLimitMs: number
Section titled “protected _rateLimitMs: number”Accessors
Section titled “Accessors”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.
host: string
Section titled “host: string”path: string
Section titled “path: string”protocol: HttpProtocol
Section titled “protocol: HttpProtocol”A default protocol and domain to use for the instance. It will be prepended to all request URIs if given during construction.
Example
Section titled “Example”'https://my.site.com/''http://my.site.com'rateLimitMs: number
Section titled “rateLimitMs: number”Methods
Section titled “Methods”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.
Returns
Section titled “Returns”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.
Returns
Section titled “Returns”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.
Returns
Section titled “Returns”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.
Returns
Section titled “Returns”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.
Returns
Section titled “Returns”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.
Returns
Section titled “Returns”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.