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

decopress

v1.0.8

Published

ExpressJS with TypeScript Decorators

Downloads

17

Readme

Decopress

Decopress is a tool to help you write cleaner Express.js code using typescript decorators.

Please if you need a feature that is not supported by decopress and its related to decorators and express open an issue in github and ask for this feature.

Quick Access

Installation

  • Using npm:
    npm install decopress
  • Using yarn:
    yarn add decopress

Quick Start

creating a controller

import {NextFunction, Request, Response} from 'express';
import {Controller, Get, Middleware} from 'decopress';

const middleware1 = (req: Request, res: Response, next: NextFunction) => {
    console.log('in middleware1');
    next();
};

const middleware2 = (req: Request, res: Response, next: NextFunction) => {
    console.log('in middleware2');
    next();
};

@Controller('/')
class ExampleController {
    @Middleware(middleware1, middleware2)
    @Get('/')
    hello(req: Request, res: Response) {
        res.json({
            message: 'hello'
        });
    }
}

connect controller with app

import express from 'express';
import {setRoutes} from 'decopress';

const app = express();

// controller we made in the top code example
const controller = new ExampleController();

setRoutes(controller, app);

Controller Class

Controller Decorator

You can use Controller decorator to create decorator classes.

Note: if you don't apply Controller decorator to controller class while you are trying to convert your controllers into routes you will face errors.

Controller accepts one argument and that the url(path) and url is required.

import {Controller} from 'decopress';

const url = '/path'

@Controller(url)
class ExampleController {
    ...
}

CMiddleware Decorator

You can use CMiddleware decorator to add middleware to your controller class.

Note: You should use CMiddleware decorator before Controller decorator and you can use CMiddleware as much as you like and it will run in order.

CMiddleware decorator accepts spread middleware of type RequestHandler from express package.

import {RequestHandler} from 'express';
import {Controller, CMiddleware} from 'decopress';

const middleware1: RequestHandler = (req, res, next) => {
    console.log('in middleware1');

    next();
};

const middleware1: RequestHandler = (req, res, next) => {
    console.log('in middleware1');

    next();
};

@CMiddleware(middleware1, middleware2)
@Controller('/')
class ExampleController {
    ...
}

Method Decorators

There is Get, Post, Put, Patch, Delete, and All method decorators they all accept only url(path) and the method of type Request handler in the class and represent the method of the name.

Note: by my convention the method name is the path(it's not required)

import {Controller, Get} from 'decopress';

@Controller('/')
class ExampleController {
    @Get('/hello')
    hello(req: Request, res: Response) {
        res.json({
            message: "hello"
        });
    }
}

Middleware Decorator

Middleware decorator is used to add middleware to request stack. it works the same way as CMiddleware decorator. and it accept arguments of type RequestHandler from express package.

Note: it is required to use it before Method Decorators.

import {RequestHandler} from 'express';
import {Controller, Get, Middleware} from 'decopress';

const middleware1: RequestHandler = (req, res, next) => {
    console.log('in middleware1');

    next();
};

const middleware1: RequestHandler = (req, res, next) => {
    console.log('in middleware1');

    next();
};

@Controller('/')
class ExampleController {
    @Middleware(middleware1, middleware2)
    @Get('/hello')
    hello(req: Request, res: Response) {
        res.json({
            message: "hello"
        });
    }
}

Use Method Decorator

Use method decorator is an special method decorator that can help you handle sub controllers all you have to do is in the method return instance of your controller.

Note: by my convention the method name for Use method decorator is as same as class name

import {Controller, Use} from 'decopress';

@Controller('/2')
class ExampleController2 {
}

@Controller('/')
class ExampleController {
    @Use('/')
    ExampleController2() {
        return new ExampleControler2();
    }
}

Get Result

getting results is easy in decopress just you have to import setRoutes and then setRoutes accept to arguments the first one is instance of your controller class and the second one is either instance of Router from express package or your app.

import express from 'express';
import {setRoutes, RoutesConfigClass} from 'decopress';
    
const app = express();

...

// I'm using ExampleController from top examples
setRoutes(<RoutesConfigClass>new ExampleController(), app);

Types

Method Type

Method type is the available types you can use in the Route type.

type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'all';

Route Type

Route type is the return type of method decorators. it contains a url of type string witch is provided url. it also contains a method witch is the selected rest method for the route and it contains stack witch represents middleware and the filnal method.

interface Route {
    url: string;
    method: Method;
    stack: RequestHandler[];
}

RouteConfig Type

RouteConfig Type is a type that represents the data of Controller. it contains the provided url in url property. it contains provided route configs in RouteConfigProperty. it contains the route object. it also contains the controller middleware and sub controllers.

import {RouterOptions} from 'express';

interface RoutesConfig {
    url: string;
    routerOptions?: RouterOptions;
    routes: {
        [key: string]: Route;
    };
    middleware: RequestHandler[];
    subControllers: RoutesConfigClass[];
}