1.0.13 โ€ข Published 2 years ago

@varuntiwari/express-ts-decorators v1.0.13

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

โŒจ Express-Typescript Decorators

Simple Typescript decorators for Express.

๐Ÿ“ˆ Installation

npm i @varuntiwari/express-ts-decorators

๐Ÿงช Usage

  • Initialize Express:
import express from 'express';

const app = express();
  • The library uses a single router to handle all the incoming requests. Use the AppRouter provided by library to setup Routing:
import express from 'express';
+ import { AppRouter } from '@varuntiwari/express-ts-decorators';

const app = express();

+ app.use(AppRouter.getInstance());
  • Now use the ErrorMiddleware provided by the library to setup Error Handling:
import express from 'express';
+ import { AppRouter, ErrorMiddleware } from '@varuntiwari/express-ts-decorators';

const app = express();

app.use(AppRouter.getInstance());
+ app.use(ErrorMiddleware);

(Use the middleware after using the AppRouter)

  • Create a new file containing your controller class and use the decorators provided by the library:
import {
  controller,
  get,
  post,
  bodyValidator,
} from '@varuntiwari/express-ts-decorators';

@controller('/product')
class ProductController {
  @get('/product/:id') /* Register a get method */
  @use(auth) /* Use one or multiple middlewares */
  getProducts(req: Request, res: Response) {
    //
  }

  @post('/product')
  @use(auth)
  @use(admin)
  createProduct(req: Request, res: Response) {
    //
  }
}

@controller('/auth')
export class AuthController {
  @post('/login')
  @bodyValidator('email', 'password') /* Validate request body */
  login(req: Request, res: Response): void {
    //
  }

  @get('/logout')
  logout(req: Request, res: Response): void {
    //
  }
}
  • Import the controller to the index.ts file:
import express from 'express';
import { AppRouter, ErrorMiddleware } from '@varuntiwari/express-ts-decorators';

+ import './controllers.ts';

const app = express();

app.use(AppRouter.getInstance());
app.use(ErrorMiddleware);
  • Start the server and you are ready to go ๐Ÿš€

โœจ Features

  • Decorators for all HTTP request methods like get, post, etc.
  • Provides request body validators.
  • Provides a single pre-configured router.
  • Integrated Error Handling, which means that no try-catch blocks are required inside controller methods.

โš™ Tools and Technologies used

  1. Typescript

๐Ÿ›  Local Installation and setup

  1. Clone the repo to your local machine.
  2. Install the required dependency for server using :

    npm install

๐ŸŽ Creating production built

  1. Build the package using

    npm run build

๐Ÿ˜Ž Team Members

โš– License

GPL-3.0

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago