Options
All
  • Public
  • Public/Protected
  • All
Menu

Class APIController

Hierarchy

  • APIController

Index

Constructors

constructor

Properties

MiddleWares

MiddleWares: any

A Map containing all used middlewares for this API

Private app

app: Application

Express Object

endpoints

endpoints: any

A map containing all endpoints of this API

Protected mainEndPoint

mainEndPoint: string

the main endpoint for this API

parameters

parameters: any

A Map containing all parameters checker functions for this API

port

port: number

The API Server PORT. Default: 51337

Private router

router: any

Express Router

Methods

AddEndPoint

  • Adds an enhdpoint to the API

    Parameters

    • endpoint: string
    • method: HTTPMethods
    • callback: EndpointCallback

      Example:

      import { APIController } from "./dist"

      const api = new APIController("/api/v1");

      api.AddEndPoint("/", "get", (req, res) => {
      res.status(200).json({ home: "home is now accessible" })
      })

      api.StartServer("true") // to apply default middlewares ["cors", "morgan", "helmet"]

    Returns void

AddMiddleWare

  • Adds a Middleware to the API that will apply to each request made to this endpoint

    Parameters

    • middlewareId: string

      Just a name for this middleware

    • callback: MiddleWareCallback

      A callback function

      Example:

      import { APIController } from "api-tools-ts"
      const api = new APIController("/api/v1")

      api.AddEndPoint("/test", (req, res) => {
      res.status(200).json({status: "success", message: "I am running"})
      })

      // This middleware will be applied each time the endpoint /test and every other endpoint you add later are requested!
      api.AddMiddleWare("whatever", (req, res, next) => {
      console.log("Hey I am your new middleware")
      next();
      })

      api.startServer("true")

    Returns void

AddMultipleMethods

  • AddMultipleMethods(endpoint: string, method: string[], callback: Function[]): void
  • Adds multiple methods to a single endpoint.

    Parameters

    • endpoint: string

      the API endpoint

    • method: string[]

      e.g: ["get", "post"]

    • callback: Function[]

      e.g [getUsers, postUser]

      The callback functions for the methods should be put in the array in order to grant each method its right callback function

      Example:


      import { APIController } from "api-tools-ts"
      const api = new APIController("/api/v1")

      const getUsers = (req, res) => {
      res.status(200).json({ method: "GET" })
      }

      const postUser = (req, res) => {
      res.status(200).json({method: "POST"})
      }

      // The "post" method here can have also multiple callback functions just like express allows

      api.AddMultipleMethods("/random", ["get", "post"], [getUsers, [checkBody, postUser]]) // now the random endpoint will have the post and get methods and each has its own callback function

      api.startServer("true")

    Returns void

AddParamChecker

  • Map the given param placeholder name(s) to the given callback(s).

    Parameters

    • param: string

      the parameter to write a condition for

    • callback: ParameterCallback

      the callback function for this paramter

      Example:


      const api = new APIController('/api/v1');

      api.AddEndPoint('/:id', 'get', (req, res) => {
      res.status(200).json({ data: 'none', status: 'ok' });
      });

      api.AddParamChecker('id', (req, res, next, value) => {
      if (value > 10) {
      res.status(404).json({ status: 'not allowed' });
      }
      next();
      });

    Returns void

Private RegisterAllMiddleWares

  • RegisterAllMiddleWares(): void

Private applyDefaultMiddleWares

  • applyDefaultMiddleWares(): void

Private createMainEndPoint

  • createMainEndPoint(): void

Private registerAllParamCheckers

  • registerAllParamCheckers(): void

startServer

  • startServer(applyDefaultMiddleWares?: { useDefaultMiddlewares: DefaultMiddlewares }): void

Generated using TypeDoc