0.3.3 • Published 2 years ago

@delysium/api-kit v0.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@delysium/api-kit

Utility KIT for Express apis.

Installation

npm i @delysium/api-kit

Usage

  • Add Error middleware to your App
import express from "express"

const app = express()
app.use(errorMiddleware)
  • Declare API handler in your routes, removing Express from your Controllers logic
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
  • Add routes to your App
import userRouter from "./routes/user.route"
app.use(userRouter)
  • 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
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 {}
    }
}
0.3.3

2 years ago

0.3.21

2 years ago

0.3.2

2 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.42

3 years ago

0.2.41

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.91

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago