From 413abe900bbbf95cb69269cf4cd2f8f6c29cc9fa Mon Sep 17 00:00:00 2001 From: oiov Date: Wed, 30 Oct 2024 16:03:13 +0800 Subject: [PATCH] upd --- app/api/admin/route.ts | 5 +--- app/api/keys/route.ts | 10 ++------ app/api/scraping/meta/route.ts | 37 ++++++++++++++++------------ app/api/scraping/screenshot/route.ts | 30 +++++++++++----------- app/api/user/admin/route.ts | 5 +--- app/api/user/admin/update/route.ts | 7 +----- 6 files changed, 41 insertions(+), 53 deletions(-) diff --git a/app/api/admin/route.ts b/app/api/admin/route.ts index 32317b6..6acf415 100644 --- a/app/api/admin/route.ts +++ b/app/api/admin/route.ts @@ -97,9 +97,6 @@ export async function GET(req: Request) { return Response.json({ list: combinedData.reverse(), total }); } catch (error) { - return Response.json(error?.statusText || error, { - status: error.status || 500, - statusText: error.statusText || "Server error", - }); + return Response.json({ statusText: "Server error" }, { status: 500 }); } } diff --git a/app/api/keys/route.ts b/app/api/keys/route.ts index eed4d3f..a97a870 100644 --- a/app/api/keys/route.ts +++ b/app/api/keys/route.ts @@ -12,14 +12,8 @@ export async function POST(req: Request) { if (res) { return Response.json(res.apiKey); } - return Response.json(res, { - status: 501, - statusText: "Server error", - }); + return Response.json({ statusText: "Server error" }, { status: 501 }); } catch (error) { - return Response.json("An error occurred", { - status: error.status || 500, - statusText: error.statusText || "Server error", - }); + return Response.json({ statusText: "Server error" }, { status: 500 }); } } diff --git a/app/api/scraping/meta/route.ts b/app/api/scraping/meta/route.ts index 5e14ae3..8348621 100644 --- a/app/api/scraping/meta/route.ts +++ b/app/api/scraping/meta/route.ts @@ -12,22 +12,23 @@ export async function GET(req: Request) { const url = new URL(req.url); const link = url.searchParams.get("url"); if (!link || !isLink(link)) { - return Response.json("Url is required", { - status: 400, - statusText: "Url is required", - }); + return Response.json( + { statusText: "Url is required" }, + { + status: 400, + }, + ); } // Get the API key from the request const custom_apiKey = url.searchParams.get("key"); if (!custom_apiKey) { return Response.json( - "API key is required. You can get your API key from Dashboard->Settings.", { - status: 400, statusText: "API key is required. You can get your API key from Dashboard->Settings.", }, + { status: 400 }, ); } @@ -37,7 +38,7 @@ export async function GET(req: Request) { return Response.json( { statusText: - "API key is required. You can get your API key from Dashboard->Settings.", + "Invalid API key. You can get your API key from Dashboard->Settings.", }, { status: 401 }, ); @@ -45,10 +46,12 @@ export async function GET(req: Request) { const res = await fetch(link); if (!res.ok) { - return Response.json("Failed to fetch url", { - status: 405, - statusText: "Failed to fetch url", - }); + return Response.json( + { statusText: "Failed to fetch url" }, + { + status: 405, + }, + ); } const html = await res.text(); @@ -93,10 +96,12 @@ export async function GET(req: Request) { payload: `https://wr.do/api/scraping/meta?url=${link}&key=${custom_apiKey}`, }); } catch (error) { - // console.log(error); - return Response.json("An error occurred", { - status: error.status || 500, - statusText: error.statusText || "Server error", - }); + console.log(error); + return Response.json( + { statusText: "Server error" }, + { + status: 500, + }, + ); } } diff --git a/app/api/scraping/screenshot/route.ts b/app/api/scraping/screenshot/route.ts index 684503b..971104b 100644 --- a/app/api/scraping/screenshot/route.ts +++ b/app/api/scraping/screenshot/route.ts @@ -24,22 +24,23 @@ export async function GET(req: Request) { // Check if the url is valid if (!link || !isLink(link)) { - return Response.json("Invalid url", { - status: 400, - statusText: "Invalid url", - }); + return Response.json( + { statusText: "Url is required" }, + { + status: 400, + }, + ); } // Get the API key from the request const custom_apiKey = url.searchParams.get("key"); if (!custom_apiKey) { return Response.json( - "API key is required. You can get your API key from Dashboard->Settings.", { - status: 400, statusText: "API key is required. You can get your API key from Dashboard->Settings.", }, + { status: 400 }, ); } @@ -49,7 +50,7 @@ export async function GET(req: Request) { return Response.json( { statusText: - "API key is required. You can get your API key from Dashboard->Settings.", + "Invalid API key. You can get your API key from Dashboard->Settings.", }, { status: 401 }, ); @@ -61,10 +62,12 @@ export async function GET(req: Request) { const res = await fetch(scrape_url); if (!res.ok) { - return Response.json("Failed to get screenshot", { - status: 406, - statusText: "Failed to get screenshot", - }); + return Response.json( + { statusText: "Failed to get screenshot" }, + { + status: 406, + }, + ); } const imageBuffer = await res.arrayBuffer(); @@ -76,9 +79,6 @@ export async function GET(req: Request) { }, }); } catch (error) { - return Response.json("Server error", { - status: error.status || 500, - statusText: "Server error", - }); + return Response.json({ statusText: "Server error" }, { status: 500 }); } } diff --git a/app/api/user/admin/route.ts b/app/api/user/admin/route.ts index e6fcb13..d91231f 100644 --- a/app/api/user/admin/route.ts +++ b/app/api/user/admin/route.ts @@ -20,9 +20,6 @@ export async function GET(req: Request) { return Response.json(data); } catch (error) { - return Response.json(error?.statusText || error, { - status: error.status || 500, - statusText: error.statusText || "Server error", - }); + return Response.json({ statusText: "Server error" }, { status: 500 }); } } diff --git a/app/api/user/admin/update/route.ts b/app/api/user/admin/update/route.ts index 0714537..70a4989 100644 --- a/app/api/user/admin/update/route.ts +++ b/app/api/user/admin/update/route.ts @@ -8,7 +8,6 @@ export async function POST(req: Request) { if (user.role !== "ADMIN") { return Response.json("Unauthorized", { status: 401, - statusText: "Unauthorized", }); } @@ -26,14 +25,10 @@ export async function POST(req: Request) { if (!res?.id) { return Response.json("An error occurred", { status: 400, - statusText: "An error occurred", }); } return Response.json("success"); } catch (error) { - return Response.json(error?.statusText || error, { - status: error.status || 500, - statusText: error.statusText || "Server error", - }); + return Response.json({ statusText: "Server error" }, { status: 500 }); } }