chore: some more clean up
This commit is contained in:
@@ -1,101 +1,101 @@
|
||||
import { goto } from '$app/navigation';
|
||||
import { userStore } from 'routes/UserStore.svelte';
|
||||
|
||||
const API = "/api";
|
||||
const API = '/api';
|
||||
|
||||
export async function get(url: string) {
|
||||
const headers = new Headers();
|
||||
//headers.append('content-type', 'application/json');
|
||||
headers.append('response-type', 'application/json');
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
|
||||
let r = await fetch(`${API}/${url}`, {
|
||||
const r = await fetch(`${API}/${url}`, {
|
||||
method: 'GET',
|
||||
headers: headers,
|
||||
headers: headers
|
||||
});
|
||||
|
||||
if (r.status === 401) {
|
||||
userStore.user = undefined;
|
||||
goto("/login")
|
||||
} else if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
if (r.status === 401) {
|
||||
userStore.user = undefined;
|
||||
goto('/login');
|
||||
} else if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
|
||||
return r.json();
|
||||
return r.json();
|
||||
}
|
||||
|
||||
export async function post(url: string, body: any) {
|
||||
const headers = new Headers();
|
||||
headers.append('content-type', 'application/json');
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
|
||||
let r = await fetch(`${API}/${url}`, {
|
||||
const r = await fetch(`${API}/${url}`, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: JSON.stringify(body),
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
if (r.status === 401) {
|
||||
userStore.user = undefined;
|
||||
goto("/login")
|
||||
throw r;
|
||||
} else if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
|
||||
return r.json();
|
||||
if (r.status === 401) {
|
||||
userStore.user = undefined;
|
||||
goto('/login');
|
||||
throw r;
|
||||
} else if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
|
||||
return r.json();
|
||||
}
|
||||
|
||||
export async function rdelete(url: string, body: any) {
|
||||
const headers = new Headers();
|
||||
headers.append('content-type', 'application/json');
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
|
||||
let r = await fetch(`${API}/${url}`, {
|
||||
const r = await fetch(`${API}/${url}`, {
|
||||
method: 'DELETE',
|
||||
headers: headers,
|
||||
body: JSON.stringify(body),
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
if (r.status === 401) {
|
||||
userStore.user = undefined;
|
||||
goto("/login")
|
||||
} else if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
if (r.status === 401) {
|
||||
userStore.user = undefined;
|
||||
goto('/login');
|
||||
} else if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
|
||||
return r.json();
|
||||
return r.json();
|
||||
}
|
||||
|
||||
export async function postFormData(url: string, body: FormData) {
|
||||
const headers = new Headers();
|
||||
//headers.append('content-type', 'multipart/form-data');
|
||||
headers.append('response-type', 'application/json');
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
if (userStore.user) {
|
||||
headers.append('token', userStore.user.token);
|
||||
}
|
||||
|
||||
let r = await fetch(`${API}/${url}`, {
|
||||
const r = await fetch(`${API}/${url}`, {
|
||||
method: 'POST',
|
||||
headers: headers,
|
||||
body: body,
|
||||
body: body
|
||||
});
|
||||
|
||||
if (r.status == 401) {
|
||||
userStore.user = undefined;
|
||||
goto('/login');
|
||||
throw new Error("Redirect");
|
||||
}
|
||||
if (r.status == 401) {
|
||||
userStore.user = undefined;
|
||||
goto('/login');
|
||||
throw new Error('Redirect');
|
||||
}
|
||||
|
||||
if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
if (r.status !== 200) {
|
||||
throw r;
|
||||
}
|
||||
|
||||
return r.json();
|
||||
return r.json();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user