npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kenobi

v0.2.1

Published

Render objects for view engines or static html

Downloads

21

Readme

Kenobi

Render external objects and array in view engines

NPM Version Build Status

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 from url.parse()

  • qs - object containing querystring values to be appended to the uri

  • method - http method (default: "GET")

  • headers - http headers (default: {})

  • body - entity body for PATCH, POST and PUT requests. Must be a Buffer or String.

  • form - when passed an object or a querystring, this sets body to a querystring representation of value, and adds Content-type: application/x-www-form-urlencoded; charset=utf-8 header. When passed no options, a FormData instance is returned (and is piped to request).

  • auth - A hash containing values user || username, pass || password, and sendImmediately (optional). See documentation above.

  • json - sets body but to JSON representation of value and adds Content-type: application/json header. Additionally, parses the response body as JSON.

  • multipart - (experimental) array of objects which contains their own headers and body attribute. Sends multipart/related request. See example below.

  • followRedirect - follow HTTP 3xx responses as redirects (default: true). This property can also be implemented as function which gets response object as a single argument and should return true if redirects should continue or false 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 on setEncoding of response data. If null, the body is returned as a Buffer.

  • 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 the url parameter (by embedding the auth info in the uri).

  • oauth - Options for OAuth HMAC-SHA1 signing. See documentation above.

  • aws - object containing AWS signing information. Should have the properties key, secret. Also requires the property bucket, unless you’re specifying your bucket as 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

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT License © Raphael Amorim