mirror of
https://github.com/Drop-OSS/drop.git
synced 2025-11-09 20:12:10 +10:00
feat: user page & $dropFetch util
This commit is contained in:
@ -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,
|
||||
});
|
||||
}
|
||||
|
||||
@ -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
37
composables/request.ts
Normal 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;
|
||||
};
|
||||
@ -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 });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user