@alotech/common
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

Common library

A Typescript simple library with some basics implementation to errors and middlewares.

How to install

$ yarn add @alotech/common

How to use

import express from 'express';
import { errorHandler, NotFoundError } from '@alotech/common';

const app = express();
app.all('*', async (req, res, next) => {
  throw new NotFoundError();
});

app.use(errorHandler);
app.listen(3000, () => {
  console.log('Listening on port 3000!');
});

Above one example of how to use the errorHandler and throw a custom NotFoundError();

Validate request example

Notice this use express-validator

import express, { Request, Response } from 'express';
import { body } from 'express-validator';
import { validateRequest, BadRequestError } from '@alotech/common';
const router = express.Router();

router.post(
  '/api/users/signin',
  [
    body('email').isEmail().withMessage('Email must be valid!'),
    body('password').trim().notEmpty().withMessage('You must supply a password'),
  ],
  validateRequest,
  async (req: Request, res: Response) => {
    const { email, password } = req.body;
    ...
    ...
    if (!existingUser) {
      throw new BadRequestError('Invalid credentials');
    }
    ...
    ...
  }
);

...

Errors

  • BadRequestError
  • DatabaseConnectionError
  • NotAuthorizedError
  • NotFoundError
  • RequestValidationError

Middlewares

  • errorHandler

Package Sidebar

Install

npm i @alotech/common

Weekly Downloads

0

Version

1.1.1

License

MIT

Unpacked Size

19.7 kB

Total Files

39

Last publish

Collaborators

  • alvloureiro