1
0
Fork 0
mirror of synced 2024-09-21 20:01:32 +12:00
budibase/packages/builder/src/api.js

60 lines
1.5 KiB
JavaScript
Raw Normal View History

import {
createAPIClient,
CookieUtils,
Constants,
} from "@budibase/frontend-core"
import { store } from "./builderStore"
import { get } from "svelte/store"
2023-12-16 01:27:16 +13:00
import { auth, navigation } from "./stores/portal"
export const API = createAPIClient({
attachHeaders: headers => {
// Attach app ID header from store
2023-04-25 19:44:57 +12:00
let appId = get(store).appId
if (appId) {
headers["x-budibase-app-id"] = appId
}
// Add csrf token if authenticated
const user = get(auth).user
if (user?.csrfToken) {
headers["x-csrf-token"] = user.csrfToken
}
},
onError: error => {
const { url, message, status, method, handled } = 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.warn(`[Builder] HTTP ${status} on ${method}:${url}\n\t${message}`)
// Logout on 403's
if (status === 403) {
// Remove cookies
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
}
},
2023-12-13 04:57:41 +13:00
onMigrationDetected: appId => {
2023-12-14 04:15:11 +13:00
const updatingUrl = `/builder/app/${appId}/updating`
2023-12-13 06:38:29 +13:00
if (window.location.pathname === updatingUrl) {
return
}
2023-12-16 01:27:16 +13:00
navigation.actions.goTo(
`${updatingUrl}?returnUrl=${encodeURIComponent(window.location)}`
)
2023-12-13 04:57:41 +13:00
},
})