2022-01-25 20:22:43 +01:00
|
|
|
import { createAPIClient } from "@budibase/frontend-core"
|
2022-06-30 20:31:06 +02:00
|
|
|
import { authStore } from "../stores/auth.js"
|
2023-06-08 16:12:50 +02:00
|
|
|
import { notificationStore, devToolsEnabled, devToolsStore } from "../stores/"
|
2021-11-26 14:25:02 +01:00
|
|
|
import { get } from "svelte/store"
|
2021-07-23 16:56:18 +02:00
|
|
|
|
2022-01-25 20:22:43 +01:00
|
|
|
export const API = createAPIClient({
|
2022-01-26 18:45:35 +01:00
|
|
|
// Enable caching of cacheable endpoints to speed things up,
|
|
|
|
enableCaching: true,
|
2020-11-12 10:07:09 +01:00
|
|
|
|
2022-01-25 20:22:43 +01:00
|
|
|
// Attach client specific headers
|
|
|
|
attachHeaders: headers => {
|
|
|
|
// Attach app ID header
|
|
|
|
headers["x-budibase-app-id"] = window["##BUDIBASE_APP_ID##"]
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2022-01-25 20:22:43 +01:00
|
|
|
// Attach client header if not inside the builder preview
|
|
|
|
if (!window["##BUDIBASE_IN_BUILDER##"]) {
|
|
|
|
headers["x-budibase-type"] = "client"
|
|
|
|
}
|
2022-01-25 23:54:50 +01:00
|
|
|
|
2022-01-31 10:40:33 +01:00
|
|
|
// Add csrf token if authenticated
|
2022-01-25 23:54:50 +01:00
|
|
|
const auth = get(authStore)
|
2022-01-31 10:40:33 +01:00
|
|
|
if (auth?.csrfToken) {
|
2022-01-25 23:54:50 +01:00
|
|
|
headers["x-csrf-token"] = auth.csrfToken
|
2020-11-19 21:04:30 +01:00
|
|
|
}
|
2022-02-24 15:03:29 +01:00
|
|
|
|
|
|
|
// Add role header
|
2023-06-08 16:12:50 +02:00
|
|
|
const $devToolsStore = get(devToolsStore)
|
|
|
|
const $devToolsEnabled = get(devToolsEnabled)
|
|
|
|
if ($devToolsEnabled && $devToolsStore.role) {
|
|
|
|
headers["x-budibase-role"] = $devToolsStore.role
|
2020-11-11 13:25:50 +01:00
|
|
|
}
|
2022-01-25 20:22:43 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
// Show an error notification for all API failures.
|
2023-10-17 11:10:51 +02:00
|
|
|
// We could also log these to Posthog.
|
2022-01-25 20:22:43 +01:00
|
|
|
// Or we could check error.status and redirect to login on a 403 etc.
|
|
|
|
onError: error => {
|
2023-06-19 19:21:14 +02:00
|
|
|
const { status, method, url, message, handled, suppressErrors } =
|
|
|
|
error || {}
|
2022-06-09 16:33:41 +02:00
|
|
|
const ignoreErrorUrls = [
|
|
|
|
"bbtel",
|
|
|
|
"/api/global/self",
|
|
|
|
"/api/tables/ta_users",
|
|
|
|
]
|
2020-11-11 13:25:50 +01:00
|
|
|
|
2022-01-25 20:22:43 +01:00
|
|
|
// Log any errors that we haven't manually handled
|
|
|
|
if (!handled) {
|
|
|
|
console.error("Unhandled error from API client", error)
|
|
|
|
return
|
|
|
|
}
|
2020-11-12 10:07:09 +01:00
|
|
|
|
2022-01-25 20:22:43 +01:00
|
|
|
// Notify all errors
|
2023-06-19 19:21:14 +02:00
|
|
|
if (message && !suppressErrors) {
|
2022-01-25 20:22:43 +01:00
|
|
|
// Don't notify if the URL contains the word analytics as it may be
|
|
|
|
// blocked by browser extensions
|
2022-04-06 09:16:24 +02:00
|
|
|
let ignore = false
|
|
|
|
for (let ignoreUrl of ignoreErrorUrls) {
|
|
|
|
if (url?.includes(ignoreUrl)) {
|
|
|
|
ignore = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ignore) {
|
2022-07-20 11:46:24 +02:00
|
|
|
const validationErrors = error?.json?.validationErrors
|
|
|
|
if (validationErrors) {
|
|
|
|
for (let field in validationErrors) {
|
2022-07-20 11:59:27 +02:00
|
|
|
notificationStore.actions.error(
|
|
|
|
`${field} ${validationErrors[field]}`
|
|
|
|
)
|
2022-07-20 11:46:24 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
notificationStore.actions.error(message)
|
|
|
|
}
|
2022-01-25 20:22:43 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 10:07:09 +01:00
|
|
|
|
2022-01-25 20:22:43 +01:00
|
|
|
// Log all errors to console
|
|
|
|
console.warn(`[Client] HTTP ${status} on ${method}:${url}\n\t${message}`)
|
|
|
|
},
|
2023-12-19 12:46:27 +01:00
|
|
|
onMigrationDetected: _appId => {
|
2023-12-19 12:27:26 +01:00
|
|
|
if (!window.MIGRATING_APP) {
|
|
|
|
// We will force a reload, that will display the updating screen until the migration is running
|
|
|
|
window.location.reload()
|
|
|
|
}
|
2023-12-19 12:24:10 +01:00
|
|
|
},
|
2022-01-25 20:22:43 +01:00
|
|
|
})
|