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

hypercontroller

v1.0.17

Published

![](media/cover.png)

Downloads

19

Readme

Hypercontroller

✅ Structured and declarative project layout with controllers, actions, and middleware
✅ Bring your own framework: use with Fastify or Express
✅ Automatic routing and mounting
✅ Helpers and best-practices for strong params and async promise-based flows
✅ TypeScript-first
✅ Generators driven (quickly add controllers, actions)

import { Controller, get, postWithRoute, permitParams } from 'hypercontroller'
const accountParams = permitParams('account', ['name'])

@Controller()
class AccountController {
  @get()
  index(_req, res) {
    res.json({ name: 'Homer Simpson' })
  }

  @postWithRoute('/')
  async update(_req, res) {
    const params = accountParams(req.body)
    const record = await save(params)
    res.json(record)
  }
}

Quick Start

Install:

$ yarn add hypercontroller

Set up a server.ts file using a framework of your choice:

import express from 'express'
import { Server, printMountpoints, ExpressAdapter } from 'hypercontroller'
import AccountController from './controllers/account'

const server = new Server(new ExpressAdapter(express))
const mountpoints = server.mount([
  new AccountController()
])

printMountpoints(mountpoints)

const port = process.env.PORT || 5160
const createServer = () =>
  server
    .start()
    .then(({ opts }) => console.log(`Listening on ${opts.port}`))
    .then(() => server)
createServer()

And run (here using ts-node):

$ ts-node server.ts

Controllers, Actions and Middleware

Hypercontroller tries to follow the same concepts as Rails ActionController.

A controller is created with the @Controller decorator and a plain class:

@Controller()
class AccountController {
    ...

You add actions (request handlers) to controllers by marking it with an HTTP verb decorator:

import { Controller, get } from 'hypercontroller'

@Controller()
class AccountController {
  @get()
  index(_req, res) {
    res.json({ name: 'Homer Simpson' })
  }
  ...

In terms of routing, hypercontroller will infer the route name from the decorated subject. When index or Index is used, it will use the index route / instead of the literal word index.

In any case, hypercontroller lets you specify a route explicitly with the WithRoute variant of the Controller decorator and each of the HTTP verb decorators.

@Controller()
class AccountController {
  @get()
  index(_req, res) {
    res.json({ name: 'Homer Simpson' })
  }

  @postWithRoute('/')
  async update(_req, res) {
    const params = accountParams(req.body)
    const record = await save(params)
    res.json(record)
  }
}

This controller will form the following routes once mounted:

 GET  /accounts
POST  /accounts

Any decorator you use accepts middleware as an array of middleware or a single instance:

@Controller(jwtAuth)
class AccountController {
  @get([cacheWithEtag, compress])
  index(_req, res) {
    res.json({ name: 'Homer Simpson' })
  }
  ...

All middleware are the same ones you would originally use with Express or Fastify.

Server and Mounting

Hypercontroller's Server is an entrypoint that takes controllers and understands their structure, and mount actions and middleware cleanly using your chosen web framework.

const server = new Server(new ExpressAdapter(express))
const mountpoints = server.mount([
  new AccountController()
])

printMountpoints(mountpoints)

You can use either ExpressAdapter or FastifyAdapter, and give each an instance of express or fastify to work with.

Hypercontroller always lets you work with your web framework directly, and exposes the current app via server.app. This way you can use existing legacy code, framework-specific modules, practices, testing harnesses and more -- you take it from here.

Strong Params

Hypercontroller makes hyperparams accessible for you to use in your actions if you want to implement strong parameter (which you should).

You create a requirement statically:

const accountParams = permitParams('account', ['name'])

And use it in your actions, or anywhere else you want:

   @get()
   index(req, res){
       const params = accoutnParams(req.body)
       ...
   }

Hyperparams is extremely performant, and modular compared to full-fledged validation libraries.

Contributing

Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).

Thanks:

To all Contributors - you make this happen, thanks!

Copyright

Copyright (c) 2019 @jondot. See LICENSE for further details.