@alexhelloworld/common

1.0.5 • Public • Published

Cross-service requests validation and errors handling library for the microservices ticketing project.

Microservices ticketing project

Installation

npm i --save @alexhelloworld/common

packaje on npmjs.com

Using:

To get current user logged in:

import { currentUser } from '@alexhelloworld/common'
const user = currentUser

Middleware For the request validation and throw Bad Request Error in case of any errors:

import { validateRequest } from '@alexhelloworld/common'

router.post(
  '/api/users/signin',
  [
    body('email').isEmail().withMessage('Email must be valid'),
    body('password').trim().notEmpty().withMessage('Password is required'),
  ],
  validateRequest,
  async (req: Request, res: Response) => {
    //Sign in code should be here
  }
)
Error handler and Errors templates

Error handler middleware to serialize all errors to the for understanfing between all the Microservices

import { errorHandler } from '@alexhelloworld/common'
const app = express()
app.use(errorHandler)

Errors reusable:

import { BadRequestError } from '@alexhelloworld/common'
import { NotFoundError } from '@alexhelloworld/common'
// 400
if (!passwordsMatch) {
  throw new BadRequestError('Invalid credentials')
}
// 404
app.get('*', () => {
  throw new NotFoundError()
})

Readme

Keywords

none

Package Sidebar

Install

npm i @alexhelloworld/common

Weekly Downloads

1

Version

1.0.5

License

ISC

Unpacked Size

17.4 kB

Total Files

28

Last publish

Collaborators

  • animalinstinct