✨ feat: refine type definitions and improve request handling in API layer
This commit is contained in:
@@ -6,7 +6,6 @@ const apiClientBaseURL = "/api/"
|
|||||||
|
|
||||||
const cryptchaSiteId = "6628f06a0938460001505119"
|
const cryptchaSiteId = "6628f06a0938460001505119"
|
||||||
|
|
||||||
// @ts-ignore
|
|
||||||
if (release && typeof context !== "undefined") {
|
if (release && typeof context !== "undefined") {
|
||||||
context.response.header("X-Release", release)
|
context.response.header("X-Release", release)
|
||||||
context.response.header("X-Build-Time", buildTime)
|
context.response.header("X-Build-Time", buildTime)
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// @ts-ignore
|
import * as sentry from "./sentry" // used when Sentry is enabled for deploy
|
||||||
import * as sentry from "./sentry"
|
|
||||||
import configClient from "../../api/hooks/config-client"
|
import configClient from "../../api/hooks/config-client"
|
||||||
|
|
||||||
export const apiBaseURL = configClient.apiClientBaseURL
|
export const apiBaseURL = configClient.apiClientBaseURL
|
||||||
@@ -8,7 +7,7 @@ export const release = configClient.release
|
|||||||
export const sentryDSN = ""
|
export const sentryDSN = ""
|
||||||
export const sentryTracingOrigins = ["localhost", "project-domain.tld", /^\//]
|
export const sentryTracingOrigins = ["localhost", "project-domain.tld", /^\//]
|
||||||
export const sentryEnvironment: string = "local"
|
export const sentryEnvironment: string = "local"
|
||||||
// need to execute early for fetch wrapping
|
|
||||||
// sentry.init(sentryDSN, sentryTracingOrigins, sentryEnvironment, release)
|
// sentry.init(sentryDSN, sentryTracingOrigins, sentryEnvironment, release)
|
||||||
|
void sentry // preserve import for deploy activation
|
||||||
|
|
||||||
export const metricCall = false
|
export const metricCall = false
|
||||||
|
|||||||
@@ -20,19 +20,21 @@ import { checkBuildVersion } from "./versionCheck"
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// Request deduplication
|
// 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
|
* Generate a deterministic cache key for a request so identical parallel calls
|
||||||
* collapse into a single network round-trip.
|
* collapse into a single network round-trip.
|
||||||
*/
|
*/
|
||||||
const getRequestCacheKey = (endpoint: string, options?: ApiOptions, body?: any): string => {
|
const getRequestCacheKey = (endpoint: string, options?: ApiOptions, body?: unknown): string => {
|
||||||
const deterministicStringify = (obj: any): string => {
|
const deterministicStringify = (obj: unknown): string => {
|
||||||
if (obj === null || obj === undefined) return String(obj)
|
if (obj === null || obj === undefined) return String(obj)
|
||||||
if (typeof obj !== "object") return JSON.stringify(obj)
|
if (typeof obj !== "object") return JSON.stringify(obj)
|
||||||
if (Array.isArray(obj)) return `[${obj.map(deterministicStringify).join(",")}]`
|
if (Array.isArray(obj)) return `[${obj.map(deterministicStringify).join(",")}]`
|
||||||
const keys = Object.keys(obj).sort()
|
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 `{${pairs.join(",")}}`
|
||||||
}
|
}
|
||||||
return deterministicStringify({ endpoint, options, body })
|
return deterministicStringify({ endpoint, options, body })
|
||||||
@@ -56,7 +58,7 @@ const getRequestCacheKey = (endpoint: string, options?: ApiOptions, body?: any):
|
|||||||
export const api = async <T>(
|
export const api = async <T>(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
options?: ApiOptions,
|
options?: ApiOptions,
|
||||||
body?: any,
|
body?: unknown,
|
||||||
signal?: AbortSignal
|
signal?: AbortSignal
|
||||||
): Promise<ApiResult<T>> => {
|
): Promise<ApiResult<T>> => {
|
||||||
const _apiBaseOverride = get(apiBaseOverride) || ""
|
const _apiBaseOverride = get(apiBaseOverride) || ""
|
||||||
@@ -72,7 +74,7 @@ export const api = async <T>(
|
|||||||
// Deduplication: skip when caller provides a signal (they want explicit control)
|
// Deduplication: skip when caller provides a signal (they want explicit control)
|
||||||
const cacheKey = getRequestCacheKey(endpoint, options, body)
|
const cacheKey = getRequestCacheKey(endpoint, options, body)
|
||||||
if (!signal && pendingRequests.has(cacheKey)) {
|
if (!signal && pendingRequests.has(cacheKey)) {
|
||||||
return pendingRequests.get(cacheKey)!
|
return pendingRequests.get(cacheKey) as Promise<ApiResult<T>>
|
||||||
}
|
}
|
||||||
|
|
||||||
const requestPromise = (async () => {
|
const requestPromise = (async () => {
|
||||||
@@ -94,10 +96,10 @@ export const api = async <T>(
|
|||||||
return data as ApiResult<T>
|
return data as ApiResult<T>
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// Don't send abort errors to Sentry
|
// 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
|
// 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 */ }
|
// if (status === 401) { /* refresh token + retry */ }
|
||||||
|
|
||||||
throw err
|
throw err
|
||||||
@@ -119,7 +121,7 @@ export const api = async <T>(
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// In-memory response cache (for getCachedEntries / getCachedEntry)
|
// 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
|
const CACHE_TTL = 1000 * 60 * 60 // 1 hour
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -132,16 +134,16 @@ type EntryTypeSwitch<T extends string> = T extends "medialib"
|
|||||||
? MedialibEntry
|
? MedialibEntry
|
||||||
: T extends "content"
|
: T extends "content"
|
||||||
? ContentEntry
|
? ContentEntry
|
||||||
: Record<string, any>
|
: Record<string, unknown>
|
||||||
|
|
||||||
export async function getDBEntries<T extends CollectionNameT>(
|
export async function getDBEntries<T extends CollectionNameT>(
|
||||||
collectionName: T,
|
collectionName: T,
|
||||||
filter?: { [key: string]: any },
|
filter?: MongoFilter,
|
||||||
sort: string = "sort",
|
sort: string = "sort",
|
||||||
limit?: number,
|
limit?: number,
|
||||||
offset?: number,
|
offset?: number,
|
||||||
projection?: string,
|
projection?: string,
|
||||||
params?: { [key: string]: string }
|
params?: Record<string, string>
|
||||||
): Promise<EntryTypeSwitch<T>[]> {
|
): Promise<EntryTypeSwitch<T>[]> {
|
||||||
const c = await api<EntryTypeSwitch<T>[]>(collectionName, {
|
const c = await api<EntryTypeSwitch<T>[]>(collectionName, {
|
||||||
filter,
|
filter,
|
||||||
@@ -156,16 +158,16 @@ export async function getDBEntries<T extends CollectionNameT>(
|
|||||||
|
|
||||||
export async function getCachedEntries<T extends CollectionNameT>(
|
export async function getCachedEntries<T extends CollectionNameT>(
|
||||||
collectionName: T,
|
collectionName: T,
|
||||||
filter?: { [key: string]: any },
|
filter?: MongoFilter,
|
||||||
sort: string = "sort",
|
sort: string = "sort",
|
||||||
limit?: number,
|
limit?: number,
|
||||||
offset?: number,
|
offset?: number,
|
||||||
projection?: string,
|
projection?: string,
|
||||||
params?: { [key: string]: string }
|
params?: Record<string, string>
|
||||||
): Promise<EntryTypeSwitch<T>[]> {
|
): Promise<EntryTypeSwitch<T>[]> {
|
||||||
const filterStr = obj2str({ collectionName, filter, sort, limit, offset, projection, params })
|
const filterStr = obj2str({ collectionName, filter, sort, limit, offset, projection, params })
|
||||||
if (cache[filterStr] && cache[filterStr].expire >= Date.now()) {
|
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)
|
const entries = await getDBEntries<T>(collectionName, filter, sort, limit, offset, projection, params)
|
||||||
cache[filterStr] = { expire: Date.now() + CACHE_TTL, data: entries }
|
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>(
|
export async function getDBEntry<T extends CollectionNameT>(
|
||||||
collectionName: T,
|
collectionName: T,
|
||||||
filter: { [key: string]: any },
|
filter: MongoFilter,
|
||||||
projection?: string,
|
projection?: string,
|
||||||
params?: { [key: string]: string }
|
params?: Record<string, string>
|
||||||
) {
|
): Promise<EntryTypeSwitch<T> | undefined> {
|
||||||
return (await getDBEntries<T>(collectionName, filter, "_id", 1, null, projection, params))?.[0]
|
return (await getDBEntries<T>(collectionName, filter, "_id", 1, undefined, projection, params))?.[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getCachedEntry<T extends CollectionNameT>(
|
export async function getCachedEntry<T extends CollectionNameT>(
|
||||||
collectionName: T,
|
collectionName: T,
|
||||||
filter: { [key: string]: any },
|
filter: MongoFilter,
|
||||||
projection?: string,
|
projection?: string,
|
||||||
params?: { [key: string]: string }
|
params?: Record<string, string>
|
||||||
) {
|
): Promise<EntryTypeSwitch<T> | undefined> {
|
||||||
return (await getCachedEntries<T>(collectionName, filter, "_id", 1, null, projection, params))?.[0]
|
return (await getCachedEntries<T>(collectionName, filter, "_id", 1, undefined, projection, params))?.[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function postDBEntry<T extends CollectionNameT>(
|
export async function postDBEntry<T extends CollectionNameT>(
|
||||||
|
|||||||
@@ -50,11 +50,7 @@ const publishLocation = (_p?: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (typeof history !== "undefined") {
|
if (typeof history !== "undefined") {
|
||||||
const historyApply = (
|
const historyApply: ProxyHandler<typeof history.pushState>["apply"] = (target, thisArg, argumentsList) => {
|
||||||
target: (this: any, ...args: readonly any[]) => unknown,
|
|
||||||
thisArg: any,
|
|
||||||
argumentsList: string | readonly any[]
|
|
||||||
) => {
|
|
||||||
publishLocation(argumentsList && argumentsList.length >= 2 && argumentsList[2])
|
publishLocation(argumentsList && argumentsList.length >= 2 && argumentsList[2])
|
||||||
Reflect.apply(target, thisArg, argumentsList)
|
Reflect.apply(target, thisArg, argumentsList)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
export function debounce<T extends (...args: any[]) => void>(func: T, wait: number): (...args: Parameters<T>) => void {
|
export function debounce<T extends (...args: never[]) => void>(
|
||||||
|
func: T,
|
||||||
|
wait: number
|
||||||
|
): (...args: Parameters<T>) => void {
|
||||||
let timeout: NodeJS.Timeout | null = null
|
let timeout: NodeJS.Timeout | null = null
|
||||||
|
|
||||||
return (...args: Parameters<T>) => {
|
return (...args: Parameters<T>) => {
|
||||||
|
|||||||
5
types/global.d.ts
vendored
5
types/global.d.ts
vendored
@@ -5,9 +5,12 @@ interface Ssr {
|
|||||||
// validUntil: any // go Time
|
// validUntil: any // go Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** MongoDB-style filter, e.g. { _id: "abc" } or { $or: [...] } */
|
||||||
|
type MongoFilter = Record<string, unknown>
|
||||||
|
|
||||||
interface ApiOptions {
|
interface ApiOptions {
|
||||||
method?: "GET" | "POST" | "PUT" | "DELETE"
|
method?: "GET" | "POST" | "PUT" | "DELETE"
|
||||||
filter?: any
|
filter?: MongoFilter
|
||||||
sort?: string
|
sort?: string
|
||||||
lookup?: string
|
lookup?: string
|
||||||
limit?: number
|
limit?: number
|
||||||
|
|||||||
Reference in New Issue
Block a user