Skip to content

Commit

Permalink
Reference the response on the request instance
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Sep 20, 2016
1 parent 9ce0e89 commit f577a21
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
38 changes: 22 additions & 16 deletions lib/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,6 +26,7 @@ export interface RequestJSON {
body: any
timeout: number
method: string
response: ResponseJSON
}

export interface TransportOptions {
Expand All @@ -42,16 +43,17 @@ export default class Request extends Base implements Promise<Response> {
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[] = []
Expand All @@ -68,11 +70,18 @@ export default class Request extends Base implements Promise<Response> {
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()
Expand All @@ -81,18 +90,11 @@ export default class Request extends Base implements Promise<Response> {

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 {
Expand Down Expand Up @@ -129,10 +131,11 @@ export default class Request extends Base implements Promise<Response> {
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
}
}

Expand Down Expand Up @@ -222,7 +225,10 @@ export default class Request extends Base implements Promise<Response> {
// 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)
)
}
Expand Down
4 changes: 1 addition & 3 deletions lib/response.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -10,7 +10,6 @@ export interface ResponseOptions extends BaseOptions {

export interface ResponseJSON {
headers: Headers
rawHeaders: RawHeaders
body: any
url: string
status: number
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit f577a21

Please sign in to comment.