Spaces:
Runtime error
Runtime error
async function fetchAPI<TBody, TResponse>( | |
url: string, | |
method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'POST', | |
body?: TBody, | |
headers?: HeadersInit | |
): Promise<TResponse> { | |
const response = await fetch(url, { | |
method, | |
headers: { | |
'Content-Type': 'application/json', | |
...headers | |
}, | |
body: JSON.stringify(body) | |
}); | |
if (!response.ok) { | |
throw new Error(`HTTP error! Status: ${response.status}`); | |
} | |
return response.json() as TResponse; | |
} | |
export default fetchAPI; | |