@@ -13,17 +13,20 @@ const AUTH_KEY = 'AUTH_TOKEN';
1313
1414type HttpMethod = 'post' | 'get' | 'put' | 'delete' ;
1515type ResponseClass < T > = new ( data : any ) => T ;
16+ type ResponseType = 'json' | 'text' ;
1617
1718interface RequestOptions {
1819 csrf ?: boolean ;
1920 authToken ?: boolean ;
2021 useStelaDomain ?: boolean ;
22+ responseType ?: ResponseType ;
2123}
2224
2325const defaultOptions : RequestOptions = {
2426 csrf : false ,
2527 authToken : true ,
2628 useStelaDomain : true ,
29+ responseType : 'json' ,
2730} ;
2831
2932export function getFirst < T > ( observable : Observable < T [ ] > ) : Observable < T > {
@@ -61,15 +64,13 @@ export class HttpV2Service {
6164 data : any = { } ,
6265 responseClass ?: ResponseClass < T > ,
6366 options : RequestOptions = defaultOptions ,
64- responseType ?: 'json' | 'text' ,
6567 ) : Observable < T [ ] > {
6668 return this . makeHttpClientRequest (
6769 endpoint ,
6870 data ,
6971 'post' ,
7072 responseClass ,
7173 this . getOptions ( options ) ,
72- responseType ,
7374 ) ;
7475 }
7576
@@ -78,15 +79,13 @@ export class HttpV2Service {
7879 data : any = { } ,
7980 responseClass ?: ResponseClass < T > ,
8081 options : RequestOptions = defaultOptions ,
81- responseType ?: 'json' | 'text' ,
8282 ) : Observable < T [ ] > {
8383 return this . makeHttpClientRequest (
8484 this . getEndpointWithData ( endpoint , data ) ,
8585 { } ,
8686 'get' ,
8787 responseClass ,
8888 this . getOptions ( options ) ,
89- responseType ,
9089 ) ;
9190 }
9291
@@ -95,15 +94,13 @@ export class HttpV2Service {
9594 data : any = { } ,
9695 responseClass ?: ResponseClass < T > ,
9796 options : RequestOptions = defaultOptions ,
98- responseType ?: 'json' | 'text' ,
9997 ) : Observable < T [ ] > {
10098 return this . makeHttpClientRequest (
10199 endpoint ,
102100 data ,
103101 'put' ,
104102 responseClass ,
105103 this . getOptions ( options ) ,
106- responseType ,
107104 ) ;
108105 }
109106
@@ -112,15 +109,13 @@ export class HttpV2Service {
112109 data : any = { } ,
113110 responseClass ?: ResponseClass < T > ,
114111 options : RequestOptions = defaultOptions ,
115- responseType ?: 'json' | 'text' ,
116112 ) : Observable < T [ ] > {
117113 return this . makeHttpClientRequest (
118114 this . getEndpointWithData ( endpoint , data ) ,
119115 { } ,
120116 'delete' ,
121117 responseClass ,
122118 this . getOptions ( options ) ,
123- responseType ,
124119 ) ;
125120 }
126121
@@ -194,56 +189,50 @@ export class HttpV2Service {
194189 data : any = { } ,
195190 method : HttpMethod ,
196191 options : RequestOptions ,
197- responseType : any = 'json' ,
198192 ) : Observable < unknown > {
193+ const requestOptions : Object = {
194+ ...this . getHeaders ( options ) ,
195+ responseType : options . responseType ,
196+ } ;
199197 if ( method === 'put' ) {
200- return this . http . put ( url , data , {
201- ...this . getHeaders ( options ) ,
202- responseType,
203- } ) ;
198+ return this . http . put ( url , data , requestOptions ) ;
204199 }
205- return this . http . post ( url , data , {
206- ...this . getHeaders ( options ) ,
207- responseType,
208- } ) ;
200+ return this . http . post ( url , data , requestOptions ) ;
209201 }
210202
211203 protected getObservableWithNoBody (
212204 url : string ,
213205 method : HttpMethod ,
214206 options : RequestOptions ,
215- responseType : any = 'json' ,
216207 ) : Observable < unknown > {
208+ const requestOptions : Object = {
209+ ...this . getHeaders ( options ) ,
210+ responseType : options . responseType ,
211+ } ;
217212 if ( method === 'delete' ) {
218- return this . http . delete ( url , {
219- ...this . getHeaders ( options ) ,
220- responseType,
221- } ) ;
213+ return this . http . delete ( url , requestOptions ) ;
222214 }
223- return this . http . get ( url , { ... this . getHeaders ( options ) , responseType } ) ;
215+ return this . http . get ( url , requestOptions ) ;
224216 }
225217
226218 protected getObservable (
227219 endpoint : string ,
228220 data : any = { } ,
229221 method : HttpMethod = 'post' ,
230222 options : RequestOptions = defaultOptions ,
231- responseType : 'json' | 'text' = 'json' ,
232223 ) : Observable < unknown > {
233224 if ( method === 'post' || method === 'put' ) {
234225 return this . getObservableWithBody (
235226 this . getFullUrl ( endpoint , options ) ,
236227 options . csrf ? this . appendCsrf ( data ) : data ,
237228 method ,
238229 options ,
239- responseType ,
240230 ) ;
241231 }
242232 return this . getObservableWithNoBody (
243233 this . getFullUrl ( endpoint , options ) ,
244234 method ,
245235 options ,
246- responseType ,
247236 ) ;
248237 }
249238
@@ -253,19 +242,12 @@ export class HttpV2Service {
253242 method : HttpMethod = 'post' ,
254243 responseClass ?: new ( data : any ) => T ,
255244 options : RequestOptions = defaultOptions ,
256- responseType : 'json' | 'text' = 'json' , // NEW: response type parameter
257245 ) : Observable < T [ ] > {
258- const observable = this . getObservable (
259- endpoint ,
260- data ,
261- method ,
262- options ,
263- responseType ,
264- ) ;
246+ const observable = this . getObservable ( endpoint , data , method , options ) ;
265247
266248 return observable . pipe (
267249 map ( ( response : Object | Array < Object > ) => {
268- if ( responseType === 'text' ) {
250+ if ( options . responseType === 'text' ) {
269251 return [ response as unknown as T ] ;
270252 }
271253
0 commit comments