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

parkes-router

v0.2.0

Published

Extension to Koa Router to simply define RESTful resources

Downloads

36

Readme

A Koa Router extension supporting dynamic CRUD resource nesting via controllers.

parkes-router helps reduce redundant syntax within resource routing by providing a streamlined way to describe resources and it's supported methods (show, index, create, update, and destroy).

Dependencies

parkes-router is built for koa 2 and requires async/await in node 7.6

Getting Started

npm install --save raisely/parkes-router

const Koa = require('koa');
const ParkesRouter = require('parkes-router');

// include the controllers you need
const fruitController = require('./path/to/controller-1');
const appleController = require('./path/to/controller-2');
const dairyController = require('./path/to/controller-3');

// setup the parkes-router
const router = new ParkesRouter();
router
	.resource('fruit', fruitController, (parent) => {
		parent.resource('apple', appleController, ['index']);
	})
	.resource('dairy', dairyController, ['create', 'index', 'show']);


// bind it to your Koa instance
const app = new Koa();
app
	.use(bodyparser)
	.use(router.routes());


// could also be mounted just like Koa Router
const mount = require('koa-mount');
app.use(mount('/v1', router.routes()));

Usage

pakes-router lets you use router.resource() to generate RESTful CRUD routes and nest resources.

parkes-router expects controllers to have methods specific to the CRUD actions it preforms.

| action | HTTP method | URL path | example | | --- | --- | --- | --- | | index | GET | /resource | GET /apples | | show | GET | /resource/:resource | GET /apples/1 | | create | POST | /resource | POST /apples | | update | PATCH | /resource | PATCH /apples/1 | | destroy | DELETE | /resource/:resource | DELETE /apples/1 |

Implementation

Collection names are automatically pluralized, so names such as apple will become apples when mapped as endpoints.

For actions requiring a resource id (create, update, destroy), the route has a parameter by the name of the resource (singular). (eg /apples/:apple)

Non nested resource

// with all CRUD actions allowed
router.resource('collection-name', controller)

// bound to specific CRUD actions
router.resource('collection-name', controller, ['show', 'index', 'create', 'update', 'destroy'])

Nested collection resource

When nesting resources, it is considered a best practice to only have a create and index method for parent controllers. This allows the grouping of specifc items (in example: GET /fruit/1/apples, PATCH /apples/1).

router.resource('parent-controller', parentController, (parent) => {
	// child with all methods allowed
	parent.resource('child-controller-1', childController1);
	// child with limited CRUD actions
	parent.resource('child-controller-2', childController2, ['index', 'create', 'destroy']);
});

Method overview

GET show

Displays a specific item (by key) within a controller collection.

parent.resource('collection', controller, ['show']);
// mounted as (GET) /collection/:key

GET index

Displays a paginated item list within a controller collection.

router.resource('collection', controller, ['index']);
// mounted as (GET) /collection

POST create

Allows the creation of items within a controller collection.

router.resource('collection', controller, ['create']);
// mounted as (POST) /collection

PATCH update

Allows the updating of a specific item within a controller collection.

router.resource('collection', controller, ['update']);
// mounted as (PATCH) /collection/:key

DELETE destroy

Allows the deletion (or disabling) of a specific item within a controller collection.

router.resource('collection', controller, ['destroy']);
// mounted as (DELETE) /collection/:key

© 2017 Agency Ventures