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

respond-to

v0.0.1

Published

Responds to requests in the desired format, the DRY way (based on Rails).

Downloads

2

Readme

Respond-to

Respond-to is an asynchronous module for Node.js that aims to replicate RoR's respond_to method. It comes with a XML formatter, it uses the built-in JSON serializer and depends on the official msgpack module.

Download

The source is available for download from GitHub. Alternatively, you can install using Node Package Manager (npm):

npm install respond-to

How to use

The easiest way to use respond-to is by calling the respondTo-function:

var respondTo = require('respond-to');
respondTo('xml', {
	users: [
		{
			id: 1,
			username: "Timvanelsloo"
		}
	]
}, function(err, data) {
	console.log(err, data);
});

This is short for:

respondTo.xml({
   ...
}, function(err, data) {});

Currently supported formats are: xml, json and msg (msgpack).

Serializing

Serializing by default is done by pulling all keys and values from an object. You can override this behaviour by implementing an as-function with the following prototype:

Something.prototype.as = function(format, options, callback) {
	
};

The format variable contains any of the supported formats (as a string). options contains a hash derived from the original options hash that was used when calling respondTo.*. It also contains a function include(key). You can call that method in order to determine what keys should be returned and what not. When you're done, you can call callback(err, result). The result can be any type of a serializable javascript object, and (when available) as gets called for that object as well.

Filtering

You can either filter by including, or by excluding certain keys. You should not both include and exclude keys at the same time.

A great feature in filtering is the ability to filter multidimensional keys.

For example, when you define an object like this:

{
    users: [
        {
            id: 1,
            name: {
                first: "Tim",
                last: "van Elsloo"
        	}
        }
    ]
}

And filter it with these keys:

{
	include: ['users.id', 'users.name.first']
}

On a sidenote, these options can be inverted into:

{
	exclude: ['users.name.last']
}

This results in:

{
	users: [
		{
			id: 1,
			name: {
				first: "Tim"
			}
		}
	]
}

You also can prototype users.name to override as(format, options, callback) with the following function:

name.as = function(format, options, callback) {
	if (options.include('first') && !options.include('last'))
		return callback(null, this.first);
	return {first: this.first, last: this.last};
};

This replaces the name-hash with only the first-value:

name: "Tim"

Please note that when you're only including certain keys, multi-dimensional keys are flattened:

['users.name.first'] == ['users', 'users.name', 'users.name.first']

Recommended: using it with Express 3.x

When you're using Express, you can also call respondTo with your response as the first parameter:

exports.index = function(req, res) {
    respondTo(res, {
        users: [
			{
				id: 1,
				username: "Timvanelsloo"
			}
		]
    });
}

Respond-to automatically determines the requested content-type in the following order:

  1. Check the query for a "format" parameter. This can be one of the supported formats (xml, json and msg). This only works when no req.params.format is set!
  2. Respond-to then checks for the req.params.format. Recommended: define a route as "/users/all.:format?".
  3. Lastly, if none of the above steps resulted in a content-type, the Accept http header is used.
  4. Default: json.

Documentation

Express

Vanilla (without Express)

Express

respondTo(res, data, options = {}, callback = null)

This function aims at abstracting all serializing processes as much as possible. The use of this function is very similar to render (except for that it isn't an instance method).

Arguments

  • res - An Express response. This always contains a reference to the original request so there's no need to explicitly include that.
  • data - A serializable javascript object.
  • options - Optional - The options-hash can be used for filtering keys.
  • callback(err, data) - Optional - Note that when a callback is defined, res.send(data) is not called.

Vanilla (without Express)

respondTo(format, data, options = {}, callback)

You can use this function when you aren't using Express.

Arguments

  • format - One of these: "json", "msg" and "xml".
  • data - A serializable javascript object.
  • options - Optional - The options-hash can be used for filtering keys.
  • callback(err, data)

respondTo.json(data, options = {}, callback)

Arguments

  • data - A serializable javascript object.
  • options - Optional - The options-hash can be used for filtering keys.
  • callback(err, data)

respondTo.msg(data, options = {}, callback)

Arguments

  • data - A serializable javascript object.
  • options - Optional - The options-hash can be used for filtering keys.
  • callback(err, data)

respondTo.xml(data, options = {}, callback)

Arguments

  • data - A serializable javascript object.
  • options - Optional - The options-hash can be used for filtering keys.
  • callback(err, data)