Handle headers as request option

This commit is contained in:
delvedor
2018-12-13 16:54:01 +01:00
parent 396686ee77
commit 3c667d38e6
2 changed files with 12 additions and 6 deletions

5
lib/Transport.d.ts vendored
View File

@ -38,11 +38,16 @@ export interface SniffMeta {
reason: string; reason: string;
} }
declare type anyObject = {
[key: string]: any;
};
export interface RequestOptions { export interface RequestOptions {
ignore?: number | number[]; ignore?: number | number[];
requestTimeout?: number | string; requestTimeout?: number | string;
maxRetries?: number; maxRetries?: number;
asStream?: boolean; asStream?: boolean;
headers?: anyObject;
} }
export default class Transport { export default class Transport {

View File

@ -74,7 +74,7 @@ class Transport {
return callback(new NoLivingConnectionsError('There are not living connections'), result) return callback(new NoLivingConnectionsError('There are not living connections'), result)
} }
params.headers = params.headers || {} const headers = options.headers || {}
// handle json body // handle json body
if (params.body != null) { if (params.body != null) {
if (shouldSerialize(params.body) === true) { if (shouldSerialize(params.body) === true) {
@ -84,9 +84,9 @@ class Transport {
return callback(err, result) return callback(err, result)
} }
} }
params.headers['Content-Type'] = 'application/json' headers['Content-Type'] = 'application/json'
if (isStream(params.body) === false) { if (isStream(params.body) === false) {
params.headers['Content-Length'] = '' + Buffer.byteLength(params.body) headers['Content-Length'] = '' + Buffer.byteLength(params.body)
} }
// handle ndjson body // handle ndjson body
} else if (params.bulkBody != null) { } else if (params.bulkBody != null) {
@ -99,16 +99,17 @@ class Transport {
} else { } else {
params.body = params.bulkBody params.body = params.bulkBody
} }
params.headers['Content-Type'] = 'application/x-ndjson' headers['Content-Type'] = 'application/x-ndjson'
if (isStream(params.body) === false) { if (isStream(params.body) === false) {
params.headers['Content-Length'] = '' + Buffer.byteLength(params.body) headers['Content-Length'] = '' + Buffer.byteLength(params.body)
} }
} }
if (this.suggestCompression === true) { if (this.suggestCompression === true) {
params.headers['Accept-Encoding'] = 'gzip,deflate' headers['Accept-Encoding'] = 'gzip,deflate'
} }
params.headers = headers
// serializes the querystring // serializes the querystring
params.querystring = this.serializer.qserialize(params.querystring) params.querystring = this.serializer.qserialize(params.querystring)
// handles request timeout // handles request timeout