| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,27 +54,45 @@ var fetch = require('unfetch') | |||
| 54 | 54 | The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](https://unpkg.com): | |
| 55 | 55 | ||
| 56 | 56 | ```html | |
| 57 | - <script src="https://unpkg.com/unfetch/dist/unfetch.umd.js"></script> | ||
| 57 | + <script src="//unpkg.com/unfetch/dist/unfetch.umd.js"></script> | ||
| 58 | 58 | ``` | |
| 59 | 59 | ||
| 60 | - This exposes a `fetch` global if not already present. | ||
| 60 | + This exposes the `unfetch()` function as a global. | ||
| 61 | 61 | ||
| 62 | 62 | * * * | |
| 63 | 63 | ||
| 64 | 64 | ## Usage | |
| 65 | 65 | ||
| 66 | + As a [**ponyfill**](https://ponyfill.com): | ||
| 67 | + | ||
| 66 | 68 | ```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 | ||
| 68 | 84 | ||
| 69 | 85 | fetch('/foo.json') | |
| 70 | 86 | .then( r => r.json() ) | |
| 71 | 87 | .then( data => { | |
| 72 | - console.log(data) | ||
| 73 | - }) | ||
| 88 | + console.log(data); | ||
| 89 | + }); | ||
| 74 | 90 | ``` | |
| 75 | 91 | ||
| 76 | 92 | ## Examples & Demos | |
| 77 | 93 | ||
| 94 | + [**Real Example on JSFiddle**](https://jsfiddle.net/developit/qrh7tLc0/) ➡️ | ||
| 95 | + | ||
| 78 | 96 | ```js | |
| 79 | 97 | // simple GET request: | |
| 80 | 98 | fetch('/foo') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,19 +1,21 @@ | |||
| 1 | 1 | { | |
| 2 | 2 | "name": "unfetch", | |
| 3 | - "version": "1.0.0", | ||
| 3 | + "version": "1.0.1", | ||
| 4 | 4 | "description": "Bare minimum 500b fetch polyfill.", | |
| 5 | + "module": "src/index.js", | ||
| 5 | 6 | "jsnext:main": "src/index.js", | |
| 6 | 7 | "main": "dist/unfetch.js", | |
| 7 | 8 | "umd:main": "dist/unfetch.umd.js", | |
| 8 | 9 | "scripts": { | |
| 9 | 10 | "test": "eslint src test && mocha --compilers js:babel-register test/**/*.js", | |
| 10 | 11 | "build": "npm-run-all clean -p rollup:* -p minify:* -s size", | |
| 11 | 12 | "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", | ||
| 14 | 15 | "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", | |
| 15 | 16 | "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", | |
| 16 | 17 | "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size)\"", | |
| 18 | + "prepublish": "npm run -s build", | ||
| 17 | 19 | "release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | |
| 18 | 20 | }, | |
| 19 | 21 | "repository": "developit/unfetch", | |
@@ -30,7 +32,8 @@ | |||
| 30 | 32 | "license": "MIT", | |
| 31 | 33 | "files": [ | |
| 32 | 34 | "src", | |
| 33 | - "dist" | ||
| 35 | + "dist", | ||
| 36 | + "polyfill.js" | ||
| 34 | 37 | ], | |
| 35 | 38 | "eslintConfig": { | |
| 36 | 39 | "parser": "babel-eslint", | |
@@ -60,6 +63,7 @@ | |||
| 60 | 63 | "rimraf": "^2.5.2", | |
| 61 | 64 | "rollup": "^0.41.4", | |
| 62 | 65 | "rollup-plugin-buble": "^0.15.0", | |
| 66 | + "rollup-plugin-post-replace": "^1.0.0", | ||
| 63 | 67 | "sinon": "^1.17.4", | |
| 64 | 68 | "sinon-chai": "^2.8.0", | |
| 65 | 69 | "strip-json-comments-cli": "^1.0.1", | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + if (!window.fetch) window.fetch = require('.'); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,13 @@ | |||
| 1 | 1 | import buble from 'rollup-plugin-buble'; | |
| 2 | + import replace from 'rollup-plugin-post-replace'; | ||
| 2 | 3 | ||
| 3 | 4 | export default { | |
| 4 | 5 | useStrict: false, | |
| 5 | 6 | plugins: [ | |
| 6 | - buble() | ||
| 7 | + buble(), | ||
| 8 | + replace({ | ||
| 9 | + 'module.exports = index;': '', | ||
| 10 | + 'var index =': 'module.exports =' | ||
| 11 | + }) | ||
| 7 | 12 | ] | |
| 8 | 13 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,4 @@ | |||
| 1 | - export default function fetch(url, options) { | ||
| 1 | + export default typeof fetch=='function' ? fetch : function(url, options) { | ||
| 2 | 2 | options = options || {}; | |
| 3 | 3 | return new Promise( (resolve, reject) => { | |
| 4 | 4 | let request = new XMLHttpRequest(); | |
@@ -14,44 +14,41 @@ export default function fetch(url, options) { | |||
| 14 | 14 | } | |
| 15 | 15 | ||
| 16 | 16 | request.onload = () => { | |
| 17 | - resolve(response(request)); | ||
| 17 | + resolve(response()); | ||
| 18 | 18 | }; | |
| 19 | 19 | ||
| 20 | - request.onerror = () => { | ||
| 21 | - reject(Error('Network Error')); | ||
| 22 | - }; | ||
| 20 | + request.onerror = reject; | ||
| 23 | 21 | ||
| 24 | - request.send(options.body || null); | ||
| 22 | + request.send(options.body); | ||
| 25 | 23 | ||
| 26 | - function response(xhr) { | ||
| 27 | - let headerText = xhr.getAllResponseHeaders(), | ||
| 28 | - keys = [], | ||
| 24 | + function response() { | ||
| 25 | + let keys = [], | ||
| 29 | 26 | all = [], | |
| 30 | 27 | 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 | + }); | ||
| 38 | 36 | ||
| 39 | 37 | 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])), | ||
| 50 | 47 | headers: { | |
| 51 | 48 | keys: () => keys, | |
| 52 | 49 | entries: () => all, | |
| 53 | 50 | get: n => headers[n.toLowerCase()], | |
| 54 | - has: n => !!headers[n.toLowerCase()] | ||
| 51 | + has: n => n.toLowerCase() in headers | ||
| 55 | 52 | } | |
| 56 | 53 | }; | |
| 57 | 54 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -44,7 +44,7 @@ describe('unfetch', () => { | |||
| 44 | 44 | ||
| 45 | 45 | expect(xhr.setRequestHeader).to.have.been.calledOnce.and.calledWith('a', 'b'); | |
| 46 | 46 | 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(); | ||
| 48 | 48 | ||
| 49 | 49 | delete global.XMLHttpRequest; | |
| 50 | 50 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments