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

45 lines
1.1 KiB
JavaScript
Raw Normal View History

import {
createAPIClient,
CookieUtils,
Constants,
} from "@budibase/frontend-core"
import { store } from "./index"
import { get } from "svelte/store"
import { notifications } from "@budibase/bbui"
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 } = error
// Log all API errors to Sentry
// analytics.captureException(error)
// Show a notification for any errors
if (message) {
notifications.error(`Error fetching ${url}: ${message}`)
}
// Logout on 403's
if (status === 403) {
// Don't do anything if fetching templates.
// TODO: clarify why this is here
2022-01-14 06:24:52 +13:00
if (url.includes("/api/templates")) {
return
2022-01-14 06:24:52 +13: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-16 06:39:40 +12:00
}
},
})