Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@barelyhuman/conrou

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@barelyhuman/conrou

> **Con**troller bound **Rou**tes

  • 0.1.2-alpha.7
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
Maintainers
1
Weekly downloads
 
Created
Source

conrou

Controller bound Routes

Simple abstraction to bind controller classes to routes using action aliases. Kinda like Laravel/Ruby on Rails

router.post('/users', 'UserController.create')
// or
router.resource('/users', 'UserController')

Installation

npm i @barelyhuman/conrou

Usage

The following uses express.Router as an example but you can use any router that has the get, post, put methods with the signature of (url:string,handler:func)

const _router = express.Router()
const router = createControllerBinder(_router)

class UserController {
  me(_, res) {
    return res.send('reaper')
  }
}

// Register the controller to the `router` container
// You are to register an uninstantiated class
router.register(UserController)

// or, you can create an alias
router.register(UserController, {
  alias: 'user',
})

router.get('/me', 'UserController.me')
// or, if you created an alias
router.get('/me', 'user.me')
// or
router.get('/me', (req, res) => {
  console.log('hello')
  res.end()
})

// to get the route for a particular controller, you can use the
// `routeForAction` method on the router.

router.routeForAction('UserController.me') //=> /me
router.routeForAction('user.me') //=> /me

Middleware

Each router interface that you use might have a different way of handling middleware and so it's not really recommended to use conrou's middleware utility for this.

Though, conrou does provide a way for you to add in middleware that mimic the execution order similar to express.

// Using `polka` as an example
const app = polka()
// Register the router as before
const router = createControllerBinder(app)

router.register(AuthController)
router.registerMiddleware('auth', (req, res, next) => {
  req.currentUser = {
    id: 1,
  }
  next()
})

router
  .get('/user', (req, res) => {
    console.log(req.currentUser) // 1
    return res.end()
  })
  // tie the named middleware to this particular route
  // this syntax / API was inspired by AdonisJS (https://github.com/adonisjs/)
  .middleware(['auth'])

LICENSE

MIT

FAQs

Package last updated on 04 Jun 2023

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