Tidy core API client

This commit is contained in:
Andrew Kingston 2022-01-20 13:55:07 +00:00
parent 9f180ca292
commit 21443b0e53
1 changed files with 8 additions and 16 deletions

View File

@ -28,9 +28,7 @@ export const createAPIClient = config => {
...config, ...config,
} }
/** // Generates an error object from an API response
* Handler for API errors.
*/
const makeErrorFromResponse = async response => { const makeErrorFromResponse = async response => {
// Try to read a message from the error // Try to read a message from the error
let message = response.statusText let message = response.statusText
@ -50,6 +48,7 @@ export const createAPIClient = config => {
} }
} }
// Generates an error object from a string
const makeError = message => { const makeError = message => {
return { return {
message, message,
@ -57,10 +56,7 @@ export const createAPIClient = config => {
} }
} }
/** // Performs an API call to the server.
* Performs an API call to the server.
* App ID header is always correctly set.
*/
const makeApiCall = async ({ const makeApiCall = async ({
method, method,
url, url,
@ -119,11 +115,9 @@ 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 = {} let cache = {}
const makeCachedApiCall = async params => { const makeCachedApiCall = async params => {
const identifier = params.url const identifier = params.url
@ -137,9 +131,7 @@ export const createAPIClient = config => {
return await cache[identifier] return await cache[identifier]
} }
/** // Constructs an API call function for a particular HTTP method.
* Constructs an API call function for a particular HTTP method.
*/
const requestApiCall = method => async params => { const requestApiCall = method => async params => {
let { url, cache = false, external = false } = params let { url, cache = false, external = false } = params
if (!external) { if (!external) {
@ -160,7 +152,7 @@ export const createAPIClient = config => {
}, },
} }
// Attach all other endpoints // Attach all endpoints
API = { API = {
...API, ...API,
...buildAnalyticsEndpoints(API), ...buildAnalyticsEndpoints(API),