Cleanup pass
This commit is contained in:
@@ -18,6 +18,39 @@ type TemplatesResponse = {
|
||||
scores?: Record<string, TemplateFacetScoreDto>;
|
||||
};
|
||||
|
||||
function parseScoresPayload(
|
||||
raw: unknown,
|
||||
): Record<string, TemplateFacetScoreDto> {
|
||||
if (raw && typeof raw === "object" && !Array.isArray(raw)) {
|
||||
return raw as Record<string, TemplateFacetScoreDto>;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
async function getTemplatesJson(
|
||||
queryString: string,
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<
|
||||
| { ok: true; data: TemplatesResponse & { error?: string }; statusOk: boolean }
|
||||
| { ok: false; error: "network" | "aborted" }
|
||||
> {
|
||||
const url =
|
||||
queryString.length > 0 ? `/api/templates?${queryString}` : "/api/templates";
|
||||
try {
|
||||
const res = await fetch(url, {
|
||||
credentials: "include",
|
||||
signal,
|
||||
});
|
||||
const data = (await res.json()) as TemplatesResponse & { error?: string };
|
||||
return { ok: true, data, statusOk: res.ok };
|
||||
} catch (e) {
|
||||
if (isAbortError(e)) {
|
||||
return { ok: false, error: "aborted" };
|
||||
}
|
||||
return { ok: false, error: "network" };
|
||||
}
|
||||
}
|
||||
|
||||
/** Matches `listRankedRuleTemplatesFromDb` / GET `/api/templates` with facet params. */
|
||||
export type TemplateFacetScoreDto = {
|
||||
score: number;
|
||||
@@ -48,27 +81,23 @@ export function isTemplatesFetchAborted(e: unknown): boolean {
|
||||
export async function fetchTemplates(
|
||||
options?: FetchTemplatesOptions,
|
||||
): Promise<RuleTemplateDto[] | { error: string }> {
|
||||
try {
|
||||
const res = await fetch("/api/templates", {
|
||||
credentials: "include",
|
||||
signal: options?.signal,
|
||||
});
|
||||
const data = (await res.json()) as TemplatesResponse & { error?: string };
|
||||
if (!res.ok) {
|
||||
return {
|
||||
error:
|
||||
typeof data.error === "string"
|
||||
? data.error
|
||||
: "Could not load templates",
|
||||
};
|
||||
}
|
||||
return Array.isArray(data.templates) ? data.templates : [];
|
||||
} catch (e) {
|
||||
if (isAbortError(e)) {
|
||||
throw e;
|
||||
const got = await getTemplatesJson("", options?.signal);
|
||||
if (got.ok === false) {
|
||||
if (got.error === "aborted") {
|
||||
throw new DOMException("Aborted", "AbortError");
|
||||
}
|
||||
return { error: "Could not load templates" };
|
||||
}
|
||||
const { data, statusOk } = got;
|
||||
if (!statusOk) {
|
||||
return {
|
||||
error:
|
||||
typeof data.error === "string"
|
||||
? data.error
|
||||
: "Could not load templates",
|
||||
};
|
||||
}
|
||||
return Array.isArray(data.templates) ? data.templates : [];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,33 +111,24 @@ export async function fetchRankedTemplatesByFacets(options: {
|
||||
if (options.facetQuery.length === 0) {
|
||||
return { error: "Could not load templates" };
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`/api/templates?${options.facetQuery}`, {
|
||||
credentials: "include",
|
||||
signal: options.signal,
|
||||
});
|
||||
const data = (await res.json()) as TemplatesResponse & { error?: string };
|
||||
if (!res.ok) {
|
||||
return {
|
||||
error:
|
||||
typeof data.error === "string"
|
||||
? data.error
|
||||
: "Could not load templates",
|
||||
};
|
||||
}
|
||||
const templates = Array.isArray(data.templates) ? data.templates : [];
|
||||
const raw = data.scores;
|
||||
const scores: Record<string, TemplateFacetScoreDto> =
|
||||
raw && typeof raw === "object" && !Array.isArray(raw)
|
||||
? (raw as Record<string, TemplateFacetScoreDto>)
|
||||
: {};
|
||||
return { templates, scores };
|
||||
} catch (e) {
|
||||
if (isAbortError(e)) {
|
||||
throw e;
|
||||
const got = await getTemplatesJson(options.facetQuery, options.signal);
|
||||
if (got.ok === false) {
|
||||
if (got.error === "aborted") {
|
||||
throw new DOMException("Aborted", "AbortError");
|
||||
}
|
||||
return { error: "Could not load templates" };
|
||||
}
|
||||
const { data, statusOk } = got;
|
||||
if (!statusOk) {
|
||||
return {
|
||||
error:
|
||||
typeof data.error === "string"
|
||||
? data.error
|
||||
: "Could not load templates",
|
||||
};
|
||||
}
|
||||
const templates = Array.isArray(data.templates) ? data.templates : [];
|
||||
return { templates, scores: parseScoresPayload(data.scores) };
|
||||
}
|
||||
|
||||
export async function fetchTemplateBySlug(
|
||||
|
||||
Reference in New Issue
Block a user