mirror of
https://github.com/Tony0410/readlater.git
synced 2026-05-24 22:01:41 +08:00
Improve POST /api/add for Apple Shortcuts
Cleaner JSON body handling - just send {"url": "..."}
No URL encoding needed.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -61,7 +61,7 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
}
|
||||
|
||||
// POST /api/add - Alternative with JSON body
|
||||
// POST /api/add - Simple POST with JSON body for Apple Shortcuts
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
@@ -71,12 +71,48 @@ export async function POST(request: NextRequest) {
|
||||
return NextResponse.json({ error: "URL is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Reuse GET logic
|
||||
const fakeRequest = new NextRequest(
|
||||
new URL(`/api/add?url=${encodeURIComponent(url)}`, request.url)
|
||||
// Check if already exists
|
||||
const existing = await db
|
||||
.select()
|
||||
.from(schema.articles)
|
||||
.where(eq(schema.articles.url, url))
|
||||
.limit(1);
|
||||
|
||||
if (existing.length > 0) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Already saved",
|
||||
title: existing[0].title,
|
||||
});
|
||||
}
|
||||
|
||||
// Extract and save
|
||||
const extracted = await extractArticle(url);
|
||||
const id = uuidv4();
|
||||
|
||||
await db.insert(schema.articles).values({
|
||||
id,
|
||||
url: url,
|
||||
title: extracted.title,
|
||||
author: extracted.author,
|
||||
siteName: extracted.siteName,
|
||||
excerpt: extracted.excerpt,
|
||||
content: extracted.content,
|
||||
textContent: extracted.textContent,
|
||||
leadImage: extracted.leadImage,
|
||||
wordCount: extracted.wordCount,
|
||||
});
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Saved",
|
||||
title: extracted.title,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Error adding article:", error);
|
||||
return NextResponse.json(
|
||||
{ error: error instanceof Error ? error.message : "Failed to save" },
|
||||
{ status: 500 }
|
||||
);
|
||||
return GET(fakeRequest);
|
||||
} catch {
|
||||
return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user