├── .gitignore
├── .travis.yml
├── appveyor.yml
├── collaborators.md
├── index.js
├── package.json
├── readme.md
└── test.js
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 |
3 | node_js:
4 | - "0.10"
5 |
6 | install:
7 | - npm install
8 |
9 | before_script:
10 | - npm install npm
11 |
12 | script:
13 | - npm test
14 |
--------------------------------------------------------------------------------
/appveyor.yml:
--------------------------------------------------------------------------------
1 | # appveyor file
2 | # http://www.appveyor.com/docs/appveyor-yml
3 |
4 | # branches to build
5 | branches:
6 | # whitelist
7 | only:
8 | - master
9 |
10 | # build version format
11 | version: "{build}"
12 |
13 | # what combinations to test
14 | environment:
15 | matrix:
16 | - nodejs_version: 0.10
17 |
18 | # Get the latest stable version of Node 0.STABLE.latest
19 | install:
20 | - ps: Update-NodeJsInstallation (Get-NodeJsLatestBuild $env:nodejs_version)
21 | - npm install
22 |
23 | build: off
24 |
25 | test_script:
26 | - node --version
27 | - npm --version
28 | - ps: npm test
29 | - cmd: npm test
--------------------------------------------------------------------------------
/collaborators.md:
--------------------------------------------------------------------------------
1 | ## Collaborators
2 |
3 | nets is only possible due to the excellent work of the following collaborators:
4 |
5 |
8 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | var req = require('request')
2 |
3 | module.exports = Nets
4 |
5 | function Nets (opts, cb) {
6 | if (typeof opts === 'string') opts = { uri: opts }
7 |
8 | // in node, if encoding === null then response will be a Buffer. we want this to be the default
9 | if (!opts.hasOwnProperty('encoding')) opts.encoding = null
10 |
11 | // in browser, we should by default convert the arraybuffer into a Buffer
12 | if (process.browser && !opts.hasOwnProperty('json') && opts.encoding === null) {
13 | opts.responseType = 'arraybuffer'
14 | var originalCb = cb
15 | cb = bufferify
16 | }
17 |
18 | function bufferify (err, resp, body) {
19 | if (body) body = new Buffer(new Uint8Array(body))
20 | originalCb(err, resp, body)
21 | }
22 |
23 | return req(opts, cb)
24 | }
25 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "nets",
3 | "version": "3.2.0",
4 | "description": "nothin but nets. http client that works in node and browsers",
5 | "main": "index.js",
6 | "scripts": {
7 | "test": "standard && node test.js"
8 | },
9 | "author": "max ogden",
10 | "license": "ISC",
11 | "browser": {
12 | "request": "xhr"
13 | },
14 | "dependencies": {
15 | "request": "^2.65.0",
16 | "xhr": "^2.1.0"
17 | },
18 | "devDependencies": {
19 | "tape": "^2.13.1",
20 | "standard": "^3.0.0-beta"
21 | },
22 | "repository": {
23 | "type": "git",
24 | "url": "https://github.com/maxogden/nets.git"
25 | },
26 | "bugs": {
27 | "url": "https://github.com/maxogden/nets/issues"
28 | },
29 | "homepage": "https://github.com/maxogden/nets",
30 | "testling": {
31 | "files": "test.js",
32 | "browsers": [
33 | "ie/8..latest",
34 | "firefox/17..latest",
35 | "firefox/nightly",
36 | "chrome/22..latest",
37 | "chrome/canary",
38 | "opera/12..latest",
39 | "opera/next",
40 | "safari/5.1..latest",
41 | "ipad/6.0..latest",
42 | "iphone/6.0..latest"
43 | ]
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # nets
2 |
3 | Mac/Linux | Windows
4 | ------------ | --------------
5 | [](https://travis-ci.org/maxogden/nets) | [](https://ci.appveyor.com/project/maxogden/nets)
6 |
7 | [](https://github.com/feross/standard)
8 |
9 | Nothin but nets. HTTP client that works the same in node and browsers
10 |
11 | Uses [xhr](https://www.npmjs.org/package/xhr) for browsers and [request](https://www.npmjs.org/package/request) for node
12 |
13 | #### get
14 |
15 | ```js
16 | var nets = require("nets")
17 |
18 | nets({ url: "http://placekitten.com/g/400/400" }, function(err, resp, body) {
19 | // body is a Buffer containing the image
20 | })
21 | ```
22 |
23 | Note that `nets` returns data as [`Buffer`](http://nodejs.org/api/buffer.html)s by default, in both node and in browsers. You can pass `{encoding: undefined}` to turn this off.
24 |
25 | #### post
26 |
27 | ```js
28 | var nets = require("nets")
29 |
30 | nets({
31 | body: '{"foo": "bar"}',
32 | url: "/foo",
33 | method: "POST",
34 | headers: {
35 | "Content-Type": "application/json"
36 | }
37 | }, function done (err, resp, body) {
38 |
39 | })
40 | ```
41 |
--------------------------------------------------------------------------------
/test.js:
--------------------------------------------------------------------------------
1 | var test = require('tape')
2 | var nets = require('./')
3 | var bin
4 | var binUrl = 'http://requestb.in'
5 |
6 | var headers = {
7 | 'x-requested-with': 'nets',
8 | 'Cache-Control': 'no-cache, no-store, must-revalidate',
9 | 'Pragma': 'no-cache',
10 | 'Expires': '0'
11 | }
12 |
13 | test('GET a new requestbin for this test run', function (t) {
14 | nets({url: binUrl + '/api/v1/bins', method: 'POST', json: true, headers: headers}, function (err, resp, newBin) {
15 | t.notOk(err, 'no err')
16 | t.ok(newBin.name, 'bin has a name')
17 | bin = newBin
18 | t.end()
19 | })
20 | })
21 |
22 | test('GET url property', function (t) {
23 | nets({url: binUrl + '/' + bin.name, headers: headers}, function (err, resp, body) {
24 | t.ok(Buffer.isBuffer(body), 'response data is a Buffer by default')
25 | t.notOk(err, 'no err')
26 | t.equal(resp.statusCode, 200, '200 OK')
27 | getRequests(function (err, resp, reqs) {
28 | t.notOk(err, 'no err')
29 | t.equal(reqs.length, 1)
30 | var req = reqs[0]
31 | t.equal(req.path, '/' + bin.name, 'path matches')
32 | t.end()
33 | })
34 | })
35 | })
36 |
37 | test('GET uri property', function (t) {
38 | nets({uri: binUrl + '/' + bin.name, headers: headers}, function (err, resp, body) {
39 | t.notOk(err, 'no err')
40 | t.equal(resp.statusCode, 200, '200 OK')
41 | getRequests(function (err, resp, reqs) {
42 | t.notOk(err, 'no err')
43 | t.equal(reqs.length, 2)
44 | var req = reqs[0]
45 | t.equal(req.path, '/' + bin.name, 'path matches')
46 | t.end()
47 | })
48 | })
49 | })
50 |
51 | test('GET w/ custom header', function (t) {
52 | headers['X-Hello'] = 'hello!'
53 | nets({url: binUrl + '/' + bin.name, headers: headers}, function (err, resp, body) {
54 | delete headers['X-Hello']
55 | t.notOk(err, 'no err')
56 | t.equal(resp.statusCode, 200, '200 OK')
57 | getRequests(function (err, resp, reqs) {
58 | t.notOk(err, 'no err')
59 | t.equal(reqs.length, 3)
60 | var req = reqs[0]
61 | t.equal(req.path, '/' + bin.name, 'path matches')
62 | t.equal(req.headers['X-Hello'], 'hello!', 'header matches')
63 | t.end()
64 | })
65 | })
66 | })
67 |
68 | test('POST', function (t) {
69 | nets({url: binUrl + '/' + bin.name, headers: headers, method: 'POST', body: 'hello'}, function (err, resp, body) {
70 | t.notOk(err, 'no err')
71 | t.equal(resp.statusCode, 200, '200 OK')
72 | getRequests(function (err, resp, reqs) {
73 | t.notOk(err, 'no err')
74 | t.equal(reqs.length, 4)
75 | var req = reqs[0]
76 | t.equal(req.path, '/' + bin.name, 'path matches')
77 | t.equal(req.body, 'hello', 'body matches')
78 | t.equal(req.method, 'POST', 'POST')
79 | t.end()
80 | })
81 | })
82 | })
83 |
84 | test('PUT', function (t) {
85 | nets({url: binUrl + '/' + bin.name, headers: headers, method: 'PUT', body: 'hello put'}, function (err, resp, body) {
86 | t.notOk(err, 'no err')
87 | t.equal(resp.statusCode, 200, '200 OK')
88 | getRequests(function (err, resp, reqs) {
89 | t.notOk(err, 'no err')
90 | t.equal(reqs.length, 5)
91 | var req = reqs[0]
92 | t.equal(req.path, '/' + bin.name, 'path matches')
93 | t.equal(req.body, 'hello put', 'body matches')
94 | t.equal(req.method, 'PUT', 'PUT')
95 | t.end()
96 | })
97 | })
98 | })
99 |
100 | test('DELETE', function (t) {
101 | nets({url: binUrl + '/' + bin.name, headers: headers, method: 'DELETE'}, function (err, resp, body) {
102 | t.notOk(err, 'no err')
103 | t.equal(resp.statusCode, 200, '200 OK')
104 | getRequests(function (err, resp, reqs) {
105 | t.notOk(err, 'no err')
106 | t.equal(reqs.length, 6)
107 | var req = reqs[0]
108 | t.equal(req.path, '/' + bin.name, 'path matches')
109 | t.equal(req.method, 'DELETE')
110 | t.end()
111 | })
112 | })
113 | })
114 |
115 | test('returns value', function (t) {
116 | t.ok(nets({url: 'http://localhost'}, function () {}), 'nets has return value')
117 | t.end()
118 | })
119 |
120 | function getRequests (cb) {
121 | nets({
122 | url: binUrl + '/api/v1/bins/' + bin.name + '/requests',
123 | json: true,
124 | headers: headers
125 | }, function (err, resp, body) {
126 | cb(err, resp, body)
127 | })
128 | }
129 |
130 | // {
131 | // "method": 'GET',
132 | // "path": "/zsdmvmzs",
133 | // "form_data": {},
134 | // "body": "",
135 | // "time": 1400675602.691776,
136 | // "content_length": 0,
137 | // "remote_addr": "192.168.101.13, 172.16.231.24, 138.62.0.21",
138 | // "headers": {
139 | // "Via": "1.1 73-46:3128 (squid/2.7.STABLE6), 1.0 cache_server:3128 (squid/2.6.STABLE21)",
140 | // "X-Request-Id": "23c1e383-23a8-497d-b9e9-c7c17935e162",
141 | // "X-Bluecoat-Via": "a640f636fca622c2",
142 | // "Host": "requestb.in",
143 | // "Cache-Control": "max-age=259200",
144 | // "Connection": "close"
145 | // },
146 | // "id": "1bqzf2",
147 | // "content_type": "",
148 | // "query_string": {}
149 | // }
150 |
--------------------------------------------------------------------------------