2022-01-20 10:40:53 +01:00
|
|
|
import { ApiVersion } from "../constants"
|
|
|
|
import { buildAnalyticsEndpoints } from "./analytics"
|
|
|
|
import { buildAppEndpoints } from "./app"
|
|
|
|
import { buildAttachmentEndpoints } from "./attachments"
|
|
|
|
import { buildAuthEndpoints } from "./auth"
|
|
|
|
import { buildAutomationEndpoints } from "./automations"
|
2022-01-24 15:32:27 +01:00
|
|
|
import { buildConfigEndpoints } from "./configs"
|
2022-01-24 13:37:22 +01:00
|
|
|
import { buildDatasourceEndpoints } from "./datasources"
|
|
|
|
import { buildFlagEndpoints } from "./flags"
|
2022-01-21 16:09:27 +01:00
|
|
|
import { buildHostingEndpoints } from "./hosting"
|
2022-01-24 17:50:33 +01:00
|
|
|
import { buildLayoutEndpoints } from "./layouts"
|
2022-01-24 15:32:27 +01:00
|
|
|
import { buildOtherEndpoints } from "./other"
|
2022-01-24 13:37:22 +01:00
|
|
|
import { buildPermissionsEndpoints } from "./permissions"
|
2022-01-20 10:40:53 +01:00
|
|
|
import { buildQueryEndpoints } from "./queries"
|
|
|
|
import { buildRelationshipEndpoints } from "./relationships"
|
2022-01-24 13:37:22 +01:00
|
|
|
import { buildRoleEndpoints } from "./roles"
|
2022-01-20 10:40:53 +01:00
|
|
|
import { buildRouteEndpoints } from "./routes"
|
|
|
|
import { buildRowEndpoints } from "./rows"
|
2022-01-20 19:42:30 +01:00
|
|
|
import { buildScreenEndpoints } from "./screens"
|
2022-01-20 10:40:53 +01:00
|
|
|
import { buildTableEndpoints } from "./tables"
|
2022-01-24 13:37:22 +01:00
|
|
|
import { buildTemplateEndpoints } from "./templates"
|
2022-01-24 15:32:27 +01:00
|
|
|
import { buildUserEndpoints } from "./user"
|
2022-02-10 19:34:55 +01:00
|
|
|
import { buildSelfEndpoints } from "./self"
|
2022-01-20 10:40:53 +01:00
|
|
|
import { buildViewEndpoints } from "./views"
|
2022-03-09 22:16:22 +01:00
|
|
|
import { buildLicensingEndpoints } from "./licensing"
|
2022-01-20 10:40:53 +01:00
|
|
|
|
|
|
|
const defaultAPIClientConfig = {
|
2022-01-26 18:44:14 +01:00
|
|
|
/**
|
|
|
|
* Certain definitions can't change at runtime for client apps, such as the
|
|
|
|
* schema of tables. The endpoints that are cacheable can be cached by passing
|
|
|
|
* in this flag. It's disabled by default to avoid bugs with stale data.
|
|
|
|
*/
|
|
|
|
enableCaching: false,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A function can be passed in to attach headers to all outgoing requests.
|
|
|
|
* This function is passed in the headers object, which should be directly
|
|
|
|
* mutated. No return value is required.
|
|
|
|
*/
|
2022-01-20 10:40:53 +01:00
|
|
|
attachHeaders: null,
|
2022-01-26 18:44:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A function can be passed in which will be invoked any time an API error
|
|
|
|
* occurs. An error is defined as a status code >= 400. This function is
|
|
|
|
* invoked before the actual JS error is thrown up the stack.
|
|
|
|
*/
|
2022-01-20 10:40:53 +01:00
|
|
|
onError: null,
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs an API client with the provided configuration.
|
|
|
|
* @param config the API client configuration
|
|
|
|
* @return {object} the API client
|
|
|
|
*/
|
|
|
|
export const createAPIClient = config => {
|
|
|
|
config = {
|
|
|
|
...defaultAPIClientConfig,
|
|
|
|
...config,
|
|
|
|
}
|
2022-06-09 16:33:41 +02:00
|
|
|
let cache = {}
|
2022-01-20 10:40:53 +01:00
|
|
|
|
2022-01-20 14:55:07 +01:00
|
|
|
// Generates an error object from an API response
|
2022-01-21 10:10:59 +01:00
|
|
|
const makeErrorFromResponse = async (response, method) => {
|
2022-01-20 10:40:53 +01:00
|
|
|
// Try to read a message from the error
|
2022-01-20 14:37:06 +01:00
|
|
|
let message = response.statusText
|
2022-01-21 16:09:27 +01:00
|
|
|
let json = null
|
2022-01-20 10:40:53 +01:00
|
|
|
try {
|
2022-01-25 09:25:16 +01:00
|
|
|
json = await response.json()
|
2022-01-20 14:37:06 +01:00
|
|
|
if (json?.message) {
|
|
|
|
message = json.message
|
|
|
|
} else if (json?.error) {
|
2022-01-20 10:40:53 +01:00
|
|
|
message = json.error
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
message,
|
2022-01-21 16:09:27 +01:00
|
|
|
json,
|
2022-01-20 10:40:53 +01:00
|
|
|
status: response.status,
|
2022-01-20 15:27:43 +01:00
|
|
|
url: response.url,
|
2022-01-21 10:10:59 +01:00
|
|
|
method,
|
|
|
|
handled: true,
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:55:07 +01:00
|
|
|
// Generates an error object from a string
|
2022-01-24 17:38:36 +01:00
|
|
|
const makeError = (message, request) => {
|
2022-01-20 10:40:53 +01:00
|
|
|
return {
|
|
|
|
message,
|
2022-01-21 16:09:27 +01:00
|
|
|
json: null,
|
2022-01-20 10:40:53 +01:00
|
|
|
status: 400,
|
2022-01-24 17:38:36 +01:00
|
|
|
url: request?.url,
|
|
|
|
method: request?.method,
|
2022-01-21 10:10:59 +01:00
|
|
|
handled: true,
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:55:07 +01:00
|
|
|
// Performs an API call to the server.
|
2022-01-20 10:40:53 +01:00
|
|
|
const makeApiCall = async ({
|
|
|
|
method,
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
json = true,
|
|
|
|
external = false,
|
2022-01-25 09:10:49 +01:00
|
|
|
parseResponse,
|
2022-01-20 10:40:53 +01:00
|
|
|
}) => {
|
2022-01-24 11:44:37 +01:00
|
|
|
// Ensure we don't do JSON processing if sending a GET request
|
|
|
|
json = json && method !== "GET"
|
|
|
|
|
2022-01-20 10:40:53 +01:00
|
|
|
// Build headers
|
|
|
|
let headers = { Accept: "application/json" }
|
|
|
|
if (!external) {
|
|
|
|
headers["x-budibase-api-version"] = ApiVersion
|
|
|
|
}
|
|
|
|
if (json) {
|
|
|
|
headers["Content-Type"] = "application/json"
|
|
|
|
}
|
|
|
|
if (config?.attachHeaders) {
|
|
|
|
config.attachHeaders(headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build request body
|
|
|
|
let requestBody = body
|
|
|
|
if (json) {
|
|
|
|
try {
|
|
|
|
requestBody = JSON.stringify(body)
|
|
|
|
} catch (error) {
|
2022-01-24 17:38:36 +01:00
|
|
|
throw makeError("Invalid JSON body", { url, method })
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make request
|
|
|
|
let response
|
|
|
|
try {
|
|
|
|
response = await fetch(url, {
|
|
|
|
method,
|
|
|
|
headers,
|
|
|
|
body: requestBody,
|
|
|
|
credentials: "same-origin",
|
|
|
|
})
|
|
|
|
} catch (error) {
|
2022-06-09 16:33:41 +02:00
|
|
|
delete cache[url]
|
2022-01-24 17:38:36 +01:00
|
|
|
throw makeError("Failed to send request", { url, method })
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle response
|
|
|
|
if (response.status >= 200 && response.status < 400) {
|
|
|
|
try {
|
2022-01-25 09:10:49 +01:00
|
|
|
if (parseResponse) {
|
|
|
|
return await parseResponse(response)
|
2022-01-24 11:44:37 +01:00
|
|
|
} else {
|
|
|
|
return await response.json()
|
|
|
|
}
|
2022-01-20 10:40:53 +01:00
|
|
|
} catch (error) {
|
2022-06-09 16:33:41 +02:00
|
|
|
delete cache[url]
|
2022-01-20 10:40:53 +01:00
|
|
|
return null
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-09 16:33:41 +02:00
|
|
|
delete cache[url]
|
2022-01-21 10:10:59 +01:00
|
|
|
throw await makeErrorFromResponse(response, method)
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 14:55:07 +01:00
|
|
|
// 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.
|
2022-01-20 10:40:53 +01: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]
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:10:59 +01:00
|
|
|
// Constructs an API call function for a particular HTTP method
|
2022-01-20 10:40:53 +01:00
|
|
|
const requestApiCall = method => async params => {
|
2022-01-21 10:10:59 +01:00
|
|
|
try {
|
|
|
|
let { url, cache = false, external = false } = params
|
|
|
|
if (!external) {
|
|
|
|
url = `/${url}`.replace("//", "/")
|
|
|
|
}
|
2022-01-26 18:44:14 +01:00
|
|
|
|
|
|
|
// Cache the request if possible and desired
|
|
|
|
const cacheRequest = cache && config?.enableCaching
|
|
|
|
const handler = cacheRequest ? makeCachedApiCall : makeApiCall
|
|
|
|
|
2022-01-21 10:10:59 +01:00
|
|
|
const enrichedParams = { ...params, method, url }
|
2022-01-26 18:44:14 +01:00
|
|
|
return await handler(enrichedParams)
|
2022-01-21 10:10:59 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (config?.onError) {
|
|
|
|
config.onError(error)
|
|
|
|
}
|
|
|
|
throw error
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the underlying core API methods
|
|
|
|
let API = {
|
|
|
|
post: requestApiCall("POST"),
|
|
|
|
get: requestApiCall("GET"),
|
|
|
|
patch: requestApiCall("PATCH"),
|
|
|
|
delete: requestApiCall("DELETE"),
|
2022-01-20 15:27:43 +01:00
|
|
|
put: requestApiCall("PUT"),
|
2022-01-20 11:16:13 +01:00
|
|
|
error: message => {
|
|
|
|
throw makeError(message)
|
|
|
|
},
|
2022-06-09 16:33:41 +02:00
|
|
|
invalidateCache: () => {
|
|
|
|
cache = {}
|
|
|
|
},
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
|
2022-01-20 14:55:07 +01:00
|
|
|
// Attach all endpoints
|
2022-01-25 20:22:43 +01:00
|
|
|
return {
|
2022-01-20 10:40:53 +01:00
|
|
|
...API,
|
|
|
|
...buildAnalyticsEndpoints(API),
|
|
|
|
...buildAppEndpoints(API),
|
|
|
|
...buildAttachmentEndpoints(API),
|
|
|
|
...buildAuthEndpoints(API),
|
|
|
|
...buildAutomationEndpoints(API),
|
2022-01-24 15:32:27 +01:00
|
|
|
...buildConfigEndpoints(API),
|
2022-01-24 13:37:22 +01:00
|
|
|
...buildDatasourceEndpoints(API),
|
|
|
|
...buildFlagEndpoints(API),
|
2022-01-21 16:09:27 +01:00
|
|
|
...buildHostingEndpoints(API),
|
2022-01-24 17:50:33 +01:00
|
|
|
...buildLayoutEndpoints(API),
|
2022-01-24 15:32:27 +01:00
|
|
|
...buildOtherEndpoints(API),
|
2022-01-24 13:37:22 +01:00
|
|
|
...buildPermissionsEndpoints(API),
|
2022-01-20 10:40:53 +01:00
|
|
|
...buildQueryEndpoints(API),
|
|
|
|
...buildRelationshipEndpoints(API),
|
2022-01-24 13:37:22 +01:00
|
|
|
...buildRoleEndpoints(API),
|
2022-01-20 10:40:53 +01:00
|
|
|
...buildRouteEndpoints(API),
|
|
|
|
...buildRowEndpoints(API),
|
2022-01-20 19:42:30 +01:00
|
|
|
...buildScreenEndpoints(API),
|
2022-01-20 10:40:53 +01:00
|
|
|
...buildTableEndpoints(API),
|
2022-01-24 13:37:22 +01:00
|
|
|
...buildTemplateEndpoints(API),
|
2022-01-24 15:32:27 +01:00
|
|
|
...buildUserEndpoints(API),
|
2022-01-20 10:40:53 +01:00
|
|
|
...buildViewEndpoints(API),
|
2022-02-10 19:34:55 +01:00
|
|
|
...buildSelfEndpoints(API),
|
2022-03-09 22:16:22 +01:00
|
|
|
...buildLicensingEndpoints(API),
|
2022-01-20 10:40:53 +01:00
|
|
|
}
|
|
|
|
}
|