Skip to content

Commit

Permalink
use fetchV2 in project detail worker
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate committed Sep 30, 2024
1 parent 93a88bb commit 540e967
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
9 changes: 6 additions & 3 deletions api/src/app/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions api/src/project/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export class ProjectController {
await this.contributionRepository.findForProject(id),
]);

if (!project) throw new NotFoundError("Project not found");

return {
project: {
...project,
Expand Down
3 changes: 2 additions & 1 deletion api/src/project/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class ProjectRepository {
}

public async findWithStats(projectId: number) {
// @TODO-ZM: handle 404
const statement = sql`
SELECT
p.id as id,
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 18 additions & 17 deletions web/cloudflare/handler/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,9 +23,6 @@ export const handleProjectRequest: PagesFunction<Env> = 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;

Expand All @@ -44,24 +43,26 @@ export const handleProjectRequest: PagesFunction<Env> = 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<Endpoints>(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" } });
};

0 comments on commit 540e967

Please sign in to comment.