Invalidate cached API responses when bad response statuses are recevied and invalidate entire API cache when switching role

This commit is contained in:
Andrew Kingston 2022-06-09 15:33:41 +01:00
parent 10258e1df8
commit 13c3390da5
3 changed files with 14 additions and 2 deletions

View File

@ -34,7 +34,11 @@ export const API = createAPIClient({
// Or we could check error.status and redirect to login on a 403 etc.
onError: error => {
const { status, method, url, message, handled } = error || {}
const ignoreErrorUrls = ["bbtel", "/api/global/self"]
const ignoreErrorUrls = [
"bbtel",
"/api/global/self",
"/api/tables/ta_users",
]
// Log any errors that we haven't manually handled
if (!handled) {

View File

@ -3,6 +3,7 @@ import { createLocalStorageStore } from "@budibase/frontend-core"
import { appStore } from "./app"
import { initialise } from "./initialise"
import { authStore } from "./auth"
import { API } from "../api"
const initialState = {
enabled: false,
@ -41,6 +42,7 @@ const createDevToolStore = () => {
...state,
role: role === "self" ? null : role,
}))
API.invalidateCache()
await authStore.actions.fetchUser()
await initialise()
}

View File

@ -57,6 +57,7 @@ export const createAPIClient = config => {
...defaultAPIClientConfig,
...config,
}
let cache = {}
// Generates an error object from an API response
const makeErrorFromResponse = async (response, method) => {
@ -139,6 +140,7 @@ export const createAPIClient = config => {
credentials: "same-origin",
})
} catch (error) {
delete cache[url]
throw makeError("Failed to send request", { url, method })
}
@ -151,9 +153,11 @@ export const createAPIClient = config => {
return await response.json()
}
} catch (error) {
delete cache[url]
return null
}
} else {
delete cache[url]
throw await makeErrorFromResponse(response, method)
}
}
@ -161,7 +165,6 @@ export const createAPIClient = config => {
// Performs an API call to the server and caches the response.
// Future invocation for this URL will return the cached result instead of
// hitting the server again.
let cache = {}
const makeCachedApiCall = async params => {
const identifier = params.url
if (!identifier) {
@ -206,6 +209,9 @@ export const createAPIClient = config => {
error: message => {
throw makeError(message)
},
invalidateCache: () => {
cache = {}
},
}
// Attach all endpoints