# @d-lab/api-kit

> API utility KIT for Express

Latest version **0.5.6** (published 2023-03-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @d-lab/api-kit
pnpm add @d-lab/api-kit
yarn add @d-lab/api-kit
bun add @d-lab/api-kit
```

## 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.5.6 |
| Published | 2023-03-30 |
| First published | 2023-02-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 57.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Kevin Andres |
| Maintainers | draym |
| Keywords | dlab, api, kit |

## Links

- npm: https://www.npmjs.com/package/@d-lab/api-kit
- Repository: https://github.com/Draym/node-api-kit.git#main
- Homepage: https://github.com/Draym/node-api-kit#readme
- Issues: https://github.com/Draym/node-api-kit/issues
- npm.io page: https://npm.io/package/@d-lab/api-kit

## Dependencies (8)

- [axios](https://npm.io/package/axios.md) ^1.3.2
- [cabin](https://npm.io/package/cabin.md) ^13.0.0
- [tslib](https://npm.io/package/tslib.md) ^2.4.0
- [express](https://npm.io/package/express.md) ^4.17.1
- [picocolors](https://npm.io/package/picocolors.md) ^1.0.0
- [class-validator](https://npm.io/package/class-validator.md) ^0.14.0
- [@d-lab/common-kit](https://npm.io/package/@d-lab/common-kit.md) ^0.6.0
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.5.1

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads
- [@modern-js/plugin-swc](https://npm.io/package/@modern-js/plugin-swc.md) — 3.4K weekly downloads

## Recent versions

- 0.5.6 (latest) — 2023-03-30
- 0.5.5 — 2023-03-22
- 0.5.4 — 2023-03-20
- 0.5.3 — 2023-03-16
- 0.5.2 — 2023-03-16
- 0.5.1 — 2023-03-15
- 0.5.0 — 2023-03-15
- 0.4.2 — 2023-03-15
- 0.4.1 — 2023-03-15
- 0.4.0 — 2023-03-14
- 0.3.5 — 2023-03-14
- 0.3.4 — 2023-03-14
- 0.3.3 — 2023-03-14
- 0.3.2 — 2023-03-09
- 0.3.1 — 2023-03-09
- … 20 more at https://npm.io/package/@d-lab/api-kit/versions

## README

# `API-Kit` ![NodeJS](https://img.shields.io/badge/node.js-6DA55F?style=for-the-badge&logo=node.js&logoColor=white)![TypeScript](https://img.shields.io/badge/typescript-%23007ACC.svg?style=for-the-badge&logo=typescript&logoColor=white) [![npm version](https://badge.fury.io/js/@d-lab%2Fapi-kit.svg)](https://badge.fury.io/js/@d-lab%2Fapi-kit)
Typescript utility KIT for Express apis, including sequelize tools.

[available on NPM](https://www.npmjs.com/package/@d-lab/api-kit)

## Installation

```bash
npm i @d-lab/api-kit
```

## Usage

- Add Error middleware to your App
```ts
import express from "express"

const app = express()
app.use(errorMiddleware)
```

- Declare API handler in your routes, removing Express from your Controllers logic
```ts
// from routes/user.route.ts
import {Router} from "express"
import {handle} from "../interfaces/api"

// your own controller
import UserController from "../controllers/user.controller"
import {validateRequest} from "../middleware"
const ctrl = new UserController()

const router = Router()
router.get("/users/:userId", handle.bind(ctrl.get))
router.post("/users", validateRequest(CreateRequest), handle.bind(ctrl.create))

export default router
```

- Compile your routes in /routes/index.ts
```ts
import userRouter from "./user.route"
const routers = [
  userRouter
]
export default routers
```

- Add routes to your App
```ts
import routers from "./routes"
app.use(routers)
```

- Create your Controllers
  - You can now consider your controllers as simple methods without considering Express anymore
  - The success response will be handled in the router and the possible errors will be handled by the error middleware when HttpException are thrown in your code
```ts
import {PathRequest, AuthBodyRequest} from "../interfaces/api"

export default class UserController {
    public get = async (req: PathRequest<GetRequest>): Promise<UserResponse> => {
        const params = req.params
        return {}
    }
    public create = async (req: BodyRequest<CreateRequest>): Promise<UserResponse> => {
        const body = req.body
        return {}
    }
}
```

---
_Source: https://npm.io/package/@d-lab/api-kit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
