@@ -24,21 +24,26 @@ export class HttpClient {
2424 // Server PORT
2525 public port : number | string = process . env . PORT ;
2626
27+ // Global headers
28+ public headers ?: Object ;
29+
2730 /**
2831 * Send a POST http request to the server
2932 * @param path Path to send to (e.g. /users)
3033 * @param data Data to send as query
3134 * @param bearer Bearer token to send
3235 * @param expect Array of status codes to expect
36+ * @param customOptions Additional request options
3337 * @return Returns a promise of HttpClientResponse
3438 */
3539 public post (
3640 path : string ,
3741 data : object ,
3842 bearer : boolean | string = false ,
39- expect : number [ ] = [ 200 , 201 , 202 , 204 ]
43+ expect : number [ ] = [ 200 , 201 , 202 , 204 ] ,
44+ customOptions ?: object
4045 ) : Promise < HttpClientResponse > {
41- const options = {
46+ let options = {
4247 method : 'POST' ,
4348 url : `${ this . url } :${ this . port } ${ path } ` ,
4449 body : data ,
@@ -48,6 +53,14 @@ export class HttpClient {
4853 if ( bearer ) {
4954 options [ 'auth' ] = { bearer : `${ bearer } ` } ;
5055 }
56+
57+ if ( this . headers ) {
58+ options [ 'headers' ] = this . headers ;
59+ }
60+
61+ if ( customOptions ) {
62+ options = Object . assign ( options , customOptions ) ;
63+ }
5164
5265 return new Promise < HttpClientResponse > ( ( accept , reject ) => {
5366 this . request ( options , ( error , response , body ) => {
@@ -75,16 +88,18 @@ export class HttpClient {
7588 * @param data Data to send as query
7689 * @param bearer Bearer token to send
7790 * @param expect Array of status codes to expect
91+ * @param customOptions Additional request options
7892 * @return Returns a promise of HttpClientResponse
7993 */
8094 public get (
8195 path : string ,
8296 data : boolean | Object = false ,
8397 bearer : boolean | string = false ,
84- expect : number [ ] = [ 200 , 202 ]
98+ expect : number [ ] = [ 200 , 202 ] ,
99+ customOptions ?: object
85100 ) : Promise < HttpClientResponse > {
86101 return new Promise < HttpClientResponse > ( ( accept , reject ) => {
87- const options = {
102+ let options = {
88103 method : 'GET' ,
89104 url : `${ this . url } :${ this . port } ${ path } ` ,
90105 json : true ,
@@ -94,6 +109,14 @@ export class HttpClient {
94109 if ( bearer ) {
95110 options [ 'auth' ] = { bearer : `${ bearer } ` } ;
96111 }
112+
113+ if ( this . headers ) {
114+ options [ 'headers' ] = this . headers ;
115+ }
116+
117+ if ( customOptions ) {
118+ options = Object . assign ( options , customOptions ) ;
119+ }
97120
98121 this . request ( options , ( error , response , body ) => {
99122 if ( error ) {
@@ -120,15 +143,17 @@ export class HttpClient {
120143 * @param data Data to send as query
121144 * @param bearer Bearer token to send
122145 * @param expect Array of status codes to expect
146+ * @param customOptions Additional request options
123147 * @return Returns a promise of HttpClientResponse
124148 */
125149 public patch (
126150 path : string ,
127151 data : object ,
128152 bearer : boolean | string = false ,
129- expect : number [ ] = [ 200 , 201 , 202 , 204 ]
153+ expect : number [ ] = [ 200 , 201 , 202 , 204 ] ,
154+ customOptions ?: object
130155 ) : Promise < HttpClientResponse > {
131- const options = {
156+ let options = {
132157 method : 'PATCH' ,
133158 url : `${ this . url } :${ this . port } ${ path } ` ,
134159 body : data ,
@@ -138,6 +163,14 @@ export class HttpClient {
138163 if ( bearer ) {
139164 options [ 'auth' ] = { bearer : `${ bearer } ` } ;
140165 }
166+
167+ if ( this . headers ) {
168+ options [ 'headers' ] = this . headers ;
169+ }
170+
171+ if ( customOptions ) {
172+ options = Object . assign ( options , customOptions ) ;
173+ }
141174
142175 return new Promise < HttpClientResponse > ( ( accept , reject ) => {
143176 this . request ( options , ( error , response , body ) => {
@@ -165,22 +198,32 @@ export class HttpClient {
165198 * @param data Data to send as query
166199 * @param bearer Bearer token to send
167200 * @param expect Array of status codes to expect
201+ * @param customOptions Additional request options
168202 * @return Returns a promise of HttpClientResponse
169203 */
170204 public delete (
171205 path : string ,
172206 bearer : boolean | string = false ,
173- expect : number [ ] = [ 200 , 202 , 204 ]
207+ expect : number [ ] = [ 200 , 202 , 204 ] ,
208+ customOptions ?: object
174209 ) : Promise < HttpClientResponse > {
175210 return new Promise < HttpClientResponse > ( ( accept , reject ) => {
176- const options = {
211+ let options = {
177212 method : 'DELETE' ,
178213 url : `${ this . url } :${ this . port } ${ path } `
179214 } ;
180215
181216 if ( bearer ) {
182217 options [ 'auth' ] = { bearer : `${ bearer } ` } ;
183218 }
219+
220+ if ( this . headers ) {
221+ options [ 'headers' ] = this . headers ;
222+ }
223+
224+ if ( customOptions ) {
225+ options = Object . assign ( options , customOptions ) ;
226+ }
184227
185228 this . request ( options , ( error , response , body ) => {
186229 if ( error ) {
0 commit comments