1
1
import { getNews } from "@/helpers/homepage" ;
2
- import { SingleType } from "@/types/strapi" ;
2
+ import { NewsSection , SingleType } from "@/types/strapi" ;
3
3
4
4
export async function GET ( ) {
5
5
const res = await fetch ( `${ process . env . CMS_API } /home?populate=deep` , {
@@ -15,21 +15,20 @@ export async function GET() {
15
15
16
16
const homePageData = ( await res . json ( ) ) as SingleType ;
17
17
18
- if ( ! homePageData . data . attributes ) {
19
- return Response . json ( "Homepage contains no Data " ) ;
18
+ if ( ! homePageData . data . attributes . body ) {
19
+ return Response . json ( "Homepage contains no data " ) ;
20
20
}
21
- const news = homePageData . data . attributes . body
22
- . map ( ( el , i ) => {
23
- if ( "newsCount" in el ) return { index : i , headline : el . headline , newsCount : el . newsCount } ;
24
- } )
25
- . filter ( Boolean ) ;
26
-
27
- for ( const newsEntry of news ) {
28
- if ( ! newsEntry ) break ;
29
- const fetchedNews = await getNews ( newsEntry ?. newsCount ) ;
30
- fetchedNews . headline = newsEntry . headline ;
31
-
32
- homePageData . data . attributes . body [ newsEntry . index ] = fetchedNews ;
21
+
22
+ const newsSections = homePageData . data . attributes . body . map ( ( contentEntry , i ) => {
23
+ if ( "newsCount" in contentEntry ) return { index : i , news : { headline : contentEntry . headline , newsCount : contentEntry . newsCount } as NewsSection } ;
24
+ } ) ;
25
+
26
+ for ( const newsSectionEntry of newsSections ) {
27
+ if ( ! newsSectionEntry ) break ;
28
+ const fetchedNews = await getNews ( newsSectionEntry ?. news . newsCount ) ;
29
+ fetchedNews . headline = newsSectionEntry . news . headline ;
30
+
31
+ homePageData . data . attributes . body [ newsSectionEntry . index ] = fetchedNews ;
33
32
}
34
33
35
34
return Response . json ( homePageData . data . attributes ) ;
0 commit comments