Files
bio_frontend/composables/useApi.ts

37 lines
1014 B
TypeScript
Raw Permalink Normal View History

/**
* API
*
* @template T -
* @param path - API (: '/users', '/users/1')
* @param opts - (method, body, headers )
* @returns Promise<T> - API
*
* @example
* // GET 요청
* const users = await useApi<User[]>('/users')
*
* // POST 요청
* const newUser = await useApi<User>('/users', {
* method: 'POST',
* body: { name: 'John', email: 'john@example.com' }
* })
*
* // PUT 요청
* const updatedUser = await useApi<User>('/users/1', {
* method: 'PUT',
* body: { name: 'John Updated' }
* })
*
* // DELETE 요청
* await useApi('/users/1', { method: 'DELETE' })
*
* // FormData 업로드
* const formData = new FormData()
* formData.append('file', file)
* await useApi('/upload', { method: 'POST', body: formData })
*/
export const useApi = <T>(path: string, opts?: any): Promise<T> => {
const { $api } = useNuxtApp();
return ($api as any)(path, opts);
};