Skip to content

BogdanDomashenko/express-ts-ioc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express typescript boilerplate

This is powerfull Express wrapper!

Features:

Error handling

import { asyncHandler, Exception } from "@lib";

export const signin = asyncHandler(async (req, res) => {
  if (!req.body.user) {
    throw Exception.badRequest("user must be provided");
  }
});

Why async handlers?

if we use asyncHandlers we shouldn't use try/catch for catching error and sending this to error hendler middleware.

Validation

We are using class-validator for validation.

Example

// dto:
import { IsNotEmpty, IsEmail } from "class-validator";

export class AuthSchema {
  @IsEmail()
  email: string;
  @IsNotEmpty()
  password: string;
}

//route
import { validateBody } from "@lib";
import { AuthSchema } from "./dto/auth.dto";

const authRouter = express.Router();

authRouter.post("/signup", validateBody(AuthSchema), signup);