Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API client, use object not arg variables #589

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class <#=Model.Class#> <#if(Model.HasClientBaseClass){#>extends <#=Model.
<#}#><#}#><#if(operation.HasResultDescription){#> * @return <#=operation.ResultDescription#>
<#}#><#if(operation.IsDeprecated){#> * @deprecated
<#}#> */
<#}#> <#=operation.OperationNameLower#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#><#if(!parameter.IsLast){#>, <#}#><#}#>): Observable<<#=operation.ResultType#>> {
<#}#> <#=operation.OperationNameLower#>(q: { <#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#><#if(!parameter.IsLast){#>, <#}#><#}#> }): Observable<<#=operation.ResultType#>> {
<#=TypeScriptTemplatePartGenerator.RenderRequestUrlCode(operation, 2)#>

<#=TypeScriptTemplatePartGenerator.RenderRequestBodyCode(operation, 2)#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class <#=Model.Class#> <#if(Model.HasClientBaseClass){#>extends <#=Model.
<#}#><#}#><#if(operation.HasResultDescription){#> * @return <#=operation.ResultDescription#>
<#}#><#if(operation.IsDeprecated){#> * @deprecated
<#}#> */
<#}#> <#=operation.OperationNameLower#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#><#if(!parameter.IsLast){#>, <#}#><#}#>): ng.IPromise<<#=operation.ResultType#>> {
<#}#> <#=operation.OperationNameLower#>(q: { <#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#><#if(!parameter.IsLast){#>, <#}#><#}#> }): ng.IPromise<<#=operation.ResultType#>> {
<#=TypeScriptTemplatePartGenerator.RenderRequestUrlCode(operation, 2)#>

<#=TypeScriptTemplatePartGenerator.RenderRequestBodyCode(operation, 2)#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class <#=Model.Class#> <#if(Model.HasClientBaseClass){#>extends <#=Model.
<#}#><#}#><#if(operation.HasResultDescription){#> * @return <#=operation.ResultDescription#>
<#}#><#if(operation.IsDeprecated){#> * @deprecated
<#}#> */
<#}#> <#=operation.OperationNameLower#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#><#if(!parameter.IsLast){#>, <#}#><#}#>): Promise<<#=operation.ResultType#>> {
<#}#> <#=operation.OperationNameLower#>(q: {<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#><#if(!parameter.IsLast){#>, <#}#><#}#> }): Promise<<#=operation.ResultType#>> {
<#=TypeScriptTemplatePartGenerator.RenderRequestUrlCode(operation, 2)#>

<#if(!operation.IsGetOrHead){#>
Expand All @@ -82,12 +82,12 @@ export class <#=Model.Class#> <#if(Model.HasClientBaseClass){#>extends <#=Model.
<#if(Model.UseTransformOptionsMethod){#>
return this.transformOptions(options_).then(transformedOptions_ => {
return this.http.fetch(url_, transformedOptions_);
}).then((response) => {
}).then((response: Response) => {
<#}else{#>
return this.http.fetch(url_, options_).then((response) => {
return this.http.fetch(url_, options_).then((response: Response) => {
<#}#>
<#if(Model.UseTransformResultMethod){#>
return this.transformResult(url_, response, (response) => this.process<#=operation.OperationNameUpper#>(response));
return this.transformResult(url_, response, (response: Response) => this.process<#=operation.OperationNameUpper#>(response));
<#}else{#>
return this.process<#=operation.OperationNameUpper#>(response);
<#}#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class <#=Model.Class#> <#if(Model.HasClientBaseClass){#>extends <#=Model.
<#}#><#}#><#if(operation.HasResultDescription){#> * @return <#=operation.ResultDescription#>
<#}#><#if(operation.IsDeprecated){#> * @deprecated
<#}#> */
<#}#> <#=operation.OperationNameLower#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#>, <#}#>onSuccess?: (<#if(operation.HasResultType){#>result: <#=operation.ResultType#><#}#>) => void, onFail?: (exception: <#=operation.ExceptionType#>, reason: string) => void) {
<#}#> <#=operation.OperationNameLower#>(q: { <#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#>, <#}#>}, onSuccess?: (<#if(operation.HasResultType){#>result: <#=operation.ResultType#><#}#>) => void, onFail?: (exception: <#=operation.ExceptionType#>, reason: string) => void) {
<#=TypeScriptTemplatePartGenerator.RenderRequestUrlCode(operation, 2)#>

<#=TypeScriptTemplatePartGenerator.RenderRequestBodyCode(operation, 2)#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class <#=Model.Class#> <#if(Model.HasClientBaseClass){#>extends <#=Model.
});
}

private <#=operation.OperationNameLower#>WithCallbacks(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#>, <#}#>onSuccess?: (result: <#=operation.ResultType#>) => void, onFail?: (exception: <#=operation.ExceptionType#>, reason: string) => void) {
private <#=operation.OperationNameLower#>WithCallbacks(q: {<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#>: <#=parameter.Type#>, <#}#> }, onSuccess?: (result: <#=operation.ResultType#>) => void, onFail?: (exception: <#=operation.ExceptionType#>, reason: string) => void) {
<#=TypeScriptTemplatePartGenerator.RenderRequestUrlCode(operation, 2)#>

<#=TypeScriptTemplatePartGenerator.RenderRequestBodyCode(operation, 2)#>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<#foreach(var response in Model.Responses){#>
if (status === <#=response.StatusCode#>) {
<# if(response.HasType){#>
let result<#=response.StatusCode#>: <#=response.Type#> = null;
let result<#=response.StatusCode#>: <#=response.Type#> | null = null;
<# if(response.IsDate){#>
result<#=response.StatusCode#> = new Date(responseText);
<# }else{
Expand All @@ -27,7 +27,7 @@ if (status === <#=response.StatusCode#>) {
} else <#}
if(Model.HasDefaultResponse){#>{
<# if(Model.DefaultResponse.HasType){#>
let result: <#=Model.DefaultResponse.Type#> = null;
let result: <#=Model.DefaultResponse.Type#> | null = null;
<# if(Model.DefaultResponse.IsDate){#>
result = new Date(responseText);
<# }else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
const content_ = new FormData();
<#foreach(var parameter in Model.FormParameters){#>
<# if(parameter.IsNullable){#>
if (<#=parameter.VariableName#> !== null)
if (q.<#=parameter.VariableName#> !== null)
<# }else{#>
if (<#=parameter.VariableName#> === null)
throw new Error("The parameter '<#=parameter.VariableName#>' cannot be null.");
if (q.<#=parameter.VariableName#> === null)
throw new Error("The parameter 'q.<#=parameter.VariableName#>' cannot be null.");
else
<# }#>
<# if(parameter.IsFile){
if(parameter.IsArray){#>
<#=parameter.VariableName#>.forEach(item_ => content_.append("<#=parameter.Name#>", item_.data, item_.fileName ? item_.fileName : "<#=parameter.Name#>") );
q.<#=parameter.VariableName#>.forEach(item_ => content_.append("<#=parameter.Name#>", item_.data, item_.fileName ? item_.fileName : "<#=parameter.Name#>") );
<# }else{#>
content_.append("<#=parameter.Name#>", <#=parameter.VariableName#>.data, <#=parameter.VariableName#>.fileName ? <#=parameter.VariableName#>.fileName : "<#=parameter.Name#>");
content_.append("<#=parameter.Name#>", q.<#=parameter.VariableName#>.data, q.<#=parameter.VariableName#>.fileName ? q.<#=parameter.VariableName#>.fileName : "<#=parameter.Name#>");
<# }
}else{#>
content_.append("<#=parameter.Name#>", <#=parameter.VariableName#>.toString());
content_.append("<#=parameter.Name#>", q.<#=parameter.VariableName#>.toString());
<# }#>
<#}#>
<# }else{#>
Expand Down
38 changes: 19 additions & 19 deletions src/NSwag.CodeGeneration.TypeScript/Templates/RequestUrlTemplate.tt
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,50 @@
<#@ import namespace="System.Linq" #>
let url_ = this.baseUrl + "/<#=Model.Path#><#if(Model.QueryParameters.Any()){#>?<#}#>";
<#foreach(var parameter in Model.PathParameters){#>
if (<#=parameter.VariableName#> === undefined || <#=parameter.VariableName#> === null)
throw new Error("The parameter '<#=parameter.VariableName#>' must be defined.");
if (q.<#=parameter.VariableName#> === undefined || q.<#=parameter.VariableName#> === null)
throw new Error("The parameter 'q.<#=parameter.VariableName#>' must be defined.");
<#if(parameter.IsDateArray){#>
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent(<#=parameter.VariableName#>.map(s_ => s_.toJSON()).join()));
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent(q.<#=parameter.VariableName#>.map(s_ => s_.toJSON()).join()));
<# }else if(parameter.IsDate){#>
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent("" + <#=parameter.VariableName#>.toJSON()));
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent("" + q.<#=parameter.VariableName#>.toJSON()));
<# }else if(parameter.IsArray){#>
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent(<#=parameter.VariableName#>.join()));
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent(q.<#=parameter.VariableName#>.join()));
<# }else{#>
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent("" + <#=parameter.VariableName#>));
url_ = url_.replace("{<#=parameter.Name#>}", encodeURIComponent("" + q.<#=parameter.VariableName#>));
<# }
}
foreach(var parameter in Model.QueryParameters){
if (parameter.IsRequired) {
if(parameter.IsNullable){#>
if (<#=parameter.VariableName#> === undefined)
throw new Error("The parameter '<#=parameter.VariableName#>' must be defined.");
if (q.<#=parameter.VariableName#> === undefined)
throw new Error("The parameter 'q.<#=parameter.VariableName#>' must be defined.");
else
<# }else{#>
if (<#=parameter.VariableName#> === undefined || <#=parameter.VariableName#> === null)
throw new Error("The parameter '<#=parameter.VariableName#>' must be defined and cannot be null.");
if (q.<#=parameter.VariableName#> === undefined || q.<#=parameter.VariableName#> === null)
throw new Error("The parameter 'q.<#=parameter.VariableName#>' must be defined and cannot be null.");
else
<# }
}else{
if(parameter.IsNullable){#>
if (<#=parameter.VariableName#> !== undefined)
if (q.<#=parameter.VariableName#> !== undefined)
<# }else{#>
if (<#=parameter.VariableName#> === null)
throw new Error("The parameter '<#=parameter.VariableName#>' cannot be null.");
else if (<#=parameter.VariableName#> !== undefined)
if (q.<#=parameter.VariableName#> === null)
throw new Error("The parameter 'q.<#=parameter.VariableName#>' cannot be null.");
else if (q.<#=parameter.VariableName#> !== undefined)
<# }
}
if(parameter.IsDateArray){#>
<#=parameter.VariableName#>.forEach(item => { url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + item.toJSON()) + "&"; });
q.<#=parameter.VariableName#>.forEach(item => { url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + item.toJSON()) + "&"; });
<# }else if(parameter.IsObjectArray){#>
<#=parameter.VariableName#>.forEach((item, index) => {
q.<#=parameter.VariableName#>.forEach((item, index) => {
for (let attr in item)
url_ += "<#=parameter.Name#>[" + index + "]." + attr + "=" + encodeURIComponent("" + item[attr]) + "&";
});
<# }else if(parameter.IsDate){#>
url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + <#=parameter.VariableName#>.toJSON()) + "&";
url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + q.<#=parameter.VariableName#>.toJSON()) + "&";
<# }else if(parameter.IsArray){#>
<#=parameter.VariableName#>.forEach(item => { url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + item) + "&"; });
q.<#=parameter.VariableName#>.forEach(item => { url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + item) + "&"; });
<# }else{#>
url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + <#=parameter.VariableName#>) + "&";
url_ += "<#=parameter.Name#>=" + encodeURIComponent("" + q.<#=parameter.VariableName#>) + "&";
<# }
}#>