@@ -14,11 +14,13 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
1414 private _routes : HttpRoutes ;
1515 private _timeout : number ;
1616 private _customRoutes : HttpRoutes ;
17+ private _defaultHeaders : JSONObject ;
1718
1819 /**
1920 * @param host Kuzzle server hostname or IP
2021 * @param options Http connection options
2122 * - `customRoutes` Add custom routes
23+ * - `headers` Default headers sent with each HTTP request (default: `{}`)
2224 * - `port` Kuzzle server port (default: `7512`)
2325 * - `ssl` Use SSL to connect to Kuzzle server. Default `false` unless port is 443 or 7443.
2426 * - `timeout` Connection timeout in milliseconds (default: `0`)
@@ -33,7 +35,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
3335 sslConnection ?: boolean ;
3436 ssl ?: boolean ;
3537 customRoutes ?: HttpRoutes ;
36- timeout ?: number
38+ timeout ?: number ,
39+ headers ?: JSONObject ,
3740 } = { }
3841 ) {
3942 super ( host , options , 'http' ) ;
@@ -48,6 +51,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
4851
4952 this . _customRoutes = options . customRoutes || { } ;
5053
54+ this . _defaultHeaders = options . headers || { } ;
55+
5156 for ( const controller of Object . keys ( this . _customRoutes ) ) {
5257 const definition = this . _customRoutes [ controller ] ;
5358
@@ -113,7 +118,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
113118 return Promise . resolve ( ) ;
114119 }
115120
116- return this . _sendHttpRequest ( { method : 'GET' , path : '/_publicApi' } )
121+ const publicApiRequest = {
122+ method : 'GET' ,
123+ path : '/_publicApi' ,
124+ payload : {
125+ headers : this . _defaultHeaders ,
126+ }
127+ } ;
128+ return this . _sendHttpRequest ( publicApiRequest )
117129 . then ( ( { result, error } ) => {
118130 if ( ! error ) {
119131 this . _routes = this . _constructRoutes ( result ) ;
@@ -133,7 +145,14 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
133145 } else if ( error . status === 404 ) {
134146 // fallback to server:info route
135147 // server:publicApi is only available since Kuzzle 1.9.0
136- return this . _sendHttpRequest ( { method : 'GET' , path : '/' } )
148+ const serverInfoRequest = {
149+ method : 'GET' ,
150+ path : '/' ,
151+ payload : {
152+ headers : this . _defaultHeaders ,
153+ }
154+ } ;
155+ return this . _sendHttpRequest ( serverInfoRequest )
137156 . then ( ( { result : res , error : err } ) => {
138157 if ( ! err ) {
139158 this . _routes = this . _constructRoutes ( res . serverInfo . kuzzle . api . routes ) ;
@@ -207,7 +226,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
207226 collection : undefined ,
208227 controller : undefined ,
209228 headers : {
210- 'Content-Type' : 'application/json'
229+ 'Content-Type' : 'application/json' ,
230+ ...this . _defaultHeaders ,
211231 } ,
212232 index : undefined ,
213233 meta : undefined ,
@@ -311,11 +331,15 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
311331 if ( formattedRequest ) {
312332 this . _sendHttpRequest ( formattedRequest )
313333 . then ( response => this . emit ( formattedRequest . payload . requestId , response ) )
314- . catch ( error => this . emit ( formattedRequest . payload . requestId , { error} ) ) ;
334+ . catch ( error => this . emit ( formattedRequest . payload . requestId , { error } ) ) ;
315335 }
316336 }
317337
318- _sendHttpRequest ( { method, path, payload = { headers : undefined , body :undefined } } ) {
338+ _sendHttpRequest ( { method, path, payload } : {
339+ method : string ,
340+ path : string ,
341+ payload : JSONObject
342+ } ) {
319343 if ( typeof XMLHttpRequest === 'undefined' ) {
320344 // NodeJS implementation, using http.request:
321345
@@ -326,12 +350,12 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
326350 path = `/${ path } ` ;
327351 }
328352 const url = `${ this . protocol } ://${ this . host } :${ this . port } ${ path } ` ;
329- const headers = payload . headers || { } ;
330- headers [ 'Content-Length' ] = Buffer . byteLength ( payload . body || '' ) ;
353+ const headers = payload && payload . headers || { } ;
354+ headers [ 'Content-Length' ] = Buffer . byteLength ( payload && payload . body || '' ) ;
331355
332356 return httpClient . request ( url , method , {
333- headers ,
334- body : payload . body ,
357+ body : payload && payload . body ,
358+ headers : headers ,
335359 timeout : this . _timeout
336360 } )
337361 . then ( response => {
@@ -362,8 +386,8 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
362386 // Authorize the reception of cookies
363387 xhr . withCredentials = this . cookieSupport ;
364388
365- for ( const header of Object . keys ( payload . headers || { } ) ) {
366- xhr . setRequestHeader ( header , payload . headers [ header ] ) ;
389+ for ( const [ header , value ] of Object . entries ( payload && payload . headers || { } ) ) {
390+ xhr . setRequestHeader ( header , value as string ) ;
367391 }
368392
369393 xhr . onload = ( ) => {
@@ -376,7 +400,7 @@ export default class HttpProtocol extends KuzzleAbstractProtocol {
376400 }
377401 } ;
378402
379- xhr . send ( payload . body ) ;
403+ xhr . send ( payload && payload . body ) ;
380404 } ) ;
381405 }
382406
0 commit comments