mirror of
https://iceshrimp.dev/iceshrimp/iceshrimp
synced 2024-11-30 15:58:05 +09: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>();
|
const field = ref<HTMLElement>();
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
api('/api/iceshrimp/v1/auth').then(res => {
|
api('/v1/auth').then(res => {
|
||||||
field.value!.textContent = JSON.stringify(res, null, 2);
|
field.value!.textContent = JSON.stringify(res, null, 2);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,15 +1,20 @@
|
|||||||
import { get as kvGet } from "idb-keyval";
|
import { get as kvGet } from "idb-keyval";
|
||||||
import { KvAccount } from "../entities/keyval.ts";
|
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 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 = {
|
const request = {
|
||||||
method: body ? 'POST' : 'GET',
|
method: body ? 'POST' : 'GET',
|
||||||
headers: token ? { authorization: `Bearer ${token}` } : undefined,
|
headers: headers,
|
||||||
body: body ? JSON.stringify(body) : undefined
|
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?
|
//FIXME: cache this somewhere?
|
||||||
|
Loading…
Reference in New Issue
Block a user