A Map containing all used middlewares for this API
Express Object
A map containing all endpoints of this API
the main endpoint for this API
A Map containing all parameters checker functions for this API
The API Server PORT. Default: 51337
Express Router
Adds an enhdpoint to the API
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"]
Adds a Middleware to the API that will apply to each request made to this endpoint
Just a name for this middleware
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")
Adds multiple methods to a single endpoint.
the API endpoint
e.g: ["get", "post"]
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")
Map the given param placeholder name(s) to the given callback(s).
the parameter to write a condition for
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();
});
Registers all Middlewares in the Express app.
Applies the default Middlewares
Adds some middlewares for the API Security
Registers all parameter checkers for this router
Starts the express server
"true" || "false" This parameter is optional. use startServer("true") || startServer("false") to enable or disable the default middlewares
Generated using TypeDoc
Initialzes the APIController Class