32 lines
575 B
JavaScript
32 lines
575 B
JavaScript
// utils/executeRequest.js
|
|
export async function executeRequest(request) {
|
|
// URL string → GET
|
|
if (typeof request === 'string') {
|
|
const res = await fetch(request)
|
|
return res.json()
|
|
}
|
|
|
|
// Request object
|
|
const {
|
|
url,
|
|
method = 'GET',
|
|
data = null,
|
|
headers = {}
|
|
} = request
|
|
|
|
const options = {
|
|
method,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...headers
|
|
}
|
|
}
|
|
|
|
if (method !== 'GET' && data !== null) {
|
|
options.body = JSON.stringify(data)
|
|
}
|
|
|
|
const res = await fetch(url, options)
|
|
return res.json()
|
|
}
|