diff --git a/.gitignore b/.gitignore index 8531e023..db727672 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ bower_components # IDEs and editors .idea +.vscode/symbols.json # misc .sass-cache diff --git a/lib/angular2/index.js b/lib/angular2/index.js index 6d96a23c..7d0c3dd8 100644 --- a/lib/angular2/index.js +++ b/lib/angular2/index.js @@ -745,7 +745,7 @@ module.exports = function generate(ctx) { let relationType = buildRelationType( model, relation ); let defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') >= 0 ? ' = []' : ''; defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') === -1 ? ' = null' : defaultTypeValue; - output.push( ` ${relation}${isInterface ? '?' : ''}: ${relationType}${defaultTypeValue};` ); + output.push( ` ${relation}${isInterface ? '?' : '?'}: ${relationType}${defaultTypeValue};` ); }); return output.join('\n'); } diff --git a/lib/angular2/shared/sockets/socket.browser.ts b/lib/angular2/shared/sockets/socket.browser.ts index 73b57ba2..59069fea 100644 --- a/lib/angular2/shared/sockets/socket.browser.ts +++ b/lib/angular2/shared/sockets/socket.browser.ts @@ -1,5 +1,8 @@ /* tslint:disable */ -import * as io from 'socket.io-client'; +// add this to compilerOptions in tsconfig.json: +// "allowSyntheticDefaultImports": true, +// "esModuleInterop": true +import io from 'socket.io-client'; /** * @author Jonathan Casarrubias * @module SocketBrowser diff --git a/lib/angular2/shared/sockets/socket.node.ts b/lib/angular2/shared/sockets/socket.node.ts index b27bcad4..251c51d5 100644 --- a/lib/angular2/shared/sockets/socket.node.ts +++ b/lib/angular2/shared/sockets/socket.node.ts @@ -1,5 +1,8 @@ /* tslint:disable */ -import * as io from 'socket.io-client'; +// add this to compilerOptions in tsconfig.json: +// "allowSyntheticDefaultImports": true, +// "esModuleInterop": true +import io from 'socket.io-client'; /** * @author Jonathan Casarrubias * @module SocketNode diff --git a/lib/helpers/index.js b/lib/helpers/index.js index fc105a25..2a8dc095 100644 --- a/lib/helpers/index.js +++ b/lib/helpers/index.js @@ -594,7 +594,7 @@ exports.buildSquema = (ctx) => { let relationType = buildRelationType(model, relation); let defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') >= 0 ? ' = []' : ''; defaultTypeValue = !isInterface && ctx.defaultValue === 'enabled' && relationType.indexOf('Array') === -1 ? ' = null' : defaultTypeValue; - output.push(` ${relation}${isInterface ? '?' : ''}${isTyped ? ':' + relationType + defaultTypeValue + ';' : ';'}`); + output.push(` ${relation}${isInterface ? '?' : '?'}${isTyped ? ':' + relationType + defaultTypeValue + ';' : ';'}`); }); return output.join('\n'); } diff --git a/lib/utils.js b/lib/utils.js index 2cea4836..eebb71da 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -39,7 +39,7 @@ exports.describeModels = function describeModels(app) { c.sharedClass.ctor.settings ); c.sharedClass.ctor.settings.sdk = Object.assign( - {enabled: true}, + { enabled: true }, c.sharedClass.ctor.settings.sdk ); // Tell SDK to blacklist specific methods @@ -84,7 +84,7 @@ exports.describeModels = function describeModels(app) { method.accepts.forEach((param, index, arr) => { if (loaded[param.arg]) { arr.splice(index, 1); - } else  { + } else { loaded[param.arg] = true; } }); @@ -92,7 +92,7 @@ exports.describeModels = function describeModels(app) { if (!method.accepts) return; // Filter out parameters that are generated from the incoming request, // or generated by functions that use those resources. - method.accepts = method.accepts.filter(function(arg) { + method.accepts = method.accepts.filter(function (arg) { if (!arg.http) return true; // Don't show derived arguments. if (typeof arg.http === 'function') return false; @@ -125,9 +125,29 @@ exports.describeModels = function describeModels(app) { buildScopes(models); + completeTargetClass(models) + return models; }; +// adds the targetClass to all relations, also those that are not +// used by a scope method. Otherwise type information will be missing +// on related models. +function completeTargetClass(models) { + for (var modelName in models) { + // Skip Account since otherwise Account Service gets a duplicate import statement + // for AccessToken + if (modelName !== 'PubAccount') { + var modelClass = models[modelName]; + var relations = modelClass.sharedClass.ctor.relations; + Object.keys(relations).forEach(key => { + var relation = relations[key]; + if (!relation.targetClass) relation.targetClass = relation.modelTo.name; + }) + } + } +} + var SCOPE_METHOD_REGEX = /^prototype.__([^_]+)__(.+)$/; function buildScopes(models) { diff --git a/tests/ng2web/src/app/room-ngrx/room-ngrx.list.component.ts b/tests/ng2web/src/app/room-ngrx/room-ngrx.list.component.ts index 20a32af4..8e2650f6 100644 --- a/tests/ng2web/src/app/room-ngrx/room-ngrx.list.component.ts +++ b/tests/ng2web/src/app/room-ngrx/room-ngrx.list.component.ts @@ -8,7 +8,10 @@ import { Observable } from 'rxjs/Observable'; import { LoopBackConfig } from '../shared/sdk/lb.config'; LoopBackConfig.setBaseURL('http://localhost:3000'); -import * as io from 'socket.io-client'; +// add this to compilerOptions in tsconfig.json: +// "allowSyntheticDefaultImports": true, +// "esModuleInterop": true +import io from 'socket.io-client'; import { Orm } from '../shared/sdk/orm'; diff --git a/tests/ng2web/src/app/room/room.list.component.ts b/tests/ng2web/src/app/room/room.list.component.ts index b00484c2..92d926ec 100644 --- a/tests/ng2web/src/app/room/room.list.component.ts +++ b/tests/ng2web/src/app/room/room.list.component.ts @@ -7,7 +7,11 @@ import { Subscription } from 'rxjs'; import { LoopBackConfig } from '../shared/sdk/lb.config'; LoopBackConfig.setBaseURL('http://localhost:3000'); -import * as io from 'socket.io-client'; +// add this to compilerOptions in tsconfig.json: +// "allowSyntheticDefaultImports": true, +// "esModuleInterop": true +import io from 'socket.io-client'; + var i = 0;