Comparing version 0.6.1 to 0.6.4
@@ -54,2 +54,18 @@ // # Introduction | ||
// We can also chain response handlers and have many handlers | ||
// for the generic 'response' event. | ||
surf.get({ | ||
url: "http://api.spire.io", | ||
headers: { | ||
accept: "text/html" | ||
} | ||
}).on(200, function(response) { | ||
assert.ok(response.content.body); | ||
console.log("√ Got API description as HTML"); | ||
}).on(function(response) { | ||
console.log("We got something besides a 200 response!"); | ||
}).on(function(response) { | ||
console.log("I'm not so happy about this non 200 response. Launch the missiles!"); | ||
}); | ||
// # Data Conversion | ||
@@ -56,0 +72,0 @@ // |
@@ -198,2 +198,22 @@ // The request object encapsulates a request, creating a Node.js HTTP request and | ||
// Allow chainable 'on's: shred.get({ ... }).on( ... ). You can pass in a | ||
// single function, a pair (event, function), or a hash: | ||
// { event: function, event: function } | ||
_.extend(Request.prototype,{ | ||
on: function(eventOrHash, listener) { | ||
var emitter = this.emitter; | ||
// Pass in a single argument as a function then make it the default response handler | ||
if (arguments.length === 1 && typeof(eventOrHash) === 'function') { | ||
emitter.on('response', eventOrHash); | ||
} else if (arguments.length === 1 && typeof(eventOrHash) === 'object') { | ||
_(eventOrHash).each(function(value,key) { | ||
emitter.on(key,value); | ||
}); | ||
} else { | ||
emitter.on(eventOrHash, listener); | ||
} | ||
return this; | ||
} | ||
}); | ||
// Add in the header methods. Again, these ensure we don't get the same header | ||
@@ -218,5 +238,4 @@ // multiple times with different case conventions. | ||
}); | ||
// ... and let someone know if we didn't get any. | ||
} else throw("No event handlers provided. Response will be unused."); | ||
} | ||
// Make sure we were give a URL or a host | ||
@@ -259,4 +278,3 @@ if (!options.url && !options.host) { | ||
var createRequest = function(request) { | ||
var timeout | ||
; | ||
var timeout ; | ||
@@ -263,0 +281,0 @@ request.log.debug("Creating request .."); |
{ "name": "shred" | ||
, "version": "0.6.1" | ||
, "version": "0.6.4" | ||
, "description": "A dead-simple HTTP client" | ||
@@ -4,0 +4,0 @@ , "keywords": [ "http", "client" ] |
@@ -17,7 +17,7 @@ var vows = require('vows') | ||
shred.get({ | ||
var req = shred.get({ | ||
url: "http://localhost:1337/200", | ||
on: { | ||
response: function(response) { | ||
promise.emit("success",response); | ||
promise.emit("success", response); | ||
}, | ||
@@ -30,3 +30,2 @@ error: function(error) { | ||
}); | ||
return promise; | ||
@@ -167,7 +166,13 @@ }, | ||
; | ||
shred.get({ | ||
var handleCount = 0; | ||
var req = shred.get({ | ||
url: "http://localhost:1337/200", | ||
on: { | ||
200: function(response) { | ||
promise.emit("success",response); | ||
handleCount++; | ||
if (handleCount == 2) { | ||
promise.emit("success", response, handleCount); | ||
} | ||
}, | ||
@@ -181,2 +186,9 @@ error: function(error) { | ||
req.on(200, function(response) { | ||
handleCount++; | ||
if (handleCount == 2) { | ||
promise.emit("success", response, handleCount); | ||
} | ||
}) | ||
return promise; | ||
@@ -186,8 +198,61 @@ }, | ||
assert.equal(response.status,200); | ||
}, | ||
"can have multiple callbacks": function(err, response, count) { | ||
assert.equal(count, 2); | ||
} | ||
}, | ||
'A GET request with multiple handlers': { | ||
topic: function() { | ||
var shred = new Shred({ logger: log }) | ||
, promise = new(Emitter) | ||
; | ||
var handleRunCount = 0; | ||
var numberOfHandlers = 0; | ||
var createHandlerFunction = function () { | ||
numberOfHandlers ++; | ||
return function (response) { | ||
handleRunCount++; | ||
if (handleRunCount == numberOfHandlers) { | ||
promise.emit("success", response, numberOfHandlers, handleRunCount); | ||
} | ||
}; | ||
}; | ||
var req = shred.get({ | ||
url: "http://localhost:1337/200", | ||
// Handler defined in the request options hash | ||
on: { | ||
response: createHandlerFunction(), | ||
error: function(error) { | ||
log.debug(error); | ||
log.info("Is rephraser running?") | ||
} | ||
} | ||
}); | ||
// Handler without an event name | ||
req.on(createHandlerFunction()); | ||
// Handler with an event name | ||
req.on('response', createHandlerFunction()); | ||
// Handler with a hash of event names | ||
req.on({ | ||
response: createHandlerFunction() | ||
}); | ||
return promise; | ||
}, | ||
"should be able to have multiple handlers": function(err, response, numberOfHandlers, handleRunCount) { | ||
// Sanity check to make sure that there are handlers: | ||
assert.notEqual(0, numberOfHandlers); | ||
assert.notEqual(1, numberOfHandlers); | ||
}, | ||
"should run all of the handlers": function (err, response, numberOfHandlers, handleRunCount) { | ||
assert.equal(handleRunCount, numberOfHandlers); | ||
} | ||
} | ||
}).export(module); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
237906
1403
3