Giter VIP home page Giter VIP logo

decoress's Introduction

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

  1. install express and decoress:
npm install decoress --save-exact
npm install express
  1. in tsconfig.json set these options:
{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

Usage

  1. first you need to import Controller and a method (for example Get) and add them to your class. you can also add your middlewares using Mw 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');
  }
}
  1. then you need to pass your controllers to setControllers

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 with next() function.

    but you can disable it in setControllers:

setControllers(app, {
  controllers: [UserController],
  options: { catchAsync: false },
});

Middlewares

you have two ways to implement middlewares:

  1. as shown above you can use Mw() decorator. for example you have validate() function which you want to use as middleware:
  @Get('/get')
  @Mw(validate(schema))
  async get(req: any, res: any) {
    res.send('...data');
  }
  1. create a wrapper around Mw() decorator. if you use a middleware repeatedly, for example validate(), you may want to use Validate(schema) instead of Mw(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');
  }
}

Inspired by

decoress's People

Contributors

dalmamad avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.