@joker7t/common-server
v1.0.1
Published
NPM page: https://www.npmjs.com/package/@joker7t/common-server
Downloads
3
Readme
common-server
NPM page: https://www.npmjs.com/package/@joker7t/common-server
Installation:
Using Yarn
yarn add @joker7t/common-server
Using NPM
npm i @joker7t/common-server
Features:
Middlewares
- validateRequest
- Use
express-validator
to validate user's requests. - Then format and return array of errors with detail message by
ApiRequestValidationError
.
- Use
- errorHandler
- Handle errors for an API: if error is an instance of
CustomError
, doserializeError
and return with defined status. If not, return with default message 'Something went wrong' and status 400.
- Handle errors for an API: if error is an instance of
- currentUser
- Check if request's session has jwt. If have jwt, this middleware will verify it with
JWT_SECRET
env and set it toreq.currentUser
. - The format of verified jwt:
{ id: string; email?: string; }
- Check if request's session has jwt. If have jwt, this middleware will verify it with
- requireAuth
- Use when API needs to authenticate, this use the result from
currentUser
middleware. - if have
currentUser
object in request then let user access the API, if not throw ApiUnauthorizedError.
- Use when API needs to authenticate, this use the result from
Error templates
- ApiRequestValidationError
- This is an enhanced version of ApiBadRequestError, built in with express validator.
- Allow you to pass your invalid params and its validate message.
Versions:
You can check all the versions here.