Skip to content

Commit

Permalink
basic http call
Browse files Browse the repository at this point in the history
  • Loading branch information
egandro committed Jun 30, 2017
1 parent bf6c882 commit 3c676a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
40 changes: 32 additions & 8 deletions lib/generator/angular.client/templates/services.ts.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class <%- ctrl.name%>Service {
ctrl.routes.forEach(function(method){%>
public <%- method.operationId%> = (<%
var p=0;
var needBody = false;
var body = '';
var resultType = '';
var resultTypeHttp = '';
var resultTypeIsPrimitive = true;
method.parameter.forEach(function(param){
%><%- param.name%><%
Expand All @@ -53,10 +58,18 @@ ctrl.routes.forEach(function(method){%>
%><%if(param.isArray) {%>[]<%}%><%
if(p+1<method.parameter.length) {%>,
<%}%><%p++;});%>): Observable<<%
resultType = 'void';
resultTypeHttp = 'any';
if(method.response.length>0) {
var response = method.response[0];%><%-
response.getMappedType('TypeScript')%><%if(response.isArray) {%>[]<%}
%><%} else {%>void<%}%>> => {<%
var response = method.response[0];
resultType = response.getMappedType('TypeScript');
resultTypeIsPrimitive = response.isPrimitive;
if(response.isArray) {
resultType += [];
}
resultTypeHttp = resultType;
}
%><%- resultType %>> => {<%
if(method.response.length>1) {%>
/* warning <%- method.response.length %> response types found -
currently only using the very first one
Expand All @@ -66,10 +79,10 @@ ctrl.routes.forEach(function(method){%>
*/<%
}%><%if( ['post', 'put', 'patch'].indexOf(method.verb)>-1) {
var param = method.getBodyParameter();
var body = '';
needBody = true;
if(param != null) { body = param.name; } %>
let body: any = <%- body; %>;<%}
if(method.hasParameterType('path')) {
if(method.hasParameterType('path')) {
%>
var url = this.actionUrl + this.__buildPath('<%- method.path%>'<%
var buildPathParams = '';
Expand All @@ -82,7 +95,8 @@ ctrl.routes.forEach(function(method){%>
});
if(buildPathParams !='') {%><%- buildPathParams; %><%}
%>);<%} else {%>
var url = this.actionUrl + '<%- method.path%>';<%}%><%
var url = this.actionUrl + '<%- method.path%>';<%
}%><%
if(method.hasParameterType('query')) {%>
url = this.__buildQuery(url<%
var buildQueryParams = '';
Expand All @@ -93,8 +107,18 @@ ctrl.routes.forEach(function(method){%>
needbuildQuery = true;
}
});
%><%- buildQueryParams; %>);
%><%}%>
%><%- buildQueryParams; %>);%><%
}%>
return this._http.<%- method.verb%>(url<%if(needBody) {%>, body<%}%>)
.map((response: Response) => <<%- resultTypeHttp %>> (
response<%
if(resultTypeIsPrimitive){
%>.text()<%
}else{
%>.json()<%}%>
)
.catch((error: Response) => this._errorreporter.handleError(error));
}
<%});%><%if(needbuildQuery) {%>
Expand Down
5 changes: 4 additions & 1 deletion lib/swagger/entites/propertytype.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class PropertyType {
get isComplexType(): boolean {
return !this.isPrimitive;
}
isPrimitive: boolean = false;
isPrimitive: boolean = true;
isReference: boolean = false;
isRequired: boolean = false;
isScalar: boolean = true;
Expand Down Expand Up @@ -55,6 +55,7 @@ export class PropertyType {
result.$ref = ref;
result.isMap = true;
result.type = 'complex';
result.isPrimitive = false;
result.isReference = true;
return result;
}
Expand All @@ -74,6 +75,7 @@ export class PropertyType {
}
result.$ref = ref;
result.type = 'complex';
result.isPrimitive = false;
result.isReference = true;
return result;
}
Expand Down Expand Up @@ -160,6 +162,7 @@ export class PropertyType {
if (data.items.$ref !== null && data.items.$ref !== undefined) {
result.$ref = data.items.$ref;
result.type = 'complex';
result.isPrimitive = false;
result.isScalar = false;
result.isArray = true;
result.isReference = true;
Expand Down

0 comments on commit 3c676a8

Please sign in to comment.