budibase/packages/builder/src/builderStore/api.js

36 lines
881 B
JavaScript
Raw Normal View History

import { store } from "./index"
import { get as svelteGet } from "svelte/store"
import { removeCookie, Cookies } from "./cookies"
2021-05-04 12:32:22 +02:00
const apiCall = method => async (
2020-09-17 17:36:39 +02:00
url,
body,
headers = { "Content-Type": "application/json" }
) => {
headers["x-budibase-app-id"] = svelteGet(store).appId
const json = headers["Content-Type"] === "application/json"
const resp = await fetch(url, {
method: method,
body: json ? JSON.stringify(body) : body,
2020-06-18 17:59:31 +02:00
headers,
})
if (resp.status === 403) {
removeCookie(Cookies.Auth)
}
return resp
}
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-19 18:19:30 +02:00
export default {
post: apiCall("POST"),
get: apiCall("GET"),
patch: apiCall("PATCH"),
delete: apiCall("DELETE"),
2021-04-13 21:26:26 +02:00
put: apiCall("PUT"),
2020-06-19 18:19:30 +02:00
}