ApiFetcher

class ApiFetcher(window: Window)(source)

A class which makes it easier to access a Kobweb API endpoint, instead of using Window.fetch directly.

This class works by wrapping Window.http but specifically for Kobweb API calls (URLs which are prefixed with "/api/").

Constructors

Link copied to clipboard
constructor(window: Window)

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(apiPath: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response

Call DELETE on a target API path.

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

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

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

Call GET on a target API path.

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

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

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

Call HEAD on a target API path.

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

Call OPTIONS on a target API path.

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

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

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

Call PATCH on a target API path.

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

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

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

Call POST on a target API path.

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

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

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

Call PUT on a target API path.

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

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

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

Like delete, but returns null if the request fails.

suspend fun <T> tryDelete(apiPath: 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(apiPath: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

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

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

Like get, but returns null if the request fails.

suspend fun <T> tryGet(apiPath: 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(apiPath: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

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

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

Like head, but returns null if the request fails.

suspend fun <T> tryHead(apiPath: 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(apiPath: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): Response?

Like options, but returns null if the request fails.

suspend fun <T> tryOptions(apiPath: 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(apiPath: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

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

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

Like patch, but returns null if the request fails.

suspend fun <T> tryPatch(apiPath: 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(apiPath: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

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

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

Like post, but returns null if the request fails.

suspend fun <T> tryPost(apiPath: 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(apiPath: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

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

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

Like put, but returns null if the request fails.

suspend fun <T> tryPut(apiPath: 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(apiPath: String, body: ByteArray? = null, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null): ByteArray?

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