1.3.0 • Published 1 year ago

simple-ts-express-decorators v1.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Simple typescript decorators for express

Build Coverage Status

Simple controller and methods typescript decorators for express. Inspired by ts-decorator-routing

Provides pure request and response from express without complicated handlers or transformers.

Installation

  1. Install module

    npm install simple-ts-express-decorators
    yarn add simple-ts-express-decorators
  2. Install reflect-metadata

    npm install reflect-metadata
    yarn add reflect-metadata
  3. Import reflect-metadata before using provided decorators

  4. Enable typescript decorators in your tsconfig.json

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

Example of usage

UsersController.ts

import {Controller, Get} from 'simple-ts-express-decorators'; 
import {Request, Response, RequestHandler} from 'express';
import multer, {memoryStorage} from 'multer';

const upload = multer({storage: memoryStorage()});

const logMiddleware: RequestHandler = (req, res, next) => {
  console.log('log');

  next();
};

@Controller('/', logMiddleware)
export class UsersController {

  @Get('/users')
  index(request: Request, response: Response) {
    response.json([
      {id: 1, username: 'example'}
    ]);
  }

  @Post('/users', upload.sindle('avatar')) // example of usage with middleware
  create(request: Request, response: Response) {
    const avatar = request.file;

    // ...save

    response.status(201);
  }
}

index.ts

import "reflect-metadata";
import * as express from 'express';
import {ControllersLoader} from 'simple-ts-express-decorators';
import {UserController} from './UserController'; 

const app = express();

new ControllersLoader({
  controllers: [UserController]
}).load(app);

app.listen(3000);

Configuration options

OptionDescription
controllersRequired. Array of controllers to load or array of glob patterns to load controllers from
containerAny container implementation with get() method. For example InversifyJS
1.3.0

1 year ago

1.2.0

3 years ago

1.2.0-0

3 years ago

1.1.0

4 years ago

1.1.0-5

4 years ago

1.1.0-4

4 years ago

1.1.0-3

4 years ago

1.1.0-2

4 years ago

1.1.0-1

4 years ago

1.1.0-0

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago