decoress v0.1.3
Decoress
A minimal package for creating express controllers using decorators in typescript. you can use your express as before. this package tends to be lightweight and only add decorators to your controllers.
Features
- very minimal and lightweight
- handling asynchronous functions
- makes your code cleaner
Installation
- install
express
anddecoress
:
npm install decoress --save-exact
npm install express
- in
tsconfig.json
set these options:
{
"emitDecoratorMetadata": true,
"experimentalDecorators": true
}
Usage
first you need to import
Controller
and a method (for exampleGet
) and add them to your class. you can also add yourmiddlewares
usingMw
decorator.for example create
Data.controller.ts
file and add these code:
import { Mw, Get, Post, Controller } from 'decoress';
function aMiddleware(req: any, res: any, next: any) {
next();
}
@Controller('/data')
export class UserController {
@Get('/get')
@Mw(aMiddleware)
async get(req: any, res: any) {
res.send('...data');
}
@Post('/post')
async post(req: any, res: any) {
res.send('...data');
}
}
- then you need to pass your
controllers
tosetControllers
for example create app.ts
file and add these to it:
import express from 'express';
import { setControllers } from 'decoress';
import { UserController } from './data.controller';
const app = express();
app.listen(3000);
setControllers(app, { controllers: [UserController] });
now if you open http://localhost:3000/data/get
in your browser you should see the response.
Settings
pathPrefix
you can set pathPrefix
in setControllers
setControllers(app, { controllers: [UserController], pathPrefix: '/api' });
now you should see the response if you open http://localhost:300/api/data/get
in your browser.
options
catchAsync
by default
decoress
handles async functions in express and catch the error and send it to errorHandler withnext()
function.but you can disable it in
setControllers
:
setControllers(app, {
controllers: [UserController],
options: { catchAsync: false },
});
Middlewares
you have two ways to implement middlewares:
- as shown above you can use
Mw()
decorator. for example you havevalidate()
function which you want to use as middleware:
@Get('/get')
@Mw(validate(schema))
async get(req: any, res: any) {
res.send('...data');
}
- create a wrapper around
Mw()
decorator. if you use a middleware repeatedly, for examplevalidate()
, you may want to useValidate(schema)
instead ofMw(validate(schema))
:
// in validateMw.ts file
import { Mw } from 'decoress';
// your wrapper
export function Validate(schema) {
// your middleware
function fn(req: any, res: any, next: any) {
// ... do something with schema or whatever
next();
}
// pass yuor middleware to Mw decorator and return it
return Mw(fn);
}
then you can use it as decorator:
// in your controller.ts file
import { Get, Controller } from 'decoress';
import { Validate } from './validateMw.ts';
import { schema } from './someFile.ts';
@Controller('/data')
export class UserController {
@Get('/get')
@Validate(schema)
async get(req: any, res: any) {
res.send('...data');
}
}