Refactor API URL configuration

This commit is contained in:
Jesse Wierzbinski 2024-04-09 01:48:13 -10:00
parent d9198cfddf
commit 577b866bb3
No known key found for this signature in database
3 changed files with 18 additions and 13 deletions

View file

@ -20,12 +20,10 @@ if (!config) {
throw new Error("Config not found");
}
const url = `${config.http.bind}:${config.http.bind_port}`;
const route = useRoute();
const data = await fetch(
new URL(`/api/v1/statuses/${route.params.uuid}`, url),
new URL(`/api/v1/statuses/${route.params.uuid}`, config.http.url),
{
headers: {
Accept: "application/json",

View file

@ -20,28 +20,32 @@ if (!config) {
throw new Error("Config not found");
}
const url = `${config.http.bind}:${config.http.bind_port}`;
const route = useRoute();
const username = (route.params.username as string).replace("@", "");
const id = await fetch(new URL(`/api/v1/accounts/search?q=${username}`, url), {
const id = await fetch(
new URL(`/api/v1/accounts/search?q=${username}`, config.http.url),
{
headers: {
Accept: "application/json",
},
})
},
)
.then((res) => res.json())
.catch(() => null);
let data = null;
if (id && id.length > 0) {
data = await fetch(new URL(`/api/v1/accounts/${id[0].id}`, url), {
data = await fetch(
new URL(`/api/v1/accounts/${id[0].id}`, config.http.url),
{
headers: {
Accept: "application/json",
},
})
},
)
.then((res) => res.json())
.catch(() => ({
error: "Failed to fetch user (it probably doesn't exist)",

View file

@ -13,6 +13,9 @@ export default defineEventHandler(async () => {
bind: config?.http.bind,
bind_port: config?.http.bind_port,
base_url: config?.http.base_url,
url: config?.http.bind.includes("http")
? `${config?.http.bind}:${config?.http.bind_port}`
: `http://${config?.http.bind}:${config?.http.bind_port}`,
},
};
});