Express-Kun
Express Kun is providing you common helper for common express use case with functional programming mindset.
Installation
yarn add express-kun
or
npm install express-kun
Philosophy
Express Kun philosophy is An helper should only extends the functionality without redefining how we write our application.
think of it as Redux vs Mobx. in Redux, you don't redefine how you write your components. redux only extends the functionality fia connect
function to access your reducer. meanwhile MobX requiring you to wrap your function in decorator. by extending instead of redefining we can achieve smoother learning curve and more expressive.
Express Kun does not dictate you. instead, you provided it with express
Router
object and returning modified Router
express object or providing callback to play with modified Router
Object.
Express Kun is your friend and servant.
API
Base
withMiddleware(router: Router, middlewares: RequestHandler[]): Router
;
with middleware apply middleware to your router and return back the midlewared router
// your routerconst router = ;// with auth middlewareconst protectedRouter = ; // also support array of middleware ex: [authMiddleware, myMiddleware2] protectedRouter;
because this is only return the midlewared router. you can chain it without modifying the first router behavior
// your routerconst router = ;// with auth middlewareconst protectedRouter = ; // also support array of middleware ex: [authMiddleware, myMiddleware2]// will apply authMiddleware and uploadMiddlewareconst protectedUploadRouter = ; protectedRouter;protectedUploadRouter)
withErrorHandler(router: Router, errorHandler: ErrorRequestHandler): Router;
withErrorHandler returning a router that when that route error it will use the provided error handler
for example
{ res;} const withErrorHandlerRoute = ; // when accessed will return json { error: true, message: 'wow error' }withErrorHandlerRoute;
this provide further more functionality to compose middleware with error handler
{ res;} { console; ;} const middlewaredRoute = ; const withErrorHandlerRoute = ; // when accessed will return json { error: true, message: 'wow error' }withErrorHandlerRoute;
partialWithMiddleware(middlewares: RequestHandler[]): (router: Router) => void
partially use withMiddleware. this is useful if you want to create abstraction of midleware. for example you want to build helper to generate route with auth middleware. you can do like this
// in generateAuthMiddleware.jsconst generateAuthMiddleware = ; // in your routes.jsconst router = ;const protectedRoute = ;
this even support supplying partialWithmiddleware with middleware for easy composition
// in generateAuthMiddleware.jsconst generateAuthMiddleware = ; // in uploadProtectedMiddleware.jsconst generateUploadProtectedMiddleware = ; // in your routes.jsconst router = ;const uploadProtectedRouter = ;
partialWithErrorHandler(errorHandler: ErrorRequestHandler): (router: Router) => void;
like partialWithMiddleware but for error handler
// in generateLoggingErrorRoute.jsconst loggedErrorHandler = ; // in your routes.jsconst router = ;const loggedRoute = ;
groupErrorHandler(routerOrApp: RouterOrApplication, handler: ErrorRequestHandler): (callback: (router: Router) => void) => void;
this wass Laravel-like route for reusable error handling. you can write it like this
// your routerconst router = ;// define error handlerconst errorHandler = { // log the error console; // sendJson res;}; // use it { // when every route defined here return an error it will passed to the error handler withErrorHandleRoute;};
by providing error this way you can provide multiple error handle for different route easily
const userRouter = ;const customerRouter = ;// define error handlerconst errorHandlerUser = { // log the error console; // sendJson res;}; const errorHandlerCustomer = { // log the error console; // sendJson res;}; // use it { // when every route defined here return an error it will passed to the error handler withErrorHandleRoute;}; { // when every route defined here return an error it will passed to the error handler withErrorHandleRoute;};
Extended
withJWTAuthMiddleware(router: Router, secretKey: string, getToken?: GetTokenFun, preCheckFun?: PreCheckFun, errorHandler?: ErrorRequestHandler, verifyOptions?: jwt.VerifyOptions): Router;
typings
;;
use
;
withJWTAuthMiddleware make it simple to create middleware for authentication if you are using jwt. the only required parameter is secretKeey
that you use
to sign your jwt. by default it will get token from the bearer from authorization header for example
Headers: ...
Authorizatation: Bearer 321lnasljndjijno214
but you can also provide getToken Function that will pass the request object as parameter and should return the token, for example
and then use like below
;
you can also provide pre check for example to check if user exist
and then use like below
withJWTAuthMiddlewarerouter, "mySecretKey", getTokenFromBearer, checkUser;
partialWithJWTAuthMiddleware(secretKey: string, getToken?: GetTokenFun, preCheckFun?: PreCheckFun, errorHandler?: ErrorRequestHandler, verifyOptions?: jwt.VerifyOptions)
this function is like withJWTAuthMiddleware but you can pass the Router later;
; // later;
Callback API
groupMiddleware(router: Router, middlewares: SupportedMiddleware): (callback: (router: Router) => void) => void;
This is like groupErrorHandler
but instead of error handler you can pass middleware here
for example
// your routerconst router = ;// define error handlerconst myMiddleware = { // log or do something console;}; // use it { // when every route defined here return an error it will passed to the error handler middlewaredRoute;};
groupPrefix(router: Router, prefix: string): (callback: (router: Router) => void) => void;
Provide laravel-like grouping route/controller with the same prefix
// your routerconst router = ;// add prefix { // the route will be '/customer/test' prefixedRoute;};
Features
- Functional Like
- extendable
- built with Typescript