@@ -2,6 +2,7 @@ package com.varabyte.kobweb.browser.http
22
33import kotlinx.browser.window
44import org.w3c.dom.Window
5+ import org.w3c.dom.WindowOrWorkerGlobalScope
56import org.w3c.fetch.RequestRedirect
67
78/* *
@@ -50,7 +51,7 @@ class AbortController {
5051 *
5152 * The class additionally exposes a `logOnError` field which globally applies to all `try...` methods.
5253 */
53- class HttpFetcher (private val window : Window ) {
54+ class HttpFetcher (private val fetchScope : WindowOrWorkerGlobalScope ) {
5455 /* *
5556 * If true, when using any of the "try" methods, log any errors, if they occur, to the console.
5657 *
@@ -81,7 +82,7 @@ class HttpFetcher(private val window: Window) {
8182 headers : Map <String , Any >? = FetchDefaults .Headers ,
8283 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
8384 abortController : AbortController ? = null
84- ): ByteArray = window .fetchBytes(HttpMethod .DELETE , resource, headers, body = null , redirect, abortController)
85+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .DELETE , resource, headers, body = null , redirect, abortController)
8586
8687 /* *
8788 * Like [delete], but returns null if the request failed for any reason.
@@ -107,7 +108,7 @@ class HttpFetcher(private val window: Window) {
107108 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
108109 abortController : AbortController ? = null
109110 ): ByteArray? =
110- window .tryFetchBytes(HttpMethod .DELETE , resource, headers, body = null , redirect, logOnError, abortController)
111+ fetchScope .tryFetchBytes(HttpMethod .DELETE , resource, headers, body = null , redirect, logOnError, abortController)
111112
112113 /* *
113114 * Call GET on a target resource.
@@ -132,7 +133,7 @@ class HttpFetcher(private val window: Window) {
132133 headers : Map <String , Any >? = FetchDefaults .Headers ,
133134 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
134135 abortController : AbortController ? = null
135- ): ByteArray = window .fetchBytes(HttpMethod .GET , resource, headers, body = null , redirect, abortController)
136+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .GET , resource, headers, body = null , redirect, abortController)
136137
137138 /* *
138139 * Like [get], but returns null if the request failed for any reason.
@@ -158,7 +159,7 @@ class HttpFetcher(private val window: Window) {
158159 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
159160 abortController : AbortController ? = null
160161 ): ByteArray? =
161- window .tryFetchBytes(HttpMethod .GET , resource, headers, body = null , redirect, logOnError, abortController)
162+ fetchScope .tryFetchBytes(HttpMethod .GET , resource, headers, body = null , redirect, logOnError, abortController)
162163
163164 /* *
164165 * Call HEAD on a target resource.
@@ -183,7 +184,7 @@ class HttpFetcher(private val window: Window) {
183184 headers : Map <String , Any >? = FetchDefaults .Headers ,
184185 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
185186 abortController : AbortController ? = null
186- ): ByteArray = window .fetchBytes(HttpMethod .HEAD , resource, headers, body = null , redirect, abortController)
187+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .HEAD , resource, headers, body = null , redirect, abortController)
187188
188189 /* *
189190 * Like [head], but returns null if the request failed for any reason.
@@ -209,7 +210,7 @@ class HttpFetcher(private val window: Window) {
209210 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
210211 abortController : AbortController ? = null
211212 ): ByteArray? =
212- window .tryFetchBytes(HttpMethod .HEAD , resource, headers, body = null , redirect, logOnError, abortController)
213+ fetchScope .tryFetchBytes(HttpMethod .HEAD , resource, headers, body = null , redirect, logOnError, abortController)
213214
214215 /* *
215216 * Call OPTIONS on a target resource.
@@ -234,7 +235,7 @@ class HttpFetcher(private val window: Window) {
234235 headers : Map <String , Any >? = FetchDefaults .Headers ,
235236 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
236237 abortController : AbortController ? = null
237- ): ByteArray = window .fetchBytes(HttpMethod .OPTIONS , resource, headers, body = null , redirect, abortController)
238+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .OPTIONS , resource, headers, body = null , redirect, abortController)
238239
239240 /* *
240241 * Like [options], but returns null if the request failed for any reason.
@@ -260,7 +261,7 @@ class HttpFetcher(private val window: Window) {
260261 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
261262 abortController : AbortController ? = null
262263 ): ByteArray? =
263- window .tryFetchBytes(HttpMethod .OPTIONS , resource, headers, body = null , redirect, logOnError, abortController)
264+ fetchScope .tryFetchBytes(HttpMethod .OPTIONS , resource, headers, body = null , redirect, logOnError, abortController)
264265
265266 /* *
266267 * Call PATCH on a target resource.
@@ -289,7 +290,7 @@ class HttpFetcher(private val window: Window) {
289290 body : ByteArray? = null,
290291 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
291292 abortController : AbortController ? = null
292- ): ByteArray = window .fetchBytes(HttpMethod .PATCH , resource, headers, body, redirect, abortController)
293+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .PATCH , resource, headers, body, redirect, abortController)
293294
294295 /* *
295296 * Like [patch], but returns null if the request failed for any reason.
@@ -317,7 +318,7 @@ class HttpFetcher(private val window: Window) {
317318 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
318319 abortController : AbortController ? = null
319320 ): ByteArray? =
320- window .tryFetchBytes(HttpMethod .PATCH , resource, headers, body, redirect, logOnError, abortController)
321+ fetchScope .tryFetchBytes(HttpMethod .PATCH , resource, headers, body, redirect, logOnError, abortController)
321322
322323 /* *
323324 * Call POST on a target resource.
@@ -346,7 +347,7 @@ class HttpFetcher(private val window: Window) {
346347 body : ByteArray? = null,
347348 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
348349 abortController : AbortController ? = null
349- ): ByteArray = window .fetchBytes(HttpMethod .POST , resource, headers, body, redirect, abortController)
350+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .POST , resource, headers, body, redirect, abortController)
350351
351352 /* *
352353 * Like [post], but returns null if the request failed for any reason.
@@ -374,7 +375,7 @@ class HttpFetcher(private val window: Window) {
374375 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
375376 abortController : AbortController ? = null
376377 ): ByteArray? =
377- window .tryFetchBytes(HttpMethod .POST , resource, headers, body, redirect, logOnError, abortController)
378+ fetchScope .tryFetchBytes(HttpMethod .POST , resource, headers, body, redirect, logOnError, abortController)
378379
379380 /* *
380381 * Call PUT on a target resource.
@@ -403,7 +404,7 @@ class HttpFetcher(private val window: Window) {
403404 body : ByteArray? = null,
404405 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
405406 abortController : AbortController ? = null
406- ): ByteArray = window .fetchBytes(HttpMethod .PUT , resource, headers, body, redirect, abortController)
407+ ): ByteArray = fetchScope .fetchBytes(HttpMethod .PUT , resource, headers, body, redirect, abortController)
407408
408409 /* *
409410 * Like [put], but returns null if the request failed for any reason.
@@ -430,8 +431,10 @@ class HttpFetcher(private val window: Window) {
430431 body : ByteArray? = null,
431432 redirect : RequestRedirect ? = FetchDefaults .Redirect ,
432433 abortController : AbortController ? = null
433- ): ByteArray? = window .tryFetchBytes(HttpMethod .PUT , resource, headers, body, redirect, logOnError, abortController)
434+ ): ByteArray? = fetchScope .tryFetchBytes(HttpMethod .PUT , resource, headers, body, redirect, logOnError, abortController)
434435}
435436
436- @Suppress(" unused" ) // We tie our class to the "Window" class on purpose, so it can be used instead of `fetch`
437- val Window .http by lazy { HttpFetcher (window) }
437+ val WindowOrWorkerGlobalScope .http: HttpFetcher get() = with (this .asDynamic()) {
438+ httpFetcher = httpFetcher ? : HttpFetcher (this )
439+ httpFetcher
440+ }
0 commit comments