@@ -11,7 +11,9 @@ import {
11
11
IntegrationEntityBridgeRequest ,
12
12
} from "./models" ;
13
13
import { CustomRouter } from "./models/custom-router.model" ;
14
+ import { CustomRoute } from "./models/custom-routes.model" ;
14
15
import { getContactCache } from "./util/get-contact-cache" ;
16
+ import { errorLogger , infoLogger } from "./util" ;
15
17
16
18
const PORT : number = Number ( process . env . PORT ) || 8080 ;
17
19
@@ -32,7 +34,8 @@ let cache: ContactCache | null = null;
32
34
33
35
export function start (
34
36
adapter : Adapter ,
35
- customRouters : CustomRouter [ ] = [ ]
37
+ customRouters : CustomRouter [ ] = [ ] ,
38
+ customRoutes : CustomRoute [ ] = [ ]
36
39
) : Server {
37
40
cache = getContactCache ( ) ;
38
41
@@ -93,6 +96,46 @@ export function start(
93
96
94
97
customRouters . forEach ( ( { path, router } ) => app . use ( path , router ) ) ;
95
98
99
+ customRoutes . forEach ( ( { requestType, path, handler : customHandler } ) => {
100
+ infoLogger ( "start" , `CustomRoute ${ path } added.` , undefined ) ;
101
+
102
+ const handler = ( req : any , res : any , next : any ) => {
103
+ try {
104
+ infoLogger ( path , "START" , undefined ) ;
105
+
106
+ customHandler ( req , res , next ) ;
107
+
108
+ infoLogger ( path , "END" , undefined ) ;
109
+ } catch ( error ) {
110
+ errorLogger (
111
+ path ,
112
+ "Error while executing custom route" ,
113
+ undefined ,
114
+ error ?? undefined
115
+ ) ;
116
+ next ( error ) ;
117
+ }
118
+ } ;
119
+
120
+ switch ( requestType ) {
121
+ case "get" :
122
+ app . get ( path , handler ) ;
123
+ break ;
124
+ case "post" :
125
+ app . post ( path , handler ) ;
126
+ break ;
127
+ case "put" :
128
+ app . put ( path , handler ) ;
129
+ break ;
130
+ case "delete" :
131
+ app . delete ( path , handler ) ;
132
+ break ;
133
+
134
+ default :
135
+ throw `CustomRoute requestType ${ requestType } not implemented!` ;
136
+ }
137
+ } ) ;
138
+
96
139
return app . listen ( PORT , ( ) => console . log ( `Listening on port ${ PORT } ` ) ) ;
97
140
}
98
141
0 commit comments