init
This commit is contained in:
25
frontend/composables/useChatApi.ts
Normal file
25
frontend/composables/useChatApi.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
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 || "Не удалось выполнить запрос к чату");
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user