@@ -50,15 +50,17 @@ module.exports = function generate(ctx) {
5050 output : '/index.ts' ,
5151 params : {
5252 isIo : ctx . isIo ,
53- models : ctx . models
53+ models : ctx . models ,
54+ buildModuleImports : buildModuleImports
5455 }
5556 } ,
5657 {
5758 template : './shared/sdk.module.ejs' ,
5859 output : '/sdk.module.ts' ,
5960 params : {
6061 isIo : ctx . isIo ,
61- models : ctx . models
62+ models : ctx . models ,
63+ buildModuleImports : buildModuleImports
6264 }
6365 } ,
6466 {
@@ -105,7 +107,10 @@ module.exports = function generate(ctx) {
105107 {
106108 template : './shared/services/core/base.ejs' ,
107109 output : '/services/core/base.service.ts' ,
108- params : { isIo : ctx . isIo }
110+ params : {
111+ isIo : ctx . isIo ,
112+ buildBaseServiceImports : buildBaseServiceImports
113+ }
109114 } ,
110115 {
111116 template : './shared/services/core/error.ejs' ,
@@ -114,7 +119,7 @@ module.exports = function generate(ctx) {
114119 } ,
115120 {
116121 template : './shared/services/core/logger.ejs' ,
117- output : '/services/core /logger.service.ts' ,
122+ output : '/services/custom /logger.service.ts' ,
118123 params : { }
119124 } ,
120125 {
@@ -279,7 +284,19 @@ module.exports = function generate(ctx) {
279284 */
280285 function buildServiceImports ( model ) {
281286 let modelName = capitalize ( model . name ) ;
282- let output = [ `import { ${ modelName } } from '../../models/${ modelName } '` ] ;
287+ let imports = [
288+ { module : 'Injectable, Inject, Optional' , from : '@angular/core' } ,
289+ { module : 'Http, Response' , from : '@angular/http' } ,
290+ { module : 'BaseLoopBackApi' , from : '../core/base.service' } ,
291+ { module : 'LoopBackConfig' , from : '../../lb.config' } ,
292+ { module : 'LoopBackAuth' , from : '../core/auth.service' } ,
293+ { module : 'LoopBackFilter' , from : '../../models/BaseModels' } ,
294+ { module : 'JSONSearchParams' , from : '../core/search.params' } ,
295+ { module : 'ErrorHandler' , from : '../core/error.service' } ,
296+ { module : 'Subject' , from : 'rxjs/Subject' } ,
297+ { module : 'rxjs/add/operator/map' } ,
298+ { module : modelName , from : `../../models/${ modelName } ` } ,
299+ ] ;
283300 let loaded = { } ; loaded [ model . name ] = true ;
284301 getModelRelations ( model ) . forEach ( ( relationName , i ) => {
285302 let targetClass = model . sharedClass . ctor . relations [ relationName ] . targetClass ;
@@ -296,18 +313,82 @@ module.exports = function generate(ctx) {
296313 let through = capitalize ( model . sharedClass . ctor . relations [ relationName ] . modelThrough . sharedClass . name ) ;
297314 if ( ! loaded [ through ] ) {
298315 loaded [ through ] = true ;
299- output . push ( `import { ${ through } } from ' ../../models/${ through } '` ) ;
316+ imports . push ( { module : through , from : ` ../../models/${ through } ` } ) ;
300317 }
301318 }
302319 // Now and after the through model was included is the right time to verify if the current model
303320 // was loaded by another relationship, this way we don't duplicate the class during imports'
304321 if ( ! loaded [ targetClass ] ) {
305322 loaded [ targetClass ] = true ;
306- output . push ( `import { ${ targetClass } } from ' ../../models/${ targetClass } '` ) ;
323+ imports . push ( { module : targetClass , from : ` ../../models/${ targetClass } ` } ) ;
307324 }
308325 } ) ;
309- output . push ( `import { LoopBackFilter } from '../../models/BaseModels'` ) ;
310- return output . join ( ';\n' ) ;
326+
327+ return buildImports ( imports ) ;
328+ }
329+ /**
330+ * @method buildModuleImports
331+ * @description
332+ * Define import statement for the SDK Module
333+ */
334+ function buildModuleImports ( models , isIndex ) {
335+ let imports = [
336+ { module : 'JSONSearchParams' , from : './services/core/search.params' } ,
337+ { module : 'ErrorHandler' , from : './services/core/error.service' } ,
338+ { module : 'LoopBackAuth' , from : './services/core/auth.service' } ,
339+ { module : 'LoggerService' , from : './services/custom/logger.service' } ,
340+ ] ;
341+
342+ if ( ! isIndex ) {
343+ imports = imports . concat ( [
344+ { module : 'HttpModule' , from : '@angular/http' } ,
345+ { module : 'CommonModule' , from : '@angular/common' } ,
346+ { module : 'NgModule, ModuleWithProviders' , from : '@angular/core' }
347+ ] ) ;
348+ }
349+
350+ Object . keys ( models ) . forEach ( modelName => {
351+ let name = capitalize ( modelName ) ;
352+ imports . push ( { module : `${ name } Api` , from : `./services/custom/${ name } ` } ) ;
353+ } ) ;
354+
355+ return buildImports ( imports ) ;
356+ }
357+ /**
358+ * @method buildBaseServiceImports
359+ * @description
360+ * Define import statement for the SDK Module
361+ */
362+ function buildBaseServiceImports ( isIo ) {
363+ let imports = [
364+ { module : 'Injectable, Inject, Optional' , from : '@angular/core' } ,
365+ { module : 'Http, Headers, Request' , from : '@angular/http' } ,
366+ { module : 'NgModule, ModuleWithProviders' , from : '@angular/core' } ,
367+ { module : 'JSONSearchParams' , from : './search.params' } ,
368+ { module : 'ErrorHandler' , from : './error.service' } ,
369+ { module : 'LoopBackAuth' , from : './auth.service' } ,
370+ { module : 'LoopBackConfig' , from : '../../lb.config' } ,
371+ { module : 'rxjs/add/operator/catch' } ,
372+ { module : 'rxjs/add/operator/map' } ,
373+ ] ;
374+
375+ if ( isIo ) {
376+ imports . push ( { module : 'Subject' , from : 'rxjs/Subject' } ) ;
377+ imports . push ( { module : 'SocketConnections' , from : '../../sockets/socket.connections' } ) ;
378+ }
379+
380+ return buildImports ( imports ) ;
381+ }
382+ /**
383+ * @method buildImports
384+ * @description
385+ * Transform an array of objects describing which should be imported into
386+ * the actual template strings
387+ */
388+ function buildImports ( imports ) {
389+ return imports . map ( item =>
390+ `import ${ ( item . from ? `{ ${ item . module } }` : `'${ item . module } '` ) } ${ ( item . from ? ` from '${ item . from } '` : '' ) } ;`
391+ ) . join ( '\n' ) ;
311392 }
312393 /**
313394 * @method normalizeMethodName
0 commit comments