@hypercolor/authenticated-router
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

@hypercolor/authenticated-router

Table of Contents

Motivation

This tool helps Express developers apply middleware chains consistently across sets of APIs. Using Express Router's standard pattern for adding middleware means you will need to manually copy and paste the set of middleware for each mounted route. AuthenticatedRouter lets you build groups of routes that all have the same upstream middleware applied.

Installation

  • NPM
    • npm install @hypercolor/authenticated-router
  • Yarn
    • yarn add @hypercolor/authenticated-router

Example

import {AuthenticatedRoute, AuthenticatedRouter} from './AuthenticatedRouter';

const router = AuthenticatedRouter.build({
  middleware: []
}, routeGroup => {
  routeGroup.route('/home').get(buildHandler(GetWelcomeScreenController));
  routeGroup.route('/post').post(buildHandler(CreatePostController));
  routeGroup.route('/post/:post_id/update').put(buildHandler(UpdatePostController));
})

expressApp.use('/mount', router.router)

AuthenticatedRouter API

  • build(options, builder)

Creates a block of routes that will have all the same middleware applied. Middleware array is passed in as options.middleware.

Routes are specified in the builder callback. The closure is invoked with an RouteGroup parameter.

RouteGroup API

The RouteGroup contains wrappers for Express Router's verbs. Pass in an Express RequestHandler to create the mount.

Express Router Wrappers

get(controller: RequestHandler): this;

post(controller: RequestHandler): this;

put(controller: RequestHandler): this;

patch(controller: RequestHandler): this;

delete(controller: RequestHandler): this;

all(controller: RequestHandler): this;

options(controller: RequestHandler): this;

head(controller: RequestHandler): this;

use(middleware: RequestHandler): this;

Add a new middleware. Will only be applied to routes added after this is called.

Project Repository

Organization Repository

Package Sidebar

Install

npm i @hypercolor/authenticated-router

Weekly Downloads

1

Version

0.0.2

License

none

Unpacked Size

10 kB

Total Files

9

Last publish

Collaborators

  • seth_hypercolor
  • andrew-hypercolor