v1.0.2: Documentation fixes, and making the nodeback API work correctly for shorthand methods

pull/4/head
Sven Slootweg 9 years ago
parent 3d34349762
commit aa36ce9455

@ -46,6 +46,32 @@ Be aware that by making a pull request, you agree to release your modifications
## Usage ## Usage
A simple example:
```javascript
var bhttp = require("bhttp");
Promise.try(function() {
return bhttp.get("http://icanhazip.com/");
}).then(function(response) {
console.log("Your IP is:", response.body.toString());
});
```
... or, using nodebacks:
```javascript
var bhttp = require("bhttp");
bhttp.get("http://icanhazip.com/", {}, function(err, response) {
console.log("Your IP is:", response.body.toString());
});
```
### Streaming
Demonstrating both streaming responses and using a stream in form data for a request:
```javascript ```javascript
var bhttp = require("bhttp"); var bhttp = require("bhttp");
@ -149,7 +175,7 @@ Makes a request, and returns the response object asynchronously. The response ob
A few extra properties are set on the response object (which is a `http.IncomingMessage`): A few extra properties are set on the response object (which is a `http.IncomingMessage`):
* __body__: When `stream` is set to `false` (the default), this will contain the response body. This can be either a string or, in the case of a JSON response, a decoded JSON object. * __body__: When `stream` is set to `false` (the default), this will contain the response body. This can be either a Buffer or, in the case of a JSON response, a decoded JSON object.
* __redirectHistory__: An array containing the redirect responses, if any, in chronological order. Response bodies are discarded by default; if you do not want this, use the `keepRedirectResponses` option. * __redirectHistory__: An array containing the redirect responses, if any, in chronological order. Response bodies are discarded by default; if you do not want this, use the `keepRedirectResponses` option.
* __request__: The request configuration that was generated by `bhttp`. You probably don't need this. * __request__: The request configuration that was generated by `bhttp`. You probably don't need this.
* __requestState__: The request state that was accumulated by `bhttp`. You probably don't need this. * __requestState__: The request state that was accumulated by `bhttp`. You probably don't need this.

@ -414,7 +414,7 @@ processResponse = (request, response, requestState) ->
# Some wrappers # Some wrappers
doPayloadRequest = (url, data, options) -> doPayloadRequest = (url, data, options, callback) ->
# A wrapper that processes the second argument to .post, .put, .patch shorthand API methods. # A wrapper that processes the second argument to .post, .put, .patch shorthand API methods.
# FIXME: Treat a {} for data as a null? Otherwise {} combined with inputBuffer/inputStream will error. # FIXME: Treat a {} for data as a null? Otherwise {} combined with inputBuffer/inputStream will error.
if isStream(data) if isStream(data)
@ -424,7 +424,7 @@ doPayloadRequest = (url, data, options) ->
else else
options.formFields = data options.formFields = data
@request url, options @request url, options, callback
redirectGet = (request, response, requestState) -> redirectGet = (request, response, requestState) ->
debug "following forced-GET redirect to %s", response.headers["location"] debug "following forced-GET redirect to %s", response.headers["location"]
@ -469,22 +469,22 @@ createCookieJar = (jar) ->
bhttpAPI = bhttpAPI =
head: (url, options = {}, callback) -> head: (url, options = {}, callback) ->
options.method = "head" options.method = "head"
@request url, options @request url, options, callback
get: (url, options = {}, callback) -> get: (url, options = {}, callback) ->
options.method = "get" options.method = "get"
@request url, options @request url, options, callback
post: (url, data, options = {}, callback) -> post: (url, data, options = {}, callback) ->
options.method = "post" options.method = "post"
doPayloadRequest.bind(this) url, data, options doPayloadRequest.bind(this) url, data, options, callback
put: (url, data, options = {}, callback) -> put: (url, data, options = {}, callback) ->
options.method = "put" options.method = "put"
doPayloadRequest.bind(this) url, data, options doPayloadRequest.bind(this) url, data, options, callback
patch: (url, data, options = {}, callback) -> patch: (url, data, options = {}, callback) ->
options.method = "patch" options.method = "patch"
doPayloadRequest.bind(this) url, data, options doPayloadRequest.bind(this) url, data, options, callback
delete: (url, data, options = {}, callback) -> delete: (url, data, options = {}, callback) ->
options.method = "delete" options.method = "delete"
@request url, options @request url, options, callback
request: (url, options = {}, callback) -> request: (url, options = {}, callback) ->
@_doRequest(url, options).nodeify(callback) @_doRequest(url, options).nodeify(callback)
_doRequest: (url, options, requestState) -> _doRequest: (url, options, requestState) ->

@ -452,7 +452,7 @@ processResponse = function(request, response, requestState) {
}); });
}; };
doPayloadRequest = function(url, data, options) { doPayloadRequest = function(url, data, options, callback) {
if (isStream(data)) { if (isStream(data)) {
options.inputStream = data; options.inputStream = data;
} else if (ofTypes(data, [Buffer]) || typeof data === "string") { } else if (ofTypes(data, [Buffer]) || typeof data === "string") {
@ -460,7 +460,7 @@ doPayloadRequest = function(url, data, options) {
} else { } else {
options.formFields = data; options.formFields = data;
} }
return this.request(url, options); return this.request(url, options, callback);
}; };
redirectGet = function(request, response, requestState) { redirectGet = function(request, response, requestState) {
@ -535,42 +535,42 @@ bhttpAPI = {
options = {}; options = {};
} }
options.method = "head"; options.method = "head";
return this.request(url, options); return this.request(url, options, callback);
}, },
get: function(url, options, callback) { get: function(url, options, callback) {
if (options == null) { if (options == null) {
options = {}; options = {};
} }
options.method = "get"; options.method = "get";
return this.request(url, options); return this.request(url, options, callback);
}, },
post: function(url, data, options, callback) { post: function(url, data, options, callback) {
if (options == null) { if (options == null) {
options = {}; options = {};
} }
options.method = "post"; options.method = "post";
return doPayloadRequest.bind(this)(url, data, options); return doPayloadRequest.bind(this)(url, data, options, callback);
}, },
put: function(url, data, options, callback) { put: function(url, data, options, callback) {
if (options == null) { if (options == null) {
options = {}; options = {};
} }
options.method = "put"; options.method = "put";
return doPayloadRequest.bind(this)(url, data, options); return doPayloadRequest.bind(this)(url, data, options, callback);
}, },
patch: function(url, data, options, callback) { patch: function(url, data, options, callback) {
if (options == null) { if (options == null) {
options = {}; options = {};
} }
options.method = "patch"; options.method = "patch";
return doPayloadRequest.bind(this)(url, data, options); return doPayloadRequest.bind(this)(url, data, options, callback);
}, },
"delete": function(url, data, options, callback) { "delete": function(url, data, options, callback) {
if (options == null) { if (options == null) {
options = {}; options = {};
} }
options.method = "delete"; options.method = "delete";
return this.request(url, options); return this.request(url, options, callback);
}, },
request: function(url, options, callback) { request: function(url, options, callback) {
if (options == null) { if (options == null) {

@ -1,6 +1,6 @@
{ {
"name": "bhttp", "name": "bhttp",
"version": "1.0.1", "version": "1.0.2",
"description": "A sane HTTP client library for Node.js with Streams2 support.", "description": "A sane HTTP client library for Node.js with Streams2 support.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

@ -0,0 +1,13 @@
var bhttp = require("./");
var stockName = "AAPL";
var debug = require("debug")("testcase1");
var util = require("util");
bhttp.get('http://finance.yahoo.com/q/ks?s=' + stockName + '+Key+Statistics', { stream: false }, function(err, res) {
if(err) {
debug('ERROR: ' + err)
return callback(err)
} else {
debug('res: ' + util.inspect(res))
}
})
Loading…
Cancel
Save