# @kz-ts/express-common

> Common library for express

Latest version **0.0.5** (published 2022-02-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install @kz-ts/express-common
pnpm add @kz-ts/express-common
yarn add @kz-ts/express-common
bun add @kz-ts/express-common
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2022-02-19 |
| First published | 2022-02-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 19.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Shubham Kumar |
| Maintainers | kshubhamz |

## Links

- npm: https://www.npmjs.com/package/@kz-ts/express-common
- npm.io page: https://npm.io/package/@kz-ts/express-common

## Dependencies (2)

- [express](https://npm.io/package/express.md) ^4.17.2
- [@types/express](https://npm.io/package/@types/express.md) ^4.17.13

## Recent versions

- 0.0.5 (latest) — 2022-02-19
- 0.0.4 — 2022-02-19
- 0.0.3 — 2022-02-19
- 0.0.2 — 2022-02-19
- 0.0.1 — 2022-02-19

## README

# EXPRESS-COMMON

A common library build for handling server exceptions and validating body.

Includes few predefined client and server exceptions which can further be extended according to the requirements.

If Exception is encountered then response will be of type `{ message: string, timestamp: Date }`

## Documentation

Want to define your own Exception ?

```
import { ExpressError, HttpStatusCode } from '@kz-ts/express-common';

class CustomError extends ExpressError {
  statusCode: HttpStatusCode = 400;

  constructor(public errMessage: string = "Default Message") {
    super(errMessage);
    Object.setPrototypeOf(this, CustomError.prototype);
  }

  serializeError(): { message: string; timestamp: Date } {
    return { message: this.errMessage, timestamp: this.timestamp };
  }
}
```

Predefined client exception:

```
BadRequest: (message?: string | undefined) => BadRequestError;
Forbidden: (message?: string | undefined) => ForbiddenError;
Locked: (message?: string | undefined) => LockedError;
NotFound: (message?: string | undefined) => NotFoundError;
UnAuthorized: (message?: string | undefined) => UnAuthorizedError;
UnProcessable: (message?: string | undefined) => UnProcessableError;
```

Predefined server exception:

```
InternalServer: (message?: string | undefined) => InternalServerError;
NotImplemented: (message?: string | undefined) => NotImplementedError;
```

**client-exceptions and server-exceptions are available on `ClientErrorResponse` & `ServerErrorResponse`. And, can be imported from `@kz-ts/express-common` directly.**

## Demo

```
import { ServerErrorHandler, Validator } from '@kz-ts/express-common'
```

- For activating error-handler, after all `app.use` insert below line
  ```
  app.use(ServerErrorHandler.HandleError)
  ```
- To use body validator apply `Validator.BodyValidator` middleware as following
  ```
  app.post('/route', Validator.BodyValidator('email', 'password'), callback: RequestHandler);
  ```

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