@@ -20,62 +20,76 @@ import {
2020export class HttpClient {
2121 private config : HttpClientConfig ;
2222 private interceptors : Interceptors ;
23-
24- constructor ( config : HttpClientConfig = { } ) {
23+ private initValues : HttpClientConfig = {
24+ baseURL : '' ,
25+ headers : { } ,
26+ params : { } ,
27+ timeout : 30000 ,
28+ interceptors : {
29+ request : [ ] ,
30+ response : [ ] ,
31+ } ,
32+ } ;
33+
34+ constructor ( config : HttpClientConfig = this . initValues ) {
2535 this . config = config ;
2636 this . interceptors = {
2737 request : config . interceptors ?. request || [ ] ,
2838 response : config . interceptors ?. response || [ ] ,
2939 } ;
3040 }
3141
32- setBaseURL ( baseURL : string ) : HttpClient {
42+ setBaseURL ( baseURL : string ) {
3343 this . config . baseURL = baseURL ;
3444 return this ;
3545 }
3646
37- setHeaders ( headers : HttpHeaders ) : HttpClient {
47+ setHeaders ( headers : HttpHeaders ) {
3848 this . config . headers = mergeHeaders ( this . config . headers , headers ) ;
3949 return this ;
4050 }
4151
42- setHeader ( key : keyof HttpHeaders , value : string ) : HttpClient {
52+ setHeader ( key : keyof HttpHeaders , value : string ) {
4353 if ( ! this . config . headers ) {
4454 this . config . headers = { } ;
4555 }
4656 this . config . headers [ key ] = value ;
4757 return this ;
4858 }
4959
50- setParams ( params : QueryParams ) : HttpClient {
60+ setParams ( params : QueryParams ) {
5161 this . config . params = mergeParams ( this . config . params , params ) ;
5262 return this ;
5363 }
5464
55- setParam ( key : string , value : string | number | boolean ) : HttpClient {
65+ setParam ( key : string , value : string | number | boolean ) {
5666 if ( ! this . config . params ) {
5767 this . config . params = { } ;
5868 }
5969 this . config . params [ key ] = value ;
6070 return this ;
6171 }
6272
63- setTimeout ( timeout : number ) : HttpClient {
73+ setTimeout ( timeout : number ) {
6474 this . config . timeout = timeout ;
6575 return this ;
6676 }
6777
68- addRequestInterceptor ( interceptor : RequestInterceptor ) : HttpClient {
78+ get getConfig ( ) {
79+ return this . config ;
80+ }
81+
82+ addRequestInterceptor ( interceptor : RequestInterceptor ) {
6983 this . interceptors . request . push ( interceptor ) ;
7084 return this ;
7185 }
7286
73- addResponseInterceptor ( interceptor : ResponseInterceptor ) : HttpClient {
87+ addResponseInterceptor ( interceptor : ResponseInterceptor ) {
7488 this . interceptors . response . push ( interceptor ) ;
7589 return this ;
7690 }
7791
78- private async applyRequestInterceptors ( config : RequestConfig ) : Promise < RequestConfig > {
92+ private async applyRequestInterceptors ( config : RequestConfig ) {
7993 let processedConfig = config ;
8094
8195 for ( const interceptor of this . interceptors . request ) {
@@ -85,7 +99,7 @@ export class HttpClient {
8599 return processedConfig ;
86100 }
87101
88- private async applyResponseInterceptors < T > ( response : HttpResponse < T > ) : Promise < HttpResponse < T > > {
102+ private async applyResponseInterceptors < T > ( response : HttpResponse < T > ) {
89103 let processedResponse = response ;
90104
91105 for ( const interceptor of this . interceptors . response ) {
@@ -95,13 +109,13 @@ export class HttpClient {
95109 return processedResponse ;
96110 }
97111
98- private async makeRequest < T = any > ( config : RequestConfig ) : Promise < HttpResponse < T > > {
112+ private async makeRequest < T = any > ( config : RequestConfig ) {
99113 try {
100114 const mergedConfig : RequestConfig = {
101115 ...config ,
102116 headers : mergeHeaders ( this . config . headers , config . headers ) ,
103117 params : mergeParams ( this . config . params , config . params ) ,
104- timeout : config . timeout || this . config . timeout || 30000 ,
118+ timeout : config . timeout || this . config . timeout || this . initValues . timeout ,
105119 } ;
106120
107121 const processedConfig = await this . applyRequestInterceptors ( mergedConfig ) ;
@@ -167,22 +181,15 @@ export class HttpClient {
167181 }
168182 }
169183
170- async get < T = any > (
171- url : string ,
172- config ?: Partial < Omit < RequestConfig , 'method' > > ,
173- ) : Promise < HttpResponse < T > > {
184+ async get < T = any > ( url : string , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
174185 return this . makeRequest < T > ( {
175186 url,
176187 method : 'GET' ,
177188 ...config ,
178189 } ) ;
179190 }
180191
181- async post < T = any > (
182- url : string ,
183- body ?: any ,
184- config ?: Partial < Omit < RequestConfig , 'method' > > ,
185- ) : Promise < HttpResponse < T > > {
192+ async post < T = any > ( url : string , body ?: any , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
186193 return this . makeRequest < T > ( {
187194 url,
188195 method : 'POST' ,
@@ -191,11 +198,7 @@ export class HttpClient {
191198 } ) ;
192199 }
193200
194- async put < T = any > (
195- url : string ,
196- body ?: any ,
197- config ?: Partial < Omit < RequestConfig , 'method' > > ,
198- ) : Promise < HttpResponse < T > > {
201+ async put < T = any > ( url : string , body ?: any , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
199202 return this . makeRequest < T > ( {
200203 url,
201204 method : 'PUT' ,
@@ -204,22 +207,15 @@ export class HttpClient {
204207 } ) ;
205208 }
206209
207- async delete < T = any > (
208- url : string ,
209- config ?: Partial < Omit < RequestConfig , 'method' > > ,
210- ) : Promise < HttpResponse < T > > {
210+ async delete < T = any > ( url : string , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
211211 return this . makeRequest < T > ( {
212212 url,
213213 method : 'DELETE' ,
214214 ...config ,
215215 } ) ;
216216 }
217217
218- async patch < T = any > (
219- url : string ,
220- body ?: any ,
221- config ?: Partial < Omit < RequestConfig , 'method' > > ,
222- ) : Promise < HttpResponse < T > > {
218+ async patch < T = any > ( url : string , body ?: any , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
223219 return this . makeRequest < T > ( {
224220 url,
225221 method : 'PATCH' ,
@@ -228,21 +224,15 @@ export class HttpClient {
228224 } ) ;
229225 }
230226
231- async head < T = any > (
232- url : string ,
233- config ?: Partial < Omit < RequestConfig , 'method' > > ,
234- ) : Promise < HttpResponse < T > > {
227+ async head < T = any > ( url : string , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
235228 return this . makeRequest < T > ( {
236229 url,
237230 method : 'HEAD' ,
238231 ...config ,
239232 } ) ;
240233 }
241234
242- async options < T = any > (
243- url : string ,
244- config ?: Partial < Omit < RequestConfig , 'method' > > ,
245- ) : Promise < HttpResponse < T > > {
235+ async options < T = any > ( url : string , config ?: Partial < Omit < RequestConfig , 'method' > > ) {
246236 return this . makeRequest < T > ( {
247237 url,
248238 method : 'OPTIONS' ,
@@ -253,8 +243,4 @@ export class HttpClient {
253243 async request < T = any > ( config : RequestConfig ) : Promise < HttpResponse < T > > {
254244 return this . makeRequest < T > ( config ) ;
255245 }
256-
257- getConfig ( ) : HttpClientConfig {
258- return this . config ;
259- }
260246}
0 commit comments