Rename constants and move functions in order of priority

Readers always start at the top, we should put the most important functions / definitions there.
This commit is contained in:
Mattias Granlund 2024-04-11 11:55:07 +02:00
parent 5abcdcca1f
commit ffbd6e7a0c

View File

@ -1,12 +1,6 @@
import { invoke } from './ipc'; import { invoke } from './ipc';
import { PUBLIC_API_BASE_URL } from '$env/static/public'; 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 = { export type Feedback = {
id: number; id: number;
user_id: number; user_id: number;
@ -48,22 +42,17 @@ export type Project = {
updated_at: string; updated_at: string;
}; };
async function parseResponseJSON(response: Response) { const API_URL = new URL('/api/', PUBLIC_API_BASE_URL);
if (response.status === 204 || response.status === 205) { const DEFAULT_HEADERS = {
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 = {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}; };
type RequestMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
function getApiUrl(path: string) {
return new URL(path, API_URL);
}
export class HttpClient { export class HttpClient {
constructor(public fetch = window.fetch) {} constructor(public fetch = window.fetch) {}
@ -82,7 +71,7 @@ export class HttpClient {
body?: FormData | object; body?: FormData | object;
headers?: Record<string, string | undefined>; headers?: Record<string, string | undefined>;
}): Promise<T> { }): Promise<T> {
const butlerHeaders = new Headers(defaultHeaders); const butlerHeaders = new Headers(DEFAULT_HEADERS);
if (params.headers) { if (params.headers) {
Object.entries(params.headers).forEach(([key, value]) => { Object.entries(params.headers).forEach(([key, value]) => {
@ -96,8 +85,8 @@ export class HttpClient {
if (params.token) butlerHeaders.set('X-Auth-Token', params.token); if (params.token) butlerHeaders.set('X-Auth-Token', params.token);
const response = await this.fetch(getUrl(params.path), { const response = await this.fetch(getApiUrl(params.path), {
method: params.method, method: params.method || 'GET',
headers: butlerHeaders, headers: butlerHeaders,
body: this.formatBody(params.body) body: this.formatBody(params.body)
}); });
@ -148,7 +137,7 @@ export class HttpClient {
async createLoginToken(): Promise<LoginToken> { async createLoginToken(): Promise<LoginToken> {
const token = await this.post<LoginToken>({ path: 'login/token.json' }); const token = await this.post<LoginToken>({ path: 'login/token.json' });
const url = new URL(token.url); const url = new URL(token.url);
url.host = apiUrl.host; url.host = API_URL.host;
return { return {
...token, ...token,
url: url.toString() 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) { export async function syncToCloud(projectId: string | undefined) {
try { try {
if (projectId) await invoke<void>('project_flush_and_push', { id: projectId }); if (projectId) await invoke<void>('project_flush_and_push', { id: projectId });