budibase/packages/client/src/api/api.js

95 lines
2.6 KiB
JavaScript
Raw Normal View History

/**
* API cache for cached request responses.
*/
import { notificationStore } from "../store"
let cache = {}
/**
* Handler for API errors.
*/
2021-05-04 12:32:22 +02:00
const handleError = error => {
2020-11-11 13:25:50 +01:00
return { error }
}
/**
* Performs an API call to the server.
* App ID header is always correctly set.
*/
const makeApiCall = async ({ method, url, body, json = true }) => {
2020-11-11 13:25:50 +01:00
try {
const requestBody = json ? JSON.stringify(body) : body
const inBuilder = window["##BUDIBASE_IN_BUILDER##"]
const headers = {
Accept: "application/json",
"x-budibase-app-id": window["##BUDIBASE_APP_ID##"],
...(json && { "Content-Type": "application/json" }),
...(!inBuilder && { "x-budibase-type": "client" }),
}
const response = await fetch(url, {
method,
headers,
body: requestBody,
2020-11-11 13:25:50 +01:00
credentials: "same-origin",
})
switch (response.status) {
case 200:
return response.json()
case 401:
notificationStore.danger("Invalid credentials")
return handleError(`Invalid credentials`)
2020-11-11 13:25:50 +01:00
case 404:
notificationStore.danger("Not found")
2020-11-11 13:25:50 +01:00
return handleError(`${url}: Not Found`)
case 400:
return handleError(`${url}: Bad Request`)
case 403:
notificationStore.danger(
"Your session has expired, or you don't have permission to access that data"
)
2020-11-11 13:25:50 +01:00
return handleError(`${url}: Forbidden`)
default:
if (response.status >= 200 && response.status < 400) {
return response.json()
}
return handleError(`${url} - ${response.statusText}`)
}
} catch (error) {
return handleError(error)
}
}
/**
* 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.
*/
2021-05-04 12:32:22 +02:00
const makeCachedApiCall = async params => {
const identifier = params.url
if (!identifier) {
return null
}
if (!cache[identifier]) {
cache[identifier] = makeApiCall(params)
cache[identifier] = await cache[identifier]
}
return await cache[identifier]
}
/**
* Constructs an API call function for a particular HTTP method.
*/
2021-05-04 12:32:22 +02:00
const requestApiCall = method => async params => {
const { url, cache = false } = params
const fixedUrl = `/${url}`.replace("//", "/")
const enrichedParams = { ...params, method, url: fixedUrl }
return await (cache ? makeCachedApiCall : makeApiCall)(enrichedParams)
}
2020-11-11 13:25:50 +01:00
export default {
post: requestApiCall("POST"),
get: requestApiCall("GET"),
patch: requestApiCall("PATCH"),
del: requestApiCall("DELETE"),
2020-11-11 13:25:50 +01:00
error: handleError,
}