kusarequest
v0.5.1
Published
Promisified simple request module. This module focuses on chainable request.
Downloads
2
Readme
kusarequest
Promisified simple request module. This module focuses on chainable request.
This module is wrapped to request and bluebird.
Installation
$ npm i kusarequest
var Kusarequest = require("kusarequest");
new Kusarequest.get(...)
Usage
Basic request
Kusarequest will use in the instance. And instance will request in the HTTP method.
Kusarequest support HTTP method => ["get", "head", "post", "put", "patch", "del"]
It return Promise. Promise's Argument is Kusarequest instance.
new Kusarequest().get("https://example.com").then(function(kusarequest) {
assert.strictEqual(kusarequest.res.body, "get");
});
Request with option
Option is equivalent to the request module option.
new Kusarequest().get("https://example.com/option", {
"qs": {
"key": "get_value"
}
})
.then(function(kusarequest) {
assert.strictEqual(kusarequest.res.body, "get_value");
});
Request with callback
Promise to return from the callback is executed.
Callback of this it has been bound from the instance.
new Kusarequest().get("https://example.com", function(res) {
// [this] is bound from the Kusarequest instance
this.container = res.body + "container";
})
.then(function(kusarequest) {
assert.strictEqual(kusarequest.container, "getcontainer");
});
Request with option and callback
Kusarequest().get("https://example.com/option", {
"qs": {
key: "get_value"
}
}, function(res) {
this.container = res.body + "container";
})
.then(function(kusarequest) {
assert.strictEqual(kusarequest.container, "get_valuecontainer");
});
Feature
Chaninable
Kusarequest has a method chain concept.
Kusarequest().post("https://example.com/form", {
"form": {
"key": "post_value"
}
})
.then(function(kusarequest) {
assert.strictEqual(kusarequest.res.body, "post post_value");
});
path only ok
Uri of the instance is okay only path.
Kusarequest().get("https://example.com/")
.then(function(kusarequest) {
// this is request to https://example.com/some/path
return kusarequest.get("/some/path");
})
.then(function(kusarequest) {
assert.strictEqual(kusarequest.res.body, "path");
});
Continuation of cookie
Kusarequest instance will take over the cookie of the each request.
This is useful, for example, when you are scraping after login.
kusarequest.post("https://example.com/login", {
"form": {
"id": "id",
"password": "password"
}
})
.then(function (kusareq) {
// kusareq keep logged in (cookie)
kusareq.get(...)
})
You can also set the cookie-jar when you make the instance.
var jar;
new Kusarequest().post("https://example.com/login")
.then(function(kusarequest) {
jar = kusarequest.jar(); // get logged in jar
})
// set logged in jar
new Kusarequest(jar).get("/after/login/pass");
.then(function(kusarequest) {
assert.strictEqual(kusarequest.res.body, "success");
});
Follow all redirect
Redirection of GET and non-GET HTTP method is automatically follow.
// exsample) /redirect-from -- redirect --> /redirect-to
kusarequest.get("/redirect-from")
.then(function (kusareq) {
console.log(kusareq.url) // /redirect-to
});
// nod-GET redirects support by default.
kusarequest.post("/redirect-from")
.then(function (kusareq) {
console.log(kusareq.url) // /redirect-to
});
Response history
Kusarequest instance has a history of each response to r_histories
property.
Kusarequest().get(BASE_URL)
.then(function(kusareqest) {
return kusareqest.get("/path");
})
.then(function(kusareqest) {
return kusareqest.get("/option", {"qs": {"key": "option"}});
})
.then(function(kusareqest) {
var bodys = kusareqest.r_histories.map(function(res) {
return res.body;
});
assert.strictEqual(bodys.join(" "), "option path get");
});
Instance property and method
instance propaty is this.
property
res
- request's response.toJSON()raw_res
- original request responser_history
-res
historycontainer
- Use this when you want to exceed the each [then]
method
jar
- get cookie-jar