Files
talorr cda36918e8 init
2026-03-27 03:36:08 +03:00

26 lines
813 B
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function useChatApi<T>(path: string, options: Parameters<typeof $fetch<T>>[1] = {}) {
const config = useRuntimeConfig();
const { token, user } = useAuth();
const headers = new Headers(options.headers as HeadersInit | undefined);
if (!headers.has("Content-Type") && options.method && options.method !== "GET") {
headers.set("Content-Type", "application/json");
}
if (token.value) {
headers.set("Authorization", `Bearer ${token.value}`);
}
if (user.value?.email) {
headers.set("x-user-email", user.value.email);
}
return $fetch<T>(`${config.public.chatApiBase}${path}`, {
...options,
headers,
credentials: "include"
}).catch((error: Error) => {
throw new Error(error.message || "Не удалось выполнить запрос к чату");
});
}