Skip to content

Commit

Permalink
Release 2.0.0-rc.6 🐛 🔫
Browse files Browse the repository at this point in the history
- Fix: #68
- Fix: #67
- Fix: #66
- Fix: #63
- Fix: #62
- Enhance: #60
- Fix: #58
  • Loading branch information
Jonathan Casarrubias committed Jul 30, 2016
1 parent 5290725 commit 4111086
Show file tree
Hide file tree
Showing 25 changed files with 1,117 additions and 643 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

This file is created to keep history of the LoopBack SDK Builder, it does not consider or keeps any history of its parent module `loopback-sdk-angular`.

## Release 2.0.0-rc.6

- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/68
- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/67
- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/66
- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/63
- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/62
- Enhance: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/60
- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/58

## Release 2.0.0-rc.5

- Fix: https://github.com/jonathan-casarrubias/loopback-sdk-builder/issues/50
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ You don't have to manually write any static code.

````sh
$ cd to/api/project
$ npm install --save-dev [email protected].5
$ npm install --save-dev [email protected].6
````

# LoopBack SDK CLI Options
Expand Down
49 changes: 37 additions & 12 deletions lib/angular2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ module.exports = function generate(ctx) {
modelName: modelName,
moduleName: ctx.moduleName,
buildPostBody: buildPostBody,
buildUrlParams: buildUrlParams
buildUrlParams: buildUrlParams,
buildRouteParams: buildRouteParams,
normalizeMethodName: normalizeMethodName
}
}
]);
Expand Down Expand Up @@ -224,7 +226,8 @@ module.exports = function generate(ctx) {
*/
function getModelImports(model) {
let relations = Object.keys(model.sharedClass.ctor.relations).filter(
relationName => model.sharedClass.ctor.relations[relationName].targetClass
relationName => model.sharedClass.ctor.relations[relationName].targetClass &&
model.sharedClass.ctor.relations[relationName].targetClass !== model.name
);
let loaded = {};
let output = new String();
Expand All @@ -243,10 +246,20 @@ module.exports = function generate(ctx) {
}
return output;
}
/**
* @method normalizeMethodName
* @description
* Normalizes method name from loopback form to a more human readable form
*/
function normalizeMethodName(methodName, capitalize) {
return methodName.split('__').map((value, index) => {
return (index < 2 && !capitalize) ? value : (value.charAt(0).toUpperCase() + value.slice(1));
}).join('');
}
/**
* @method buildPostBody
* @description
* Define which properties should be passed while using post method
* Define which properties should be passed while posting data (POST, PUT, PATCH)
*/
function buildPostBody(postData) {
let output = new String();
Expand All @@ -261,29 +274,41 @@ module.exports = function generate(ctx) {
/**
* @method buildUrlParams
* @description
* Define which properties should be passed while using post method
* Define which properties should be passed using query string
*/
function buildUrlParams(model, methodName, urlParams) {
let output = new String();
// filter params that should not go over url query string
urlParams = urlParams.filter(param => !param.arg.match(/(id|fk|data|options)/g));
urlParams = urlParams.filter(param => !param.arg.match(/(id|fk|data|options|credentials)/g));
if (model.isUser && methodName === 'logout')
output = `${output}\n access_token: this.auth.getAccessTokenId(),`;
output = `${output}\n urlParams.access_token = this.auth.getAccessTokenId();`;
if (urlParams && urlParams.length > 0) {
urlParams.forEach((param, i) => {
output = `${output}\n if (${param.arg}) urlParams.${param.arg} = ${param.arg};`;
});
}
return output;
}
/**
* @method buildRouteParams TODO
* @description
* Define which properties should be passed as route params
*/
function buildRouteParams(routeParams) {
let output = new String();
if (routeParams && routeParams.length > 0) {
routeParams.forEach((param, i) => {
output = `${output}\n ${param.arg}: ${param.arg}`;
output = (i < urlParams.length - 1) ? `${output},` : `${output}\n `;
output = (i < routeParams.length - 1) ? `${output},` : `${output}\n `;
});
}
return output;
}
/*
<%
params.forEach(function(param, i, arr) {
if (param.arg !== 'id' && param.arg !== 'fk') {
%>
if (<%= param.arg %>) params.<%= param.arg %> = <%= param.arg %>;<%
}});
routeParams.forEach(function(param, i, arr) { %>
<%= param.arg %>: <%= param.arg %><%= i < arr.length -1 ? ',' : '' %><%
});%>
%>
*/
// TODO REVIEW TO SEE IF THIS IS CORRECT AND VALUABLE (JC)
Expand Down
11 changes: 1 addition & 10 deletions lib/angular2/shared/models/base.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable */
export interface LoopBackFilterInterface {
export interface LoopBackFilter {
fields?: any;
include?: any;
limit?: any;
Expand All @@ -9,15 +9,6 @@ export interface LoopBackFilterInterface {
where?: any;
}

export class LoopBackFilter implements LoopBackFilterInterface {
fields: any;
include: any;
limit: any;
order: any;
skip: any;
offset: any;
where: any;
}
<% if (!loadAccessToken) { %>
export interface AccessTokenInterface {
id?: string;
Expand Down
10 changes: 8 additions & 2 deletions lib/angular2/shared/services/core/base.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ export abstract class BaseLoopBackApi {
socket.on(event, res => subject.next(res));
return subject.asObservable();
} else {<% } -%>
let body = (typeof postBody === 'object' && postBody.data && Object.keys(postBody).length === 1)
? postBody.data : postBody;
// Body fix for built in remote methods using "data" or "credentials" that are the actual body
// Custom remote method properties are different and need to be wrapped into a body object
let body;
if (typeof postBody === 'object' && (postBody.data || postBody.credentials) && Object.keys(postBody).length === 1) {
body = (postBody.data) ? postBody.data : postBody.credentials;
} else {
body = postBody;
}
this.searchParams.setJSON(urlParams);
let request = new Request({
headers : headers,
Expand Down
114 changes: 31 additions & 83 deletions lib/angular2/shared/services/custom/service.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ if (httpVerb == 'POST' || httpVerb == 'PUT' || httpVerb == 'PATCH') {
});
}
-%>
public <%-: methodName.split('__').map(function (value, index) {
return index < 2 ? value : (value.charAt(0).toUpperCase() + value.slice(1));
}).join('') %>(<%
public <%- normalizeMethodName(methodName) %>(<%
urlParams.forEach(function(param, i, arr) {
if ((param.http && param.http.source === 'path')
// Crunch for "exists()" method
Expand All @@ -94,13 +92,10 @@ if (httpVerb == 'POST' || httpVerb == 'PUT' || httpVerb == 'PATCH') {
) {
let method: string = <%-: httpVerb | q %>;
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() + <%-: fullPath | q %>;
let routeParams: any = {<%
routeParams.forEach(function(param, i, arr) { %>
<%= param.arg %>: <%= param.arg %><%= i < arr.length -1 ? ',' : '' %><%
});%>
};
let urlParams : any = {<%- buildUrlParams(model, methodName, urlParams) %>};
let postBody : any = {<%- buildPostBody(postData) %>};<%
let routeParams: any = {<%- buildRouteParams(routeParams) %>};
let postBody: any = {<%- buildPostBody(postData) %>};
let urlParams: any = {};
<%- buildUrlParams(model, methodName, urlParams) %><%
if (model.isUser && methodName === 'logout') { %>
this.auth.clearStorage();
this.auth.clearUser(); <%
Expand All @@ -119,15 +114,17 @@ if (model.isUser && methodName === 'login') { %>
);
return result;
<%
} else if (model.isUser && methodName === 'logout') { %>
let logout: Subject<boolean> = new Subject<boolean>();
result.subscribe(() => {}, () => logout.next(true));
return logout.asObservable();<%
} else if (methodName.match(/(^create$|upsert|updateAll|findById|findOne|find)/g)) { %>
} else if (methodName.match(/(^create$)/g)) { %>
return result.map((result: <%- modelName %> | Array<<%- modelName %>>) => Array.isArray(result)
? result.map((instance: <%- modelName %>)=> new <%-: modelName %>(instance))
: new <%-: modelName %>(result));<%
} else { %>;
} else if (methodName.match(/(upsert|findById|findOne)/g)) { %>
return result.map((result: <%- modelName %>) => new <%-: modelName %>(result));<%
} else if (methodName.match(/(^find)/g)) { %>
return result.map(
(result: Array<<%- modelName %>>) => result.map((instance: <%- modelName %>) => new <%-: modelName %>(instance))
);<%
} else { %>;
return result;<%
} %>
}
Expand All @@ -136,11 +133,11 @@ if (model.isUser && methodName === 'login') { %>
!methodName.match(/log(in|out)/g) &&
methodName !== 'resetPassword' &&
httpVerb !== 'GET' &&
httpVerb !== 'HEAD'){ -%>
public on<%-: methodName.split('__').map(function (value) {
return value.charAt(0).toUpperCase() + value.slice(1);
}).join('') %>(<%
params.forEach(function(param, i, arr) { if (i > 0 && param.arg !== 'fk') { -%>, <% }
httpVerb !== 'HEAD'){
-%>
public on<%- normalizeMethodName(methodName, true) %>(<%
urlParams.forEach(function(param, i, arr) { if (i > 0 && param.arg !== 'fk') { -%>, <% }
if ((param.http && param.http.source === 'path')
// Crunch for "exists()" method
|| (param.arg === 'id' && methodName === 'exists')) {
Expand All @@ -159,52 +156,15 @@ if (model.isUser && methodName === 'login') { %>
else {
%><%= !param.required ? ' = undefined' : '' %><%
} -%>
<% }); //params.foreach
<% }); //urlParams.foreach
-%>
) {
let method: string = <%-: httpVerb | q %>;
let url: string = "/" + LoopBackConfig.getApiVersion() + <%-: fullPath | q %>;
let urlParams: any = {<%
urlParams.forEach(function(param, i, arr) { if (param.arg !== 'fk') { %>
<%= param.arg %>: <%= param.arg %><%= (i < arr.length -1 && param.arg !== 'id') ? ',' : '' %><%
}});%>
};
let params: any = {};<%
params.forEach(function(param, i, arr) {
if (param.arg === 'filter'
|| (param.http && param.http.source === 'query')
// Crunch for count()
|| (methodName === 'count' && param.arg === 'where')) {
%>
if (<%= param.arg %> !== undefined) {
params.<%= param.arg %> = <%= param.arg %>;
}<%
}
});
%>
let result = this.request(method, url, urlParams, params<%=
postData && postData.length > 0 ? ", " + postData[0].arg : '' %>, true)<%
if (model.isUser && methodName === 'login') { %>
.share();
result.subscribe(
(response: { id: string, userId: string, user: any }) => {
this.auth.setUser(response.id, response.userId, response.user);
this.auth.setRememberMe(true);
this.auth.save();
},
() => null
)<%
} else if (model.isUser && methodName === 'logout') { %>
.share();
result.subscribe(
() => {
this.auth.clearUser();
this.auth.clearStorage();
},
() => null
)<%
} %>;
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() + <%-: fullPath | q %>;
let routeParams: any = {<%- buildRouteParams(routeParams.filter(function(param) { return param.arg !== 'fk'; })) %>};
let postBody: any = {};
let urlParams: any = {};
let result = this.request(method, url, routeParams, urlParams, postBody, true);
return result;
}
<% }} -%>
Expand All @@ -226,26 +186,14 @@ if (model.isUser && methodName === 'login') { %>
*/
public getCurrent(): any {
let method: string = "GET";
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() + <%-: model.getPath() | q %> + "/:id";
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() + "/Users" + "/:id";
let id: any = this.auth.getCurrentUserId();
if (id == null) {
id = '__anonymous__';
}
let urlParams: any = {
id: id
};
let result = this.request(method, url, urlParams)
.share();
result.subscribe(
(response: { resource: any }) => {
this.auth.setCurrentUserData(response);
return response.resource;
},
() => null
);
return result;
if (id == null)
id = '__anonymous__';
let routeParams: any = { id: id };
let urlParams: any = {};
let postBody: any = {};
return this.request(method, url, routeParams, urlParams, postBody);
}
<% } -%>

Expand Down
9 changes: 9 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/**
* @author Miroslav Bajtos <[email protected]>
* @license MIT
* @description
* Based on the original services.js discovery file from the loopback-sdk-angular
* It has been updated to fit the loopback-sdk-builder needs.
*
* https://github.com/strongloop/loopback-sdk-angular/blob/master/lib/services.js
*/
exports.describeModels = function describeModels(app) {
var models = {};
app.handler('rest').adapter.getClasses().forEach(function(c) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "loopback-sdk-builder",
"version": "2.0.0-rc.5",
"version": "2.0.0-rc.6",
"description": "Tool for auto-generating Software Development Kits (SDKs) for LoopBack",
"bin": {
"lb-ng": "bin/lb-ng",
Expand Down
10 changes: 10 additions & 0 deletions tests/angular2/common/models/message.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
"type": "hasMany",
"model": "like",
"foreignKey": ""
},
"replies": {
"type": "hasMany",
"model": "Message",
"foreignKey": "parentId"
},
"parent": {
"type": "belongsTo",
"model": "Message",
"foreignKey": "parentId"
}
},
"acls": [],
Expand Down
10 changes: 4 additions & 6 deletions tests/angular2/common/models/room.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module.exports = function (Room) {

Room.greetPost = function greet(a, b, c, next) {
next(null, `${a}:${b}:${c}`);
};
Room.greetPost = greet;

Room.remoteMethod(
'greetPost',
Expand All @@ -17,9 +15,7 @@ module.exports = function (Room) {
}
);

Room.greetGet = function greet(a, b, c, next) {
next(null, `${a}:${b}:${c}`);
};
Room.greetGet = greet;

Room.remoteMethod(
'greetGet',
Expand All @@ -34,3 +30,5 @@ module.exports = function (Room) {
}
);
};

function greet(a, b, c, next) { next(null, `${a}:${b}:${c}`); }
3 changes: 3 additions & 0 deletions tests/angular2/common/models/room.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"options": {
"validateUpsert": true
},
"mixins": {
"PubSub": true
},
"properties": {
"name": {
"type": "string",
Expand Down
Loading

0 comments on commit 4111086

Please sign in to comment.