diff --git a/lib/request.ts b/lib/request.ts index e3b0820..a7f541d 100644 --- a/lib/request.ts +++ b/lib/request.ts @@ -3,7 +3,7 @@ import extend = require('xtend') import Promise = require('any-promise') import { compose } from 'throwback' import Base, { BaseOptions, Headers } from './base' -import Response, { ResponseOptions } from './response' +import Response, { ResponseOptions, ResponseJSON } from './response' import PopsicleError from './error' export interface DefaultsOptions extends BaseOptions { @@ -26,6 +26,7 @@ export interface RequestJSON { body: any timeout: number method: string + response: ResponseJSON } export interface TransportOptions { @@ -42,16 +43,17 @@ export default class Request extends Base implements Promise { timeout: number body: any transport: TransportOptions + response: Response middleware: Middleware[] = [] opened = false aborted = false uploaded = 0 downloaded = 0 - uploadedBytes: number = null - downloadedBytes: number = null - uploadLength: number = null - downloadLength: number = null + uploadedBytes: number + downloadedBytes: number + uploadLength: number + downloadLength: number _raw: any _progress: ProgressFunction[] = [] @@ -68,11 +70,18 @@ export default class Request extends Base implements Promise { this.body = options.body // Internal promise representation. - const promised = new Promise((resolve, reject) => { + const $promise = new Promise((resolve, reject) => { this._resolve = resolve this._reject = reject }) + // Extend to avoid mutations of the transport object. + this.transport = extend(options.transport) + + // Automatically `use` default middleware functions. + this.use(options.use || this.transport.use) + this.progress(options.progress) + // External promise representation, resolves _after_ middleware has been // attached by relying on promises always resolving on the "next tick". this._promise = Promise.resolve() @@ -81,18 +90,11 @@ export default class Request extends Base implements Promise { const cb = () => { this._handle() - return promised + return $promise } return run(this, cb) }) - - // Extend to avoid mutations of the transport object. - this.transport = extend(options.transport) - - // Automatically `use` default middleware functions. - this.use(options.use || this.transport.use) - this.progress(options.progress) } error (message: string, code: string, original?: Error): PopsicleError { @@ -129,10 +131,11 @@ export default class Request extends Base implements Promise { toJSON (): RequestJSON { return { url: this.url, + method: this.method, headers: this.headers, body: this.body, timeout: this.timeout, - method: this.method + response: this.response ? this.response.toJSON() : null } } @@ -222,7 +225,10 @@ export default class Request extends Base implements Promise { // Proxy the transport promise into the current request. return this.transport.open(this) .then( - res => this._resolve(res), + response => { + this.response = response + this._resolve(response) + }, err => this._reject(err) ) } diff --git a/lib/response.ts b/lib/response.ts index 8dc7e4e..c0c95a8 100644 --- a/lib/response.ts +++ b/lib/response.ts @@ -1,4 +1,4 @@ -import Base, { BaseOptions, Headers, RawHeaders } from './base' +import Base, { BaseOptions, Headers } from './base' import Request from './request' import PopsicleError from './error' @@ -10,7 +10,6 @@ export interface ResponseOptions extends BaseOptions { export interface ResponseJSON { headers: Headers - rawHeaders: RawHeaders body: any url: string status: number @@ -38,7 +37,6 @@ export default class Response extends Base { return { url: this.url, headers: this.headers, - rawHeaders: this.rawHeaders, body: this.body, status: this.status, statusText: this.statusText diff --git a/lib/test/index.ts b/lib/test/index.ts index 3cc58be..d09729d 100644 --- a/lib/test/index.ts +++ b/lib/test/index.ts @@ -69,11 +69,12 @@ test('create a popsicle#Request instance', function (t) { test('use the same response in promise chains', function (t) { const req = popsicle.get(REMOTE_URL + '/echo') - t.plan(13) + t.plan(14) return req .then(function (res) { t.ok(res instanceof popsicle.Response) + t.equal(req.response, res) // Not all browsers support `responseURL`. t.ok(typeof res.url === 'string' || res.url == null) @@ -88,8 +89,8 @@ test('use the same response in promise chains', function (t) { t.equal(typeof res.statusType, 'function') t.equal(typeof res.toJSON, 'function') - t.deepEqual(Object.keys(req.toJSON()), ['url', 'headers', 'body', 'timeout', 'method']) - t.deepEqual(Object.keys(res.toJSON()), ['url', 'headers', 'rawHeaders', 'body', 'status', 'statusText']) + t.deepEqual(Object.keys(req.toJSON()), ['url', 'method', 'headers', 'body', 'timeout', 'response']) + t.deepEqual(Object.keys(res.toJSON()), ['url', 'headers', 'body', 'status', 'statusText']) return req .then(function (res2) {