https://github.com/draym/node-api-kit
toolkit for api development
https://github.com/draym/node-api-kit
api-rest sdk
Last synced: 7 days ago
JSON representation
toolkit for api development
- Host: GitHub
- URL: https://github.com/draym/node-api-kit
- Owner: Draym
- Created: 2023-02-01T04:44:46.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-05T09:35:00.000Z (about 3 years ago)
- Last Synced: 2025-02-10T19:53:03.030Z (over 1 year ago)
- Topics: api-rest, sdk
- Language: TypeScript
- Homepage:
- Size: 389 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `API-Kit`  [](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): Promise => {
const params = req.params
return {}
}
public create = async (req: BodyRequest): Promise => {
const body = req.body
return {}
}
}
```