Skip to content
Snippets Groups Projects
Commit 8d113cac authored by Konrad Mohrfeldt's avatar Konrad Mohrfeldt :koala:
Browse files

feat: allow retrieve API store method to use cached results

The cached result is usually sufficient and we don’t necessarily need
fresh data from the API.
parent f9056ea7
No related branches found
No related tags found
No related merge requests found
...@@ -132,7 +132,15 @@ export function APIListUnpaginated<T extends APIObject>(api: ExtendableAPI<T>) { ...@@ -132,7 +132,15 @@ export function APIListUnpaginated<T extends APIObject>(api: ExtendableAPI<T>) {
return { list } return { list }
} }
export function APIRetrieve<T extends APIObject>(api: ExtendableAPI<T>) { export function APIRetrieve<T extends APIObject>(api: ExtendableAPI<T>) {
async function retrieve(id: ID, requestInit?: RequestInit): Promise<T | null> { async function retrieve(
id: ID,
requestInit?: RequestInit,
options?: { useCached?: boolean },
): Promise<T | null> {
if (options?.useCached && api.itemMap.value.has(id)) {
return api.itemMap.value.get(id) as T
}
const res = await fetch(api.createRequest(api.endpoint(id.toString()), requestInit)) const res = await fetch(api.createRequest(api.endpoint(id.toString()), requestInit))
if (res.status === 404) { if (res.status === 404) {
api.itemMap.value.delete(id) api.itemMap.value.delete(id)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment