mirror of
https://github.com/tooot-app/app
synced 2025-04-13 18:01:58 +02:00
22 lines
591 B
JavaScript
22 lines
591 B
JavaScript
export async function client(endpoint, { body, ...customConfig } = {}) {
|
|
let data
|
|
try {
|
|
const response = await window.fetch(endpoint, config)
|
|
data = await response.json()
|
|
if (response.ok) {
|
|
return data
|
|
}
|
|
throw new Error(response.statusText)
|
|
} catch (err) {
|
|
return Promise.reject(err.message ? err.message : data)
|
|
}
|
|
}
|
|
|
|
client.get = function (endpoint, customConfig = {}) {
|
|
return client(endpoint, { ...customConfig, method: 'GET' })
|
|
}
|
|
|
|
client.post = function (endpoint, body, customConfig = {}) {
|
|
return client(endpoint, { ...customConfig, body })
|
|
}
|