Skip to content

Commit

Permalink
add xttp cli
Browse files Browse the repository at this point in the history
  • Loading branch information
lsongdev committed Jun 14, 2019
1 parent 0655dab commit 275136e
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

*.log
yarn.lock

node_modules/
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: node_js
node_js:
- "stable"
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
## xttp [![xttp](https://img.shields.io/npm/v/xttp.svg)](https://npmjs.org/xttp)
## xttp

> simple http client in node js
[![xttp](https://img.shields.io/npm/v/xttp.svg)](https://npmjs.org/xttp)
[![Build Status](https://travis-ci.org/song940/xttp.svg?branch=master)](https://travis-ci.org/song940/xttp)

### Installation

```bash
Expand Down Expand Up @@ -67,4 +70,4 @@ xttp

This work is licensed under the [MIT license](./LICENSE).

---
---
33 changes: 33 additions & 0 deletions bin/xttp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node

const xttp = require('..');
const http = require('http');

const [method, url] = process.argv.slice(2);

function color(str, c){
return "\x1b[" + c + "m" + str + "\x1b[0m";
};

const header = res => {
const { httpVersion } = res.response;
const { status, statusText } = res;
console.log(color(`HTTP/${httpVersion} ${status} ${statusText}`, 32));
Object.keys(res.headers).forEach(header => {
console.log(`${color(header, 36)}: ${res.headers[header]}`);
});
return res.auto();
};

const response = body => {
console.log();
console.log(body);
};

xttp({
method,
url
})
.then(header)
.then(response)
.catch(err => console.error(err))
47 changes: 0 additions & 47 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1 @@
const xttp = require('..');

// xttp
// .get('https://httpbin.org/get?a=b')
// .then(res => res.json())
// .then(res => console.log(res));

// // case 1:
xttp('https://httpbin.org/post', {
method: 'post',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: '{}'
})
.then(res => res.json())
.then(res => {
console.log(res);
});

// // case 2:
// xttp('https://httpbin.org/get?a=b', {
// method: 'get',
// query: { c: 'd' },
// headers: {
// 'User-Agent': 'xttp/1.0'
// }
// })
// .then(res => res.json())
// .then(res => {
// console.log(res);
// });

// case 3:
// xttp
// .create({
// url: 'aaa',
// query: { a: 'b' }
// })
// .header('')
// .post('https://httpbin.org/post?e=f')
// .query('c', 'd')
// .query({ g: 'h' })
// .json({ name: 'xttp' })
// .then(res => res.json())
// .then(res => {
// console.log(res);
// });
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,19 @@ Xttp.Response.prototype.__defineGetter__('status', function(){
return this.response.statusCode;
});

Xttp.Response.prototype.__defineGetter__('statusText', function(){
return http.STATUS_CODES[this.status];
});

Xttp.Response.prototype.__defineGetter__('headers', function(){
return this.response.headers;
});

Xttp.Response.prototype.__defineGetter__('contentType', function(){
const header = this.response.headers['content-type'];
return header && MIME.Header.parseValue(header).value;
});

Xttp.Response.prototype.pipe = function(stream){
return this.response.pipe(stream);
};
Expand All @@ -220,6 +229,15 @@ Xttp.Response.prototype.json = function(options){
return this.text(options).then(JSON.parse);
};

Xttp.Response.prototype.auto = function(){
switch(this.contentType){
case 'application/json':
return this.json();
default:
return this.text();
}
};

Xttp.Request.prototype.then = function(resolve, reject){
return this.end().then(resolve, reject);
};
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "xttp",
"version": "1.0.3",
"version": "1.0.5",
"description": "simple & powerful http client in Node.js",
"main": "index.js",
"directories": {
"example": "example"
},
"bin": {
"xttp": "./bin/xttp.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test"
},
"repository": {
"type": "git",
Expand Down
57 changes: 57 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const xttp = require('..');
const test = require('./test');
const assert = require('assert');

test('xttp#request', async () => {
const res = await xttp('https://httpbin.org/get?a=b');
assert.equal(res.status, 200);
const body = await res.json();
assert.deepEqual(body.args, { a: 'b' });
assert.equal(res.contentType, 'application/json');
});

test('xttp#get', async () => {
const res = await xttp.get('https://httpbin.org/get?a=b');
assert.equal(res.status, 200);
});

test('xttp#post', async () => {
const res = await xttp('https://httpbin.org/post', {
method: 'post',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: '{}'
});
assert.equal(res.status, 200);
});

test('xttp#get with query', async () => {
const res = await xttp('https://httpbin.org/get?a=b', {
method: 'get',
query: { c: 'd' },
headers: {
'User-Agent': 'xttp/1.0'
}
});
const body = await res.json();
assert.equal(res.status, 200);
assert.deepEqual(body.args, { a: 'b', c: 'd' });
});

test('xttp#create', async () => {
const res = await xttp
.create({
url: 'aaa',
query: { a: 'b' }
})
.header('foo', 'bar')
.post('https://httpbin.org/post?e=f')
.query('c', 'd')
.query({ g: 'h' })
.json({ name: 'xttp' });
const body = await res.json();
assert.equal(res.status, 200);
assert.deepEqual(body.args, { a: 'b', c: 'd', e: 'f', g: 'h' });

});
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { inspect } = require('util');
/**
* super tiny testing framework
*
* @author Liu song <[email protected]>
* @github https://github.com/song940
*/
const test = async (title, fn) => {
try {
await fn();
console.log(color(` ✔ ${title}`, 32));
} catch (err) {
console.error(color(` ✘ ${title}`, 31));
console.log();
console.log(color(` ${err.name}`, 31));
console.error(color(` expected: ${inspect(err.expected)}`, 32));
console.error(color(` actual: ${inspect(err.actual)}`, 31));
console.log();
}
};

function color(str, c) {
return "\x1b[" + c + "m" + str + "\x1b[0m";
};

module.exports = test;

0 comments on commit 275136e

Please sign in to comment.