WIP: initial prototype

- Added options parameter in API methods
- Updated typings
This commit is contained in:
delvedor
2018-12-12 16:47:29 +01:00
parent 7c1b58d703
commit b91b1ad1de
4 changed files with 413 additions and 391 deletions

6
lib/Connection.d.ts vendored
View File

@ -14,6 +14,10 @@ interface ConnectionOptions {
roles?: any;
}
interface RequestOptions extends http.ClientRequestArgs {
asStream?: boolean;
}
export interface AgentOptions {
keepAlive: boolean;
keepAliveMsecs: number;
@ -46,7 +50,7 @@ export default class Connection {
_status: string;
_agent: http.Agent;
constructor(opts?: ConnectionOptions);
request(params: http.ClientRequestArgs, callback: (err: Error | null, response: http.IncomingMessage | null) => void): http.ClientRequest;
request(params: RequestOptions, callback: (err: Error | null, response: http.IncomingMessage | null) => void): http.ClientRequest;
close(): Connection;
setRole(role: string, enabled: boolean): Connection;
status: string;

9
lib/Transport.d.ts vendored
View File

@ -38,6 +38,13 @@ export interface SniffMeta {
reason: string;
}
export interface RequestOptions {
ignore?: number | number[];
requestTimeout?: number | string;
maxRetries?: number;
asStream?: boolean;
}
export default class Transport {
static sniffReasons: {
SNIFF_ON_START: string;
@ -58,7 +65,7 @@ export default class Transport {
_nextSniff: number;
_isSniffing: boolean;
constructor(opts: TransportOptions);
request(params: any, callback: (err: Error | null, result: ApiResponse) => void): any;
request(params: any, options: RequestOptions, callback: (err: Error | null, result: ApiResponse) => void): any;
getConnection(): Connection | null;
sniff(callback?: (...args: any[]) => void): void;
}

View File

@ -33,8 +33,13 @@ class Transport {
}
}
request (params, callback) {
request (params, options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
callback = once(callback)
// TODO: return in the result the metadata
const meta = {
connection: null,
request: null,
@ -48,7 +53,7 @@ class Transport {
headers: null,
warnings: null
}
const maxRetries = params.maxRetries || this.maxRetries
const maxRetries = options.maxRetries || this.maxRetries
var request = { abort: noop }
const makeRequest = () => {
@ -96,11 +101,12 @@ class Transport {
// serializes the querystring
params.querystring = this.serializer.qserialize(params.querystring)
// handles request timeout
params.timeout = toMs(params.requestTimeout || this.requestTimeout)
params.timeout = toMs(options.requestTimeout || this.requestTimeout)
meta.request = params
this.emit('request', null, meta)
if (options.asStream === true) params.asStream = true
// perform the actual http request
return meta.connection.request(params, onResponse)
}
@ -139,7 +145,7 @@ class Transport {
result.warnings = headers['warning'].split(/(?!\B"[^"]*),(?![^"]*"\B)/)
}
if (params.asStream === true) {
if (options.asStream === true) {
result.body = response
meta.response = result
this.emit('response', null, meta)
@ -180,7 +186,7 @@ class Transport {
// we should ignore the statusCode if the user has configured the `ignore` field with
// the statusCode we just got or if the request method is HEAD and the statusCode is 404
const ignoreStatusCode = (Array.isArray(params.ignore) && params.ignore.indexOf(statusCode) > -1) ||
const ignoreStatusCode = (Array.isArray(options.ignore) && options.ignore.indexOf(statusCode) > -1) ||
(isHead === true && statusCode === 404)
if (ignoreStatusCode === false &&