New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

createrest

Package Overview
Dependencies
Maintainers
3
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

createrest

REST routes constructor for express and koa

  • 0.15.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created
Source

createrest npm

Docs at https://createrest.js.org

Usage example

// routes.js
const {
  createRest,
  printRoutes,
} = require('createrest')

const before1 = () => { console.log('before1()') }
const before2 = () => { console.log('before2()') }
const before3 = () => { console.log('before3()') }
const after1 = () => { console.log('after1()') }
const after2 = () => { console.log('after2()') }
const after3 = () => { console.log('after3()') }
const post1 = () => { console.log('post1()') }
const get1 = () => { console.log('get1()') }
const get2 = () => { console.log('get2()') }
const put3 = () => { console.log('put3()') }

const ExampleController = {
  beforeEach() { console.log('Call before each handler') },
  afterEach() {},
  read() {},
  create() {},
  update() {},
  destroy () {},
}

const BooksController = {
  beforeEach() { console.log('Call before each handler') },
  afterEach() {},
  index() {},
  create() {},
  read() {},
  update() {},
  patch() {},
  destroy () {},
}

const routes = createRest(root => {
  root.beforeEach(before1)
  root.afterEach(after1)

  root.post('/', post1)

  root.scope('demo', demoRoute => {
    demoRoute.beforeEach(before2)
    demoRoute.afterEach(after2)

    demoRoute.get('/', get1)
    demoRoute.get('/foo', get2)

    demoRoute.scope('bar', barRoute => {
      barRoute.beforeEach(before3)
      barRoute.afterEach(after3)

      barRoute.put('/', put3)

      barRoute.crud('example', ExampleController)
    })
  })

  root.resources('books', BooksController)
})

module.exports = routes

Express

More in it's repo createrest-express

const { createRestExpress } = require('createrest-express')
const express = require('express')
const routes = require('./routes')

const app = express()

app.use(createRestExpress(routes))

app.listen(4001, () => {
  printRoutes(routes)
})

Koa

More in it's repo createrest-koa

const Koa = require('koa')
const { createKoaRouter } = require('createrest-koa')
const routes = require('./routes')

const app = new Koa()
const router = createKoaRouter(routes)

app.use(router.routes(), router.allowedMethods())

app.listen(3000, () => {
  printRoutes(routes)
})

Output:

POST / -> before1(), post1(), after1()
GET /demo/ -> before1(), before2(), get1(), after2(), after1()
GET /demo/foo/ -> before1(), before2(), get2(), after2(), after1()
PUT /demo/bar/ -> before1(), before2(), before3(), put3(), after3(), after2(), after1()
GET /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), read(), afterEach(), after3(), after2(), after1()
POST /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), create(), afterEach(), after3(), after2(), after1()
PUT /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), update(), afterEach(), after3(), after2(), after1()
DELETE /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), destroy(), afterEach(), after3(), after2(), after1()
GET /books/ -> before1(), beforeEach(), index(), afterEach(), after1()
POST /books/ -> before1(), beforeEach(), create(), afterEach(), after1()
GET /books/:bookId/ -> before1(), beforeEach(), read(), afterEach(), after1()
PUT /books/:bookId/ -> before1(), beforeEach(), update(), afterEach(), after1()
PATCH /books/:bookId/ -> before1(), beforeEach(), patch(), afterEach(), after1()
DELETE /books/:bookId/ -> before1(), beforeEach(), destroy(), afterEach(), after1()

FAQs

Package last updated on 30 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc