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

next-model-api-router

v0.1.0

Published

Api Router for next-model package.

Downloads

4

Readme

NextModelApiRouter

Api Router for NextModel package.

Build Status

Features:

  • Shared Router for Server and Client.
  • Allows to plug in every server or client side Api connnector.
  • Allows Api-Endpoint to de on a different domain
  • Api Versioning
  • Custom Routes and Actions

Roadmap / Where can i contribute

See GitHub project for current progress/tasks

  • Fix Typos
  • Add user credentials to queries
  • Add more examples
  • There are already some tests, but not every test case is covered

TOC

Examples

Create Router with Api route:

const router = new NextModelApiRouter({
  domain: 'http://example.com',
  path: 'api',
  version: 'v1',
});

router.resouce('User'); // modelName

Routes:

all    : POST http://example.com/api/v1/users
first  : POST http://example.com/api/v1/users/first
last   : POST http://example.com/api/v1/users/last
count  : POST http://example.com/api/v1/users/count
insert : POST http://example.com/api/v1/users/create

show   : POST http://example.com/api/v1/user/:user_id
update : POST http://example.com/api/v1/user/:user_id/update
delete : POST http://example.com/api/v1/user/:user_id/delete

Pick only some default actions:

router.resouce('User', {
  only: ['all'],
});

Routes:

all    : POST /users

Modify default actions:

router.resouce('User', {
  only: {
    all: { method: 'get' },
  },
});

Routes:

all    : GET  /users

Exclude some default actions:

router.resouce('User', {
  except: ['create', 'update', 'delete'],
});

Routes:

all    : POST /users
first  : POST /users/first
last   : POST /users/last
count  : POST /users/count

show   : POST /user/:user_id

Add collection actions:

router.resouce('User', {
  only: [],
  collection: ['foo'],
});

Routes:

foo    : POST /users/foo

Customize collection actions:

router.resouce('User', {
  only: [],
  collection: {
    foo: { path: 'bar' },
  },
});

Routes:

foo    : POST /users/bar

Add member actions:

router.resouce('User', {
  only: [],
  member: ['foo'],
});

Routes:

foo    : POST /user/:user_id/foo

Customize member actions:

router.resouce('User', {
  only: [],
  member: {
    foo: { path: 'bar' },
  },
});

Routes:

foo    : POST /user/:user_id/bar

Api Route

The Api Route is defined while creating an router. The Domain, Path and Api version can be defined:

const router = new NextModelApiRouter({
  domain: 'http://example.com',
  path: 'api',
  version: 'v1',
});

router.resouce('User');

Routes:

all    : POST http://example.com/api/v1/users
first  : POST http://example.com/api/v1/users/first
last   : POST http://example.com/api/v1/users/last
count  : POST http://example.com/api/v1/users/count
insert : POST http://example.com/api/v1/users/create

show   : POST http://example.com/api/v1/user/:user_id
update : POST http://example.com/api/v1/user/:user_id/update
delete : POST http://example.com/api/v1/user/:user_id/delete

Api Domain

The Api Domain is optional and only used on the Client side. Use this parameter when your Api is running on an different domain than the UI.

Default: ''

const router = new NextModelApiRouter({
  domain: 'http://example.com',
});
http://example.com/ ...

Domain does not need to have an protocol defined.

const router = new NextModelApiRouter({
  domain: '//example.com',
});
//example.com/ ...

Api Path

The path defines the root folder in which the Api runs.

Default: ''

const router = new NextModelApiRouter({
  path: 'api',
});
/api/ ...

The path can be nested:

const router = new NextModelApiRouter({
  path: 'app/api',
});
/app/api/ ...

Api Version

The Api version is usually defined among with the Api Path.

Default: ''

const router = new NextModelApiRouter({
  path: 'api',
  version: 'v1',
});
/api/v1/ ...

Can be in a nested path:

const router = new NextModelApiRouter({
  path: 'app/api',
  version: 'latest',
});
/app/api/latest/ ...

Resource Routes

Each resource added to the Router has some predefined Api routes.

Default Routes for User model:

all    : POST /users
first  : POST /users/first
last   : POST /users/last
count  : POST /users/count
insert : POST /users/create

show   : POST /user/:user_id
update : POST /user/:user_id/update
delete : POST /user/:user_id/delete

Defaults

Each resource can have some defaults which are used for every route.

Default:

{
  collectionTransform: 'pluralize',
  memberTransform: 'singularize',
  method: 'post',
  postfix: '',
}

The name of the resource is inherited by the tableName of the model. This can be changed with the name property:

router.resouce('User', {
  collection: {
    defaults: { name: 'foo' },
  },
});

Routes:

all    : POST /foos
first  : POST /foos/first
last   : POST /foos/last
count  : POST /foos/count
insert : POST /foos/create

show   : POST /foo/:foo_id
update : POST /foo/:foo_id/update
delete : POST /foo/:foo_id/delete

Path

The path can be defined for every action. The default action has already predefined paths. The custom collection or member actions have a default path which matches the action name.

router.resouce('User', {
  collection: {
    foo: {},
  },
});

Routes:

foo    : POST /users/foo

With custom path:

router.resouce('User', {
  collection: {
    foo: { path: 'bar' },
  },
});

Routes:

foo    : POST /users/bar

Method

The http methods url can be defined for every action (or globally per resource with defaults).

Default: 'post'

Please Note: The default is post for every route, cause the filter payload can be too large for query strings. Its not recommended to use get or other requests where payload is within the querystring.

router.resouce('User', {
  only: {
    all: { method: 'get' },
    first: { method: 'get' },
    last: { method: 'get' },
    count: { method: 'get' },
    create: { method: 'post' },
    show: { method: 'get' },
    update: { method: 'put' },
    delete: { method: 'delete' },
  },
});

Routes:

all    : GET    /users
first  : GET    /users/first
last   : GET    /users/last
count  : GET    /users/count
create : POST   /users/create

show   : GET    /user/:user_id
update : POST   /user/:user_id/update
delete : DELETE /user/:user_id/delete

Name

The name of the resouce can be changed per action, but its recommended to change it per resource with the defaults settings.

router.resouce('User', {
  only: {
    all: {},
    first: {},
    last: { name: 'foo' },
    update: {},
    delete: { name: 'bar' },
  },
});

Routes:

all    : POST /users
first  : POST /users/first
last   : POST /foos/last

update : POST /user/:user_id/update
delete : POST /bar/:bar_id/delete

Transform

The name is automatically transformed to plural on collection actions and transformed to singular on member actions by the default settings. Possible transforms are pluralize, singularize and none.

router.resouce('User', {
  only: {
    all: {},
    first: {},
    last: { transform: 'singularize' },
    update: {},
    delete: { transform: 'pluralize' },
  },
});

Routes:

all    : POST /users
first  : POST /users/first
last   : POST /user/last

update : POST /user/:user_id/update
delete : POST /users/:user_id/delete

Postfix

The name of the resouce can be changed per action, but its recommended to change it per resource with the defaults settings.

router.resouce('User', {
  only: {
    all: {},
    first: {},
    last: { postfix: '.json' },
    update: {},
    delete: { postfix: '.json' },
  },
});
all    : POST /users
first  : POST /users/first
last   : POST /users/last.json

update : POST /user/:user_id/update
delete : POST /user/:user_id/delete.json

Changelog

See history for more details.

  • 0.1.0 2017-03-15 First public release