FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge branch 'master' into master · developit/unfetch@12cad0e · GitHub

Repository navigation

Commit 12cad0e

Browse files
authored
Merge branch 'master' into master
2 parents da85a92 + f0a1401 commit 12cad0e

6 files changed

Lines changed: 63 additions & 38 deletions

File tree

‎README.md‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,45 @@ var fetch = require('unfetch')
5454
The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com):
5555

5656
```html
57-
<script src="https://unpkg.com/unfetch/dist/unfetch.umd.js"></script>
57+
<script src="//unpkg.com/unfetch/dist/unfetch.umd.js"></script>
5858
```
5959

60-
This exposes a `fetch` global if not already present.
60+
This exposes the `unfetch()` function as a global.
6161

6262
* * *
6363

6464
## Usage
6565

66+
As a [**ponyfill**](https://ponyfill.com):
67+
6668
```js
67-
import fetch from 'unfetch'
69+
import fetch from 'unfetch';
70+
71+
fetch('/foo.json')
72+
.then( r => r.json() )
73+
.then( data => {
74+
console.log(data);
75+
});
76+
```
77+
78+
Globally, as a [**polyfill**](https://ponyfill.com/#polyfill):
79+
80+
```js
81+
import 'unfetch/polyfill';
82+
83+
// "fetch" is now installed globally if it wasn't already available
6884

6985
fetch('/foo.json')
7086
.then( r => r.json() )
7187
.then( data => {
72-
console.log(data)
73-
})
88+
console.log(data);
89+
});
7490
```
7591

7692
## Examples & Demos
7793

94+
[**Real Example on JSFiddle**](https://jsfiddle.net/developit/qrh7tLc0/) ➡️
95+
7896
```js
7997
// simple GET request:
8098
fetch('/foo')

‎package.json‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
{
22
"name": "unfetch",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Bare minimum 500b fetch polyfill.",
5+
"module": "src/index.js",
56
"jsnext:main": "src/index.js",
67
"main": "dist/unfetch.js",
78
"umd:main": "dist/unfetch.umd.js",
89
"scripts": {
910
"test": "eslint src test && mocha --compilers js:babel-register test/**/*.js",
1011
"build": "npm-run-all clean -p rollup:* -p minify:* -s size",
1112
"clean": "rimraf dist && mkdirp dist",
12-
"rollup:cjs": "rollup -c rollup.config.js -m -f cjs -n $npm_package_name $npm_package_jsnext_main -o $npm_package_main",
13-
"rollup:umd": "rollup -c rollup.config.js -m -f umd -n $npm_package_name $npm_package_jsnext_main -o $npm_package_umd_main",
13+
"rollup:cjs": "rollup -c rollup.config.js -m -f cjs -n $npm_package_name $npm_package_module -o $npm_package_main",
14+
"rollup:umd": "rollup -c rollup.config.js -m -f umd -n $npm_package_name $npm_package_module -o $npm_package_umd_main",
1415
"minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map",
1516
"minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map",
1617
"size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size)\"",
18+
"prepublish": "npm run -s build",
1719
"release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"
1820
},
1921
"repository": "developit/unfetch",
@@ -30,7 +32,8 @@
3032
"license": "MIT",
3133
"files": [
3234
"src",
33-
"dist"
35+
"dist",
36+
"polyfill.js"
3437
],
3538
"eslintConfig": {
3639
"parser": "babel-eslint",
@@ -60,6 +63,7 @@
6063
"rimraf": "^2.5.2",
6164
"rollup": "^0.41.4",
6265
"rollup-plugin-buble": "^0.15.0",
66+
"rollup-plugin-post-replace": "^1.0.0",
6367
"sinon": "^1.17.4",
6468
"sinon-chai": "^2.8.0",
6569
"strip-json-comments-cli": "^1.0.1",

‎polyfill.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
if (!window.fetch) window.fetch = require('.');

‎rollup.config.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import buble from 'rollup-plugin-buble';
2+
import replace from 'rollup-plugin-post-replace';
23

34
export default {
45
useStrict: false,
56
plugins: [
6-
buble()
7+
buble(),
8+
replace({
9+
'module.exports = index;': '',
10+
'var index =': 'module.exports ='
11+
})
712
]
813
};

‎src/index.js‎

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function fetch(url, options) {
1+
export default typeof fetch=='function' ? fetch : function(url, options) {
22
options = options || {};
33
return new Promise( (resolve, reject) => {
44
let request = new XMLHttpRequest();
@@ -14,44 +14,41 @@ export default function fetch(url, options) {
1414
}
1515

1616
request.onload = () => {
17-
resolve(response(request));
17+
resolve(response());
1818
};
1919

20-
request.onerror = () => {
21-
reject(Error('Network Error'));
22-
};
20+
request.onerror = reject;
2321

24-
request.send(options.body || null);
22+
request.send(options.body);
2523

26-
function response(xhr) {
27-
let headerText = xhr.getAllResponseHeaders(),
28-
keys = [],
24+
function response() {
25+
let keys = [],
2926
all = [],
3027
headers = {},
31-
reg = /^\s*(.*?)\s*\:\s*([\s\S]*?)\s*$/gm,
32-
match, key;
33-
while ((match=reg.exec(headerText))) {
34-
keys.push(key = match[1].toLowerCase());
35-
all.push([key, match[2]]);
36-
headers[key] = (headers[key]?(headers[key]+','):'') + match[2];
37-
}
28+
header;
29+
30+
request.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm, (m, key, value) => {
31+
keys.push(key = key.toLowerCase());
32+
all.push([key, value]);
33+
header = headers[key];
34+
headers[key] = header ? `${header},${value}` : value;
35+
});
3836

3937
return {
40-
type: 'cors',
41-
ok: xhr.status/200|0 == 1, // 200-399
42-
status: xhr.status,
43-
statusText: xhr.statusText,
44-
url: xhr.responseURL,
45-
clone: () => response(xhr),
46-
text: () => Promise.resolve(xhr.responseText),
47-
json: () => Promise.resolve(xhr.responseText).then(JSON.parse),
48-
xml: () => Promise.resolve(xhr.responseXML),
49-
blob: () => Promise.resolve(xhr.response),
38+
ok: (request.status/200|0) == 1, // 200-399
39+
status: request.status,
40+
statusText: request.statusText,
41+
url: request.responseURL,
42+
clone: response,
43+
text: () => Promise.resolve(request.responseText),
44+
json: () => Promise.resolve(request.responseText).then(JSON.parse),
45+
xml: () => Promise.resolve(request.responseXML),
46+
blob: () => Promise.resolve(new Blob([request.response])),
5047
headers: {
5148
keys: () => keys,
5249
entries: () => all,
5350
get: n => headers[n.toLowerCase()],
54-
has: n => !!headers[n.toLowerCase()]
51+
has: n => n.toLowerCase() in headers
5552
}
5653
};
5754
}

‎test/index.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('unfetch', () => {
4444

4545
expect(xhr.setRequestHeader).to.have.been.calledOnce.and.calledWith('a', 'b');
4646
expect(xhr.open).to.have.been.calledOnce.and.calledWith('get', '/foo');
47-
expect(xhr.send).to.have.been.calledOnce.and.calledWith(null);
47+
expect(xhr.send).to.have.been.calledOnce.and.calledWith();
4848

4949
delete global.XMLHttpRequest;
5050
});

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL