2020-11-03 14:45:49 +01:00
|
|
|
import { store } from "./index"
|
|
|
|
import { get as svelteGet } from "svelte/store"
|
2021-04-14 16:43:34 +02:00
|
|
|
import { removeCookie, Cookies } from "./cookies"
|
2020-11-03 14:45:49 +01:00
|
|
|
|
2021-06-15 20:39:40 +02: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) {
|
|
|
|
removeCookie(Cookies.Auth)
|
2021-06-22 14:05:15 +02:00
|
|
|
// reload after removing cookie, go to login
|
|
|
|
location.reload()
|
2021-06-15 20:39:40 +02:00
|
|
|
}
|
|
|
|
return resp
|
2021-04-14 16:43:34 +02:00
|
|
|
}
|
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-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
|
|
|
}
|