Browse Source

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

master
Sven Slootweg 8 years ago
parent
commit
aa36ce9455
  1. 28
      README.md
  2. 16
      lib/bhttp.coffee
  3. 16
      lib/bhttp.js
  4. 2
      package.json
  5. 13
      testcase1.js

28
README.md

@ -46,6 +46,32 @@ Be aware that by making a pull request, you agree to release your modifications
## 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
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`):
* __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.
* __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.

16
lib/bhttp.coffee

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

16
lib/bhttp.js

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

2
package.json

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

13
testcase1.js

@ -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