From 540e96732d211544fd9341aafcf6a807725824dc Mon Sep 17 00:00:00 2001 From: Zakaria Mansouri Date: Mon, 30 Sep 2024 18:47:21 +0200 Subject: [PATCH] use fetchV2 in project detail worker --- api/src/app/endpoints.ts | 9 +++++--- api/src/project/controller.ts | 2 ++ api/src/project/repository.ts | 3 ++- package-lock.json | 1 + web/cloudflare/handler/project.ts | 35 ++++++++++++++++--------------- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/api/src/app/endpoints.ts b/api/src/app/endpoints.ts index f3d66f03c..050c5ab75 100644 --- a/api/src/app/endpoints.ts +++ b/api/src/app/endpoints.ts @@ -2,6 +2,7 @@ import { GetContributionsResponse } from "src/contribution/types"; import { GetContributorsResponse } from "src/contributor/types"; import { GetMilestonesResponse } from "src/milestone/types"; import { + GetProjectNameResponse, GetProjectResponse, GetProjectsForSitemapResponse, GetProjectsResponse, @@ -16,11 +17,13 @@ export interface Endpoints { "api:projects/for-sitemap": { response: GetProjectsForSitemapResponse; }; + "api:projects/:id/name": { + response: GetProjectNameResponse; + params: { id: string }; + }; "api:Projects/:id": { response: GetProjectResponse; - params: { - id: string; - }; + params: { id: string }; }; "api:Contributions": { response: GetContributionsResponse; diff --git a/api/src/project/controller.ts b/api/src/project/controller.ts index 1051fb962..051313b09 100644 --- a/api/src/project/controller.ts +++ b/api/src/project/controller.ts @@ -49,6 +49,8 @@ export class ProjectController { await this.contributionRepository.findForProject(id), ]); + if (!project) throw new NotFoundError("Project not found"); + return { project: { ...project, diff --git a/api/src/project/repository.ts b/api/src/project/repository.ts index cd1a326a0..0e9e34313 100644 --- a/api/src/project/repository.ts +++ b/api/src/project/repository.ts @@ -30,7 +30,6 @@ export class ProjectRepository { } public async findWithStats(projectId: number) { - // @TODO-ZM: handle 404 const statement = sql` SELECT p.id as id, @@ -53,6 +52,8 @@ export class ProjectRepository { r.project_id `; const raw = this.sqliteService.db.get(statement); + if (!raw) return null; + const unStringifiedRaw = unStringifyDeep(raw); const camelCased = camelCaseObject(unStringifiedRaw); return camelCased; diff --git a/package-lock.json b/package-lock.json index 002923b15..e79c90c68 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29377,6 +29377,7 @@ "name": "@dzcode.io/cloudflare", "version": "0.0.0", "dependencies": { + "@dzcode.io/api": "*", "@dzcode.io/models": "*", "@dzcode.io/utils": "*", "@dzcode.io/web": "*" diff --git a/web/cloudflare/handler/project.ts b/web/cloudflare/handler/project.ts index 09c571989..87070564e 100644 --- a/web/cloudflare/handler/project.ts +++ b/web/cloudflare/handler/project.ts @@ -10,6 +10,8 @@ import { fsConfig } from "@dzcode.io/utils/dist/config"; import { plainLocalize } from "@dzcode.io/web/dist/components/locale/utils"; import { dictionary, AllDictionaryKeys } from "@dzcode.io/web/dist/components/locale/dictionary"; import { LanguageEntity } from "@dzcode.io/models/dist/language"; +import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory"; +import { Endpoints } from "@dzcode.io/api/dist/app/endpoints"; export interface Env { STAGE: Environment; @@ -21,9 +23,6 @@ export const handleProjectRequest: PagesFunction = async (context) => { console.log(`⚠️ No STAGE provided, falling back to "development"`); stage = "development"; } - const fullstackConfig = fsConfig(stage); - - const apiUrl = fullstackConfig.api.url; const pathName = new URL(context.request.url).pathname; @@ -44,24 +43,26 @@ export const handleProjectRequest: PagesFunction = async (context) => { const localize = (key: AllDictionaryKeys) => plainLocalize(dictionary, language, key, "NO-TRANSLATION"); - // @TODO-ZM: use fetchV2 - const projectResponse = await fetch(`${apiUrl}/Projects/${projectId}/name`); + const fullstackConfig = fsConfig(stage); + const fetchV2 = fetchV2Factory(fullstackConfig); + + try { + const { project } = await fetchV2("api:projects/:id/name", { params: { id: projectId } }); + const pageTitle = `${localize("project-title-pre")} ${project.name} ${localize("project-title-post")}`; + + const newData = htmlTemplate + .replace(/{{template-title}}/g, pageTitle) + .replace(/{{template-description}}/g, localize("projects-description")) + .replace(/{{template-lang}}/g, language); + + return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } }); + } catch (error) { + // @TODO-ZM: log error to sentry + console.error(error); - if (!projectResponse.ok) { return new Response(notFound, { headers: { "content-type": "text/html; charset=utf-8" }, status: 404, }); } - - const projectData = await projectResponse.json(); - // @ts-expect-error @TODO-ZM: import @dzcode.io/api - const pageTitle = `${localize("project-title-pre")} ${projectData.project.name} ${localize("project-title-post")}`; - - const newData = htmlTemplate - .replace(/{{template-title}}/g, pageTitle) - .replace(/{{template-description}}/g, localize("projects-description")) - .replace(/{{template-lang}}/g, language); - - return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } }); };