mirror of
https://iceshrimp.dev/crimekillz/iceshrimp-161sh.git
synced 2024-11-21 19:53:50 +01:00
[frontend] Improve api helper function
This commit is contained in:
parent
80fbba219b
commit
d7d2401ca0
@ -5,7 +5,7 @@ import { onMounted, ref } from "vue";
|
||||
const field = ref<HTMLElement>();
|
||||
|
||||
onMounted(() => {
|
||||
api('/api/iceshrimp/v1/auth').then(res => {
|
||||
api('/v1/auth').then(res => {
|
||||
field.value!.textContent = JSON.stringify(res, null, 2);
|
||||
});
|
||||
});
|
||||
|
@ -1,15 +1,20 @@
|
||||
import { get as kvGet } from "idb-keyval";
|
||||
import { KvAccount } from "../entities/keyval.ts";
|
||||
|
||||
export async function api(endpoint: string, body?: object) {
|
||||
export async function api(endpoint: string, body?: object, prefix: string = '/api/iceshrimp') {
|
||||
const token = (await getCurrentAccount())?.token ?? null;
|
||||
const headers: Record<string, string> = {};
|
||||
|
||||
if (token != null) headers['Authorization'] = `Bearer ${token}`;
|
||||
if (body != null) headers['Content-Type'] = `application/json`;
|
||||
|
||||
const request = {
|
||||
method: body ? 'POST' : 'GET',
|
||||
headers: token ? { authorization: `Bearer ${token}` } : undefined,
|
||||
headers: headers,
|
||||
body: body ? JSON.stringify(body) : undefined
|
||||
};
|
||||
|
||||
return fetch(endpoint, request).then(res => res.json());
|
||||
return fetch(prefix + endpoint, request).then(res => res.json());
|
||||
}
|
||||
|
||||
//FIXME: cache this somewhere?
|
||||
|
Loading…
Reference in New Issue
Block a user