@ -19,19 +19,28 @@ import {RegExRoute} from "../http/routing/RegExRoute.ts";
export type RouteHandlerReturnValue = SyncRouteHandlerReturnValue | AsyncRouteHandlerReturnValue
export type SyncRouteHandlerReturnValue = Request | ResponseFactory | void | undefined
export type AsyncRouteHandlerReturnValue = Promise < SyncRouteHandlerReturnValue >
export type RouteHandlerFunction = ( request : Request ) = > RouteHandlerReturnValue
export type RouteHandlerFunction = ( ( request : Request , arg? : any ) = > RouteHandlerReturnValue ) | ( ( request : Request ) = > RouteHandlerReturnValue )
export interface RouteHandlerWithArgument {
handler : string ,
arg : any ,
}
export type RouteHandlerDefinition = string | RouteHandlerWithArgument
/ * *
* A class that can handle requests .
* /
export interface RouteHandlerClass {
handleRequest ( request : Request ) : RouteHandlerReturnValue
handleRequest ( request : Request , arg? : any ) : RouteHandlerReturnValue
}
/ * *
* Base type defining a single route handler .
* /
export type RouteHandler = RouteHandlerFunction | RouteHandlerClass
export interface RouteHandler {
handler : RouteHandlerFunction | RouteHandlerClass ,
arg : any ,
}
/ * *
* Base type for a collection of route handlers .
@ -60,8 +69,12 @@ export interface RouteDefinition {
* /
export function isRouteHandler ( what : any ) : what is RouteHandler {
return (
typeof what === 'function'
|| isRouteHandlerClass ( what )
typeof what === 'object'
&& what . handler
&& (
typeof what . handler === 'function'
|| isRouteHandlerClass ( what . handler )
)
)
}
@ -72,7 +85,7 @@ export function isRouteHandler(what: any): what is RouteHandler {
* /
export function isRouteHandlerClass ( what : any ) : what is RouteHandlerClass {
return (
what && typeof what . handleRequest === 'function' && what . handleRequest . length == = 1
what && typeof what . handleRequest === 'function' && what . handleRequest . length > = 1
)
}
@ -139,13 +152,15 @@ export default class Routing extends LifecycleUnit {
* @param { Array < string > } group
* @return RouteHandlers
* /
public build_handler ( group : string [ ] ) : RouteHandlers {
public build_handler ( group : RouteHandlerDefinition [ ] ) : RouteHandlers {
const handlers : RouteHandlers = [ ]
for ( const item of group ) {
const ref = Canonical . resolve ( item )
const handler_ref = typeof item === 'object' ? item.handler : item
const arg : any = typeof item === 'object' ? item.arg : undefined
const ref = Canonical . resolve ( handler_ref )
if ( ! ref . resource ) {
this . logger . error ( ` Invalid canonical reference for route: ${ item } . Reference must include resource (e.g. controller::)! ` )
this . logger . error ( ` Invalid canonical reference for route: ${ handler_ref } . Reference must include resource (e.g. controller::)! ` )
continue
}
@ -153,10 +168,15 @@ export default class Routing extends LifecycleUnit {
const resolved = resource . get ( ref . item )
if ( ! ref . particular ) {
if ( isRouteHandler ( resolved ) ) {
handlers . push ( resolved )
const handler = {
handler : resolved ,
arg ,
}
if ( isRouteHandler ( handler ) ) {
handlers . push ( handler )
} else {
throw new TypeError ( ` Invalid canonical reference for route: ${ item } . Reference is not a valid route handler. ` )
throw new TypeError ( ` Invalid canonical reference for route: ${ handler_ref } . Reference is not a valid route handler. ` )
}
} else {
if ( isBindable ( resolved ) ) {
@ -165,16 +185,21 @@ export default class Routing extends LifecycleUnit {
handler = resolved . get_bound_method ( ref . particular )
} catch ( e ) {
this . logger . error ( e )
throw new Error ( ` Invalid canonical reference for route: ${ item } . Reference particular could not be bound. ` )
throw new Error ( ` Invalid canonical reference for route: ${ handler_ref } . Reference particular could not be bound. ` )
}
const handler_obj = {
handler ,
arg
}
if ( isRouteHandler ( handler ) ) {
handlers . push ( handler )
if ( isRouteHandler ( handler _obj ) ) {
handlers . push ( handler _obj )
} else {
throw new TypeError ( ` Invalid canonical reference for route: ${ item } . Reference is not a valid route handler. ` )
throw new TypeError ( ` Invalid canonical reference for route: ${ handler_ref } . Reference is not a valid route handler. ` )
}
} else {
throw new TypeError ( ` Invalid canonical reference for route: ${ item } . Reference specifies particular, but resolved resource is not bindable. ` )
throw new TypeError ( ` Invalid canonical reference for route: ${ handler_ref } . Reference specifies particular, but resolved resource is not bindable. ` )
}
}
}