@@ -19,11 +19,12 @@ export class SanityArticlesRepository implements ArticlesRepository {
1919 current : z . string ( ) ,
2020 } ) ,
2121 publishedAt : z . string ( ) ,
22+ body : z . any ( ) ,
2223 } )
2324
2425 async findMany ( ) : Promise < Article [ ] > {
25- const articleResponseSchema = z . array ( this . articleSchema )
26- const CONTENT_QUERY = `*[_type == "post"] {
26+ const articlesSchema = z . array ( this . articleSchema )
27+ const query = `*[_type == "post"] {
2728 ...,
2829 author->,
2930 mainImage {
@@ -34,23 +35,9 @@ export class SanityArticlesRepository implements ArticlesRepository {
3435 body
3536 }
3637 `
37- const contentResponse = await client . fetch ( CONTENT_QUERY )
38- console . log ( contentResponse )
39- const articlesResponse = articleResponseSchema . parse ( contentResponse )
40- return articlesResponse . map ( ( article ) => {
41- return {
42- id : article . slug . current ,
43- title : article . title ,
44- description :
45- article . description . length > 120
46- ? article . description . substring ( 0 , 120 ) . concat ( '...' )
47- : article . description ,
48- author : article . author . name ,
49- createdAt : article . publishedAt ,
50- imageUrl : 'https://cdn.sanity.io/' . concat ( article . mainImage . asset . path ) ,
51- slug : article . slug . current ,
52- } as Article
53- } )
38+ const contentResponse = await client . fetch ( query )
39+ const articlesResponse = articlesSchema . parse ( contentResponse )
40+ return articlesResponse . map ( this . map )
5441 }
5542
5643 async findBySlug ( slug : string ) {
@@ -65,27 +52,24 @@ export class SanityArticlesRepository implements ArticlesRepository {
6552 body
6653 }`
6754
68- const params = { slug }
55+ const contentResponse = await client . fetch ( query , { slug } )
56+ const article = this . articleSchema . parse ( contentResponse )
57+ return this . map ( article )
58+ }
6959
70- try {
71- const contentResponse = await client . fetch ( query , params )
72- console . log ( 'Artigo:' , contentResponse )
73- const article = this . articleSchema . parse ( contentResponse )
74- return {
75- id : article . slug . current ,
76- title : article . title ,
77- description :
78- article . description . length > 120
79- ? article . description . substring ( 0 , 120 ) . concat ( '...' )
80- : article . description ,
81- author : article . author . name ,
82- createdAt : article . publishedAt ,
83- imageUrl : 'https://cdn.sanity.io/' . concat ( article . mainImage . asset . path ) ,
84- slug : article . slug . current ,
85- } as Article
86- } catch ( error ) {
87- console . error ( 'Erro ao buscar artigo:' , error )
88- return null
60+ private map ( article : z . infer < typeof this . articleSchema > ) {
61+ return {
62+ id : article . slug . current ,
63+ title : article . title ,
64+ description :
65+ article . description . length > 120
66+ ? article . description . substring ( 0 , 120 ) . concat ( '...' )
67+ : article . description ,
68+ author : article . author . name ,
69+ createdAt : article . publishedAt ,
70+ imageUrl : 'https://cdn.sanity.io/' . concat ( article . mainImage . asset . path ) ,
71+ slug : article . slug . current ,
72+ body : article . body ,
8973 }
9074 }
9175}
0 commit comments