Making a custom ContextAdapter
# So, you got a middleware-based Node layer that is neither Koa/Express and you want to be able to use entity-routes ?
That can be done in 4 steps.
You need to make a MiddlewareAdapter and pass everything you want/need in your "context". Here is how Koa/Express
middleware adapters are implemented :
# 1 type AnyMiddlewareAdapter = ( mw : Function ) => ( ... args : any ) => any ; 3 const expressMiddlewareAdapter = ( mw : Function ) => ( req , res , next ) => mw ( makeExpressContextAdapter ( req , res ) , next ) ; 4 const koaMiddleareAdapter = ( mw : Function ) => ( ctx : Context , next : Next ) => mw ( makeKoaContextAdapter ( ctx ) , next ) ; 2. ContextAdapter factory
# Then you need to make the context adapter factory using the parameters you passed previously. Here is how context
adapter factory are implemented for Koa/Express :
Koa Express
# 1 export type KoaContextAdapter = ContextAdapter & { ctx : Context } ; 3 export const makeKoaContextAdapter = ( ctx : Context ) => 9 return this . ctx . method ; 12 return this . ctx . request . body ; 15 return this . ctx . params ; 18 return this . ctx . query ; 21 return this . ctx . state ; 26 set responseBody ( value ) { 27 this . ctx . body = value ; 30 return this . ctx . status ; 33 this . ctx . status = value ; 35 } as KoaContextAdapter ) ; # 1 export type ExpressContextAdapter = ContextAdapter & { req : Request ; res : Response } ; 3 export const makeExpressContextAdapter = ( req : Request , res : Response ) => 8 return this . req . method ; 14 return this . req . params ; 17 return this . req . query ; 20 return this . res . locals ; 23 return this . res . locals . responseBody ; 25 set responseBody ( value : any ) { 26 this . res . locals . responseBody = value ; 27 this . res . send ( value ) . end ( ) ; 30 return this . res . statusCode ; 33 this . res . status ( value ) ; 35 } as ExpressContextAdapter ) ; You must define a routerRegisterFn, which register a BridgeRoute to a XXX route in XXX router, where XXX can be
Koa/Express, etc.
Koa Express
# 1 import { Middleware } from "koa" ; 2 import * as Router from "koa-router" ; 4 export function registerKoaRouteFromBridgeRoute ( instance : Router , route : BridgeRouterRoute < Middleware > ) { 5 instance . register ( route . path , route . methods , route . middlewares , { name : route . name } ) ; # 1 import { Router , RequestHandler } from "express" ; 3 export function registerExpressRouteFromBridgeRoute ( instance : Router , route : BridgeRouterRoute < RequestHandler > ) { 4 route . methods . forEach ( ( verb ) => instance [ verb ] ( route . path , route . middlewares ) ) ; Finally, you can put these together and make your own makeXXXEntityRouters :
# 1 export async function makeExpressEntityRouters ( 2 args : Omit < MakeEntityRouters , "options" > & { options ? : EntityRouteOptions } 4 return makeEntityRouters ( { 8 routerFactoryFn : Router , 9 routerRegisterFn : registerExpressRouteFromBridgeRoute , 10 middlewareAdapter : ( mw : Function ) => ( req , res , next ) => mw ( makeExpressContextAdapter ( req , res ) , next ) ,