mirror of
https://github.com/Drop-OSS/drop.git
synced 2026-01-31 15:37:09 +01:00
feat: refactor news and migrate rest of useFetch to $dropFetch
This commit is contained in:
@@ -9,10 +9,7 @@ export const useCollections = async () => {
|
||||
// @ts-expect-error
|
||||
const state = useState<FullCollection[]>("collections", () => undefined);
|
||||
if (state.value === undefined) {
|
||||
const headers = useRequestHeaders(["cookie"]);
|
||||
state.value = await $dropFetch<FullCollection[]>("/api/v1/collection", {
|
||||
headers,
|
||||
});
|
||||
state.value = await $dropFetch<FullCollection[]>("/api/v1/collection");
|
||||
}
|
||||
|
||||
return state;
|
||||
@@ -41,8 +38,5 @@ export const useLibrary = async () => {
|
||||
|
||||
export async function refreshLibrary() {
|
||||
const state = useState<FullCollection>("library");
|
||||
const headers = useRequestHeaders(["cookie"]);
|
||||
state.value = await $dropFetch<FullCollection>("/api/v1/collection/default", {
|
||||
headers,
|
||||
});
|
||||
state.value = await $dropFetch<FullCollection>("/api/v1/collection/default");
|
||||
}
|
||||
|
||||
@@ -1,35 +1,40 @@
|
||||
export const useNews = () => {
|
||||
const getAll = async (options?: {
|
||||
limit?: number;
|
||||
skip?: number;
|
||||
orderBy?: "asc" | "desc";
|
||||
tags?: string[];
|
||||
search?: string;
|
||||
}) => {
|
||||
const query = new URLSearchParams();
|
||||
import type { Article } from "@prisma/client";
|
||||
import type { SerializeObject } from "nitropack";
|
||||
|
||||
if (options?.limit) query.set("limit", options.limit.toString());
|
||||
if (options?.skip) query.set("skip", options.skip.toString());
|
||||
if (options?.orderBy) query.set("order", options.orderBy);
|
||||
if (options?.tags?.length) query.set("tags", options.tags.join(","));
|
||||
if (options?.search) query.set("search", options.search);
|
||||
export const useNews = () =>
|
||||
useState<
|
||||
| Array<
|
||||
SerializeObject<
|
||||
Article & {
|
||||
tags: Array<{ id: string; name: string }>;
|
||||
author: { displayName: string; id: string } | null;
|
||||
}
|
||||
>
|
||||
>
|
||||
| undefined
|
||||
>("news", () => undefined);
|
||||
|
||||
return await useFetch(`/api/v1/news?${query.toString()}`);
|
||||
};
|
||||
export const fetchNews = async (options?: {
|
||||
limit?: number;
|
||||
skip?: number;
|
||||
orderBy?: "asc" | "desc";
|
||||
tags?: string[];
|
||||
search?: string;
|
||||
}) => {
|
||||
const query = new URLSearchParams();
|
||||
|
||||
const getById = async (id: string) => {
|
||||
return await useFetch(`/api/v1/news/${id}`);
|
||||
};
|
||||
if (options?.limit) query.set("limit", options.limit.toString());
|
||||
if (options?.skip) query.set("skip", options.skip.toString());
|
||||
if (options?.orderBy) query.set("order", options.orderBy);
|
||||
if (options?.tags?.length) query.set("tags", options.tags.join(","));
|
||||
if (options?.search) query.set("search", options.search);
|
||||
|
||||
const remove = async (id: string) => {
|
||||
return await $dropFetch(`/api/v1/admin/news/${id}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
};
|
||||
const news = useNews();
|
||||
|
||||
return {
|
||||
getAll,
|
||||
getById,
|
||||
remove,
|
||||
};
|
||||
// @ts-ignore
|
||||
const newValue = await $dropFetch(`/api/v1/news?${query.toString()}`);
|
||||
|
||||
news.value = newValue;
|
||||
|
||||
return newValue;
|
||||
};
|
||||
|
||||
@@ -31,7 +31,11 @@ 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);
|
||||
const headers = useRequestHeaders(["cookie"]);
|
||||
const { data, error } = await useFetch(request, {
|
||||
...opts,
|
||||
headers: { ...opts?.headers, ...headers },
|
||||
} as any);
|
||||
if (error.value) throw error.value;
|
||||
return data.value as any;
|
||||
};
|
||||
|
||||
@@ -6,11 +6,10 @@ import type { User } from "@prisma/client";
|
||||
|
||||
export const useUser = () => useState<User | undefined | null>(undefined);
|
||||
export const updateUser = async () => {
|
||||
const headers = useRequestHeaders(["cookie"]);
|
||||
|
||||
const user = useUser();
|
||||
if (user.value === null) return;
|
||||
|
||||
// SSR calls have to be after uses
|
||||
user.value = await $dropFetch<User | null>("/api/v1/user", { headers });
|
||||
user.value = await $dropFetch<User | null>("/api/v1/user");
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user