Skip to content
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
25 changes: 25 additions & 0 deletions lib/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ ejs.filters.q = function(obj) {
return JSON.stringify(obj, null, 2);
};

ejs.filters.getPropertyOfFirstEndpoint = function(remoteObj, item) {
return getPropertyOfFirstEndpoint(remoteObj, item);
};

// this method is added to support both flavors of the getEndPoints method
// in strong-remoting 2.x methods `getHttpMethod()` and `getFullPath()` were used
// in strong-remoting 3.x we have deprecated those methods in favor of getEndPoints()
function getPropertyOfFirstEndpoint(remoteObj, item) {
// strong-remoting 3.x
if (typeof remoteObj.getEndpoints == 'function') {
return remoteObj.getEndpoints()[0][item];
} else { // strong-remoting 2.x
if (item == 'verb') {
return remoteObj.getHttpMethod();
} else if (item == 'fullPath') {
return remoteObj.getFullPath();
} else {
throw new Error(g.f('Unsupported endpoint property: %s'));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if item is neither verb nor fullPath? Should we throw an error instead of returning undefined?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

}
}

/**
* Generate Angular $resource services for the given loopback application.
*
Expand Down Expand Up @@ -73,6 +95,9 @@ module.exports = function generateServices(app, options) {
models: models,
urlBase: options.apiUrl.replace(/\/+$/, ''),
includeCommonModules: options.includeCommonModules,
helpers: {
getPropertyOfFirstEndpoint: getPropertyOfFirstEndpoint,
},
});
};

Expand Down
9 changes: 5 additions & 4 deletions lib/services.template.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
'LoopBackResource', 'LoopBackAuth', '$injector',
function(Resource, LoopBackAuth, $injector) {
var R = Resource(
urlBase + <%-: meta.ctor.getEndpoints()[0].fullPath | q %>,
urlBase + <%-: meta.ctor | getPropertyOfFirstEndpoint:'fullPath' | q %>,
<% /*
Constructor arguments are hardcoded for now.
We should generate it from sharedCtor.accepts instead.
Expand Down Expand Up @@ -121,8 +121,8 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' &&
<% }); //action.params.foreach -%>
},
<% } -%>
url: urlBase + <%-: action.getEndpoints()[0].fullPath | q %>,
method: <%-: action.getEndpoints()[0].verb | q %>,
url: urlBase + <%-: action | getPropertyOfFirstEndpoint:'fullPath' | q %>,
method: <%-: action | getPropertyOfFirstEndpoint:'verb' | q %>,
},
<% }); // meta.methods.foreach -%>
<% if (meta.isUser) { -%>
Expand Down Expand Up @@ -528,7 +528,8 @@ action.description = '<em>\n' +
<%
var params = action.accepts;
var postData;
if (action.getEndpoints()[0].verb == 'POST' || action.getEndpoints()[0].verb == 'PUT') {
var actionHttpMethod = helpers.getPropertyOfFirstEndpoint(action, 'verb');
if (['POST', 'PUT'].indexOf(actionHttpMethod) != -1) {
params = params.filter(function(arg) {
return arg.http && (arg.http.source == 'query' || arg.http.source == 'path');
});
Expand Down