|
|
|
@ -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
|
|
|
|
|