Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into ie-fixes
Browse files Browse the repository at this point in the history
Conflicts:
	fetch.js
  • Loading branch information
dgraham committed Nov 13, 2014
2 parents d5a000d + c35893f commit ddf7a7d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 68 deletions.
39 changes: 27 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,33 @@ This can be also be installed with `npm`.
$ npm install github/fetch --save
```

### Using with npm and Browserify

```js
require('es6-promise').polyfill();
require('fetch');
```

Note: if you install **fetch** with npm you won't be able to specify a fuzzy
version or range of versions for **fetch** in your `package.json` as npm does
not support installing fuzzy versions from modules not published on their
registry.

Instead, you can choose to install **fetch** at an exact specific version by
changing `package.json` to:

```
"fetch": "https://github.com/github/fetch/archive/v0.1.0.tar.gz
```

Alternatively, if you would prefer to install **fetch** at a fuzzy version
you can install it via bower to install it and use the [debowerify transform](https://github.com/eugeneware/debowerify).

Full worked examples of these two approaches to using **fetch** in Browserify are avalable:

- [Fetch API + Browserify](https://github.com/matthew-andrews/fetch-browserify-demo)
- [Fetch API + Browserify + Bower](https://github.com/matthew-andrews/fetch-browserify-bower-demo)

## Usage

The `fetch` function supports any HTTP method. We'll focus on GET and POST
Expand Down Expand Up @@ -75,18 +102,6 @@ fetch('/query', {
})
```

### Post form fields

```javascript
fetch('/query', {
method: 'post',
body: {
name: 'Hubot',
login: 'hubot'
}
})
```

### Post JSON

```javascript
Expand Down
39 changes: 9 additions & 30 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
}

function Body() {
this.body = null
this._body = null
this.bodyUsed = false

this.arrayBuffer = function() {
Expand All @@ -78,11 +78,11 @@

this.blob = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(new Blob([this.body]))
return rejected ? rejected : Promise.resolve(new Blob([this._body]))
}

this.formData = function() {
return Promise.resolve(decode(this.body))
return Promise.resolve(decode(this._body))
}

this.json = function() {
Expand All @@ -91,7 +91,7 @@
return rejected
}

var body = this.body
var body = this._body
return new Promise(function(resolve, reject) {
try {
resolve(JSON.parse(body))
Expand All @@ -103,7 +103,7 @@

this.text = function() {
var rejected = consumed(this)
return rejected ? rejected : Promise.resolve(this.body)
return rejected ? rejected : Promise.resolve(this._body)
}

return this
Expand All @@ -120,23 +120,14 @@
function Request(url, options) {
options = options || {}
this.url = url
this.body = options.body
this._body = options.body
this.credentials = options.credentials || null
this.headers = new Headers(options.headers)
this.method = normalizeMethod(options.method || 'GET')
this.mode = options.mode || null
this.referrer = null
}

function encode(params) {
return Object.getOwnPropertyNames(params).filter(function(name) {
return params[name] !== undefined
}).map(function(name) {
var value = (params[name] === null) ? '' : params[name]
return encodeURIComponent(name) + '=' + encodeURIComponent(value)
}).join('&').replace(/%20/g, '+')
}

function decode(body) {
var form = new FormData()
body.trim().split('&').forEach(function(bytes) {
Expand All @@ -150,15 +141,6 @@
return form
}

function isObject(value) {
try {
return Object.getPrototypeOf(value) === Object.prototype
} catch (ex) {
// Probably a string literal.
return false
}
}

function headers(xhr) {
var head = new Headers()
var pairs = xhr.getAllResponseHeaders().trim().split('\n')
Expand Down Expand Up @@ -206,11 +188,8 @@
})
})

var body = self.body
if (isObject(self.body)) {
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
body = encode(self.body)
} else if (body === undefined) {
var body = self._body
if (body === undefined) {
body = null
}
xhr.send(body)
Expand All @@ -220,7 +199,7 @@
Body.call(Request.prototype)

function Response(body, options) {
this.body = body
this._body = body
this.type = 'default'
this.url = null
this.status = options.status
Expand Down
42 changes: 16 additions & 26 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ var blobSupport = (function() {
asyncTest('populates response body', 2, function() {
fetch('/hello').then(function(response) {
equal(response.status, 200)
equal(response.body, 'hi')
return response.text()
}).then(function(body) {
equal(body, 'hi')
start()
})
})
Expand All @@ -23,9 +25,10 @@ asyncTest('sends request headers', 2, function() {
'X-Test': '42'
}
}).then(function(response) {
var headers = JSON.parse(response.body).headers
equal(headers['accept'], 'application/json')
equal(headers['x-test'], '42')
return response.json()
}).then(function(json) {
equal(json.headers['accept'], 'application/json')
equal(json.headers['x-test'], '42')
start()
})
})
Expand All @@ -41,7 +44,9 @@ asyncTest('parses response headers', 2, function() {
asyncTest('resolves promise on 500 error', 2, function() {
fetch('/boom').then(function(response) {
equal(response.status, 500)
equal(response.body, 'boom')
return response.text()
}).then(function(body) {
equal(body, 'boom')
start()
})
})
Expand Down Expand Up @@ -114,30 +119,15 @@ if (blobSupport) {
})
}

asyncTest('post sends encoded body', 2, function() {
asyncTest('post sets content-type header', 2, function() {
fetch('/request', {
method: 'post',
body: {
name: 'Hubot',
title: 'Hubot Robawt',
undef: undefined,
nil: null
}
body: new FormData()
}).then(function(response) {
var request = JSON.parse(response.body);
equal(request.method, 'POST')
equal(request.data, 'name=Hubot&title=Hubot+Robawt&nil=')
start()
})
})

asyncTest('post sets content-type header', 1, function() {
fetch('/request', {
method: 'post',
body: {}
}).then(function(response) {
var request = JSON.parse(response.body);
equal(request.headers['content-type'], 'application/x-www-form-urlencoded; charset=UTF-8')
return response.json()
}).then(function(json) {
equal(json.method, 'POST')
ok(/^multipart\/form-data;/.test(json.headers['content-type']))
start()
})
})
Expand Down

0 comments on commit ddf7a7d

Please sign in to comment.