| 1 | export default async function fetchAPI( |
| 2 | url: string, |
| 3 | options: RequestInit & { data?: any } = {} |
| 4 | ) { |
| 5 | try { |
| 6 | const res = await fetch(`/api${url}`, { |
| 7 | ...options, |
| 8 | headers: { |
| 9 | ...options.headers, |
| 10 | Accept: 'application/json', |
| 11 | 'Content-Type': 'application/json; charset=utf-8', |
| 12 | }, |
| 13 | ...(options.data ? { body: JSON.stringify(options.data) } : {}), |
| 14 | }) |
| 15 | const data = await res.json() |
| 16 | return data |
| 17 | } catch (e: any) { |
| 18 | if (e.message === 'cancelled') { |
| 19 | // Cancelled by browser |
| 20 | console.log('Request Cancelled by the Browser', e) |
| 21 | } else { |
| 22 | console.error('Network Error, CORS or timeout.') |
| 23 | } |
| 24 | } |
| 25 | } |