0.1.21 • Published 4 years ago

express-ts-decorator v0.1.21

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

express-ts-decorator

The Express typescript easy to use decorator project

Install

  # use npm
  ~ npm i express-ts-decorator
  # use yarn
  ~ yarn add express-ts-decorator

Usage

  1. Set up your Controller
// ./controllers/hello.ts
import { Controller, Get } from "express-ts-descorator";

@Controller("/api/hello")
export class Hello {
  @Get("")
  public hello() {
    return "hello";
  }

  @Get("/json")
  public jsonHello() {
    return {
      message: "hello"
    };
  }
}
  1. Set up your App
// app.ts
import * as express from "express";
import { ControllerLoader, ExpressApp } from "express-ts-descroator";
import * as Path from "path";
import * as cors from "cors";

@ControllerLoader({
  filePath: Path.join(__dirname, "./controllers") // path to your controllers file
  autoInjectRoutes: true // auto inject router config to express when App constructor
})
class App extends ExpressApp {

  // if use auto inject routes
  // some middlewares can set in this array which you want it run before router
  beforeRouterInjectMiddlewares = [
    (req: any, res: any, next: any) => {
      console.log("in before router middleware");
      return next();
    },
    bodyParser.json(),
    {
      active: () => process.env.NODE_ENV === "DEVELOPMENT", // if some middleware just run in production or development you can use this
      middleware: cors()
    }
  ];

  constructor() {
    super(express());
  }
}

const app = new App();

// use the middlewares like that
// app.use(bodyParser.json());

// inject the loader routes config
// if you set the `autoInjectRoutes` is false in ControllerLoader,
// you can inject router by yourself like that
app.routes.forEach(router => {
  app.use(router);
});

// catch the error like that
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
  // do sth here
});

app.express.listen(3000);
  1. Run!
  ~ ts-node app.ts

Test

  ~ yarn test

Validator

you can use the Joi to validate your http request parameter

// ./controllers/hello.ts
import { Controller, Get, Validator } from "express-ts-descorator";
import * as Joi from "@hapi/joi";

// support the `query`, `body`, `params` validator in http request.
@Validator({
  query: Joi.object()
    .keys({
      name: Joi.string().required() // will throw error when request.query.name not exist or not the string
    })
    .requried()
  // body: .....
  // params: .....
})
@Controller("/api/hello")
export class Hello {
  @Get("")
  public hello() {
    return "hello";
  }

  @Get("/json")
  public jsonHello() {
    return {
      message: "hello"
    };
  }
}

Middlewares

import { Controller, Get, Middlewares, Validator } from "express-ts-decorator";
import * as Joi from "@hapi/joi";
import { Request, Response, NextFunction } from "express";

@Controller("/api/hello")
export class Hello {
  @Validator({
    query: Joi.object()
      .keys({
        name: Joi.string().required()
      })
      .required()
  })
  @Middlewares(function(req: Request, res: Response, next: NextFunction) {
    console.log("in");
    // You can do something in here before call the controller main function
    next();
  })
  @Get("")
  public hello() {
    return "hello";
  }

  @Get("/json")
  public jsonHello() {
    return {
      message: "hello"
    };
  }
}
0.1.20

4 years ago

0.1.21

4 years ago

0.1.19

4 years ago

0.1.18

4 years ago

0.1.16

4 years ago

0.1.17

4 years ago

0.1.15

4 years ago

0.1.14

4 years ago

0.1.12

4 years ago

0.1.13

4 years ago

0.1.10

4 years ago

0.1.11

4 years ago

0.1.9

4 years ago

0.1.8

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.1

5 years ago