Invalidate cached API responses when bad response statuses are recevied and invalidate entire API cache when switching role
This commit is contained in:
parent
9d26b06958
commit
12961806ff
|
@ -34,7 +34,11 @@ export const API = createAPIClient({
|
||||||
// Or we could check error.status and redirect to login on a 403 etc.
|
// Or we could check error.status and redirect to login on a 403 etc.
|
||||||
onError: error => {
|
onError: error => {
|
||||||
const { status, method, url, message, handled } = 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
|
// Log any errors that we haven't manually handled
|
||||||
if (!handled) {
|
if (!handled) {
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { createLocalStorageStore } from "@budibase/frontend-core"
|
||||||
import { appStore } from "./app"
|
import { appStore } from "./app"
|
||||||
import { initialise } from "./initialise"
|
import { initialise } from "./initialise"
|
||||||
import { authStore } from "./auth"
|
import { authStore } from "./auth"
|
||||||
|
import { API } from "../api"
|
||||||
|
|
||||||
const initialState = {
|
const initialState = {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
@ -41,6 +42,7 @@ const createDevToolStore = () => {
|
||||||
...state,
|
...state,
|
||||||
role: role === "self" ? null : role,
|
role: role === "self" ? null : role,
|
||||||
}))
|
}))
|
||||||
|
API.invalidateCache()
|
||||||
await authStore.actions.fetchUser()
|
await authStore.actions.fetchUser()
|
||||||
await initialise()
|
await initialise()
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ export const createAPIClient = config => {
|
||||||
...defaultAPIClientConfig,
|
...defaultAPIClientConfig,
|
||||||
...config,
|
...config,
|
||||||
}
|
}
|
||||||
|
let cache = {}
|
||||||
|
|
||||||
// Generates an error object from an API response
|
// Generates an error object from an API response
|
||||||
const makeErrorFromResponse = async (response, method) => {
|
const makeErrorFromResponse = async (response, method) => {
|
||||||
|
@ -139,6 +140,7 @@ export const createAPIClient = config => {
|
||||||
credentials: "same-origin",
|
credentials: "same-origin",
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
delete cache[url]
|
||||||
throw makeError("Failed to send request", { url, method })
|
throw makeError("Failed to send request", { url, method })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,9 +153,11 @@ export const createAPIClient = config => {
|
||||||
return await response.json()
|
return await response.json()
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
delete cache[url]
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
delete cache[url]
|
||||||
throw await makeErrorFromResponse(response, method)
|
throw await makeErrorFromResponse(response, method)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -161,7 +165,6 @@ export const createAPIClient = config => {
|
||||||
// Performs an API call to the server and caches the response.
|
// Performs an API call to the server and caches the response.
|
||||||
// Future invocation for this URL will return the cached result instead of
|
// Future invocation for this URL will return the cached result instead of
|
||||||
// hitting the server again.
|
// hitting the server again.
|
||||||
let cache = {}
|
|
||||||
const makeCachedApiCall = async params => {
|
const makeCachedApiCall = async params => {
|
||||||
const identifier = params.url
|
const identifier = params.url
|
||||||
if (!identifier) {
|
if (!identifier) {
|
||||||
|
@ -206,6 +209,9 @@ export const createAPIClient = config => {
|
||||||
error: message => {
|
error: message => {
|
||||||
throw makeError(message)
|
throw makeError(message)
|
||||||
},
|
},
|
||||||
|
invalidateCache: () => {
|
||||||
|
cache = {}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attach all endpoints
|
// Attach all endpoints
|
||||||
|
|
Loading…
Reference in New Issue