@@ -10,6 +10,8 @@ import { fsConfig } from "@dzcode.io/utils/dist/config";
1010import { plainLocalize } from "@dzcode.io/web/dist/components/locale/utils" ;
1111import { dictionary , AllDictionaryKeys } from "@dzcode.io/web/dist/components/locale/dictionary" ;
1212import { 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
1416export 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-t i t l e } } / g, pageTitle )
55+ . replace ( / { { template-d e s c r i p t i o n } } / g, localize ( "projects-description" ) )
56+ . replace ( / { { template-l a n g } } / 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-t i t l e } } / g, pageTitle )
63- . replace ( / { { template-d e s c r i p t i o n } } / g, localize ( "projects-description" ) )
64- . replace ( / { { template-l a n g } } / g, language ) ;
65-
66- return new Response ( newData , { headers : { "content-type" : "text/html; charset=utf-8" } } ) ;
6768} ;
0 commit comments