2025-08-28 16:00:25 +09:00
|
|
|
import { useRuntimeConfig } from "#imports";
|
2025-08-08 13:11:33 +09:00
|
|
|
|
2025-08-28 16:00:25 +09:00
|
|
|
export const useApi = async <T>(
|
2025-08-08 13:11:33 +09:00
|
|
|
path: string,
|
|
|
|
options: {
|
2025-08-28 16:00:25 +09:00
|
|
|
method?: "get" | "post" | "put" | "delete";
|
|
|
|
body?: any;
|
|
|
|
query?: Record<string, any>;
|
|
|
|
headers?: HeadersInit;
|
2025-08-28 16:47:24 +09:00
|
|
|
credentials?: RequestCredentials;
|
2025-08-08 13:11:33 +09:00
|
|
|
} = {}
|
2025-08-28 16:00:25 +09:00
|
|
|
): Promise<T> => {
|
2025-08-27 17:06:25 +09:00
|
|
|
const config = useRuntimeConfig();
|
2025-08-28 16:00:25 +09:00
|
|
|
const method = options.method ? options.method.toUpperCase() : "GET";
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await $fetch<T>(
|
|
|
|
`${config.public.apiBase}${config.public.contextPath}${path}`,
|
|
|
|
{
|
|
|
|
method: method as any,
|
|
|
|
body: options.body,
|
|
|
|
query: options.query,
|
2025-08-28 16:47:24 +09:00
|
|
|
credentials: options.credentials || "include", // 쿠키 자동 전송
|
2025-08-28 16:00:25 +09:00
|
|
|
headers: {
|
2025-08-28 16:47:24 +09:00
|
|
|
"Content-Type": "application/json",
|
2025-08-28 16:00:25 +09:00
|
|
|
...options.headers,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response;
|
|
|
|
} catch (error) {
|
|
|
|
console.error("API 호출 실패:", error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
};
|