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

express-decorated-router

v1.0.6

Published

Define Express routes using TypeScript decorators

Downloads

25

Readme

Express Decorated Router

Define your Express routes in a nice, expressive way using TypeScript decorators!

NPM

Greenkeeper badge Coverage Status Build Status


Table of Contents

Basic usage

import * as express from 'express';
import {Controller, ControllerMiddleware, POST, RouteMiddleware, ExpressDecoratedRouter} from 'express-decorated-router';

@Controller('/auth')
@ControllerMiddleware(someMiddleware(), moreMiddleware())
class MyAuthController {

  @POST('/login')
  @RouteMiddleware(onlyApplyToThisRoute())
  public static login(req: express.Request, res: express.Response): void {
    doSomeMagic();
  }
}

const app: express.Application = express();

ExpressDecoratedRouter.applyRoutes(app);
ExpressDecoratedRouter.reset();

API

Decorators

ALL(path: PathParams)

Use this handler for any HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/ALL.ts:8


Controller(root?: string, options?: RouterOptions)

Register this class as a controller

Returns: ClassDecorator

Parameters

| | Type | Required | Default value | Description | | --- | --- | --- | --- | --- | | root | string | :x: | "/" | The root path for this controller | | options | RouterOptions | :x: | | Options passed to the Express router initialisation function. |

Defined in decorators/Controller.ts:9


ControllerMiddleware(first: RequestHandler, ...middleware: RequestHandler[])

Define middleware for this controller. Any child controller which defines this class as its @Parent will inherit this middleware.

Returns: ClassDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | first | RequestHandler | :heavy_check_mark: | A middleware handler | | middleware | RequestHandler[] | :x: | 0..n additional middleware handlers |

Defined in decorators/ControllerMiddleware.ts:10


DELETE(path: PathParams)

Use this handler for the DELETE HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/DELETE.ts:8


GET(path: PathParams)

Use this handler for the GET HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/GET.ts:8


HEAD(path: PathParams)

Use this handler for the HEAD HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/OPTIONS.ts:8


Method(httpMethod: string, path: PathParams)

Use this handler for the given HTTP method. The method must be one understood by Express' router.METHOD() method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | httpMethod | string | :heavy_check_mark: | The HTTP method | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

  • See: https://expressjs.com/en/4x/api.html#router.METHOD

Defined in decorators/method/Method.ts:10


OPTIONS(path: PathParams)

Use this handler for the OPTIONS HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/HEAD.ts:8


PATCH(path: PathParams)

Use this handler for the PATCH HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/PATCH.ts:8


POST(path: PathParams)

Use this handler for the POST HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/POST.ts:8


PUT(path: PathParams)

Use this handler for the PUT HTTP method

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | path | PathParams | :heavy_check_mark: | The path this handler will be responsible for |

Defined in decorators/method/PUT.ts:8


Parent(parentController: Function)

Define another controller as this controller's parent, inheriting its root path and middleware.

Returns: ClassDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | parentController | Function | :heavy_check_mark: | The parent controller |

Defined in decorators/Parent.ts:7


RouteMiddleware(first: RequestHandler, ...middleware: RequestHandler[])

Define middleware for this route

Returns: MethodDecorator

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | first | RequestHandler | :heavy_check_mark: | A middleware handler | | middleware | RequestHandler[] | :x: | 0..n additional middleware handlers |

Defined in decorators/RouteMiddleware.ts:9


Classes

ExpressDecoratedRouter

Public interface for the express-decorated-router library

Defined in ExpressDecoratedRouter.ts:42

public static applyRoutes(app: IRouter)

Apply routes to the Express application. You should call reset() after calling this.

Returns: ExpressDecoratedRouter

Parameters

| | Type | Required | Description | | --- | --- | --- | --- | | app | IRouter | :heavy_check_mark: | The Express application |

  • Throws: {ParentControllerError} If the input of a @Parent decoration has not been decorated with @Controller
  • Throws: {UnregisteredControllerError} If a class decorated with @Parent was not annotated with @Controller

Defined in ExpressDecoratedRouter.ts:139

public static reset()

Reset the library, freeing resources. You should call this method after calling applyRoutes()

Returns: ExpressDecoratedRouter

Defined in ExpressDecoratedRouter.ts:155


ParentControllerError

Thrown when an input of a @Parent decoration has not been decorated with @Controller

Extends: Error

Defined in errors/ParentControllerError.ts:4

public child

The child controller

Defined in errors/ParentControllerError.ts:6

public parent

The parent controller

Defined in errors/ParentControllerError.ts:8


UnregisteredControllerError

Thrown when a class decorated with @Parent was not annotated with @Controller

Extends: Error

Defined in errors/UnregisteredControllerError.ts:4

public controller

The controller

Defined in errors/UnregisteredControllerError.ts:6


Example app

An example app can be found in the example directory.

Common problems

Routes do not get registered

You must import/require the files containing your routes before you call applyRoutes(). When in doubt, set the DEBUG environment variable to express-decorated-router and see exactly what's going on.

Good practices

  • Always call ExpressDecoratedRouter.reset() after applying your routes to free up resources