feat: user page & $dropFetch util

This commit is contained in:
DecDuck
2025-03-14 12:22:08 +11:00
parent 3225f536ce
commit bd1cb67cd0
39 changed files with 416 additions and 166 deletions

View File

@ -10,7 +10,7 @@ export const useCollections = async () => {
const state = useState<FullCollection[]>("collections", () => undefined);
if (state.value === undefined) {
const headers = useRequestHeaders(["cookie"]);
state.value = await $fetch<FullCollection[]>("/api/v1/collection", {
state.value = await $dropFetch<FullCollection[]>("/api/v1/collection", {
headers,
});
}
@ -20,7 +20,7 @@ export const useCollections = async () => {
export async function refreshCollection(id: string) {
const state = useState<FullCollection[]>("collections");
const collection = await $fetch<FullCollection>(`/api/v1/collection/${id}`);
const collection = await $dropFetch<FullCollection>(`/api/v1/collection/${id}`);
const index = state.value.findIndex((e) => e.id == id);
if (index == -1) {
state.value.push(collection);
@ -42,7 +42,7 @@ export const useLibrary = async () => {
export async function refreshLibrary() {
const state = useState<FullCollection>("library");
const headers = useRequestHeaders(["cookie"]);
state.value = await $fetch<FullCollection>("/api/v1/collection/default", {
state.value = await $dropFetch<FullCollection>("/api/v1/collection/default", {
headers,
});
}

View File

@ -22,7 +22,7 @@ export const useNews = () => {
};
const remove = async (id: string) => {
return await $fetch(`/api/v1/admin/news/${id}`, {
return await $dropFetch(`/api/v1/admin/news/${id}`, {
method: "DELETE",
});
};

37
composables/request.ts Normal file
View File

@ -0,0 +1,37 @@
import type {
$Fetch,
ExtractedRouteMethod,
NitroFetchOptions,
NitroFetchRequest,
TypedInternalResponse,
} from "nitropack/types";
interface DropFetch<
DefaultT = unknown,
DefaultR extends NitroFetchRequest = NitroFetchRequest
> {
<
T = DefaultT,
R extends NitroFetchRequest = DefaultR,
O extends NitroFetchOptions<R> = NitroFetchOptions<R>
>(
request: R,
opts?: O
): Promise<
// @ts-ignore
TypedInternalResponse<
R,
T,
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
>
>;
}
export const $dropFetch: DropFetch = async (request, opts) => {
if (!getCurrentInstance()?.proxy) {
return (await $fetch(request, opts)) as any;
}
const { data, error } = await useFetch(request, opts as any);
if (error.value) throw error.value;
return data.value as any;
};

View File

@ -12,5 +12,5 @@ export const updateUser = async () => {
if (user.value === null) return;
// SSR calls have to be after uses
user.value = await $fetch<User | null>("/api/v1/user", { headers });
user.value = await $dropFetch<User | null>("/api/v1/user", { headers });
};