Skip to content

Add option to leave out definitions for common modules + namespace models #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ module.exports = function generateServices(app, options) {
options = extend({
ngModuleName: 'lbServices',
apiUrl: '/',
includeCommonModules: true,
namespaceModels: false,
namespaceDelimiter: '.',
}, options);

var models = describeModels(app, options);
Expand All @@ -67,13 +70,26 @@ module.exports = function generateServices(app, options) {
moduleName: options.ngModuleName,
models: models,
urlBase: options.apiUrl.replace(/\/+$/, ''),
includeCommonModules: options.includeCommonModules,
});
};

function getFormattedModelName(modelName, options) {
// Always capitalize first letter of model name
var resourceModelName = modelName[0].toUpperCase() + modelName.slice(1);

// Prefix with the module name and delimiter if namespacing is on
if (options.namespaceModels) {
resourceModelName = options.ngModuleName +
options.namespaceDelimiter + resourceModelName;
}
return resourceModelName;
}

function describeModels(app, options) {
var result = {};
app.handler('rest').adapter.getClasses().forEach(function(c) {
var name = c.name;
var name = getFormattedModelName(c.name, options);
c.description = c.sharedClass.ctor.settings.description;

if (!c.ctor) {
Expand Down
7 changes: 3 additions & 4 deletions lib/services.template.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&

<% for (var modelName in models) {
var meta = models[modelName];
// capitalize the model name
modelName = modelName[0].toUpperCase() + modelName.slice(1);
-%>
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modelName won't be available any more, are you sure it's not used anywhere in the template?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modelName should be available inside the for loop, as this deleted code just updates the variable, rather than creating a new one (the formatting that used to be in the template is now in getFormattedModelName in services.js). I double-checked that there isn't anywhere outside the for loop where modelName is used - it's just inside this for loop that we reference it directly.

* @ngdoc object
Expand Down Expand Up @@ -300,7 +298,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
}]);

<% } // for modelName in models -%>

<% if (includeCommonModules) { %>
module
.factory('LoopBackAuth', function() {
var props = ['accessTokenId', 'currentUserId', 'rememberMe'];
Expand Down Expand Up @@ -490,7 +488,8 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
return LoopBackResource;
}];
});
<%
<% } // end if (includeCommonModules)

function getJsDocType(arg) {
var type = arg.type == 'any' ? '*' : arg.type;
if (!arg.required) type += '=';
Expand Down
4 changes: 3 additions & 1 deletion test.e2e/given.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ define(['angular', 'angularMocks', 'angularResource'], function(angular) {
* ```
*/
given.servicesForLoopBackApp = function(options, cb) {
options.name = generateUniqueServiceName(options.name);
if (!options.name) {
options.name = generateUniqueServiceName(options.name);
}

var promise = callSetup(options)
.then(function(config) { return config.servicesUrl; })
Expand Down
144 changes: 144 additions & 0 deletions test.e2e/spec/services.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,150 @@ define(['angular', 'given', 'util'], function(angular, given, util) {
});
});

describe('$resource generated with includeCommonModules:false', function() {
var $injector;
before(function() {
return given.servicesForLoopBackApp(
{
models: {
Product: {
properties: {
name: 'string',
price: { type: 'number' },
},
},
},
includeCommonModules: false,
})
.then(function(createInjector) {
$injector = createInjector();
});
});

it('does not have "LoopBackAuth module"', function() {
expect(function() {
$injector.get('LoopBackAuth');
}).to.throw(/Unknown provider/);
});

it('does not have "LoopBackResource provider"', function() {
expect(function() {
$injector.get('LoopBackResource');
}).to.throw(/Unknown provider/);
});

it('does not have "LoopBackAuthRequestInterceptor module"',
function() {
expect(function() {
$injector.get('LoopBackAuthRequestInterceptor');
}).to.throw(/Unknown provider/);
});
});

describe('$resource generated with includeCommonModules:true (by default)',
function() {
var $injector;
before(function() {
return given.servicesForLoopBackApp(
{
models: {
Product: {
properties: {
name: 'string',
price: { type: 'number' },
},
},
},
})
.then(function(createInjector) {
$injector = createInjector();
});
});

it('has "LoopBackAuth module"', function() {
expect(function() {
$injector.get('LoopBackAuth');
}).to.not.throw();
});

it('has "LoopBackResource provider"', function() {
expect(function() {
$injector.get('LoopBackResource');
}).to.not.throw();
});

it('has "LoopBackAuthRequestInterceptor module"',
function() {
expect(function() {
$injector.get('LoopBackAuthRequestInterceptor');
}).to.not.throw();
});
});

describe('$resource generated with namespaceModels:true', function() {
var $injector;
before(function() {
return given.servicesForLoopBackApp(
{
models: {
Product: {
properties: {
name: 'string',
price: { type: 'number' },
},
},
},
name: 'lbServices',
namespaceModels: true,
})
.then(function(createInjector) {
$injector = createInjector();
});
});

it('defines the "Product" model as "lbServices.Product"', function() {
expect(function() {
$injector.get('Product');
}).to.throw(/Unknown provider/);
expect(function() {
$injector.get('lbServices.Product');
}).to.not.throw();
});
});

describe('$resource generated with namespaceModels:true and ' +
'namespaceDelimiter:_', function() {
var $injector;
before(function() {
return given.servicesForLoopBackApp(
{
models: {
Product: {
properties: {
name: 'string',
price: { type: 'number' },
},
},
},
name: 'lbServices',
namespaceModels: true,
namespaceDelimiter: '_',
})
.then(function(createInjector) {
$injector = createInjector();
});
});

it('defines the "Product" model as "lbServices.Product"', function() {
expect(function() {
$injector.get('Product');
}).to.throw(/Unknown provider/);
expect(function() {
$injector.get('lbServices_Product');
}).to.not.throw();
});
});

describe('for models with belongsTo relation', function() {
var $injector, Town, Country, testData;
before(function() {
Expand Down
36 changes: 23 additions & 13 deletions test.e2e/test-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ masterApp.post('/setup', function(req, res, next) {
var name = opts.name;
var models = opts.models;
var enableAuth = opts.enableAuth;
var includeSchema = opts.includeSchema;
var setupFn = compileSetupFn(name, opts.setupFn);

if (!name)
Expand Down Expand Up @@ -105,19 +104,8 @@ masterApp.post('/setup', function(req, res, next) {
res.send(500, err);
return;
}

try {
if (includeSchema) {
// the new options-based API
servicesScript = generator.services(lbApp, {
ngModuleName: name,
apiUrl: apiUrl,
includeSchema: includeSchema,
});
} else {
// the old API, test it to verify backwards compatibility
servicesScript = generator.services(lbApp, name, apiUrl);
}
servicesScript = generateService(generator, lbApp, apiUrl, opts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep "the old API" branch, we need to keep it covered by unit-tests to prevent regressions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, this is handled by generateService, ignore my comment then.

} catch (err) {
console.error('Cannot generate services script:', err.stack);
servicesScript = 'throw new Error("Error generating services script.");';
Expand Down Expand Up @@ -161,6 +149,28 @@ masterApp.listen(port, function() {
runAndExit(process.argv[2], process.argv.slice(3));
});

function generateService(generator, lbApp, apiUrl, opts) {
var servicesScript;
if (opts.includeSchema !== undefined ||
opts.includeCommonModules !== undefined ||
opts.namespaceModels !== undefined ||
opts.namespaceDelimiter !== undefined) {
// the new options-based API

// build options object for new options-based API
var generatorOptions = opts;
generatorOptions.ngModuleName = opts.name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are missing apiUrl here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes! Added - thanks!

generatorOptions.apiUrl = apiUrl;

servicesScript = generator.services(lbApp, generatorOptions);
} else {
// the old API, test it to verify backwards compatibility
servicesScript = generator.services(lbApp, opts.name, apiUrl);
}

return servicesScript;
}

function runAndExit(cmd, args) {
console.log('Running %s %s', cmd, args.join(' '));
var child = require('child_process').spawn(cmd, args, { stdio: 'inherit' });
Expand Down