@@ -12,19 +12,44 @@ export async function getPageContent(slug: string[]) {
12
12
} ,
13
13
} ;
14
14
const query = qs . stringify ( params , { addQueryPrefix : true } ) ;
15
- const respose = await fetch ( `${ process . env . CMS_API } /menus/1${ query } ` , {
16
- // headers: {
17
- // authorization: `Bearer ${process.env.CMS_TOKEN}`,
18
- // },
19
- } ) ;
20
- const data = ( await respose . json ( ) ) . data ;
21
- const page = data . attributes . items . data . find ( ( item : { attributes : { url : string } } ) => item . attributes . url . endsWith ( slug [ 0 ] ) ) ;
22
- const pageRelation = page ?. attributes ?. page_relation . data ;
23
-
24
- let pageContent ;
25
- if ( pageRelation ) {
15
+ let response , data , page , pageRelation , pageContent ;
16
+
17
+ try {
18
+ response = await fetch ( `${ process . env . CMS_API } /menus/1${ query } ` , {
19
+ // headers: {
20
+ // authorization: `Bearer ${process.env.CMS_TOKEN}`,
21
+ // },
22
+ } ) ;
23
+
24
+ if ( ! response . ok ) {
25
+ throw new Error ( `Failed to fetch menu data: ${ response . statusText } ` ) ;
26
+ }
27
+
28
+ data = await response . json ( ) ;
29
+ } catch ( error ) {
30
+ console . error ( "Error fetching menu data:" , error ) ;
31
+ return { error : "Failed to fetch menu data" } ;
32
+ }
33
+
34
+ try {
35
+ page = data . data . attributes . items . data . find ( ( item : { attributes : { url : string } } ) => item . attributes . url . endsWith ( slug [ 0 ] ) ) ;
36
+ pageRelation = page ?. attributes ?. page_relation ?. data ;
37
+
38
+ if ( ! pageRelation ) {
39
+ throw new Error ( "Page relation not found" ) ;
40
+ }
41
+
26
42
const pageRes = await fetch ( `${ process . env . CMS_API } /pages/${ pageRelation . id } ?populate=*` ) ;
27
- pageContent = ( await pageRes . json ( ) ) . data ;
43
+
44
+ if ( ! pageRes . ok ) {
45
+ throw new Error ( `Failed to fetch page data: ${ pageRes . statusText } ` ) ;
46
+ }
47
+
48
+ pageContent = await pageRes . json ( ) ;
49
+ } catch ( error ) {
50
+ console . error ( "Error fetching page data:" , error ) ;
51
+ return { error : "Failed to fetch page data" } ;
28
52
}
29
- return { page : pageContent } ;
53
+
54
+ return { page : pageContent . data } ;
30
55
}
0 commit comments