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-05-03 09:31:09 +02:00
|
|
|
const apiCall = (method) => async (
|
2020-09-17 17:36:39 +02:00
|
|
|
url,
|
|
|
|
body,
|
2020-09-15 17:22:13 +02:00
|
|
|
headers = { "Content-Type": "application/json" }
|
|
|
|
) => {
|
2020-11-03 14:45:49 +01:00
|
|
|
headers["x-budibase-app-id"] = svelteGet(store).appId
|
2021-03-15 13:10:21 +01:00
|
|
|
const json = headers["Content-Type"] === "application/json"
|
2021-04-14 16:43:34 +02:00
|
|
|
const resp = await fetch(url, {
|
2020-02-03 10:24:25 +01:00
|
|
|
method: method,
|
2021-03-15 13:10:21 +01:00
|
|
|
body: json ? JSON.stringify(body) : body,
|
2020-06-18 17:59:31 +02:00
|
|
|
headers,
|
2020-02-03 10:24:25 +01:00
|
|
|
})
|
2021-04-14 16:43:34 +02:00
|
|
|
if (resp.status === 403) {
|
|
|
|
removeCookie(Cookies.Auth)
|
|
|
|
}
|
|
|
|
return resp
|
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
|
|
|
}
|