diff --git a/lib/request.ts b/lib/request.ts index 91640e0..2e5fc87 100644 --- a/lib/request.ts +++ b/lib/request.ts @@ -54,6 +54,7 @@ export default class Request extends Base { response: Response raw: any errored: Error + transport: TransportOptions aborted = false timedout = false @@ -65,11 +66,6 @@ export default class Request extends Base { private _uploadedBytes: number = null private _downloadedBytes: number = null - // Properties populated by the request mechanism. - private _open: OpenHandler - private _abort: AbortHandler - private _use: Middleware[] - private _promise: Promise private _before: Function[] = [] @@ -91,12 +87,10 @@ export default class Request extends Base { process.nextTick(() => start(this).then(resolve, reject)) }) - if (options.transport) { - this._transport(options.transport) - } + this.transport = options.transport - // Automatically set up middleware functions. - this.use(options.use || this._use) + // Automatically `use` default middleware functions. + this.use(options.use || this.transport.use) this.always(removeListeners) } @@ -115,12 +109,6 @@ export default class Request extends Base { return err } - private _transport ({ open, abort, use }: TransportOptions) { - this._open = open - this._abort = abort - this._use = use - } - then (onFulfilled: (response?: Response) => any, onRejected?: (error?: PopsicleError) => any) { return this._promise.then(onFulfilled, onRejected) } @@ -176,8 +164,8 @@ export default class Request extends Base { emitProgress(this) this._progress = null - if (this._abort) { - this._abort(this) + if (this.transport.abort) { + this.transport.abort(this) } } @@ -286,7 +274,7 @@ function start (request: Request) { req.opened = true - return req._open(request) + return req.transport.open(request) }) .then(function (options: ResponseOptions) { if (request.errored) { diff --git a/popsicle.js b/popsicle.js index 5dba071..c9c27ac 100644 --- a/popsicle.js +++ b/popsicle.js @@ -325,10 +325,8 @@ var Request = (function (_super) { this._promise = new Promise(function (resolve, reject) { process.nextTick(function () { return start(_this).then(resolve, reject); }); }); - if (options.transport) { - this._transport(options.transport); - } - this.use(options.use || this._use); + this.transport = options.transport; + this.use(options.use || this.transport.use); this.always(removeListeners); } Request.prototype.use = function (fn) { @@ -343,12 +341,6 @@ var Request = (function (_super) { err.original = original; return err; }; - Request.prototype._transport = function (_a) { - var open = _a.open, abort = _a.abort, use = _a.use; - this._open = open; - this._abort = abort; - this._use = use; - }; Request.prototype.then = function (onFulfilled, onRejected) { return this._promise.then(onFulfilled, onRejected); }; @@ -391,8 +383,8 @@ var Request = (function (_super) { if (this.opened) { emitProgress(this); this._progress = null; - if (this._abort) { - this._abort(this); + if (this.transport.abort) { + this.transport.abort(this); } } return this; @@ -498,7 +490,7 @@ function start(request) { }, timeout); } req.opened = true; - return req._open(request); + return req.transport.open(request); }) .then(function (options) { if (request.errored) {