Skip to content

Commit 514fbb7

Browse files
committed
fix: update API routes to match type interfaces
- Remove description field from template creation (not in Template type) - Add isPublic field to snippet creation (required by Snippet type) - Fix type validation for consistency
1 parent 5e83aad commit 514fbb7

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

app/api/snippets/route.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { getServerSession } from "next-auth";
3-
import { authOptions } from "@/lib/auth";
42
import { rateLimitMiddleware } from "@/lib/middleware/rate-limit-middleware";
53
import { SnippetService } from "@/lib/db/services/snippetService";
4+
import { getAuthenticatedUser } from "@/lib/helpers/apiAuth";
65

76
// GET /api/snippets - Get all code snippets
87
async function handleGetSnippets(request: NextRequest): Promise<NextResponse> {
@@ -45,9 +44,9 @@ async function handleCreateSnippet(
4544
request: NextRequest,
4645
): Promise<NextResponse> {
4746
try {
48-
// Check authentication
49-
const session = await getServerSession(authOptions);
50-
if (!session?.user) {
47+
// Check authentication (supports both session and API token)
48+
const user = await getAuthenticatedUser(request);
49+
if (!user) {
5150
return NextResponse.json(
5251
{ error: "Unauthorized - Please login to create snippets" },
5352
{ status: 401 },
@@ -97,6 +96,7 @@ async function handleCreateSnippet(
9796
code,
9897
tags: tags || [],
9998
category: category || "general",
99+
isPublic: false,
100100
createdAt: new Date().toISOString(),
101101
updatedAt: new Date().toISOString(),
102102
});

app/api/templates/route.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { NextRequest, NextResponse } from "next/server";
2-
import { getServerSession } from "next-auth";
3-
import { authOptions } from "@/lib/auth";
42
import { rateLimitMiddleware } from "@/lib/middleware/rate-limit-middleware";
53
import { TemplateService } from "@/lib/db/services/templateService";
4+
import { getAuthenticatedUser } from "@/lib/helpers/apiAuth";
65

76
// GET /api/templates - Get all templates
87
async function handleGetTemplates(request: NextRequest): Promise<NextResponse> {
@@ -44,9 +43,9 @@ async function handleCreateTemplate(
4443
request: NextRequest,
4544
): Promise<NextResponse> {
4645
try {
47-
// Check authentication
48-
const session = await getServerSession(authOptions);
49-
if (!session?.user) {
46+
// Check authentication (supports both session and API token)
47+
const user = await getAuthenticatedUser(request);
48+
if (!user) {
5049
return NextResponse.json(
5150
{ error: "Unauthorized - Please login to create templates" },
5251
{ status: 401 },
@@ -60,32 +59,34 @@ async function handleCreateTemplate(
6059
return NextResponse.json({ error: "Invalid JSON" }, { status: 400 });
6160
}
6261

63-
const { name, description, content, category, tags } =
62+
// Accept both 'name' and 'title' for compatibility
63+
const { name, title, description, content, category, tags } =
6464
(body as Record<string, unknown>) ?? {};
6565

66-
if (typeof name !== "string" || typeof content !== "string") {
66+
// Use 'name' if provided, otherwise use 'title'
67+
const templateName = (name as string) || (title as string);
68+
69+
if (typeof templateName !== "string" || typeof content !== "string") {
6770
return NextResponse.json(
68-
{ error: "Name and content are required" },
71+
{ error: "Name/title and content are required" },
6972
{ status: 400 },
7073
);
7174
}
7275

7376
// Validate required fields
74-
if (!name || !content) {
77+
if (!templateName || !content) {
7578
return NextResponse.json(
76-
{ error: "Name and content are required" },
79+
{ error: "Name/title and content are required" },
7780
{ status: 400 },
7881
);
7982
}
8083

8184
// Create template in database
8285
const newTemplate = await TemplateService.createTemplate({
83-
name,
84-
description: typeof description === "string" ? description : "",
86+
name: templateName, // Use the resolved name
8587
content,
8688
category: (typeof category === "string" ? category : "general") as "email" | "blog" | "social" | "general",
8789
tags: Array.isArray(tags) ? tags.map(String) : [],
88-
createdAt: new Date().toISOString(),
8990
});
9091

9192
return NextResponse.json(

0 commit comments

Comments
 (0)