Skip to content

Commit

Permalink
Merge pull request #196 from mean-expert-official/development
Browse files Browse the repository at this point in the history
	Release 2.1.0-alpha.17
  • Loading branch information
jonathan-casarrubias authored Nov 11, 2016
2 parents 341475a + b161ed5 commit 9f7b483
Show file tree
Hide file tree
Showing 22 changed files with 2,371 additions and 2,210 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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.1.0-beta.17

- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/190
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/189
- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/184

## Release 2.1.0-beta.16

- Fix: https://github.com/mean-expert-official/loopback-sdk-builder/issues/185
Expand Down
6 changes: 4 additions & 2 deletions bin/lb-sdk
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ console.log(chalk.green('* /_/
console.log(chalk.green('* *'));
console.log(chalk.green('*========================================================================================*'));
console.log(chalk.green('* *'));
console.log(chalk.green('* Developed & Maintained by The MEAN Expert · http://mean.expert *'));
console.log(chalk.green('* http://github.com/mean-expert-official *'));
console.log(chalk.green('* Developed & Maintained by Jonathan Casarrubias *'));
console.log(chalk.green('* MEAN Expert · http://mean.expert · http://github.com/mean-expert-official *'));
console.log(chalk.green('* *'));
console.log(chalk.green('* CONTRIBUTORS *'));
console.log(chalk.green('* João Ribeiro <http://jonnybgod.ghost.io> *'));
console.log(chalk.green('* Nikolay Matiushenkov <https://github.com/mnvx> *'));
console.log(chalk.green('* Sylvain Dumont <https://www.weboaks.com> *'));
console.log(chalk.green('* Yonggang Luo <https://github.com/lygstate> *'));
console.log(chalk.green('* Chris Tunbridge <https://github.com/Destreyf> *'));
console.log(chalk.green('* Miguel Serrano <https://github.com/Serranom4> *'));
console.log(chalk.green('* *'));
console.log(chalk.green('* ORIGINALLY FORKED FROM *'));
console.log(chalk.green('* Miroslav Bajtos\' <[email protected]> *'));
Expand Down
22 changes: 18 additions & 4 deletions lib/angular2/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ module.exports = function generate(ctx) {
buildMethodParams: buildMethodParams,
buildServiceImports: buildServiceImports,
normalizeMethodName: normalizeMethodName,
buildObservableType: buildObservableType
buildObservableType: buildObservableType,
paramIsContext: paramIsContext
}
}
]);
Expand Down Expand Up @@ -464,6 +465,11 @@ module.exports = function generate(ctx) {
let availableClasses = relations.map((relationName, index) =>
model.sharedClass.ctor.relations[relationName].targetClass
);

params = params.filter(param => {
return !paramIsContext(param)
});

relations.forEach(relationName => {
if (model.sharedClass.ctor.relations[relationName].modelThrough) {
let throughName = capitalize(
Expand Down Expand Up @@ -529,6 +535,14 @@ module.exports = function generate(ctx) {
function paramIsFunction(param) {
return typeof param.http === 'function'
}
/**
* @method paramIsContext
* @description
* Testing if the param is a http.context
*/
function paramIsContext(param) {
return (typeof param.http !== 'undefined' && typeof param.http.source !== 'undefined' && param.http.source === 'context');
}
/**
* @method buildPostBody
* @description
Expand Down Expand Up @@ -566,17 +580,17 @@ module.exports = function generate(ctx) {
// filter params that should not go over url query string
urlParams = urlParams.filter(param => {
// Filter out route params and function params
if (paramIsRoute(param) || paramIsFunction(param)) {
if (paramIsRoute(param) || paramIsFunction(param) || paramIsContext(param)) {
return false
}
// Filter out body params
return (!param.http || param.http.source != 'body')
});
if (model.isUser && methodName === 'logout')
output.push(` urlParams.access_token = this.auth.getAccessTokenId();`);
output.push(` _urlParams.access_token = this.auth.getAccessTokenId();`);
if (urlParams && urlParams.length > 0) {
urlParams.forEach((param, i) => {
output.push(` if (${param.arg}) urlParams.${param.arg} = ${param.arg};`);
output.push(` if (${param.arg}) _urlParams.${param.arg} = ${param.arg};`);
});
}
return output.join('\n');
Expand Down
24 changes: 21 additions & 3 deletions lib/angular2/shared/models/flref.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export class FireLoopRef<T> {
private instance: any;
private socket: any;
private name: string;
private current: { name: string };
private parent: FireLoopRef<T>;
private childs: any = {};
private observables: any = {};

constructor(name: string, socket: any, parent: FireLoopRef<any> = null) {
constructor(name: string, socket: any, parent: FireLoopRef<any> = null, current = null) {
this.name = name;
this.current = current;
this.parent = parent;
this.socket = socket;
return this;
Expand All @@ -23,11 +25,23 @@ export class FireLoopRef<T> {
Math.floor(Math.random() * 100700) *
Math.floor(Math.random() * 198500);
let subject: Subject<T> = new Subject<T>();
this.socket.emit(`${this.name}.${event}`, {
let config: {id: any, data: any, current: any, parent: any } = {
id: id,
data: data,
current: this.current,
parent: this.parent && this.parent.instance ? this.parent.instance : null
});
};
if (!this.parent || (this.parent && this.current)) {
this.socket.emit(`${this.name}.${event}`, config);
} else {
let interval = setInterval(() => {
if (this.current) {
config.current = this.current;
this.socket.emit(`${this.name}.${event}`, config);
clearInterval(interval);
}
}, 500);
}
this.socket.on(`${this.name}.value.result.${id}`, (res: any) =>
subject.next(res.error ? Observable.throw(res.error) : res)
);
Expand Down Expand Up @@ -98,6 +112,10 @@ export class FireLoopRef<T> {
if (!this.parent) {
let childName = `${this.name}.${name}`;
if (this.childs[childName]) { return this.childs[childName]; }
this.socket.emit(`${this.name}.relation.request`, { relation: name });
this.socket.on(`${this.name}.relation.request.result`, (Model: { name: string }) => {
this.childs[childName].current = Model;
});
this.childs[childName] = new FireLoopRef<T>(childName, this.socket, this);
return this.childs[childName];
} else {
Expand Down
47 changes: 25 additions & 22 deletions lib/angular2/shared/services/custom/service.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ export class <%-: modelName %>Api extends BaseLoopBackApi {
if (methodName === 'createChangeStream') { -%>
public createChangeStream(): Observable<any> {
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
let _url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
<%-: fullPath | q %>;
let subject = new Subject();
if (typeof EventSource !== 'undefined') {
let emit = (msg: any) => subject.next(JSON.parse(msg.data));
var source = new EventSource(url);
var source = new EventSource(_url);
source.addEventListener('data', emit);
source.onerror = emit;
} else {
Expand All @@ -62,18 +62,18 @@ export class <%-: modelName %>Api extends BaseLoopBackApi {
var routeParams = action.accepts
-%>
public <%- normalizeMethodName(methodName) %>(<%- buildMethodParams(model, methodName, action.accepts) %>): Observable<<%- buildObservableType(modelName, methodName) %>> {
let method: string = <%-: httpVerb | q %>;
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
let _method: string = <%-: httpVerb | q %>;
let _url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
<%-: fullPath | q %>;
let routeParams: any = {<%- buildRouteParams(routeParams) %>};
let postBody: any = {<%- buildPostBody(postData) %>};
let urlParams: any = {};<%-
let _routeParams: any = {<%- buildRouteParams(routeParams) %>};
let _postBody: any = {<%- buildPostBody(postData) %>};
let _urlParams: any = {};<%-
buildUrlParams(model, methodName, urlParams) %><%
if (model.isUser && methodName === 'logout') { %>
this.auth.clear(); <%
} -%>
let result = this.request(method, url, routeParams, urlParams, postBody)<%
let result = this.request(_method, _url, _routeParams, _urlParams, _postBody)<%
if (model.isUser && methodName === 'login') { %>
.map(
(response: SDKToken) => {
Expand Down Expand Up @@ -104,13 +104,13 @@ if (model.isUser && methodName === 'login') { %>
-%>
public on<%- normalizeMethodName(methodName, true) %>(<%- buildMethodParams(model, methodName, action.accepts, true) %>): Observable<<%- buildObservableType(modelName, methodName) %>> {
let method: string = <%-: httpVerb | q %>;
let url: string = "/" + LoopBackConfig.getApiVersion() +
let _method: string = <%-: httpVerb | q %>;
let _url: string = "/" + 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);
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);
<%
if (methodName.match(/(^create$|upsert|^findBy|^findOne$)/g)) { %>
return result.map((instance: <%- modelName %>) => new <%-: modelName %>(instance));<%
Expand Down Expand Up @@ -140,15 +140,15 @@ if (methodName.match(/(^create$|upsert|^findBy|^findOne$)/g)) { %>
* from the server.
*/
public getCurrent(): any {
let method: string = "GET";
let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() + <%-: model.getPath() | q %> + "/:id";
let _method: string = "GET";
let _url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() + <%-: model.getPath() | q %> + "/:id";
let id: any = this.auth.getCurrentUserId();
if (id == null)
id = '__anonymous__';
let routeParams: any = { id: id };
let urlParams: any = {};
let postBody: any = {};
return this.request(method, url, routeParams, urlParams, postBody);
let _routeParams: any = { id: id };
let _urlParams: any = {};
let _postBody: any = {};
return this.request(_method, _url, _routeParams, _urlParams, _postBody);
}
/**
* Get data of the currently logged user that was returned by the last
Expand Down Expand Up @@ -218,14 +218,17 @@ action.description = '<em>\n' +
} -%>
* <%-: action.description | replace:/\n/g, '\n * ' %>
<%
var params = action.accepts;
var params = action.accepts.filter(param => {
return !paramIsContext(param);
});
var postData;
if (httpVerb == 'POST' || httpVerb == 'PUT' || httpVerb == 'PATCH') {
params = params.filter(function(arg) {
return arg.http && (arg.http.source == 'query' || arg.http.source == 'path');
});
postData = action.accepts.filter(function(arg) {
return params.indexOf(arg) == -1;
return params.indexOf(arg) == -1 && !paramIsContext(arg);
});
}
-%>
Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mean-expert/loopback-sdk-builder",
"version": "2.1.0-beta.16",
"version": "2.1.0-beta.17",
"description": "Tool for auto-generating Software Development Kits (SDKs) for LoopBack",
"bin": {
"lb-sdk": "bin/lb-sdk"
Expand Down Expand Up @@ -50,13 +50,24 @@
"name": "Yonggang Luo",
"email": "[email protected]",
"url": "https://github.com/lygstate"
},
{
"name": "Chris Tunbridge",
"email": "[email protected]",
"url": "https://github.com/Destreyf"
},
{
"name": "Miguel Serrano",
"email": "[email protected]",
"url": "https://github.com/Serranom4"
}
],
"license": "MIT",
"bugs": {
"url": "https://github.com/mean-expert-official/loopback-sdk-builder/issues"
},
"dependencies": {
"@mean-expert/loopback-component-realtime": "^1.0.0-beta.8",
"chalk": "1.1.3",
"ejs": "1.0",
"extfs": "0.0.7",
Expand Down
35 changes: 32 additions & 3 deletions tests/angular2/common/models/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = function (Room) {
Room.findByRoom = (room, next) => {
next(null, room);
};

Room.remoteMethod(
'findByRoom',
{
Expand All @@ -35,7 +35,7 @@ module.exports = function (Room) {
Room.greetPost = (b1, b2, b3, next) => {
next(null, `${b1.a}:${b2.b}:${b3.c}`);
};

Room.remoteMethod(
'greetPost',
{
Expand Down Expand Up @@ -80,6 +80,35 @@ module.exports = function (Room) {
http: { path: '/slimshady', verb: 'get' }
}
);

/**
* Mock endpoint thta checks to see if we can read the context and returns a valid result to test it.
* @param room
* @param context
* @param next
*/
Room.findByRoomContext = function(room, context, next){
var host = (typeof(context.req) !== 'undefined' && typeof(context.req.hostname) !== 'undefined' ? context.req.hostname : false);

if(host) {
room.name += host;
} else {
room = {id: -1, name: ""};
}
next(null, room);
};

Room.remoteMethod(
'findByRoomContext',
{
accepts: [
{ arg: 'room', type: 'object', http: { source: 'body' }},
{ arg: 'remoteCtx', description: '**Do not implement in clients**.', type: Object, injectCtx: true, http: { source: 'context' }}
],
returns: { arg: 'room', type: 'object', root: true },
http: { path: '/findByRoomContext', verb: 'post' }
}
);
};

function greet(a, b, c, next) { next(null, `${a}:${b}:${c}`); }
function greet(a, b, c, next) { next(null, `${a}:${b}:${c}`); }
2 changes: 1 addition & 1 deletion tests/angular2/loopback/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"port": 3000,
"remoting": {
"context": {
"enableHttpContext": false
"enableHttpContext": true
},
"rest": {
"normalizeHttpPath": false,
Expand Down
2 changes: 1 addition & 1 deletion tests/angular2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"@mean-expert/loopback-component-realtime": "^1.0.0-beta.5",
"@mean-expert/loopback-component-realtime": "1.0.0-beta.8",
"@types/socket.io-client": "^1.4.27",
"compression": "^1.0.3",
"config-chain": "^1.1.10",
Expand Down
17 changes: 17 additions & 0 deletions tests/angular2/src/app/room-service.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,21 @@ describe('Service: Room Service', () => {
})))));
})
));

/**
* This test is to validate that contexts support is working
* i modify the name of the room appending the host to the name if it works
* if it doesn't work i set room.id = -1 and the name to blank
*/
it('should find by mock room to test custom remote method with context enabled',
async(inject([RoomApi], (roomApi: RoomApi) => {
let room = new Room({ id: 42, name: 'my awesome room' });
return roomApi.findByRoomContext(room)
.subscribe((instance: Room) => {
expect(room.id).toBe(instance.id);
// I append the host onto the instance name so it shouldn't match now
expect(room.name).not.toBe(instance.name);
});
})
));
});
Loading

0 comments on commit 9f7b483

Please sign in to comment.