0.3.3 • Published 1 year ago

@delysium/api-kit v0.3.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

0.3.21

1 year ago

0.3.2

1 year ago

0.3.12

1 year ago

0.3.11

1 year ago

0.3.1

1 year ago

0.3.0

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.5

1 year ago

0.2.42

1 year ago

0.2.41

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.91

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

1 year ago

0.1.3

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.0

1 year ago