Awesome
Kenobi
Render external objects and array in view engines
Install
npm install kenobi
How 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.name
Request 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 theuri
-
method
- http method (default:"GET"
) -
headers
- http headers (default:{}
) -
body
- entity body for PATCH, POST and PUT requests. Must be aBuffer
orString
. -
form
- when passed an object or a querystring, this setsbody
to a querystring representation of value, and addsContent-type: application/x-www-form-urlencoded; charset=utf-8
header. When passed no options, aFormData
instance is returned (and is piped to request). -
auth
- A hash containing valuesuser
||username
,pass
||password
, andsendImmediately
(optional). See documentation above. -
json
- setsbody
but to JSON representation of value and addsContent-type: application/json
header. Additionally, parses the response body as JSON. -
multipart
- (experimental) array of objects which contains their own headers andbody
attribute. Sendsmultipart/related
request. See example below. -
followRedirect
- follow HTTP 3xx responses as redirects (default:true
). This property can also be implemented as function which getsresponse
object as a single argument and should returntrue
if redirects should continue orfalse
otherwise. -
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 onsetEncoding
of response data. Ifnull
, thebody
is 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 theurl
parameter (by embedding the auth info in theuri
). -
oauth
- Options for OAuth HMAC-SHA1 signing. See documentation above. -
aws
-object
containing AWS signing information. Should have the propertieskey
,secret
. Also requires the propertybucket
, unless you’re specifying yourbucket
as part of the path, or the request doesn’t use a bucket (i.e. GET Services)
View Engines
Who's using Kenobi
You're using Kenobi too? Let me know :)
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