agent-next
The idea is simple:
- Lets create a function
send(req, cb)
. - Lets say that request is
.url
,.headers
and.body
- Lets say that response is
.status
,.headers
and.body
.
Given that:
// cookie support? Easy! { } // redirects? Easy! { } // Everything is easy.
What this project gives?
- Middlewares
- cookies
- redirects
- unzip
- body parser
- serialize (support for JSON request bodies)
- timeout
- baseUrl (setup base url for all requests)
- handler (normalize results, i.e. attach
req
to errors, etc)
-
Request
,Response
prototypes in the vein of superagent -
Agent
abstraction as an easy way to setup yoursend
function and issueing requests.
What advantages it has over other libs (like request or superagent)?
It is tremendously simpler and far more flexible. For example, you can swap entire http implementation and still have all advanced functionality available.
Examples
basic
var agent = agent
advanced
// Setup an agent specifically for the Github API from scratchvar Agent = var github = Agent // get some info about agent-nextgithub
setup with options
var Agent = // superagent likevar agent = // tweakvar agent =
Notes
- Basic
send
function accepts strings, buffers and simple streams as an request body. - Response body is a simple stream (if not overrided by middleware)
req.url
must be an instance ofAgent.Url
, not a string or an arbitrary object.- You must always either consume, abort or dump response body, usually via middlewares.
- Middlewares should always pass the response object, even on errors, i.e.
always do
cb(err, res)
, not justcb(err)
.
Installation
via npm
npm install agent-next
License
(The MIT License)
Copyright (c) 2013 Eldar Gabdullin eldargab@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.