Files
community-rule/lib/create/fetchTemplates.ts
T
adilallo 6498d52541 Skip setup questions on the template path and collect name, photo, and description without recommendations.
Customize opens a dialog then walks the template decks; Use without changes lands on final review with Has Name Field. Template cards stay first, the engine is not called, and the catalog no longer shows a recommended tag.
2026-11-09 17:34:33 -07:00

139 lines
3.6 KiB
TypeScript

/**
* Client fetch for curated rule templates (GET /api/templates).
*/
export type RuleTemplateDto = {
id: string;
slug: string;
title: string;
category: string | null;
description: string | null;
body: unknown;
sortOrder: number;
featured: boolean;
};
type TemplatesResponse = {
templates?: RuleTemplateDto[];
};
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" };
}
}
export type FetchTemplatesOptions = {
signal?: AbortSignal;
};
function isAbortError(e: unknown): boolean {
return (
(e instanceof DOMException && e.name === "AbortError") ||
(e instanceof Error && e.name === "AbortError")
);
}
/** For callers that `catch` around `fetchTemplates` / `fetchTemplateBySlug`. */
export function isTemplatesFetchAborted(e: unknown): boolean {
return isAbortError(e);
}
export async function fetchTemplates(
options?: FetchTemplatesOptions,
): Promise<RuleTemplateDto[] | { error: string }> {
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 : [];
}
export type TemplateDetailDto = {
template: RuleTemplateDto;
methods: Array<{ section: string; slug: string }>;
};
export async function fetchTemplateDetailBySlug(
slug: string,
options?: FetchTemplatesOptions,
): Promise<TemplateDetailDto | null | { error: string }> {
const encoded = encodeURIComponent(slug);
try {
const res = await fetch(`/api/templates/${encoded}`, {
credentials: "include",
signal: options?.signal,
});
const data = (await res.json()) as TemplateDetailDto & {
error?: string | { message?: string };
};
if (!res.ok) {
if (res.status === 404) return null;
const err = data.error;
const message =
typeof err === "string"
? err
: err && typeof err === "object" && typeof err.message === "string"
? err.message
: "Could not load template";
return { error: message };
}
if (!data.template || typeof data.template.slug !== "string") {
return { error: "Could not load template" };
}
return {
template: data.template,
methods: Array.isArray(data.methods) ? data.methods : [],
};
} catch (e) {
if (isAbortError(e)) {
throw new DOMException("Aborted", "AbortError");
}
return { error: "Could not load template" };
}
}
export async function fetchTemplateBySlug(
slug: string,
options?: FetchTemplatesOptions,
): Promise<RuleTemplateDto | null | { error: string }> {
const result = await fetchTemplateDetailBySlug(slug, options);
if ("error" in result) {
return result;
}
if (result === null) {
return null;
}
return result.template;
}