kenobi v0.2.1
Kenobi
Render external objects and array in view engines
Install
npm install kenobiHow it works
In this example we are using the product hunter's API.
The options variable sets the request.
var options = {
url: "https://api.producthunt.com/v1/posts",
auth: {
bearer: "XXXXXXX"
},
headers: {
'User-Agent': 'request'
},method: "GET", json: true, timeout: 10000 };
After it is sent as the first parameter, after is set the page view path.
kenobi(options, '/views/index.ejs', function(page){ res.end(page); // Response });
Detail: You can sent only the url string instead request object. However
the request body act as default (see default properties), ex:
kenobi('https://api.producthunt.com/v1/posts', '/views/index.ejs', function(page){
res.end(page); // Response
});So, then we can treat the object in view. Then a global object is returned. Accessed by the name of body. See how it happens in ejs example:
<body>
<% if (_.posts.length) { %>
<ul>
<% _.posts.forEach(function(post){ %>
<li><%= post.name %></li>
<% }) %>
</ul>
<% } %>
</body>Get response without view
For return only response object:
kenobi(options, function(response, err){if (err) res.end(err); res.send(response); });
Send only a local object (without external request) and render in template:
Use request false
var object = {name: 'luke', request: false};
kenobi(object, pathTofile, function(page, response, err){
// For local objects cases, response always be null
if (err) res.end(err);
res.end(page);
});Using HTML instead template option:
This option is still very limited, the current version is still not possible to make operations and comparisons with the variables passed to the html. Ex:
anything.js
var object = {name: 'Obi Wan'};
kenobi(object, 'index.html', function(page, response, err){
if (err) res.end(err);
res.send(page);
});index.html
...
<body>
<h1>{{name}}</h1>
</body>
...Callback Return
page String = result of rendering
response Object = response from request
error Object = error from operation, if not exist must be null
kenobi(options, pathTofile, function(page, response, err){ console.log("Response: " + response); res.send(page); });
In Response Object, you can access some data like statusCode, body...
Template Examples
It sent an object to the template with the primary key accessed by an underscore. Examples:
Ejs:
<% _.posts.forEach(function(post){ %>
<li><%= post.name %></li>
<% }) %>Jade:
each post in _.posts
li= post.nameRequest Object (optional params)
The first argument can be either a url or an object. The only required option is uri, all others are optional.
uri||url- fully qualified uri or a parsed url object fromurl.parse()qs- object containing querystring values to be appended to theurimethod- http method (default:"GET")headers- http headers (default:{})body- entity body for PATCH, POST and PUT requests. Must be aBufferorString.form- when passed an object or a querystring, this setsbodyto a querystring representation of value, and addsContent-type: application/x-www-form-urlencoded; charset=utf-8header. When passed no options, aFormDatainstance is returned (and is piped to request).auth- A hash containing valuesuser||username,pass||password, andsendImmediately(optional). See documentation above.json- setsbodybut to JSON representation of value and addsContent-type: application/jsonheader. Additionally, parses the response body as JSON.multipart- (experimental) array of objects which contains their own headers andbodyattribute. Sendsmultipart/relatedrequest. See example below.followRedirect- follow HTTP 3xx responses as redirects (default:true). This property can also be implemented as function which getsresponseobject as a single argument and should returntrueif redirects should continue orfalseotherwise.followAllRedirects- follow non-GET HTTP 3xx responses as redirects (default:false).maxRedirects- the maximum number of redirects to follow (default:10).encoding- Encoding to be used onsetEncodingof response data. Ifnull, thebodyis returned as aBuffer.timeout- Integer containing the number of milliseconds to wait for a request to respond before aborting the request.proxy- An HTTP proxy to be used. Supports proxy Auth with Basic Auth, identical to support for theurlparameter (by embedding the auth info in theuri).oauth- Options for OAuth HMAC-SHA1 signing. See documentation above.aws-objectcontaining AWS signing information. Should have the propertieskey,secret. Also requires the propertybucket, unless you’re specifying yourbucketas part of the path, or the request doesn’t use a bucket (i.e. GET Services)
View Engines
History
See Changelog for more details.
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -m 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request :D
License
MIT License © Raphael Amorim