@@ -4,13 +4,19 @@ import { Comment, Post, Profile, Tag, PostTag } from '../../types/supabase';
44import { HttpClient , HttpHeaders , HttpParams } from '@angular/common/http' ;
55import { map , Observable } from 'rxjs' ;
66import { environment } from '../../../environments/environment' ;
7+ import { createApiUrl } from '../../helpers/api-url-builder' ;
78
89@Injectable ( { providedIn : 'root' } )
910export class ReaderApiService {
1011 supabaseService = inject ( SupabaseService ) ;
1112 http = inject ( HttpClient ) ;
1213 private readonly baseUrl = `${ environment . supabaseUrl } /rest/v1/` ;
1314 private readonly apiKey = environment . supabaseKey ;
15+ private headers = new HttpHeaders ( {
16+ apikey : this . apiKey ,
17+ Authorization : `Bearer ${ this . apiKey } ` ,
18+ Accept : 'application/json' ,
19+ } ) ;
1420
1521 getPost ( id : string ) : Observable < Post > {
1622 const selectQuery = `
@@ -60,27 +66,20 @@ export class ReaderApiService {
6066 . eq ( 'post_id' , postId ) ;
6167 }
6268
63- getPosts ( ) : Observable < Post [ ] | null > {
64- const selectQuery = `
65- *,
66- author:profiles(id,username,avatar_url),
67- post_tags(tags(id,name,color,icon))
68- `
69- . replace ( / \s + / g, ' ' )
70- . trim ( ) ;
71-
72- const params = new HttpParams ( )
73- . set ( 'select' , selectQuery )
74- . set ( 'is_draft' , 'eq.false' )
75- . set ( 'order' , 'created_at.desc' ) ;
69+ getPosts ( ) : Observable < Post [ ] > {
70+ const query = createApiUrl ( 'posts' )
71+ . select (
72+ '*' ,
73+ 'author:profiles(id,username,avatar_url)' ,
74+ 'post_tags(tags(id,name,color,icon))' ,
75+ )
76+ . where ( 'is_draft' , 'eq' , false )
77+ . orderBy ( 'created_at' , 'desc' )
78+ . build ( ) ;
7679
77- const headers = new HttpHeaders ( {
78- apikey : this . apiKey ,
79- Authorization : `Bearer ${ this . apiKey } ` ,
80- Accept : 'application/json' ,
80+ return this . http . get < Post [ ] > ( `${ this . baseUrl } /${ query } ` , {
81+ headers : this . headers ,
8182 } ) ;
82-
83- return this . http . get < Post [ ] > ( `${ this . baseUrl } posts` , { headers, params } ) ;
8483 }
8584
8685 async getProfiles ( ) : Promise < Profile [ ] | null > {
0 commit comments