# @joker7t/common-server

> NPM page: https://www.npmjs.com/package/@joker7t/common-server

Latest version **1.0.1** (published 2022-05-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install @joker7t/common-server
pnpm add @joker7t/common-server
yarn add @joker7t/common-server
bun add @joker7t/common-server
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2022-05-28 |
| First published | 2022-05-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 (+3 in 1 direct dependencies) |
| Install scripts | no |
| Author | Toan Nguyen Ba |
| Maintainers | joker7nbt |

## Links

- npm: https://www.npmjs.com/package/@joker7t/common-server
- Homepage: https://github.com/joker7t/common-utils/tree/master/packages/server/#readme
- npm.io page: https://npm.io/package/@joker7t/common-server

## Dependencies (4)

- [express](https://npm.io/package/express.md) ^4.17.1
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [express-validator](https://npm.io/package/express-validator.md) ^6.12.1
- [@joker7t/common-core](https://npm.io/package/@joker7t/common-core.md) ^1.0.2

## Recent versions

- 1.0.1 (latest) — 2022-05-28
- 1.0.0 — 2022-05-19

## 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`.
- **errorHandler**
  - Handle errors for an API: if error is an instance of `CustomError`, do `serializeError` and return with defined status. If not, return with default message **'Something went wrong'** and status **400**.
- **currentUser**
  - Check if request's session has jwt. If have jwt, this middleware will verify it with `JWT_SECRET` env and set it to `req.currentUser`. 
  - The format of verified jwt:
  ```typescript
    {
        id: string;
        email?: string;
    }
  ```
- **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](https://github.com/joker7t/common-utils/blob/master/packages/core/src/errors/api-unauthorized-error.ts).

### Error templates
- ApiRequestValidationError
  - This is an enhanced version of [ApiBadRequestError](https://github.com/joker7t/common-utils/blob/master/packages/core/src/errors/api-bad-request-error.ts), built in with express validator.
  - Allow you to pass your invalid params and its validate message.

## **Versions:**
You can check all the versions [here](https://github.com/joker7t/common-utils#common-server).

---
_Source: https://npm.io/package/@joker7t/common-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
