Files
bio_frontend/composables/useApi.ts

37 lines
934 B
TypeScript

import { useRuntimeConfig } from "#imports";
export const useApi = async <T>(
path: string,
options: {
method?: "get" | "post" | "put" | "delete";
body?: any;
query?: Record<string, any>;
headers?: HeadersInit;
credentials?: RequestCredentials;
} = {}
): Promise<T> => {
const config = useRuntimeConfig();
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,
credentials: options.credentials || "include", // 쿠키 자동 전송
headers: {
"Content-Type": "application/json",
...options.headers,
},
}
);
return response;
} catch (error) {
console.error("API 호출 실패:", error);
throw error;
}
};