@@ -9,7 +9,7 @@ import { Type } from '../../core/decorators';
9
9
import { enumByMethod , LifecycleComponentEnum } from './enums' ;
10
10
import { LifecycleManager } from './lifecycle' ;
11
11
import { RouteBuilder } from './route' ;
12
- import { CoreRoute , HapiConfig , HapinessHTTPHandlerResponse } from './interfaces' ;
12
+ import { ConnectionOptions , CoreRoute , HapiConfig , HTTPHandlerResponse } from './interfaces' ;
13
13
import { Observable } from 'rxjs' ;
14
14
import { RouteConfiguration , Server , Request , ReplyNoContinue , ReplyWithContinue } from 'hapi' ;
15
15
@@ -32,7 +32,12 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
32
32
onExtensionLoad ( module : CoreModule , config : HapiConfig ) : Observable < Extension > {
33
33
return Observable
34
34
. of ( new Server ( config . options ) )
35
- . do ( _ => _ . connection ( Object . assign ( { } , config , { options : undefined } ) ) )
35
+ . flatMap ( server => Observable
36
+ . of ( Object . assign ( { } , config , { options : undefined } ) )
37
+ . map ( _ => this . formatConnections ( _ ) )
38
+ . do ( _ => _ . forEach ( connection => server . connection ( connection ) ) )
39
+ . map ( _ => server )
40
+ )
36
41
. flatMap ( server =>
37
42
Observable
38
43
. of ( {
@@ -63,6 +68,23 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
63
68
. flatMap ( _ => server . start ( ) ) ;
64
69
}
65
70
71
+ /**
72
+ * Format the config provided
73
+ * to a list of ConnectionOptions
74
+ *
75
+ * @param {HapiConfig } config
76
+ * @returns ConnectionOptions
77
+ */
78
+ private formatConnections ( config : HapiConfig ) : ConnectionOptions [ ] {
79
+ return [ ]
80
+ . concat ( ! ! ( < any > config ) . connections ?
81
+ ( < any > config ) . connections :
82
+ config
83
+ )
84
+ . filter ( _ => ! ! _ )
85
+ . map ( _ => < ConnectionOptions > _ ) ;
86
+ }
87
+
66
88
/**
67
89
* Register a HapiJS Plugin
68
90
*
@@ -71,36 +93,50 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
71
93
* @returns Observable
72
94
*/
73
95
private registerPlugin ( module : CoreModule , server : Server ) : Observable < CoreRoute [ ] > {
74
- const register : any = ( s , o , n ) => n ( ) ;
75
- register . attributes = {
76
- name : module . name ,
77
- version : module . version
78
- } ;
79
- return Observable
80
- . fromPromise ( server . register ( register ) )
81
- . flatMap ( _ => this . addRoutes ( module , server ) ) ;
96
+ return this
97
+ . buildRoutes ( module )
98
+ . flatMap ( routes => Observable
99
+ . of ( < any > this . registerHandler ( routes ) )
100
+ . do ( _ => _ . attributes = { name : module . name , version : module . version } )
101
+ . flatMap ( _ => Observable
102
+ . fromPromise ( server . register ( _ ) )
103
+ . map ( __ => routes )
104
+ )
105
+ ) ;
82
106
}
83
107
84
108
/**
85
109
* Add routes from CoreModule
86
110
*
87
- * @param {CoreModule } module
88
- * @param {Server } server
111
+ * @param {CoreRoute[] } module
89
112
* @returns Observable
90
113
*/
91
- private addRoutes ( module : CoreModule , server : Server ) : Observable < CoreRoute [ ] > {
92
- return Observable
93
- . from ( RouteBuilder . buildRoutes ( module ) )
94
- . do ( _ =>
95
- server
96
- . route ( < RouteConfiguration > {
114
+ private registerHandler ( routes : CoreRoute [ ] = [ ] ) : ( s , o , n ) => void {
115
+ return ( server : Server , options , next ) => {
116
+ routes
117
+ . forEach ( _ => {
118
+ const _server = ! ! _ . labels ? server . select ( _ . labels ) : server ;
119
+ _server . route ( < RouteConfiguration > {
97
120
method : _ . method ,
98
121
path : _ . path ,
99
122
config : Object . assign ( {
100
123
handler : ( request , reply ) => this . httpHandler ( request , reply , _ )
101
124
} , _ . config )
102
- } )
103
- )
125
+ } ) ;
126
+ } ) ;
127
+ next ( ) ;
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Build CoreRoute based on a module
133
+ *
134
+ * @param {CoreModule } module
135
+ * @returns Observable
136
+ */
137
+ private buildRoutes ( module : CoreModule ) : Observable < CoreRoute [ ] > {
138
+ return Observable
139
+ . from ( RouteBuilder . buildRoutes ( module ) )
104
140
. toArray ( ) ;
105
141
}
106
142
@@ -135,14 +171,26 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
135
171
) ;
136
172
}
137
173
138
- private formatResponse ( data : any ) : HapinessHTTPHandlerResponse {
174
+ /**
175
+ * Format response to HTTPHandlerResponse object
176
+ *
177
+ * @param {any } data
178
+ * @returns HTTPHandlerResponse
179
+ */
180
+ private formatResponse ( data : any ) : HTTPHandlerResponse {
139
181
return {
140
182
statusCode : ! ! data ? data . statusCode || 200 : 200 ,
141
183
headers : ! ! data ? data . headers || { } : { } ,
142
184
response : ! ! data ? data . response || data : data
143
185
} ;
144
186
}
145
187
188
+ /**
189
+ * Check of response is not empty
190
+ *
191
+ * @param {any } response
192
+ * @returns boolean
193
+ */
146
194
private isValid ( response : any ) : boolean {
147
195
return typeof ( response ) !== 'undefined' && response !== null ;
148
196
}
@@ -172,6 +220,17 @@ export class HttpServerExt implements OnExtensionLoad, OnModuleInstantiated {
172
220
. map ( _ => module ) ;
173
221
}
174
222
223
+ /**
224
+ * Lifecycle Event Handler
225
+ * Instantiate the Lifecycle component
226
+ * And trigger the hook
227
+ *
228
+ * @param {Type<any> } lifecycle
229
+ * @param {CoreModule } module
230
+ * @param {Request } request
231
+ * @param {ReplyWithContinue } reply
232
+ * @returns Observable
233
+ */
175
234
private eventHandler ( lifecycle : Type < any > , module : CoreModule , request : Request , reply : ReplyWithContinue ) : Observable < any > {
176
235
return Observable
177
236
. of ( lifecycle )
0 commit comments