1
0
Fork 0
mirror of synced 2024-09-13 07:53:31 +12:00
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 22:32:22 +12:00
const apiCall = method => async (
2020-09-18 03:36:39 +12: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-19 03:59:31 +12: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 19:03:11 +12:00
2020-06-20 04:19:30 +12:00
export default {
post: apiCall("POST"),
get: apiCall("GET"),
patch: apiCall("PATCH"),
delete: apiCall("DELETE"),
2021-04-14 07:26:26 +12:00
put: apiCall("PUT"),
2020-06-20 04:19:30 +12:00
}