UNPKG

motion

Version:

motion - moving development forward

74 lines (73 loc) 7.56 kB
{ "_args": [ [ "node-fetch@https://registry.npmjs.org/node-fetch/-/node-fetch-1.3.3.tgz", "/Users/nw/flint/packages/flint" ] ], "_from": "node-fetch@1.3.3", "_id": "node-fetch@1.3.3", "_inCache": true, "_location": "/node-fetch", "_phantomChildren": {}, "_requested": { "name": "node-fetch", "raw": "node-fetch@https://registry.npmjs.org/node-fetch/-/node-fetch-1.3.3.tgz", "rawSpec": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.3.3.tgz", "scope": null, "spec": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.3.3.tgz", "type": "remote" }, "_requiredBy": [ "/" ], "_resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-1.3.3.tgz", "_shasum": "880228be75fd207a8a9baa2042ead437e6edaf84", "_shrinkwrap": null, "_spec": "node-fetch@https://registry.npmjs.org/node-fetch/-/node-fetch-1.3.3.tgz", "_where": "/Users/nw/flint/packages/flint", "author": { "name": "David Frank" }, "bugs": { "url": "https://github.com/bitinn/node-fetch/issues" }, "dependencies": { "encoding": "^0.1.11" }, "description": "A light-weight module that brings window.fetch to node.js and io.js", "devDependencies": { "bluebird": "^2.9.1", "chai": "^1.10.0", "chai-as-promised": "^4.1.1", "coveralls": "^2.11.2", "form-data": "^1.0.0-rc1", "istanbul": "^0.3.5", "mocha": "^2.1.0", "parted": "^0.1.1", "promise": "^6.1.0", "resumer": "0.0.0" }, "homepage": "https://github.com/bitinn/node-fetch", "keywords": [ "fetch", "http", "promise" ], "license": "MIT", "main": "index.js", "name": "node-fetch", "optionalDependencies": {}, "readme": "\nnode-fetch\n==========\n\n[![npm version][npm-image]][npm-url]\n[![build status][travis-image]][travis-url]\n[![coverage status][coveralls-image]][coveralls-url]\n\nA light-weight module that brings `window.fetch` to node.js & io.js\n\n\n# Motivation\n\nI really like the notion of Matt Andrews' [isomorphic-fetch](https://github.com/matthew-andrews/isomorphic-fetch): it bridges the API gap between client-side and server-side http requests, so developers have less to worry about.\n\nInstead of implementing `XMLHttpRequest` in node to run browser-specific [fetch polyfill](https://github.com/github/fetch), why not go from node's `http` to `fetch` API directly? Node has native stream support, browserify build targets (browsers) don't, so underneath they are going to be vastly different anyway.\n\nHence `node-fetch`, minimal code for a `window.fetch` compatible API on node.js/io.js runtime.\n\n\n# Features\n\n- Stay consistent with `window.fetch` API.\n- Make conscious trade-off when following [whatwg fetch spec](https://fetch.spec.whatwg.org/) and [stream spec](https://streams.spec.whatwg.org/) implementation details, document known difference.\n- Use native promise, but allow substituting it with [insert your favorite promise library].\n- Use native stream for body, on both request and response.\n- Decode content encoding (gzip/deflate) properly, and convert string output (such as `res.text()` and `res.json()`) to utf-8 automatically.\n- Useful extensions such as timeout, redirect limit, response size limit.\n\n\n# Difference from client-side fetch\n\n- See [Known Differences](https://github.com/bitinn/node-fetch/blob/master/LIMITS.md) for details.\n- If you happen to use a missing feature that `window.fetch` offers, feel free to open an issue.\n- Pull requests are welcomed too!\n\n\n# Install\n\n`npm install node-fetch --save`\n\n\n# Usage\n\n```javascript\nvar fetch = require('node-fetch');\n\n// If you are not on node v0.12, set a Promise library first, eg.\n// fetch.Promise = require('bluebird');\n\n// plain text or html\n\nfetch('https://github.com/')\n\t.then(function(res) {\n\t\treturn res.text();\n\t}).then(function(body) {\n\t\tconsole.log(body);\n\t});\n\n// json\n\nfetch('https://api.github.com/users/github')\n\t.then(function(res) {\n\t\treturn res.json();\n\t}).then(function(json) {\n\t\tconsole.log(json);\n\t});\n\n// meta\n\nfetch('https://github.com/')\n\t.then(function(res) {\n\t\tconsole.log(res.ok);\n\t\tconsole.log(res.status);\n\t\tconsole.log(res.statusText);\n\t\tconsole.log(res.headers.raw());\n\t\tconsole.log(res.headers.get('content-type'));\n\t});\n\n// post\n\nfetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })\n\t.then(function(res) {\n\t\treturn res.json();\n\t}).then(function(json) {\n\t\tconsole.log(json);\n\t});\n\n// post with stream from resumer\n\nvar resumer = require('resumer');\nvar stream = resumer().queue('a=1').end();\nfetch('http://httpbin.org/post', { method: 'POST', body: stream })\n\t.then(function(res) {\n\t\treturn res.json();\n\t}).then(function(json) {\n\t\tconsole.log(json);\n\t});\n\n// post with form-data (detect multipart)\n\nvar FormData = require('form-data');\nvar form = new FormData();\nform.append('a', 1);\nfetch('http://httpbin.org/post', { method: 'POST', body: form })\n\t.then(function(res) {\n\t\treturn res.json();\n\t}).then(function(json) {\n\t\tconsole.log(json);\n\t});\n\n// post with form-data (custom headers)\n\nvar FormData = require('form-data');\nvar form = new FormData();\nform.append('a', 1);\nfetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })\n\t.then(function(res) {\n\t\treturn res.json();\n\t}).then(function(json) {\n\t\tconsole.log(json);\n\t});\n\n// node 0.11+, yield with co\n\nvar co = require('co');\nco(function *() {\n\tvar res = yield fetch('https://api.github.com/users/github');\n\tvar json = yield res.json();\n\tconsole.log(res);\n});\n```\n\nSee [test cases](https://github.com/bitinn/node-fetch/blob/master/test/test.js) for more examples.\n\n\n# API\n\n## fetch(url, options)\n\nReturns a `Promise`\n\n### Url\n\nShould be an absolute url, eg `http://example.com/`\n\n### Options\n\ndefault values are shown, note that only `method`, `headers` and `body` are allowed in `window.fetch`, others are node.js extensions.\n\n```\n{\n\tmethod: 'GET'\n\t, headers: {} // request header, format {a:1} or {b:[1,2,3]}\n\t, follow: 20 // maximum redirect count, 0 to not follow redirect\n\t, timeout: 0 // req/res timeout in ms, 0 to disable, timeout reset on redirect\n\t, compress: true // support gzip/deflate content encoding, false to disable\n\t, size: 0 // maximum response body size in bytes, 0 to disable\n\t, body: empty // request body, can be a string or readable stream\n\t, agent: null // custom http.Agent instance\n}\n```\n\n\n# License\n\nMIT\n\n\n# Acknowledgement\n\nThanks to [github/fetch](https://github.com/github/fetch) for providing a solid implementation reference.\n\n\n[npm-image]: https://img.shields.io/npm/v/node-fetch.svg?style=flat-square\n[npm-url]: https://www.npmjs.com/package/node-fetch\n[travis-image]: https://img.shields.io/travis/bitinn/node-fetch.svg?style=flat-square\n[travis-url]: https://travis-ci.org/bitinn/node-fetch\n[coveralls-image]: https://img.shields.io/coveralls/bitinn/node-fetch.svg?style=flat-square\n[coveralls-url]: https://coveralls.io/r/bitinn/node-fetch\n", "readmeFilename": "README.md", "repository": { "type": "git", "url": "git+https://github.com/bitinn/node-fetch.git" }, "scripts": { "coverage": "istanbul cover _mocha --report lcovonly -- -R spec test/test.js && cat ./coverage/lcov.info | coveralls", "report": "istanbul cover _mocha -- -R spec test/test.js", "test": "mocha test/test.js" }, "version": "1.3.3" }