Skip to content

Latest commit

 

History

History
52 lines (36 loc) · 1.07 KB

README.md

File metadata and controls

52 lines (36 loc) · 1.07 KB

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);