12 lines
555 B
TypeScript
12 lines
555 B
TypeScript
// components/utilities/buying-guide/dx.ts
|
|
export type Q = Record<string, any>;
|
|
|
|
export async function dxGet<T>(path: string, query?: Q): Promise<T> {
|
|
const qs = query ? "?" + new URLSearchParams(Object.entries(query).flatMap(([k, v]) =>
|
|
Array.isArray(v) ? v.map(x => [k, String(x)]) : [[k, String(v)]]
|
|
)).toString() : "";
|
|
const res = await fetch(`/api/dx/${path}${qs}`, { credentials: "include" });
|
|
if (!res.ok) throw new Error(`${res.status} ${await res.text()}`);
|
|
const json = await res.json();
|
|
return json?.data ?? json;
|
|
}
|