1
0
Fork 0
mirror of synced 2024-09-18 10:20:11 +12:00
budibase/packages/builder/src/builderStore/api.js
2020-06-18 16:59:31 +01:00

40 lines
1 KiB
JavaScript

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
}
const response = await fetch(url, {
method: method,
body: body && JSON.stringify(body),
headers,
})
return response
}
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")
// 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