@@ -13,6 +13,34 @@ const debug = {
1313 */
1414export class SakuraApiConfig {
1515
16+ /**
17+ * Same as the instance method, but static, and it won't try to use the last loaded config since that
18+ * requires an instance.
19+ */
20+ static dataSources ( config : { dbConnections ?: any [ ] } ) : SakuraMongoDbConnection {
21+ config = config || { } ;
22+
23+ if ( ! config . dbConnections ) {
24+ debug . normal ( `.dataSources, no config (config: ${ ! ! config } ,`
25+ + `config.dbConnections: ${ ! ! ( config || { } as any ) . dbConnections } )` ) ;
26+
27+ config . dbConnections = [ ] ;
28+ }
29+
30+ if ( ! Array . isArray ( config . dbConnections ) ) {
31+ throw new Error ( 'Invalid dbConnections array. The "dbConnections" object should be an array' ) ;
32+ }
33+
34+ const dbConns = new SakuraMongoDbConnection ( ) ;
35+
36+ debug . normal ( `Adding ${ config . dbConnections . length } dbConnections.` ) ;
37+ for ( const conn of config . dbConnections ) {
38+ dbConns . addConnection ( conn . name , conn . url , conn . mongoClientOptions ) ;
39+ }
40+
41+ return dbConns ;
42+ }
43+
1644 /**
1745 * The configuration that was loaded from the various json and ts files and the environmental
1846 * variables that were set at the time the configuration was last loaded.
@@ -82,7 +110,7 @@ export class SakuraApiConfig {
82110 path = path || process . env . SAKURA_API_CONFIG || 'config/environment.json' ;
83111 debug . normal ( `.load path: '${ path } '` ) ;
84112
85- let config = { } ;
113+ const config = { } ;
86114 let baseConfig = { } ;
87115 let baseJsConfig = { } ;
88116
@@ -98,7 +126,7 @@ export class SakuraApiConfig {
98126 }
99127
100128 // environment.js
101- let jsPath = changeFileExtension ( path , 'js' ) ;
129+ const jsPath = changeFileExtension ( path , 'js' ) ;
102130 debug . normal ( `loading ${ jsPath } ` ) ;
103131 try {
104132 baseJsConfig = require ( `${ process . cwd ( ) } /${ jsPath } ` ) ;
@@ -109,13 +137,13 @@ export class SakuraApiConfig {
109137 handleLoadError ( err , path , true ) ;
110138 }
111139
112- let env = process . env . NODE_ENV ;
140+ const env = process . env . NODE_ENV ;
113141 let envConfig = { } ;
114142 let envJsConfig = { } ;
115143 if ( env && env . NODE_ENV !== '' ) {
116144 // environment.{env}.json
117- let pathParts = path . split ( '/' ) ;
118- let fileParts = pathParts [ pathParts . length - 1 ] . split ( '.' ) ;
145+ const pathParts = path . split ( '/' ) ;
146+ const fileParts = pathParts [ pathParts . length - 1 ] . split ( '.' ) ;
119147
120148 fileParts . splice ( fileParts . length - 1 , 0 , env ) ;
121149 pathParts [ pathParts . length - 1 ] = fileParts . join ( '.' ) ;
@@ -152,69 +180,40 @@ export class SakuraApiConfig {
152180 return config ;
153181
154182 //////////
155- function changeFileExtension ( path : string , newExtension : string ) {
156- let pathParts = path . split ( '/' ) ;
157- let fileParts = pathParts [ pathParts . length - 1 ] . split ( '.' ) ;
183+ function changeFileExtension ( targetPath : string , newExtension : string ) {
184+ const pathParts = targetPath . split ( '/' ) ;
185+ const fileParts = pathParts [ pathParts . length - 1 ] . split ( '.' ) ;
158186 fileParts [ fileParts . length - 1 ] = newExtension ;
159187
160188 pathParts [ pathParts . length - 1 ] = fileParts . join ( '.' ) ;
161189 return pathParts . join ( '/' ) ;
162190 }
163191
164- function handleLoadError ( err : Error , path : string , noDefault : boolean ) {
165- if ( err [ ' code' ] === 'ENOENT' ) {
192+ function handleLoadError ( err : Error , targetPath : string , noDefault : boolean ) {
193+ if ( ( err as any ) . code === 'ENOENT' ) {
166194 // NOOP: the config file is empty, just default to {}
167- debug . normal ( `.load config file empty, defaulting to {} for path: '${ path } '` ) ;
195+ debug . normal ( `.load config file empty, defaulting to {} for path: '${ targetPath } '` ) ;
168196 return ;
169197 } else if ( err . message . startsWith ( 'Cannot find module' ) ) {
170198 // NOOP: a ts config file wasn't found
171- debug . normal ( `.load config file wasn't found, defaulting to {} for path: '${ path } '` ) ;
199+ debug . normal ( `.load config file wasn't found, defaulting to {} for path: '${ targetPath } '` ) ;
172200 return ;
173201 } else if ( err . message === 'Unexpected end of JSON input' ) {
174- let e = new Error ( err . message ) ;
175- e [ ' code' ] = 'INVALID_JSON_EMPTY' ;
176- e [ ' path' ] = path ;
177- debug . normal ( `.load path: '${ path } ', error:` , err ) ;
178- throw e ;
202+ const jsonInputErr = new Error ( err . message ) ;
203+ ( jsonInputErr as any ) . code = 'INVALID_JSON_EMPTY' ;
204+ ( jsonInputErr as any ) . path = targetPath ;
205+ debug . normal ( `.load path: '${ targetPath } ', error:` , err ) ;
206+ throw jsonInputErr ;
179207 } else if ( err . message . startsWith ( 'Unexpected token' ) ) {
180- let e = new Error ( err . message ) ;
181- e [ ' code' ] = 'INVALID_JSON_INVALID' ;
182- e [ ' path' ] = path ;
183- debug . normal ( `.load path: '${ path } ', error:` , err ) ;
184- throw e ;
208+ const tokenErr = new Error ( err . message ) ;
209+ ( tokenErr as any ) . code = 'INVALID_JSON_INVALID' ;
210+ ( tokenErr as any ) . path = targetPath ;
211+ debug . normal ( `.load path: '${ targetPath } ', error:` , err ) ;
212+ throw tokenErr ;
185213 } else {
186- debug . normal ( `.load path: '${ path } ', error:` , err ) ;
214+ debug . normal ( `.load path: '${ targetPath } ', error:` , err ) ;
187215 throw err ;
188216 }
189217 }
190218 }
191-
192- /**
193- * Same as the instance method, but static, and it won't try to use the last loaded config since that
194- * requires an instance.
195- */
196- static dataSources ( config : { dbConnections ?: any [ ] } ) : SakuraMongoDbConnection {
197- config = config || { } ;
198-
199- if ( ! config . dbConnections ) {
200- debug . normal ( `.dataSources, no config (config: ${ ! ! config } ,`
201- + `config.dbConnections: ${ ! ! ( config || < any > { } ) . dbConnections } )` ) ;
202-
203- config . dbConnections = [ ] ;
204- //return null;
205- }
206-
207- if ( ! Array . isArray ( config . dbConnections ) ) {
208- throw new Error ( 'Invalid dbConnections array. The "dbConnections" object should be an array' ) ;
209- }
210-
211- let dbConns = new SakuraMongoDbConnection ( ) ;
212-
213- debug . normal ( `Adding ${ config . dbConnections . length } dbConnections.` ) ;
214- for ( let conn of config . dbConnections ) {
215- dbConns . addConnection ( conn . name , conn . url , conn . mongoClientOptions ) ;
216- }
217-
218- return dbConns ;
219- }
220219}
0 commit comments