Skip to content

Commit 540e967

Browse files
committed
use fetchV2 in project detail worker
1 parent 93a88bb commit 540e967

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

api/src/app/endpoints.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { GetContributionsResponse } from "src/contribution/types";
22
import { GetContributorsResponse } from "src/contributor/types";
33
import { GetMilestonesResponse } from "src/milestone/types";
44
import {
5+
GetProjectNameResponse,
56
GetProjectResponse,
67
GetProjectsForSitemapResponse,
78
GetProjectsResponse,
@@ -16,11 +17,13 @@ export interface Endpoints {
1617
"api:projects/for-sitemap": {
1718
response: GetProjectsForSitemapResponse;
1819
};
20+
"api:projects/:id/name": {
21+
response: GetProjectNameResponse;
22+
params: { id: string };
23+
};
1924
"api:Projects/:id": {
2025
response: GetProjectResponse;
21-
params: {
22-
id: string;
23-
};
26+
params: { id: string };
2427
};
2528
"api:Contributions": {
2629
response: GetContributionsResponse;

api/src/project/controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export class ProjectController {
4949
await this.contributionRepository.findForProject(id),
5050
]);
5151

52+
if (!project) throw new NotFoundError("Project not found");
53+
5254
return {
5355
project: {
5456
...project,

api/src/project/repository.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class ProjectRepository {
3030
}
3131

3232
public async findWithStats(projectId: number) {
33-
// @TODO-ZM: handle 404
3433
const statement = sql`
3534
SELECT
3635
p.id as id,
@@ -53,6 +52,8 @@ export class ProjectRepository {
5352
r.project_id
5453
`;
5554
const raw = this.sqliteService.db.get(statement);
55+
if (!raw) return null;
56+
5657
const unStringifiedRaw = unStringifyDeep(raw);
5758
const camelCased = camelCaseObject(unStringifiedRaw);
5859
return camelCased;

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/cloudflare/handler/project.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import { fsConfig } from "@dzcode.io/utils/dist/config";
1010
import { plainLocalize } from "@dzcode.io/web/dist/components/locale/utils";
1111
import { dictionary, AllDictionaryKeys } from "@dzcode.io/web/dist/components/locale/dictionary";
1212
import { LanguageEntity } from "@dzcode.io/models/dist/language";
13+
import { fetchV2Factory } from "@dzcode.io/utils/dist/fetch/factory";
14+
import { Endpoints } from "@dzcode.io/api/dist/app/endpoints";
1315

1416
export interface Env {
1517
STAGE: Environment;
@@ -21,9 +23,6 @@ export const handleProjectRequest: PagesFunction<Env> = async (context) => {
2123
console.log(`⚠️ No STAGE provided, falling back to "development"`);
2224
stage = "development";
2325
}
24-
const fullstackConfig = fsConfig(stage);
25-
26-
const apiUrl = fullstackConfig.api.url;
2726

2827
const pathName = new URL(context.request.url).pathname;
2928

@@ -44,24 +43,26 @@ export const handleProjectRequest: PagesFunction<Env> = async (context) => {
4443
const localize = (key: AllDictionaryKeys) =>
4544
plainLocalize(dictionary, language, key, "NO-TRANSLATION");
4645

47-
// @TODO-ZM: use fetchV2
48-
const projectResponse = await fetch(`${apiUrl}/Projects/${projectId}/name`);
46+
const fullstackConfig = fsConfig(stage);
47+
const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig);
48+
49+
try {
50+
const { project } = await fetchV2("api:projects/:id/name", { params: { id: projectId } });
51+
const pageTitle = `${localize("project-title-pre")} ${project.name} ${localize("project-title-post")}`;
52+
53+
const newData = htmlTemplate
54+
.replace(/{{template-title}}/g, pageTitle)
55+
.replace(/{{template-description}}/g, localize("projects-description"))
56+
.replace(/{{template-lang}}/g, language);
57+
58+
return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } });
59+
} catch (error) {
60+
// @TODO-ZM: log error to sentry
61+
console.error(error);
4962

50-
if (!projectResponse.ok) {
5163
return new Response(notFound, {
5264
headers: { "content-type": "text/html; charset=utf-8" },
5365
status: 404,
5466
});
5567
}
56-
57-
const projectData = await projectResponse.json();
58-
// @ts-expect-error @TODO-ZM: import @dzcode.io/api
59-
const pageTitle = `${localize("project-title-pre")} ${projectData.project.name} ${localize("project-title-post")}`;
60-
61-
const newData = htmlTemplate
62-
.replace(/{{template-title}}/g, pageTitle)
63-
.replace(/{{template-description}}/g, localize("projects-description"))
64-
.replace(/{{template-lang}}/g, language);
65-
66-
return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } });
6768
};

0 commit comments

Comments
 (0)