|
|
|
|
@@ -20,19 +20,21 @@ import { checkBuildVersion } from "./versionCheck"
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Request deduplication
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
const pendingRequests = new Map<string, Promise<any>>()
|
|
|
|
|
const pendingRequests = new Map<string, Promise<ApiResult<unknown>>>()
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate a deterministic cache key for a request so identical parallel calls
|
|
|
|
|
* collapse into a single network round-trip.
|
|
|
|
|
*/
|
|
|
|
|
const getRequestCacheKey = (endpoint: string, options?: ApiOptions, body?: any): string => {
|
|
|
|
|
const deterministicStringify = (obj: any): string => {
|
|
|
|
|
const getRequestCacheKey = (endpoint: string, options?: ApiOptions, body?: unknown): string => {
|
|
|
|
|
const deterministicStringify = (obj: unknown): string => {
|
|
|
|
|
if (obj === null || obj === undefined) return String(obj)
|
|
|
|
|
if (typeof obj !== "object") return JSON.stringify(obj)
|
|
|
|
|
if (Array.isArray(obj)) return `[${obj.map(deterministicStringify).join(",")}]`
|
|
|
|
|
const keys = Object.keys(obj).sort()
|
|
|
|
|
const pairs = keys.map((key) => `${JSON.stringify(key)}:${deterministicStringify(obj[key])}`)
|
|
|
|
|
const pairs = keys.map(
|
|
|
|
|
(key) => `${JSON.stringify(key)}:${deterministicStringify((obj as Record<string, unknown>)[key])}`
|
|
|
|
|
)
|
|
|
|
|
return `{${pairs.join(",")}}`
|
|
|
|
|
}
|
|
|
|
|
return deterministicStringify({ endpoint, options, body })
|
|
|
|
|
@@ -56,7 +58,7 @@ const getRequestCacheKey = (endpoint: string, options?: ApiOptions, body?: any):
|
|
|
|
|
export const api = async <T>(
|
|
|
|
|
endpoint: string,
|
|
|
|
|
options?: ApiOptions,
|
|
|
|
|
body?: any,
|
|
|
|
|
body?: unknown,
|
|
|
|
|
signal?: AbortSignal
|
|
|
|
|
): Promise<ApiResult<T>> => {
|
|
|
|
|
const _apiBaseOverride = get(apiBaseOverride) || ""
|
|
|
|
|
@@ -72,7 +74,7 @@ export const api = async <T>(
|
|
|
|
|
// Deduplication: skip when caller provides a signal (they want explicit control)
|
|
|
|
|
const cacheKey = getRequestCacheKey(endpoint, options, body)
|
|
|
|
|
if (!signal && pendingRequests.has(cacheKey)) {
|
|
|
|
|
return pendingRequests.get(cacheKey)!
|
|
|
|
|
return pendingRequests.get(cacheKey) as Promise<ApiResult<T>>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requestPromise = (async () => {
|
|
|
|
|
@@ -94,10 +96,10 @@ export const api = async <T>(
|
|
|
|
|
return data as ApiResult<T>
|
|
|
|
|
} catch (err) {
|
|
|
|
|
// Don't send abort errors to Sentry
|
|
|
|
|
if ((err as any)?.name === "AbortError") throw err
|
|
|
|
|
if (err instanceof DOMException && err.name === "AbortError") throw err
|
|
|
|
|
|
|
|
|
|
// Auth: 401 handling placeholder
|
|
|
|
|
// const status = (err as any)?.response?.status
|
|
|
|
|
// const status = (err as { response?: { status?: number } })?.response?.status
|
|
|
|
|
// if (status === 401) { /* refresh token + retry */ }
|
|
|
|
|
|
|
|
|
|
throw err
|
|
|
|
|
@@ -119,7 +121,7 @@ export const api = async <T>(
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// In-memory response cache (for getCachedEntries / getCachedEntry)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
const cache: { [key: string]: { expire: number; data: any } } = {}
|
|
|
|
|
const cache: Record<string, { expire: number; data: unknown }> = {}
|
|
|
|
|
const CACHE_TTL = 1000 * 60 * 60 // 1 hour
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
@@ -132,16 +134,16 @@ type EntryTypeSwitch<T extends string> = T extends "medialib"
|
|
|
|
|
? MedialibEntry
|
|
|
|
|
: T extends "content"
|
|
|
|
|
? ContentEntry
|
|
|
|
|
: Record<string, any>
|
|
|
|
|
: Record<string, unknown>
|
|
|
|
|
|
|
|
|
|
export async function getDBEntries<T extends CollectionNameT>(
|
|
|
|
|
collectionName: T,
|
|
|
|
|
filter?: { [key: string]: any },
|
|
|
|
|
filter?: MongoFilter,
|
|
|
|
|
sort: string = "sort",
|
|
|
|
|
limit?: number,
|
|
|
|
|
offset?: number,
|
|
|
|
|
projection?: string,
|
|
|
|
|
params?: { [key: string]: string }
|
|
|
|
|
params?: Record<string, string>
|
|
|
|
|
): Promise<EntryTypeSwitch<T>[]> {
|
|
|
|
|
const c = await api<EntryTypeSwitch<T>[]>(collectionName, {
|
|
|
|
|
filter,
|
|
|
|
|
@@ -156,16 +158,16 @@ export async function getDBEntries<T extends CollectionNameT>(
|
|
|
|
|
|
|
|
|
|
export async function getCachedEntries<T extends CollectionNameT>(
|
|
|
|
|
collectionName: T,
|
|
|
|
|
filter?: { [key: string]: any },
|
|
|
|
|
filter?: MongoFilter,
|
|
|
|
|
sort: string = "sort",
|
|
|
|
|
limit?: number,
|
|
|
|
|
offset?: number,
|
|
|
|
|
projection?: string,
|
|
|
|
|
params?: { [key: string]: string }
|
|
|
|
|
params?: Record<string, string>
|
|
|
|
|
): Promise<EntryTypeSwitch<T>[]> {
|
|
|
|
|
const filterStr = obj2str({ collectionName, filter, sort, limit, offset, projection, params })
|
|
|
|
|
if (cache[filterStr] && cache[filterStr].expire >= Date.now()) {
|
|
|
|
|
return cache[filterStr].data
|
|
|
|
|
return cache[filterStr].data as EntryTypeSwitch<T>[]
|
|
|
|
|
}
|
|
|
|
|
const entries = await getDBEntries<T>(collectionName, filter, sort, limit, offset, projection, params)
|
|
|
|
|
cache[filterStr] = { expire: Date.now() + CACHE_TTL, data: entries }
|
|
|
|
|
@@ -174,20 +176,20 @@ export async function getCachedEntries<T extends CollectionNameT>(
|
|
|
|
|
|
|
|
|
|
export async function getDBEntry<T extends CollectionNameT>(
|
|
|
|
|
collectionName: T,
|
|
|
|
|
filter: { [key: string]: any },
|
|
|
|
|
filter: MongoFilter,
|
|
|
|
|
projection?: string,
|
|
|
|
|
params?: { [key: string]: string }
|
|
|
|
|
) {
|
|
|
|
|
return (await getDBEntries<T>(collectionName, filter, "_id", 1, null, projection, params))?.[0]
|
|
|
|
|
params?: Record<string, string>
|
|
|
|
|
): Promise<EntryTypeSwitch<T> | undefined> {
|
|
|
|
|
return (await getDBEntries<T>(collectionName, filter, "_id", 1, undefined, projection, params))?.[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getCachedEntry<T extends CollectionNameT>(
|
|
|
|
|
collectionName: T,
|
|
|
|
|
filter: { [key: string]: any },
|
|
|
|
|
filter: MongoFilter,
|
|
|
|
|
projection?: string,
|
|
|
|
|
params?: { [key: string]: string }
|
|
|
|
|
) {
|
|
|
|
|
return (await getCachedEntries<T>(collectionName, filter, "_id", 1, null, projection, params))?.[0]
|
|
|
|
|
params?: Record<string, string>
|
|
|
|
|
): Promise<EntryTypeSwitch<T> | undefined> {
|
|
|
|
|
return (await getCachedEntries<T>(collectionName, filter, "_id", 1, undefined, projection, params))?.[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function postDBEntry<T extends CollectionNameT>(
|
|
|
|
|
|