1
0
Fork 0
mirror of synced 2024-09-13 07:53:31 +12:00
budibase/packages/builder/src/builderStore/api.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

import { store } from "./index"
import { get as svelteGet } from "svelte/store"
import { CookieUtils, Constants } from "@budibase/frontend-core"
2021-06-16 06:39:40 +12:00
const apiCall =
method =>
async (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,
headers,
})
if (resp.status === 403) {
2022-01-14 06:24:52 +13:00
if (url.includes("/api/templates")) {
return { json: () => [] }
}
CookieUtils.removeCookie(Constants.Cookies.Auth)
// reload after removing cookie, go to login
if (!url.includes("self") && !url.includes("login")) {
location.reload()
}
2021-06-16 06:39:40 +12:00
}
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
}