From ffbd6e7a0c396c968eb2d5f8fcd82ba74eff8dc0 Mon Sep 17 00:00:00 2001 From: Mattias Granlund Date: Thu, 11 Apr 2024 11:55:07 +0200 Subject: [PATCH] Rename constants and move functions in order of priority Readers always start at the top, we should put the most important functions / definitions there. --- app/src/lib/backend/httpClient.ts | 45 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/app/src/lib/backend/httpClient.ts b/app/src/lib/backend/httpClient.ts index 844289208..f9c3c47fb 100644 --- a/app/src/lib/backend/httpClient.ts +++ b/app/src/lib/backend/httpClient.ts @@ -1,12 +1,6 @@ import { invoke } from './ipc'; import { PUBLIC_API_BASE_URL } from '$env/static/public'; -const apiUrl = new URL('/api/', new URL(PUBLIC_API_BASE_URL)); - -function getUrl(path: string) { - return new URL(path, apiUrl).toString(); -} - export type Feedback = { id: number; user_id: number; @@ -48,22 +42,17 @@ export type Project = { updated_at: string; }; -async function parseResponseJSON(response: Response) { - if (response.status === 204 || response.status === 205) { - return null; - } else if (response.status >= 400) { - throw new Error(`HTTP Error ${response.statusText}: ${await response.text()}`); - } else { - return await response.json(); - } -} - -export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; - -const defaultHeaders = { +const API_URL = new URL('/api/', PUBLIC_API_BASE_URL); +const DEFAULT_HEADERS = { 'Content-Type': 'application/json' }; +type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE'; + +function getApiUrl(path: string) { + return new URL(path, API_URL); +} + export class HttpClient { constructor(public fetch = window.fetch) {} @@ -82,7 +71,7 @@ export class HttpClient { body?: FormData | object; headers?: Record; }): Promise { - const butlerHeaders = new Headers(defaultHeaders); + const butlerHeaders = new Headers(DEFAULT_HEADERS); if (params.headers) { Object.entries(params.headers).forEach(([key, value]) => { @@ -96,8 +85,8 @@ export class HttpClient { if (params.token) butlerHeaders.set('X-Auth-Token', params.token); - const response = await this.fetch(getUrl(params.path), { - method: params.method, + const response = await this.fetch(getApiUrl(params.path), { + method: params.method || 'GET', headers: butlerHeaders, body: this.formatBody(params.body) }); @@ -148,7 +137,7 @@ export class HttpClient { async createLoginToken(): Promise { const token = await this.post({ path: 'login/token.json' }); const url = new URL(token.url); - url.host = apiUrl.host; + url.host = API_URL.host; return { ...token, url: url.toString() @@ -240,6 +229,16 @@ export class HttpClient { } } +async function parseResponseJSON(response: Response) { + if (response.status === 204 || response.status === 205) { + return null; + } else if (response.status >= 400) { + throw new Error(`HTTP Error ${response.statusText}: ${await response.text()}`); + } else { + return await response.json(); + } +} + export async function syncToCloud(projectId: string | undefined) { try { if (projectId) await invoke('project_flush_and_push', { id: projectId });