Files
bio_frontend/composables/useApi.ts
2025-08-27 17:06:25 +09:00

33 lines
963 B
TypeScript

import { useFetch, useRuntimeConfig } from '#imports';
export const useApi = <T>(
path: string,
options: {
method?: 'get' | 'post' | 'put' | 'delete'
body?: any
query?: Record<string, any>
headers?: HeadersInit
server?: boolean // ← 이 줄 추가!
} = {}
) => {
const userStore = useUserStore();
const config = useRuntimeConfig();
const method = options.method ? options.method.toUpperCase() : 'GET'
return useFetch<T>(() => `${config.public.apiBase}${config.public.contextPath}${path}`, {
method: method as any, // 타입 강제 우회
body: options.body,
query: options.query,
headers: {
Authorization: 'Bearer ' + userStore.getToken,
...options.headers,
},
onResponse({response}){
const accessToken = response.headers.get("Authorization") || "";
userStore.setToken(accessToken.replace("Bearer ", ""));
},
server: options.server // ← 이 줄 추가!
})
}