0.0.3 • Published 3 years ago

@capujs/framework v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

@capujs/framework

A framework build declarative and beautifully organizated based in class controllers with decorators usage in Express.js

Installation

Install with yarn

yarn add @capujs/framework

#install types
yarn -D add @types/express
yarn -D add @types/cors

Its important enable decorators in you project tsconfig.json

{
	"emitDecoratorMetadata": true,
	"experimentalDecorators": true
}

Example of usage

  1. Create file ExampleController.ts
import { Controller, Get } from "@capujs/framework";

@Controller("/example")
export class ExampleController {
	@Get("/")
	testMethod() {
		return "Hello Word";
	}
}
  1. Create app.ts
import { createServer} from '@capujs/framework';
import { ExampleController } from './ExampleController';

const app = createServer({
   controllers [ExampleController ]
});

//Listen express
const PORT = 3000;

app.listen(PORT,()=> console.log(`Server ready on port ${PORT}`);

Decorators Reference

Controllers

DecoratorExampleDescription
@Controller(prefix?: string)@Controller("/test") class TestControllerRegister class with this decorator a registred as controller and its anotated methods are registred. it using express `res.send()

Methods for Controllers

DecoratorExampleDescription
@Get(path?:string)@Get('/users') all()Method marked with this decorator register a request with GET method
@Post(path?:string)@Post('/users') save()Method marked with this decorator register a request with POST method
@Put(path?:string)@Put('/users/:id') update()Method marked with this decorator register a request with PUT method
@Patch(path?:string)@Patch('/users/:id') update()Method marked with this decorator register a request with PATCH method
@Delete(path?:string)@Delete('/users/:id') remove()Method marked with this decorator register a request with DELETE method
@Head(path?:string)@Head('/users/:id') head()Method marked with this decorator register a request with HEAD method
@All(path?:string)@All('/users/:id') all()Method marked with this decorator register a request with ALL method
@Options(path?:string)@Options('/users/:id') all()Method marked with this decorator register a request with OPTIONS method

Decorators for Parameters (inject in methods)

DecoratorExampleDescription
@Body(options?:IParamOptions)save(@Body() data: CustomDto)Inject a body an parameters options can using bodyParser middleware. using req.body
@BodyParam(name:string, options?:IParamOptions)save(@BodyParam('name') name:string)Inject a body an parameters options can using bodyParser middleware. using req.body.name
@Params()get(@Params() params: any)Inject all parameters req.params
@Param(name:string, options?:IParamOptions)get(@Param('id') id: string)Inject a query parameter req.params.id
@HeaderParams()get(@HeaderParams() params: any)Inject all parameters req.headers
@HeaderParam(name:string, options?:IParamOptions)get(@HeaderParam("authorization") authorization: string)Inject a header parameter req.headers.authorization
@CookieParams()get(@Cookies( cookies: any)Inject all cookies parameter req.cookies
@CookieParam(name:string, options?:IParamOptions)get(@Cookie("token") token: string)Inject a cookie parameter req.cookies.token
@UploadFiles(name:string, options?:IUploadOptions)post(@UploadFiles("photos") photos: any[])Inject multiple upload files from parameter req.files using middleware express-fileupload
@UploadFile(name:string, options?:IUploadOptions)post(@UploadFile("photo") photo: any)Inject single upload file from parameter req.files using middleware express-fileupload

Decorators for Middlewares

DecoratorExampleDescription
@Use(...middleware: RequestHandler[])@Use(isAuthenticated) class ProfileControllerRegister global middleware.

Decorators on Response

DecoratorExampleDescription
@HttpCode(code: number)@HttpCode(201) signup()Set custom http code after executing successfuly
@OnNull(codeOrError: number | Error)@OnNull(200) get()Set a http code when controller return a null
@OnUndefined(codeOrError: number | Error)@OnNull(200) get()Set a http code when controller return a undefined
@Header(name: string, value: string)@Header('Authorization','some-token') profile()Set a http header after executing successfuly