64
apps/web/src/app/api/ai/templates/[key]/route.ts
Executable file
64
apps/web/src/app/api/ai/templates/[key]/route.ts
Executable file
@@ -0,0 +1,64 @@
|
||||
import { handleApi } from "@/lib/api";
|
||||
import { requireUser } from "@/lib/services/user";
|
||||
import {
|
||||
getPromptTemplate,
|
||||
updatePromptTemplate,
|
||||
resetPromptTemplate,
|
||||
previewTemplate,
|
||||
getTemplateVersions,
|
||||
restoreTemplateVersion,
|
||||
} from "@/lib/services/ai-templates";
|
||||
|
||||
export async function GET(
|
||||
_request: Request,
|
||||
{ params }: { params: Promise<{ key: string }> }
|
||||
) {
|
||||
const { key } = await params;
|
||||
return handleApi(async () => {
|
||||
const { user } = await requireUser();
|
||||
const template = await getPromptTemplate(user.id, key);
|
||||
const versions = await getTemplateVersions(user.id, key);
|
||||
return { template, versions };
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ key: string }> }
|
||||
) {
|
||||
const { key } = await params;
|
||||
const body = await request.json();
|
||||
return handleApi(async () => {
|
||||
const { user } = await requireUser();
|
||||
const template = await updatePromptTemplate(user.id, key, body);
|
||||
return { template };
|
||||
});
|
||||
}
|
||||
|
||||
export async function POST(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ key: string }> }
|
||||
) {
|
||||
const { key } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
const action = searchParams.get("action");
|
||||
const body = await request.json().catch(() => ({}));
|
||||
|
||||
return handleApi(async () => {
|
||||
const { user } = await requireUser();
|
||||
|
||||
if (action === "reset") {
|
||||
const template = await resetPromptTemplate(user.id, key);
|
||||
return { template };
|
||||
}
|
||||
if (action === "preview") {
|
||||
const rendered = await previewTemplate(user.id, key, body.body);
|
||||
return { rendered };
|
||||
}
|
||||
if (action === "restore" && body.version) {
|
||||
const template = await restoreTemplateVersion(user.id, key, body.version);
|
||||
return { template };
|
||||
}
|
||||
throw new Error("Unknown action");
|
||||
});
|
||||
}
|
||||
12
apps/web/src/app/api/ai/templates/route.ts
Executable file
12
apps/web/src/app/api/ai/templates/route.ts
Executable file
@@ -0,0 +1,12 @@
|
||||
import { handleApi } from "@/lib/api";
|
||||
import { requireUser } from "@/lib/services/user";
|
||||
import { getPromptTemplates } from "@/lib/services/ai-templates";
|
||||
import { PLACEHOLDER_DOCS } from "@/lib/ai/prompts/defaults";
|
||||
|
||||
export async function GET() {
|
||||
return handleApi(async () => {
|
||||
const { user } = await requireUser();
|
||||
const templates = await getPromptTemplates(user.id);
|
||||
return { templates, placeholders: PLACEHOLDER_DOCS };
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user