Skip to content

Commit 8dece3d

Browse files
committed
PER-9985
Move the response type to the request options in the http-v2 service
1 parent 4b08988 commit 8dece3d

File tree

3 files changed

+30
-44
lines changed

3 files changed

+30
-44
lines changed

src/app/shared/services/api/idpuser.repo.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ export class IdPuser extends BaseRepo {
1818
'/v2/idpuser/send-enable-code',
1919
{ method, value },
2020
undefined,
21-
{},
22-
'text',
21+
{
22+
responseType: 'text',
23+
},
2324
),
2425
);
2526
}
@@ -34,8 +35,9 @@ export class IdPuser extends BaseRepo {
3435
code,
3536
},
3637
undefined,
37-
{},
38-
'text',
38+
{
39+
responseType: 'text',
40+
},
3941
),
4042
);
4143
}
@@ -46,8 +48,7 @@ export class IdPuser extends BaseRepo {
4648
'/v2/idpuser/send-disable-code',
4749
{ methodId },
4850
undefined,
49-
{},
50-
'text',
51+
{ responseType: 'text' },
5152
),
5253
);
5354
}
@@ -58,8 +59,9 @@ export class IdPuser extends BaseRepo {
5859
'/v2/idpuser/disable-two-factor',
5960
{ code, methodId },
6061
undefined,
61-
{},
62-
'text',
62+
{
63+
responseType: 'text',
64+
},
6365
),
6466
);
6567
}

src/app/shared/services/http-v2/http-v2.service.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ describe('HttpV2Service', () => {
287287

288288
it('should correctly handle responseType: text', (done) => {
289289
service
290-
.post('/api/v2/health', {}, undefined, {}, 'text')
290+
.post('/api/v2/health', {}, undefined, {
291+
responseType: 'text',
292+
})
291293
.toPromise()
292294
.then((response) => {
293295
expect(typeof response[0]).toBe('string');

src/app/shared/services/http-v2/http-v2.service.ts

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ const AUTH_KEY = 'AUTH_TOKEN';
1313

1414
type HttpMethod = 'post' | 'get' | 'put' | 'delete';
1515
type ResponseClass<T> = new (data: any) => T;
16+
type ResponseType = 'json' | 'text';
1617

1718
interface RequestOptions {
1819
csrf?: boolean;
1920
authToken?: boolean;
2021
useStelaDomain?: boolean;
22+
responseType?: ResponseType;
2123
}
2224

2325
const defaultOptions: RequestOptions = {
2426
csrf: false,
2527
authToken: true,
2628
useStelaDomain: true,
29+
responseType: 'json',
2730
};
2831

2932
export 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

Comments
 (0)