|
| 1 | +declare const htmlTemplate: string; // @ts-expect-error cloudflare converts this to a string using esbuild |
| 2 | +import htmlTemplate from "../public/template.html"; |
| 3 | +declare const notFoundEn: string; // @ts-expect-error cloudflare converts this to a string using esbuild |
| 4 | +import notFoundEn from "../public/404.html"; |
| 5 | +declare const notFoundAr: string; // @ts-expect-error cloudflare converts this to a string using esbuild |
| 6 | +import notFoundAr from "../public/ar/404.html"; |
| 7 | + |
| 8 | +import { Environment, environments } from "@dzcode.io/utils/dist/config/environment"; |
| 9 | +import { fsConfig } from "@dzcode.io/utils/dist/config"; |
| 10 | +import { plainLocalize } from "@dzcode.io/web/dist/components/locale/utils"; |
| 11 | +import { dictionary, AllDictionaryKeys } from "@dzcode.io/web/dist/components/locale/dictionary"; |
| 12 | +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"; |
| 15 | + |
| 16 | +export interface Env { |
| 17 | + STAGE: Environment; |
| 18 | +} |
| 19 | + |
| 20 | +export const handleContributionRequest: PagesFunction<Env> = async (context) => { |
| 21 | + let stage = context.env.STAGE; |
| 22 | + if (!environments.includes(stage)) { |
| 23 | + console.log(`⚠️ No STAGE provided, falling back to "development"`); |
| 24 | + stage = "development"; |
| 25 | + } |
| 26 | + |
| 27 | + const pathName = new URL(context.request.url).pathname; |
| 28 | + |
| 29 | + const languageRegex = /^\/(ar|en)\//i; |
| 30 | + const language = (pathName?.match(languageRegex)?.[1]?.toLowerCase() || |
| 31 | + "en") as LanguageEntity["code"]; |
| 32 | + const notFound = language === "ar" ? notFoundAr : notFoundEn; |
| 33 | + |
| 34 | + const contributionIdRegex = /contribute\/(.*)-(.*)-(.*)/; |
| 35 | + const contributionId = |
| 36 | + pathName?.match(contributionIdRegex)?.[2] + "-" + pathName?.match(contributionIdRegex)?.[3]; |
| 37 | + |
| 38 | + if (!contributionId) |
| 39 | + return new Response(notFound, { |
| 40 | + headers: { "content-type": "text/html; charset=utf-8" }, |
| 41 | + status: 404, |
| 42 | + }); |
| 43 | + |
| 44 | + const localize = (key: AllDictionaryKeys) => |
| 45 | + plainLocalize(dictionary, language, key, "NO-TRANSLATION"); |
| 46 | + |
| 47 | + const fullstackConfig = fsConfig(stage); |
| 48 | + const fetchV2 = fetchV2Factory<Endpoints>(fullstackConfig); |
| 49 | + |
| 50 | + try { |
| 51 | + const { contribution } = await fetchV2("api:contributions/:id/title", { |
| 52 | + params: { id: contributionId }, |
| 53 | + }); |
| 54 | + const pageTitle = `${localize("contribution-title-pre")} ${contribution.title} ${localize("contribution-title-post")}`; |
| 55 | + |
| 56 | + const newData = htmlTemplate |
| 57 | + .replace(/{{template-title}}/g, pageTitle) |
| 58 | + .replace(/{{template-description}}/g, localize("contribute-description")) |
| 59 | + .replace(/{{template-lang}}/g, language); |
| 60 | + |
| 61 | + return new Response(newData, { headers: { "content-type": "text/html; charset=utf-8" } }); |
| 62 | + } catch (error) { |
| 63 | + // @TODO-ZM: log error to sentry |
| 64 | + console.error(error); |
| 65 | + |
| 66 | + return new Response(notFound, { |
| 67 | + headers: { "content-type": "text/html; charset=utf-8" }, |
| 68 | + status: 404, |
| 69 | + }); |
| 70 | + } |
| 71 | +}; |
0 commit comments