Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lsongdev committed Aug 31, 2018
0 parents commit 44323ed
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

*.log

node_modules/
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

*.log

node_modules/
19 changes: 19 additions & 0 deletions LICENSE
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.
65 changes: 65 additions & 0 deletions README.md
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).

---
34 changes: 34 additions & 0 deletions example/index.js
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);
});
96 changes: 96 additions & 0 deletions index.js
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;
26 changes: 26 additions & 0 deletions package.json
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"
]
}

0 comments on commit 44323ed

Please sign in to comment.