-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 44323ed
Showing
7 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
*.log | ||
|
||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
*.log | ||
|
||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2018 lsong | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
## xttp [![xttp](https://img.shields.io/npm/v/xttp.svg)](https://npmjs.org/xttp) | ||
|
||
> simple http client in node js | ||
### Installation | ||
|
||
```bash | ||
$ npm install xttp | ||
``` | ||
|
||
### Example | ||
|
||
```js | ||
const xttp = require('xttp'); | ||
|
||
// case 1: | ||
xttp('https://api.github.com/users/song940/orgs', { | ||
method: 'get', | ||
headers: { | ||
'User-Agent': 'xttp/1.0' | ||
} | ||
}, async (err, res) => { | ||
const data = await res.json(); | ||
console.log(err, data); | ||
}); | ||
|
||
// case 2: | ||
xttp('https://api.github.com/users/song940/orgs', { | ||
method: 'get', | ||
headers: { | ||
'User-Agent': 'xttp/1.0' | ||
} | ||
}) | ||
.then(res => res.json()) | ||
.then(res => { | ||
console.log(res); | ||
}); | ||
|
||
// case 3: | ||
xttp | ||
.create() | ||
.get('https://api.github.com/users/song940/orgs') | ||
.header('User-Agent', 'Xttp/0.1') | ||
.then(res => res.json()) | ||
.then(res => { | ||
console.log(res); | ||
}); | ||
|
||
``` | ||
|
||
### Contributing | ||
- Fork this Repo first | ||
- Clone your Repo | ||
- Install dependencies by `$ npm install` | ||
- Checkout a feature branch | ||
- Feel free to add your features | ||
- Make sure your features are fully tested | ||
- Publish your local branch, Open a pull request | ||
- Enjoy hacking <3 | ||
|
||
### MIT | ||
|
||
This work is licensed under the [MIT license](./LICENSE). | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const xttp = require('..'); | ||
|
||
// case 1: | ||
xttp('https://api.github.com/users/song940/orgs', { | ||
method: 'get', | ||
headers: { | ||
'User-Agent': 'xttp/1.0' | ||
} | ||
}, async (err, res) => { | ||
const data = await res.json(); | ||
console.log(err, data); | ||
}); | ||
|
||
// case 2: | ||
xttp('https://api.github.com/users/song940/orgs', { | ||
method: 'get', | ||
headers: { | ||
'User-Agent': 'xttp/1.0' | ||
} | ||
}) | ||
.then(res => res.json()) | ||
.then(res => { | ||
console.log(res); | ||
}); | ||
|
||
// case 3: | ||
xttp | ||
.create() | ||
.get('https://api.github.com/users/song940/orgs') | ||
.header('User-Agent', 'Xttp/0.1') | ||
.then(res => res.json()) | ||
.then(res => { | ||
console.log(res); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
const URI = require('url'); | ||
const http = require('http'); | ||
const https = require('https'); | ||
/** | ||
* Xttp | ||
* @param {*} url | ||
* @param {*} params | ||
* @param {*} fn | ||
*/ | ||
function Xttp(url, params, fn) { | ||
const request = new Xttp.Request(url, params); | ||
request.then(x => fn && fn(null, x), fn); | ||
return request; | ||
}; | ||
|
||
/** | ||
* create | ||
* @param {*} url | ||
* @param {*} params | ||
*/ | ||
Xttp.create = (url, params) => { | ||
return new Xttp.Request(url, params); | ||
}; | ||
|
||
/** | ||
* Request | ||
* @param {*} url | ||
* @param {*} params | ||
*/ | ||
Xttp.Request = function(url, params){ | ||
if(url) this.get(url); | ||
return Object.assign(this, { | ||
body: '', | ||
headers: {} | ||
}, params); | ||
}; | ||
|
||
Xttp.Request.prototype.get = function(url){ | ||
return Object.assign(this, URI.parse(url, true)); | ||
}; | ||
|
||
Xttp.Request.prototype.header = function(key, value){ | ||
if(typeof key === 'object'){ | ||
this.headers = key; | ||
}else{ | ||
this.headers[key] = value; | ||
} | ||
return this; | ||
}; | ||
|
||
Xttp.Request.prototype.send = function(body){ | ||
this.body = body; | ||
return this; | ||
}; | ||
|
||
Xttp.Request.prototype.end = function(){ | ||
this.headers['Content-Length'] = Buffer.byteLength(this.body); | ||
const p = new Promise((response, reject) => { | ||
const client = this.protocol === 'http:' ? http : https; | ||
this.req = client.request(this, response); | ||
this.req.on('error', reject); | ||
this.req.write(this.body); | ||
this.req.end(); | ||
}); | ||
return p.then(res => new Xttp.Response(res)); | ||
}; | ||
|
||
Xttp.Response = function(res){ | ||
this.res = res; | ||
return this; | ||
}; | ||
|
||
Xttp.Response.prototype.data = function(){ | ||
if(this._data) return Promise.resolve(this._data); | ||
return new Promise((resolve, reject) => { | ||
const buffer = []; | ||
this.res | ||
.on('error', reject) | ||
.on('data', chunk => buffer.push(chunk)) | ||
.on('end', () => resolve(this._data = Buffer.concat(buffer))); | ||
}); | ||
}; | ||
|
||
Xttp.Response.prototype.text = function({ encoding = 'utf8' } = {}){ | ||
return this.data().then(x => x.toString(encoding)); | ||
}; | ||
|
||
Xttp.Response.prototype.json = function(options){ | ||
return this.text(options).then(JSON.parse); | ||
}; | ||
|
||
Xttp.Request.prototype.then = function(resolve, reject){ | ||
return this.end().then(resolve, reject); | ||
}; | ||
|
||
module.exports = Xttp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "xttp", | ||
"version": "0.0.1", | ||
"description": "simple http client in node js", | ||
"main": "index.js", | ||
"directories": { | ||
"example": "example" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/song940/xttp.git" | ||
}, | ||
"author": "lsong", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/song940/xttp/issues" | ||
}, | ||
"homepage": "https://github.com/song940/xttp#readme", | ||
"keywords": [ | ||
"http", | ||
"request" | ||
] | ||
} |