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

jsdatarouter

v0.0.1

Published

A library to ease the development of RESTful api's written with express and js-data

Downloads

4

Readme

JS-Data Router

This library provides a set of default Express routes and handlers for use with JSData to enable rapid RESTful API prototyping

Installation

npm install should get you all you need: npm install jsdatarouter

Usage

The JSDataRouter is an ES6 class that takes a JSData resource definition (the same returned by DS.defineResource) and express router object in its constructor and sets up RESTful routes and handlers on the router passed in. If no router is provided it will create a new one using express.Router()

var DS = require('../DataStore'),
  JSDataRouter = require('jsdatarouter'),
  express = require('express'),
  router = new JSDataRouter(DS.store.definitions.state).router;

module.exports = function (app) {
  app.use('/todo', router);
};

Default Actions

CREATE: POST /

create:

calls the JSData create function with req.body as the argument

afterCreate:

sends the resource instance created in the create function and calls end()


DESTROY: DELETE /:resourceId

destroy

calls the JSData destroy function with resourceId as the argument

afterDestroy:

sends a blank 200 response


FIND: GET /:resourceId

find:

calls next()

afterFind:

sends the found resource instance and calls end()


FINDALL: GET /

findAll:

retrieves all resource instances from the DataStore by calling the JSData findAll function with req.query and a param object of the form { with: req.with } allowing you to specify eager-loads

afterFindAll:

sends the resource instances located in the findAll function and calls end()


UPDATE: PUT /:resourceId

update:

calls the JSData update function on the resource instance with the id of resourceId using req.body as an argument

afterUpdate:

sends the resource instance that was updated in the update function and calls end()

Extending Actions

Every function on the JSDataRouter class can be re-implemented to add additional functionality. The example below adds server-side validation to a CREATE route before creating the resource instance.

function validateCreateInput(req) {
  // ...
}

class TodoRouter extends JsDataRouter {
  beforeCreate(req, res, next) {
    validateCreateInput(req);
    var errors = req.validationErrors();
    if(errors) {
      res.send('validation errors: ' + util.inspect(errors), 400).end();
      return;
    }
    next();
  }
}

Param Callback

Routes that include a resourceId parameter will automatically retrieve the resource instance that has the specified ID form the DataStore using an express param callback, and make it available via req.resource. Query parameters and eager-loads (specified on req.with) along to the JSData find function when looking up the resource instance.

Options

By default all routes are enabled, but an optional options object can be passed into the constructor as a 3rd argument that allows you to disable each action individually. The example below will setup a new router with only the destroy route disabled.

var router = new JSDataRouter(
  DS.store.definitions.state,
  express.Router(), {
    destroy: false
  }
);