diff --git a/client/src/server/server.ts b/client/src/server/server.ts index b411ac5..8c3a67c 100755 --- a/client/src/server/server.ts +++ b/client/src/server/server.ts @@ -117,12 +117,14 @@ export class WebSocketServer extends EventDispatcher implements Server { this.errorListener = fn } - fetch(path: string, init: RequestInit = {}) { + async fetch(path: string, init: RequestInit = {}) { init.headers = { ...init.headers, ...(this.token && { 'Authorization': 'Bearer ' + this.token }) } - return fetch(`${this.httpUrl}/${path}`, init) + const resp = await fetch(`${this.httpUrl}/${path}`, init) + if (!resp.ok) throw await resp.text() + return resp } query(type: K, content: Send[K], options: Partial = {}): Promise { diff --git a/client/src/ui/index.svelte b/client/src/ui/index.svelte index 18b2dc3..7426e47 100644 --- a/client/src/ui/index.svelte +++ b/client/src/ui/index.svelte @@ -6,7 +6,6 @@ import storage from '../storage' import { WebSocketServer } from '../server/server' import { server, serverCfg } from './global' - import { queryConfig } from './lib/util' export let url = '' @@ -26,7 +25,6 @@ }) await connected - // let config = await queryConfig($serverCfg, params.mapName) let config = await $server.query('config', params.mapName) console.log('joining map', config) diff --git a/client/src/ui/lib/util.ts b/client/src/ui/lib/util.ts index 128edd9..174ebdd 100644 --- a/client/src/ui/lib/util.ts +++ b/client/src/ui/lib/util.ts @@ -27,6 +27,7 @@ export async function download(path: string, name: string) { const id = showInfo(`Downloading '${name}'…`, 'none') try { const resp = await fetch(path) + if (!resp.ok) throw await resp.text() const data = await resp.blob() const url = URL.createObjectURL(data) @@ -48,7 +49,6 @@ export async function uploadMap(url: string, name: string, file: Blob) { method: 'PUT', body: file, }) - if (!resp.ok) throw await resp.text() } @@ -58,7 +58,6 @@ export async function createMap(url: string, name: string, create: MapCreation) headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(create), }) - if (!resp.ok) throw await resp.text() } @@ -96,13 +95,15 @@ export async function queryMaps(url: string): Promise { } const resp = await fetch(`${url}/maps`) + if (!resp.ok) throw await resp.text() + const maps: MapDetail[] = await resp.json() sortMaps(maps) return maps } -export async function queryConfig(server: WebSocketServer, mapName: string): Promise { - const resp = await server.fetch(`maps/${mapName}/config`) +export async function queryConfig(url: string, mapName: string): Promise { + const resp = await fetch(`${url}/maps/${mapName}/config`) const config: Config = await resp.json() return config } diff --git a/client/src/ui/routes/lobby.svelte b/client/src/ui/routes/lobby.svelte index bbfd862..6debaad 100644 --- a/client/src/ui/routes/lobby.svelte +++ b/client/src/ui/routes/lobby.svelte @@ -1,6 +1,6 @@