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

48 lines
1.2 KiB
JavaScript
Raw Normal View History

import {
createAPIClient,
CookieUtils,
Constants,
} from "@budibase/frontend-core"
import { store } from "./index"
import { get } from "svelte/store"
export const API = createAPIClient({
attachHeaders: headers => {
// Attach app ID header from store
headers["x-budibase-app-id"] = get(store).appId
},
onError: error => {
const { url, message, status, method, handled } = error || {}
// Log all API errors to Sentry
// analytics.captureException(error)
// Log any errors that we haven't manually handled
if (!handled) {
console.error("Unhandled error from API client", error)
return
}
// Log all errors to console
console.error(`HTTP ${status} on ${method}:${url}:\n\t${message}`)
// Logout on 403's
if (status === 403) {
// Don't do anything if fetching templates.
// TODO: clarify why this is here
2022-01-13 18:24:52 +01:00
if (url.includes("/api/templates")) {
return
2022-01-13 18:24:52 +01:00
}
// Remove the auth cookie
CookieUtils.removeCookie(Constants.Cookies.Auth)
// Reload after removing cookie, go to login
if (!url.includes("self") && !url.includes("login")) {
location.reload()
}
2021-06-15 20:39:40 +02:00
}
},
})