HttpFetcher

A class with a slightly friendlier API than using Window.fetch directly by providing HTTP method helper methods.

For example:

// Without HttpFetcher
window.fetch(HttpMethod.GET, "/some/api/path")

// With HttpFetcher
window.http.get("/some/api/path")
// ... or a version that won't throw exceptions
// window.http.tryGet("/some/api/path")

The class additionally exposes a logOnError property which globally applies to all try... methods.

Constructors

Link copied to clipboard
constructor(fetchScope: WindowOrWorkerGlobalScope)

Properties

Link copied to clipboard

If true, when using any of the "try" methods, logs any errors, if they occur, to the console.

Functions

Link copied to clipboard
suspend fun delete(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call DELETE on a target resource.

Link copied to clipboard
suspend fun deleteBytes(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray

Call DELETE on a target resource, returning the response body as a raw array of bytes.

Link copied to clipboard
suspend fun get(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call GET on a target resource.

Link copied to clipboard
suspend fun getBytes(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray

Call GET on a target resource, returning the response body as a raw array of bytes.

Link copied to clipboard
suspend fun head(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call HEAD on a target resource.

Link copied to clipboard
suspend fun options(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call OPTIONS on a target resource.

Link copied to clipboard
suspend fun optionsBytes(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray

Call OPTIONS on a target resource, returning the response body as a raw array of bytes.

Link copied to clipboard
suspend fun patch(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call PATCH on a target resource.

Link copied to clipboard
suspend fun patchBytes(resource: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray

Call PATCH on a target resource, returning the response body as a raw array of bytes.

Link copied to clipboard
suspend fun post(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call POST on a target resource.

Link copied to clipboard
suspend fun postBytes(resource: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray

Call POST on a target resource, returning the response body as a raw array of bytes.

Link copied to clipboard
suspend fun put(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call PUT on a target resource.

Link copied to clipboard
suspend fun putBytes(resource: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray

Call PUT on a target resource, returning the response body as a raw array of bytes.

Link copied to clipboard
suspend fun tryDelete(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryDelete(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like delete, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryDeleteBytes(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

Like deleteBytes, but returns null instead of throwing if the request fails or its body can't be read.

Link copied to clipboard
suspend fun tryGet(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryGet(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like get, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryGetBytes(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

Like getBytes, but returns null instead of throwing if the request fails or its body can't be read.

Link copied to clipboard
suspend fun tryHead(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryHead(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like head, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryOptions(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryOptions(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like options, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryOptionsBytes(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

Like optionsBytes, but returns null instead of throwing if the request fails or its body can't be read.

Link copied to clipboard
suspend fun tryPatch(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryPatch(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like patch, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryPatchBytes(resource: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

Like patchBytes, but returns null instead of throwing if the request fails or its body can't be read.

Link copied to clipboard
suspend fun tryPost(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryPost(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like post, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryPostBytes(resource: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

Like postBytes, but returns null instead of throwing if the request fails or its body can't be read.

Link copied to clipboard
suspend fun tryPut(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?
suspend fun <T> tryPut(resource: String, body: RequestBody? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, transform: suspend Response.() -> T): T?

Like put, but returns null instead of throwing if the request fails.

Link copied to clipboard
suspend fun tryPutBytes(resource: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

Like putBytes, but returns null instead of throwing if the request fails or its body can't be read.