1.4.4 • Published 3 years ago
decorated-router v1.4.4
Router with Typescript decorators
This is an easy way to use Express and define controllers with no route files and run your application with dependency injection. Let's take a look.
Installation
npm install decorated-router
Basic usage
Loader (index) file
import { App, METHOD, HEADER } from 'decorated-router';
@App({
controllers: [
Controller
],
server: {
port: 3000,
methods: [METHOD.GET, METHOD.POST, METHOD.PUT, METHOD.DELETE, METHOD.PATCH, METHOD.OPTIONS],
headers: [HEADER.ORIGIN, HEADER.XREQUESTEDWITH, HEADER.CONTENTTYPE, HEADER.ACCEPT, HEADER.AUTHORIZATION]
}
})
class loader {}
Controllers
import { Controller, Get, Delete, Post, Put, Path, Query, Body } from 'decorated-router';
import { Response } from 'express';
@Controller({
url: '/controller',
cors: "*",
auth: AuthenticationMiddleware
})
export class Controller {
constructor(
private _someInjectedService: SomeInjectedService
) { }
@Get('/to/:to')
getTo(@Path('to') to) {
return new Promise((resolve, reject) => {
resolve({ to });
})
}
@Delete('/from') // ?from=someValue
delete(@Query('from') from, res: Response) {
setTimeout(() => {
res.json({ from });
}, 1000);
}
@Post('/when')
post(@Body() someDto: SomeDTO) {
return { start: someDto.getStart() };
}
@Put('/where')
put(@Body() someDto: SomeDTO) {
return this._someInjectedService.doThings(someDto.getEnd());
}
}
Data Objects
import { DataObject } from 'decorated-router';
@DataObject()
export class SomeDTO {
private start: number;
private end: number;
public getStart(): number {
return this.start;
}
public getEnd(): number {
return this.end;
}
}
Dependency Injection
import { Injectable } from 'decorated-router';
@Injectable()
export class SomeInjectedService {
private name: string
constructor(
private _someOtherInjectedService: SomeOtherInjectedService
) { }
doThings(end: number): Promise<any> {
return new Promise((resolve, reject) => {
if (this._someOtherInjectedService.someMagic) {
resolve(end);
} else {
reject('error');
}
});
}
}
1.4.4
3 years ago
1.4.3
3 years ago
1.4.2
3 years ago
1.4.1
3 years ago
1.4.0
4 years ago
1.3.9
4 years ago
1.3.8
4 years ago
1.3.7
4 years ago
1.3.6
4 years ago
1.3.5
4 years ago
1.3.4
4 years ago
1.3.3
4 years ago
1.3.2
4 years ago
1.3.1
4 years ago
1.3.0
5 years ago
1.2.6
5 years ago
1.2.5
5 years ago
1.2.4
5 years ago
1.2.3
5 years ago
1.2.2
5 years ago
1.2.0
5 years ago
1.1.4
5 years ago
1.1.3
6 years ago
1.1.2
6 years ago
1.1.1
6 years ago
1.1.0
6 years ago
1.0.8
6 years ago
1.0.7
6 years ago
1.0.6
6 years ago
1.0.5
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
6 years ago