
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
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.
npm install decopress
yarn add decopress
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);
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 {
...
}
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 {
...
}
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 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 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();
}
}
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);
Method type is the available types you can use in the Route type.
type Method = 'get' | 'post' | 'put' | 'patch' | 'delete' | 'all';
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 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[];
}
FAQs
ExpressJS with TypeScript Decorators
The npm package decopress receives a total of 12 weekly downloads. As such, decopress popularity was classified as not popular.
We found that decopress demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.