2022-01-20 19:42:30 +01:00
|
|
|
import {
|
|
|
|
createAPIClient,
|
|
|
|
CookieUtils,
|
|
|
|
Constants,
|
|
|
|
} from "@budibase/frontend-core"
|
2020-11-03 14:45:49 +01:00
|
|
|
import { store } from "./index"
|
2022-01-20 19:42:30 +01:00
|
|
|
import { get } from "svelte/store"
|
|
|
|
import { notifications } from "@budibase/bbui"
|
2020-11-03 14:45:49 +01:00
|
|
|
|
2022-01-20 19:42:30 +01:00
|
|
|
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-13 18:24:52 +01:00
|
|
|
if (url.includes("/api/templates")) {
|
2022-01-20 19:42:30 +01:00
|
|
|
return
|
2022-01-13 18:24:52 +01:00
|
|
|
}
|
2022-01-20 19:42:30 +01:00
|
|
|
|
|
|
|
// Remove the auth cookie
|
2022-01-20 12:19:37 +01:00
|
|
|
CookieUtils.removeCookie(Constants.Cookies.Auth)
|
2022-01-20 19:42:30 +01:00
|
|
|
|
|
|
|
// Reload after removing cookie, go to login
|
2021-10-15 18:52:06 +02:00
|
|
|
if (!url.includes("self") && !url.includes("login")) {
|
2021-06-22 17:54:25 +02:00
|
|
|
location.reload()
|
|
|
|
}
|
2021-06-15 20:39:40 +02:00
|
|
|
}
|
2022-01-20 19:42:30 +01:00
|
|
|
},
|
|
|
|
})
|