Skip to content

Commit

Permalink
moved plugins to their own repo, changed the before and after events …
Browse files Browse the repository at this point in the history
…to use a formal event object so that events are preventable and stoppable and added some tests
  • Loading branch information
James Newell committed Feb 3, 2015
1 parent 2d75554 commit f099452
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 293 deletions.
147 changes: 112 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,78 @@ A pluggable HTTP client.

- Support for HTTP and HTTPS
- Support for streaming
- Pluggable API
- Pluggable API with plugins for:
- following redirects
- compression
- parsing JSON
- authentication
- ... and more

## Usage

### GET

Callback style:

var Client = require('go-fetch');
var Client = require('go-fetch');
var body = require('go-fetch-body-parser');

Client()
.use(Client.plugins.body())
.get('http://httpbin.org/html', function(error, response) {
.use(body())
.get('http://httpbin.org/html', function(error, response) {

console.log(
'Error: '+(error ? error : 'no error')+'\n'+
'Status: '+response.getStatus()+'\n'+
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n'+
(response.getBody() ? response.getBody().substr(0, 100)+'...' : '')
);
console.log(
'Error: '+(error ? error : 'no error')+'\n'+
'Status: '+response.getStatus()+'\n'+
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n'+
(response.getBody() ? response.getBody().substr(0, 100)+'...' : '')
);

})
})
;

### POST

Callback style:

var Client = require('go-fetch');

var Client = require('go-fetch');
var body = require('go-fetch-body-parser');
var contentType = require('go-fetch-content-type');

Client()
.use(Client.plugins.body())
.use(contentType)
.use(body.json())
.post('http://httpbin.org/post', {'Content-Type': 'application/json'}, JSON.stringify({msg: 'Go fetch!'}), function(error, response) {

console.log(
'Error: '+(error ? error : 'no error')+'\n'+
'Status: '+response.getStatus()+'\n'+
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n'+
(response.getBody() ? response.getBody().substr(0, 100)+'...' : '')
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n',
response.getBody()
);

})
;

Post a stream:

var fs = require('fs');
var Client = require('go-fetch');
var body = require('go-fetch-body-parser');

Client()
.use(body())
.post('http://httpbin.org/post', {'Content-Type': 'text/x-markdown'}, fs.createReadStream(__dirname+'/../README.md'), function(error, response) {

console.log(
'Error: '+(error ? error : 'no error')+'\n'+
'Status: '+response.getStatus()+'\n'+
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n',
response.getBody()
);

})
;


## API

Expand Down Expand Up @@ -112,17 +140,31 @@ Remove an event listener.

Emitted before the request is sent to the server with the following arguments:

- request : Request
- response : Response
- event : Client.Event
- .getName() : string
- .getEmitter() : Client
- .isDefaultPrevented() : bool
- .preventDefault()
- .isPropagationStopped() : bool
- .stopPropagation()
- .request : Client.Request
- .response : Client.Response

Useful for plugins setting data on the request e.g. OAuth signature

##### after

Emitted after the request is sent to the server with the following arguments:

- request : Request
- response : Response

- event : Client.Event
- .getName() : string
- .getEmitter() : Client
- .isPropagationStopped() : bool
- .stopPropagation()
- .request : Client.Request
- .response : Client.Response


Useful for plugins processing and setting data on the response e.g. gzip/deflate

Expand Down Expand Up @@ -188,47 +230,82 @@ Abort the response.

## Plugins

Plugins are functions that are passed the client object do something with it. Plugins are executed when `.use()`d. Using the `before` and `after` events, plugins are able to add helper methods to the `Request` and `Response` objects, modify the request data sent to the server and process the response data received from the server.
Plugins are functions that are passed the client object to do something with it. Plugins are executed when they are `.use()`d. Using the `before` and `after` events, plugins are able to add helper methods to the `Request` and `Response` objects, modify the request data sent to the server, process the response data received from the server, or cancel the request and use a locally built response.

### Example

Here's an example plugin that adds an `.isError()` method to the `Response` object.

function plugin(client) {
client.on('after', function (request, response) {

response.isError = function() {
return response.getStatus() >= 400 && response.getStatus() < 600;
client.on('after', function (event) {
event.response.isError = function() {
return this.getStatus() >= 400 && this.getStatus() < 600;
};
});
}

### .prefixUrl(url)
Here's an example plugin that returns a mocked request instead of a real one.

function(client) {
client.on('before', function(event) {
event.preventDefault();
event.response
.setStatus(201)
.setHeader('Content-Type', 'application/json; charset=utf-8')
.setBody(JSON.stringify({
message: 'Hello World!'
}))
;
});
}

### [prefix-url](https://www.npmjs.com/package/go-fetch-prefix-url)

Prefix each request URL with another URL unless the request URL already starts with a prefix of "http(s)://"
Prefix each request URL with another URL.

### .contentType
### [content-type](https://www.npmjs.com/package/go-fetch-content-type)

Parse the `Content-Type` header and add `.contentType` and `.charset` properties to the request object
Parse the Content-Type header.

### [bodyParser](https://www.npmjs.com/package/go-fetch-body-parser)
### [body-parser](https://www.npmjs.com/package/go-fetch-body-parser)

Concatenate the response stream into a string and update the response body.
Concatenate and parse the response stream.

### [OAuth1](https://www.npmjs.com/package/go-fetch-oauth1)
### [auth](https://www.npmjs.com/package/go-fetch-auth)

Basic HTTP auth.

### [oauth1](https://www.npmjs.com/package/go-fetch-oauth1)

OAuth v1 authentication.

### [follow-redirects](https://www.npmjs.com/package/go-fetch-follow-redirects)

Automatically follow redirects.

### [compression](https://www.npmjs.com/package/go-fetch-follow-compression)

Decompress compressed responses from the server.

## ToDo

- Tests
- Plugins:
- Compression (gzip/deflate)
- Cookie Jar
- OAuth v2
- Support for XMLHttpRequest in the browser

## Changelog

### v2.0.0

- moved `prefixUrl`, `contentType` and `body` plugins into their own repositories
- changed the arguments passed to the `before` and `after` event handlers - handlers now receive a formal event object that allows propagation to be stopped and the request to be prevented
- adding some tests
- cleaning up documentation

## License

The MIT License (MIT)
Expand Down
4 changes: 2 additions & 2 deletions example/GET.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Client = require('..');
var body = require('go-fetch-body-parser');
var Client = require('..');
var body = require('go-fetch-body-parser');

Client()
.use(body())
Expand Down
7 changes: 4 additions & 3 deletions example/POST.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var Client = require('..');
var body = require('go-fetch-body-parser');
var Client = require('..');
var body = require('go-fetch-body-parser');
var contentType = require('go-fetch-content-type');

Client()
.use(Client.plugins.contentType)
.use(contentType)
.use(body.json())
.post('http://httpbin.org/post', {'Content-Type': 'application/json'}, JSON.stringify({msg: 'Go fetch!'}), function(error, response) {

Expand Down
17 changes: 17 additions & 0 deletions example/POST_stream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var fs = require('fs');
var Client = require('..');
var body = require('go-fetch-body-parser');

Client()
.use(body())
.post('http://httpbin.org/post', {'Content-Type': 'text/x-markdown'}, fs.createReadStream(__dirname+'/../README.md'), function(error, response) {

console.log(
'Error: '+(error ? error : 'no error')+'\n'+
'Status: '+response.getStatus()+'\n'+
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n',
response.getBody()
);

})
;
27 changes: 27 additions & 0 deletions example/plugin_mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var Client = require('..');
var body = require('go-fetch-body-parser');

Client()
.use(function(client) {
client.on('before', function(event) {
event.preventDefault();
event.response
.setStatus(201)
.setHeader('Content-Type', 'application/json; charset=utf-8')
.setBody(JSON.stringify({
message: 'Hello World!'
}))
;
})
})
.get('http://api.myservice.io', function(error, response) {

console.log(
'Error: '+(error ? error : 'no error')+'\n'+
'Status: '+response.getStatus()+'\n'+
'Headers: '+JSON.stringify(response.getHeaders()).substr(0, 100)+'...'+'\n'+
(response.getBody() ? response.getBody().substr(0, 100)+'...' : '')
);

})
;

0 comments on commit f099452

Please sign in to comment.