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 🙏

© 2025 – Pkg Stats / Ryan Hefner

restpack

v0.0.6

Published

Library to pack/unpack data for restful API

Readme

restpack

A library to pack/unpack data for restful APIs, it aims to make developer to implement restful APIs easily.

Installation

You can install it via NPM:

npm install restpack

Usage

RestPack can be used on server-side and client-side both, to make developers to pack/unpack data as restful APIs.

Get Started

Server-side:

var RestPack = require('restpack');

var restpack = new RestPack();

// Set data which is sent out to client, RestPack will be packing it
restpack.setData({
  tags: [
    'Apple', 'Orange'
  ]
});
  
// we can send result directly if server is based on Koa
restpack.sendKoa(this);
// response: { tags: [ 'Apple', 'Orange' ] }

If you are using browserify or webpack as build tool, you are able to use RestPack what is just like you do on server-side.

Client-side:

var RestPack = require('restpack');

// You might access restful API with any kinds of Ajax solutions, then getting HTTP status code and response messages.
var restpack = new RestPack(statusCode, data);

// Getting data
console.log(restpack.getData());

Response For Errors

Here is an example to show how to make a response from server to client.

Server-side:

var RestPack = require('restpack');

var restpack = new RestPack();

// HTTP status code 422 is for Validation faild, and errors we set will be appended to response message.
restpack
  .setStatus(RestPack.Status.ValidationFailed)
  .appendError('username', RestPack.Code.AlreadyExist)
  .appendError('email', RestPack.Code.Invalid)
  .appendError('name', RestPack.Code.Required);
  
// we can send result directly if server is based on Koa
restpack.sendKoa(this);
/*
  status code: 422
  response: {
    message: 'Validation Failed',
    errors: [
      { field: 'username', code: 4  },
      { field: 'email', code: 2  },
      { field: 'name', code: 1  }
    ]
  }
*/

You can use RestPack to get status and error messages by parsing data sent from server.

Client-side:

var RestPack = require('restpack');

// You might access restful API with any kinds of Ajax solutions, then getting HTTP status code and response messages.
var restpack = new RestPack(statusCode, data);

// It's validation failed
if (restpack.status == RestPack.Status.ValidationFailed) {

  // Getting all field errors
  restpack.errors.forEach(function(err) {
      console.log('Field:', err.field);
      
      // Type of error
      switch(err.code) {
      RestPack.Code.Required:
        console.log('Code: Required');
      RestPack.Code.Invalid:
        console.log('Code: Invalid');
      RestPack.Code.NotExist:
        console.log('Code: NotExist');
      RestPack.Code.AlreadyExist:
        console.log('Code: AlreadyExist');
      }
  });
}

Status In Common Use

Several status in common use has been supported already.

RestPack.Status = {
    OK: { status: 200 },
    Created: { status: 201 },
    BadRequest: { status: 400 },
    Forbidden: { status: 403 },
    ServiceExpired: { status: 403, message: 'Service Expired' },
    AccountBlocked: { status: 403, message: 'Account Blocked' },
    PermissionDenied: { status: 403, message: 'Permission Denied' },
    NotFound: { status: 404 },
    ValidationFailed: { status: 422, message: 'Validation Failed' },
    Error: { status: 500 },
    ServiceUnavailable: { status: 503, message: 'Service Unavailable' }
};

Errors In Common Use

Several error code in common use has been supported already.

RestPack.Code.Required
RestPack.Code.Invalid
RestPack.Code.NotExist
RestPack.Code.AlreadyExist

License

Licensed under the MIT License

Authors

Copyright(c) 2016 Fred Chien <[email protected]>