2020-06-18 17:59:31 +02:00
|
|
|
const apiCall = (method, instanceId) => async (url, body) => {
|
|
|
|
const headers = {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
"x-user-agent": "Budibase Builder",
|
|
|
|
}
|
|
|
|
if (instanceId) {
|
|
|
|
headers["x-budibase-instanceid"] = instanceId
|
|
|
|
}
|
2020-04-28 15:39:35 +02:00
|
|
|
const response = await fetch(url, {
|
2020-02-03 10:24:25 +01:00
|
|
|
method: method,
|
|
|
|
body: body && JSON.stringify(body),
|
2020-06-18 17:59:31 +02:00
|
|
|
headers,
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
2019-07-28 09:03:11 +02:00
|
|
|
|
2020-05-07 11:53:34 +02:00
|
|
|
return response
|
2020-04-28 15:39:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-03 18:05:36 +02:00
|
|
|
export const post = apiCall("POST")
|
|
|
|
export const get = apiCall("GET")
|
|
|
|
export const patch = apiCall("PATCH")
|
|
|
|
export const del = apiCall("DELETE")
|
|
|
|
export const put = apiCall("PUT")
|
2019-07-28 09:03:11 +02:00
|
|
|
|
2020-06-18 17:59:31 +02:00
|
|
|
// usage: api(instanceId).post(...) ... will supply instance Id in header
|
|
|
|
const api = instanceId => ({
|
|
|
|
post: apiCall("POST", instanceId),
|
|
|
|
get: apiCall("GET", instanceId),
|
|
|
|
patch: apiCall("PATCH", instanceId),
|
|
|
|
delete: apiCall("DELETE", instanceId),
|
|
|
|
put: apiCall("PUT", instanceId),
|
|
|
|
})
|
|
|
|
|
|
|
|
// usage: api.post(...)... will not supply instanceid in header
|
|
|
|
api.post = apiCall("POST")
|
|
|
|
api.get = apiCall("GET")
|
|
|
|
api.patch = apiCall("PATCH")
|
|
|
|
api.delete = apiCall("DELETE")
|
|
|
|
api.put = apiCall("PUT")
|
|
|
|
|
|
|
|
export default api
|