From ac607228930c7fad83bdfcd76c4f4820beeb5fe8 Mon Sep 17 00:00:00 2001 From: Nathan Sarrazin Date: Fri, 28 Feb 2025 09:28:19 +0000 Subject: [PATCH] feat: use universal load function for `tools/[toolId]` --- src/routes/tools/[toolId]/+layout.server.ts | 46 --------------------- src/routes/tools/[toolId]/+layout.ts | 26 ++++++++++++ 2 files changed, 26 insertions(+), 46 deletions(-) delete mode 100644 src/routes/tools/[toolId]/+layout.server.ts create mode 100644 src/routes/tools/[toolId]/+layout.ts diff --git a/src/routes/tools/[toolId]/+layout.server.ts b/src/routes/tools/[toolId]/+layout.server.ts deleted file mode 100644 index 3fa4ed30503..00000000000 --- a/src/routes/tools/[toolId]/+layout.server.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { base } from "$app/paths"; -import { collections } from "$lib/server/database.js"; -import { toolFromConfigs } from "$lib/server/tools/index.js"; -import { ReviewStatus } from "$lib/types/Review.js"; -import { redirect } from "@sveltejs/kit"; -import { ObjectId } from "mongodb"; - -export const load = async ({ params, locals }) => { - const tool = await collections.tools.findOne({ _id: new ObjectId(params.toolId) }); - - if (!tool) { - const tool = toolFromConfigs.find((el) => el._id.toString() === params.toolId); - if (!tool) { - redirect(302, `${base}/tools`); - } - return { - tool: { - ...tool, - _id: tool._id.toString(), - call: undefined, - createdById: null, - createdByName: null, - createdByMe: false, - reported: false, - review: ReviewStatus.APPROVED, - }, - }; - } - - const reported = await collections.reports.findOne({ - contentId: tool._id, - object: "tool", - }); - - return { - tool: { - ...tool, - _id: tool._id.toString(), - call: undefined, - createdById: tool.createdById.toString(), - createdByMe: - tool.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(), - reported: !!reported, - }, - }; -}; diff --git a/src/routes/tools/[toolId]/+layout.ts b/src/routes/tools/[toolId]/+layout.ts new file mode 100644 index 00000000000..4efd11a8d8d --- /dev/null +++ b/src/routes/tools/[toolId]/+layout.ts @@ -0,0 +1,26 @@ +import { base } from "$app/paths"; +import type { ReviewStatus } from "$lib/types/Review"; +import type { Tool } from "$lib/types/Tool.js"; +import type { Serialize } from "$lib/utils/serialize"; +import { error } from "@sveltejs/kit"; + +export const load = async ({ params, fetch }) => { + const r = await fetch(`${base}/api/v2/tools/${params.toolId}`); + + if (!r.ok) { + throw error(r.status, r.statusText); + } + + const data = await r.json(); + + return { + tool: data as Serialize< + Tool & { + createdById: string | null; + createdByMe: boolean; + reported: boolean; + review: ReviewStatus; + } + >, + }; +};