@@ -24,6 +24,14 @@ export interface Routing {
2424
2525export type Parsers = Partial < Record < ContentType , RequestHandler [ ] > > ;
2626
27+ interface InitProps {
28+ app : IRouter ;
29+ getLogger : GetLogger ;
30+ config : CommonConfig ;
31+ routing : Routing ;
32+ parsers ?: Parsers ;
33+ }
34+
2735const lineUp = ( methods : CORSMethod [ ] ) =>
2836 methods // auxiliary methods go last
2937 . sort ( ( a , b ) => + isMethod ( b ) - + isMethod ( a ) || a . localeCompare ( b ) )
@@ -50,34 +58,31 @@ const makeCorsHeaders = (accessMethods: CORSMethod[]) => ({
5058
5159type Siblings = Map < CORSMethod , [ RequestHandler [ ] , AbstractEndpoint ] > ;
5260
53- export const initRouting = ( {
61+ /** This fn exists to reduce the complexity of initRouting and to ensure the disposal of Diagnostics ASAP */
62+ const collectSiblings = ( {
5463 app,
5564 getLogger,
5665 config,
5766 routing,
5867 parsers,
59- } : {
60- app : IRouter ;
61- getLogger : GetLogger ;
62- config : CommonConfig ;
63- routing : Routing ;
64- parsers ?: Parsers ;
65- } ) => {
66- let doc = isProduction ( ) ? undefined : new Diagnostics ( getLogger ( ) ) ; // disposable
68+ } : InitProps ) => {
69+ const doc = isProduction ( ) ? undefined : new Diagnostics ( getLogger ( ) ) ;
6770 const familiar = new Map < string , Siblings > ( ) ;
6871 const onEndpoint : OnEndpoint = ( method , path , endpoint ) => {
69- if ( ! isProduction ( ) ) {
70- doc ?. checkSchema ( endpoint , { path, method } ) ;
71- doc ?. checkPathParams ( path , endpoint , { method } ) ;
72- }
72+ doc ?. checkSchema ( endpoint , { path, method } ) ;
73+ doc ?. checkPathParams ( path , endpoint , { method } ) ;
7374 const matchingParsers = parsers ?. [ endpoint . requestType ] || [ ] ;
7475 const value = R . pair ( matchingParsers , endpoint ) ;
7576 if ( ! familiar . has ( path ) )
7677 familiar . set ( path , new Map ( config . cors ? [ [ "options" , value ] ] : [ ] ) ) ;
7778 familiar . get ( path ) ?. set ( method , value ) ;
7879 } ;
7980 walkRouting ( { routing, onEndpoint, onStatic : app . use . bind ( app ) } ) ;
80- doc = undefined ; // hint for garbage collector
81+ return familiar ;
82+ } ;
83+
84+ export const initRouting = ( { app, config, getLogger, ...rest } : InitProps ) => {
85+ const familiar = collectSiblings ( { app, getLogger, config, ...rest } ) ;
8186 const deprioritized = new Map < string , RequestHandler > ( ) ;
8287 for ( const [ path , methods ] of familiar ) {
8388 const accessMethods = Array . from ( methods . keys ( ) ) ;
0 commit comments