Skip to content

Commit 5958195

Browse files
committed
Building postBody routeParams urlParams in a consistence way.
1 parent b61e258 commit 5958195

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

lib/angular2/index.js

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -499,20 +499,46 @@ module.exports = function generate(ctx) {
499499
}
500500
return output.join(', ');
501501
}
502+
/**
503+
* @method paramIsRoute
504+
* @description
505+
* Testing if the param is route type
506+
*/
507+
function paramIsRoute(param) {
508+
return (param.http && param.http.source === 'path') || (param.arg && param.arg.match(/(id|fk|file|container)/));
509+
}
510+
/**
511+
* @method paramIsFunction
512+
* @description
513+
* Testing if the param is function type
514+
*/
515+
function paramIsFunction(param) {
516+
return typeof param.http === 'function'
517+
}
502518
/**
503519
* @method buildPostBody
504520
* @description
505521
* Define which properties should be passed while posting data (POST, PUT, PATCH)
506522
*/
507523
function buildPostBody(postData) {
508524
let output = [];
509-
if (postData && postData.length > 0) {
510-
output.push('');
511-
let l = postData.length;
512-
postData.forEach((property, i) => {
513-
output.push(` ${property.arg}: ${property.arg}${(i < l - 1) ? ',' : ''}`);
514-
});
515-
output.push(' ');
525+
if (Array.isArray(postData)) {
526+
postData = postData.filter(param => {
527+
// Filter out route params and function params
528+
if (paramIsRoute(param) || paramIsFunction(param)) {
529+
return false
530+
}
531+
// Make sure the param is body
532+
return param.http && param.http.source == 'body'
533+
})
534+
if (postData.length > 0) {
535+
output.push('');
536+
let l = postData.length;
537+
postData.forEach((property, i) => {
538+
output.push(` ${property.arg}: ${property.arg}${(i < l - 1) ? ',' : ''}`);
539+
});
540+
output.push(' ');
541+
}
516542
}
517543
return output.join('\n');
518544
}
@@ -525,13 +551,12 @@ module.exports = function generate(ctx) {
525551
let output = [''];
526552
// filter params that should not go over url query string
527553
urlParams = urlParams.filter(param => {
528-
let isRouteParam = (param.http && param.http.source === 'path') || (param.arg && param.arg.match(/(id|fk|file|container)/))
529-
// Filter out route params
530-
if (isRouteParam) {
531-
return false
532-
}
533-
// Filter out body params
534-
return (!param.http || param.http.source != 'body')
554+
// Filter out route params and function params
555+
if (paramIsRoute(param) || paramIsFunction(param)) {
556+
return false
557+
}
558+
// Filter out body params
559+
return (!param.http || param.http.source != 'body')
535560
});
536561
if (model.isUser && methodName === 'logout')
537562
output.push(` urlParams.access_token = this.auth.getAccessTokenId();`);
@@ -549,12 +574,15 @@ module.exports = function generate(ctx) {
549574
*/
550575
function buildRouteParams(routeParams) {
551576
let output = [];
552-
if (routeParams && routeParams.length > 0) {
553-
output.push('');
554-
routeParams.forEach((param, i) => {
555-
output.push(` ${param.arg}: ${param.arg}${(i < routeParams.length - 1) ? ',' : ''}`);
556-
});
557-
output.push(' ');
577+
if (routeParams) {
578+
routeParams = routeParams.filter(paramIsRoute)
579+
if (routeParams.length > 0) {
580+
output.push('');
581+
routeParams.forEach((param, i) => {
582+
output.push(` ${param.arg}: ${param.arg}${(i < routeParams.length - 1) ? ',' : ''}`);
583+
});
584+
output.push(' ');
585+
}
558586
}
559587
return output.join('\n');
560588
}

lib/angular2/shared/services/custom/service.ejs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,14 @@ export class <%-: modelName %>Api extends BaseLoopBackApi {
5252
}
5353
5454
// SET URL PARAMS
55-
var urlParams = action.accepts.filter(function(param) {
56-
return !(param.arg === "access_token" && methodName === "logout") && !(param.http && param.http.source === 'path');
57-
});
55+
var urlParams = action.accepts;
5856
// SET POST BODY
5957
var postData;
6058
if (httpVerb == 'POST' || httpVerb == 'PUT' || httpVerb == 'PATCH') {
61-
postData = action.accepts.filter(function(param) {
62-
return !(param.http && (param.http.source == 'query' || param.http.source == 'path'))
63-
&& !(param.arg === "access_token" && methodName === "logout");
64-
});
59+
postData = action.accepts;
6560
}
6661
// SET ROUTE PARAMS
67-
var routeParams = action.accepts.filter(function(param) {
68-
return (param.http && param.http.source === 'path') || (param.arg && param.arg.match(/(id|fk|file|container)/));
69-
});
62+
var routeParams = action.accepts
7063
-%>
7164
public <%- normalizeMethodName(methodName) %>(<%- buildMethodParams(model, methodName, action.accepts) %>): Observable<<%- buildObservableType(modelName, methodName) %>> {
7265
let method: string = <%-: httpVerb | q %>;

0 commit comments

Comments
 (0)