Skip to content

Commit

Permalink
Update the api interface, use fetch instead of request
Browse files Browse the repository at this point in the history
  • Loading branch information
berwin committed Apr 12, 2017
1 parent 3404d89 commit 92eb7eb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
# think-request
[![Build Status](https://travis-ci.org/thinkjs/think-request.svg?branch=master)](https://travis-ci.org/thinkjs/think-request)
[![Coverage Status](https://coveralls.io/repos/github/thinkjs/think-request/badge.svg?branch=master)](https://coveralls.io/github/thinkjs/think-request?branch=master)
# think-fetch
[![Build Status](https://travis-ci.org/thinkjs/think-fetch.svg?branch=master)](https://travis-ci.org/thinkjs/think-fetch)
[![Coverage Status](https://coveralls.io/repos/github/thinkjs/think-fetch/badge.svg?branch=master)](https://coveralls.io/github/thinkjs/think-fetch?branch=master)

Request for ThinkJS 3.x
Fetch for ThinkJS 3.x

## Install

```
$ npm install think-request
$ npm install think-fetch
```

## How to use

config file `src/config/extend.js`

```javascript
const request = require('think-request');
const fetch = require('think-fetch');

module.exports = [
request, // HTTP request client.
fetch, // HTTP request client.
];
```

## Methods in Controller

```javascript
module.exports = class extends think.Controller {
async indexAction(){
this.ctx.body = await this.curl('https://api.github.com/repos/thinkjs/think-request');
async indexAction () {

// plain text or html
const text = await this.fetch('https://github.com/').then(res => res.text());

// json
const json = await this.fetch('https://api.github.com/repos/thinkjs/think-fetch').then(res => res.json());

// post
const body = await this.fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' }).then(res => res.json());
}
}
```

[More Methods Click](https://github.com/request/request-promise)
[More Methods Click](https://github.com/bitinn/node-fetch)
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const request = require('request-promise-native');
const fetch = require('node-fetch');

const extend = {
curl: request
fetch
};

module.exports = {
Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"name": "think-request",
"name": "think-fetch",
"version": "1.0.0",
"description": "Request for ThinkJS 3.x",
"description": "Fetch for ThinkJS 3.x",
"main": "index.js",
"dependencies": {
"request": "^2.34",
"request-promise-native": "^1.0.3"
"node-fetch": "^1.6.3"
},
"devDependencies": {
"ava": "^0.18.2",
Expand All @@ -24,17 +23,17 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/thinkjs/think-request.git"
"url": "git+https://github.com/thinkjs/think-fetch.git"
},
"keywords": [
"thinkjs3",
"request",
"fetch",
"think-extend"
],
"author": "Berwin <liubowen.niubi@gmail.com> (https://github.com/berwin)",
"license": "MIT",
"bugs": {
"url": "https://github.com/thinkjs/think-request/issues"
"url": "https://github.com/thinkjs/think-fetch/issues"
},
"homepage": "https://github.com/thinkjs/think-request#readme"
"homepage": "https://github.com/thinkjs/think-fetch#readme"
}
44 changes: 16 additions & 28 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const test = require('ava');
const startServer = require('./server.js');
const curl = require('../index.js').controller.curl;
const fetch = require('../index.js').controller.fetch;
let stopServer = null;

test.before(t => {
Expand All @@ -9,44 +9,32 @@ test.before(t => {
});
});

test('Request', t => {
return curl('http://127.0.0.1:1995/200').then(body => {
t.is(body, 'GET /200');
test('Fetch', t => {
return fetch('http://127.0.0.1:1995/200').then(res => {
t.true(res.ok);
});
});

test('Response 404', t => {
return curl('http://127.0.0.1:1995/404').catch(error => {
t.is(error.statusCode, 404);
});
test('should return a promise', t => {
const p = fetch('http://127.0.0.1:1995/200');
t.true(p instanceof fetch.Promise)
});

test('Post Method', t => {
return curl.post('http://127.0.0.1:1995/200').then(body => {
t.is(body, 'POST /200');
test('Should return 404 status', t => {
return fetch('http://127.0.0.1:1995/404').then(res => {
t.is(res.status, 404);
});
});

test('Options', t => {
const options = {
method: 'DELETE',
uri: 'http://127.0.0.1:1995/200'
};

return curl(options).then(body => {
t.is(body, 'DELETE /200');
test('Should can get the correct text', t => {
return fetch('http://127.0.0.1:1995/200').then(res => res.text()).then(text => {
t.is(text, 'GET /200');
});
});

test('resolveWithFullResponse', t => {
const options = {
method: 'DELETE',
uri: 'http://127.0.0.1:1995/200',
resolveWithFullResponse: true
};

return curl(options).then(res => {
t.is(res.body, 'DELETE /200');
test('Should can get the correct json', t => {
return fetch('http://127.0.0.1:1995/json').then(res => res.json()).then(json => {
t.deepEqual(json, {name: 'value'});
});
});

Expand Down
12 changes: 9 additions & 3 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@ const url = require('url');
module.exports = (port, fn) => {
const server = http.createServer((req, res) => {
const path = url.parse(req.url).pathname;
const status = path.split('/')[1];
const param = path.split('/')[1];

switch (status) {
switch (param) {
case 'json':
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({
name: 'value'
}));
break;
case 301:
res.writeHead(301, { Location: '/200' });
res.end();
break;
default:
res.writeHead(status, {'Content-Type': 'text/plain'});
res.writeHead(param, {'Content-Type': 'text/plain'});
res.end(req.method + ' ' + path);
}
});
Expand Down

0 comments on commit 92eb7eb

Please sign in to comment.