✨ feat: implement new API layer with request deduplication, caching, and Sentry integration
This commit is contained in:
@@ -40,13 +40,10 @@ function obj2str(obj) {
|
||||
* @param {string} endpoint
|
||||
* @param {ApiOptions} options
|
||||
* @param {any} body
|
||||
* @param {import("../../../frontend/src/sentry")} sentry
|
||||
* @param {typeof fetch} _fetch
|
||||
* @param {any} sentry
|
||||
* @returns {Promise<ApiResult<any>>}
|
||||
*/
|
||||
function apiRequest(endpoint, options, body, sentry, _fetch) {
|
||||
// TODO cache only for GET
|
||||
|
||||
function apiRequest(endpoint, options, body, sentry) {
|
||||
// first check cache if on client
|
||||
const cacheKey = obj2str({ endpoint: endpoint, options: options })
|
||||
|
||||
@@ -64,7 +61,13 @@ function apiRequest(endpoint, options, body, sentry, _fetch) {
|
||||
|
||||
let query = "&count=1"
|
||||
if (options?.filter) query += "&filter=" + encodeURIComponent(JSON.stringify(options.filter))
|
||||
if (options?.sort) query += "&sort=" + options.sort + "&sort=_id"
|
||||
if (options?.sort) {
|
||||
query += "&sort=" + options.sort
|
||||
// Only append _id as secondary sort if not already sorting by _id
|
||||
if (options.sort !== "_id" && options.sort !== "-_id") {
|
||||
query += "&sort=_id"
|
||||
}
|
||||
}
|
||||
if (options?.limit) query += "&limit=" + options.limit
|
||||
if (options?.offset) query += "&offset=" + options.offset
|
||||
if (options?.projection) query += "&projection=" + options.projection
|
||||
@@ -94,12 +97,12 @@ function apiRequest(endpoint, options, body, sentry, _fetch) {
|
||||
// client
|
||||
let url = (endpoint.startsWith("/") ? "" : apiClientBaseURL) + endpoint + (query ? "?" + query : "")
|
||||
|
||||
const span = sentry?.currentTransaction()?.startChild({
|
||||
const span = sentry?.startChildSpan?.({
|
||||
op: "fetch",
|
||||
description: method + " " + url,
|
||||
data: Object.assign({}, options, { url }),
|
||||
name: method + " " + url,
|
||||
attributes: Object.assign({}, options, { url }),
|
||||
})
|
||||
const trace_id = span?.toTraceparent()
|
||||
const trace_id = span ? sentry?.spanToTraceHeader?.(span) : undefined
|
||||
if (trace_id) {
|
||||
headers["sentry-trace"] = trace_id
|
||||
}
|
||||
@@ -108,23 +111,42 @@ function apiRequest(endpoint, options, body, sentry, _fetch) {
|
||||
const requestOptions = {
|
||||
method,
|
||||
mode: "cors",
|
||||
credentials: "include",
|
||||
headers,
|
||||
}
|
||||
|
||||
// Add AbortSignal if provided
|
||||
if (options?.signal) {
|
||||
requestOptions.signal = options.signal
|
||||
}
|
||||
|
||||
if (method === "POST" || method === "PUT") {
|
||||
requestOptions.body = JSON.stringify(body)
|
||||
}
|
||||
|
||||
const response = _fetch(url, requestOptions).then((response) => {
|
||||
return response?.json().then((json) => {
|
||||
if (response?.status < 200 || response?.status >= 400) {
|
||||
return Promise.reject({ response, data: json })
|
||||
}
|
||||
return Promise.resolve({ data: json || null, count: response.headers?.get("x-results-count") || 0 })
|
||||
const response = fetch(url, requestOptions)
|
||||
.then((response) => {
|
||||
return response?.json().then((json) => {
|
||||
span?.end()
|
||||
if (response?.status < 200 || response?.status >= 400) {
|
||||
return Promise.reject({ response, data: json })
|
||||
}
|
||||
return Promise.resolve({
|
||||
data: json || null,
|
||||
count: response.headers?.get("x-results-count") || 0,
|
||||
buildTime: response.headers?.get("x-build-time") || null,
|
||||
})
|
||||
})
|
||||
})
|
||||
.catch((error) => {
|
||||
span?.end()
|
||||
// Re-throw AbortError as-is so it can be handled upstream
|
||||
if (error?.name === "AbortError") {
|
||||
console.log("🚫 Request aborted:", url)
|
||||
throw error
|
||||
}
|
||||
throw error
|
||||
})
|
||||
})
|
||||
|
||||
span?.end()
|
||||
|
||||
// @ts-ignore
|
||||
return response
|
||||
|
||||
Reference in New Issue
Block a user