diff --git a/src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs b/src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs index 97ef1bf67b..2114128942 100644 --- a/src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs +++ b/src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs @@ -19,7 +19,7 @@ public void AddMessage([FromBody]Foo message) { } } - + public class UrlEncodedRequestConsumingController: Controller { [HttpPost] @@ -80,7 +80,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes Assert.DoesNotContain("export class DiscussionClient", code); Assert.DoesNotContain("export interface IDiscussionClient", code); } - + [Fact] public async Task When_consumes_is_url_encoded_then_construct_url_encoded_request() { @@ -118,6 +118,7 @@ public async Task Add_cancel_token_to_every_call() var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings { Template = TypeScriptTemplate.Axios, + UseAbortSignal = false, TypeScriptGeneratorSettings = { TypeScriptVersion = 2.0m @@ -128,5 +129,29 @@ public async Task Add_cancel_token_to_every_call() // Assert Assert.Contains("cancelToken?: CancelToken | undefined", code); } + + [Fact] + public async Task When_abort_signal() + { + // Arrange + var generator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings()); + var document = await generator.GenerateForControllerAsync(); + var json = document.ToJson(); + + // Act + var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings + { + Template = TypeScriptTemplate.Axios, + UseAbortSignal = true, + TypeScriptGeneratorSettings = + { + TypeScriptVersion = 2.0m + } + }); + var code = codeGen.GenerateFile(); + + // Assert + Assert.Contains("signal?: AbortSignal | undefined", code); + } } } diff --git a/src/NSwag.CodeGeneration.TypeScript/Templates/AxiosClient.liquid b/src/NSwag.CodeGeneration.TypeScript/Templates/AxiosClient.liquid index a30984b406..1a5a9ba82a 100644 --- a/src/NSwag.CodeGeneration.TypeScript/Templates/AxiosClient.liquid +++ b/src/NSwag.CodeGeneration.TypeScript/Templates/AxiosClient.liquid @@ -32,7 +32,7 @@ {% for operation in Operations %} {% template Client.Method.Documentation %} - {{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false %}, {% endif %}{% endfor %} {% if operation.Parameters.size > 0 %},{%endif%} cancelToken?: CancelToken | undefined): Promise<{{ operation.ResultType }}> { + {{ operation.MethodAccessModifier }}{{ operation.ActualOperationName }}({% for parameter in operation.Parameters %}{{ parameter.VariableName }}{% if GenerateOptionalParameters and parameter.IsOptional %}?{% endif %}: {{ parameter.Type }}{{ parameter.TypePostfix }}{% if parameter.IsLast == false %}, {% endif %}{% endfor %}{% if UseAbortSignal %}{% if operation.Parameters.size > 0 %}, {% endif %}signal?: AbortSignal | undefined{% else %}{% if operation.Parameters.size > 0 %},{%endif%} cancelToken?: CancelToken | undefined{% endif %}): Promise<{{ operation.ResultType }}> { {% template Client.RequestUrl %} {% if operation.HasBody -%} @@ -59,7 +59,11 @@ "Accept": "{{ operation.Produces }}" {% endif -%} }, +{%- if UseAbortSignal -%} + signal +{%- else -%} cancelToken +{%- endif -%} }; {% if UseTransformOptionsMethod -%} diff --git a/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGeneratorSettings.cs b/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGeneratorSettings.cs index 640d93d253..b1df7c1344 100644 --- a/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGeneratorSettings.cs +++ b/src/NSwag.CodeGeneration.TypeScript/TypeScriptClientGeneratorSettings.cs @@ -91,7 +91,7 @@ public TypeScriptClientGeneratorSettings() /// Gets or sets the name of the exception class (default 'ApiException'). public string ExceptionClass { get; set; } - /// Gets or sets a value indicating whether to use the AbortSignal (Fetch/Aurelia template only, default: false). + /// Gets or sets a value indicating whether to use the AbortSignal (Aurelia/Axios/Fetch template only, default: false). public bool UseAbortSignal { get; set; } = false; // TODO: Angular specific => move diff --git a/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToTypeScriptClientCommand.cs b/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToTypeScriptClientCommand.cs index 5bb545f600..455818330e 100644 --- a/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToTypeScriptClientCommand.cs +++ b/src/NSwag.Commands/Commands/CodeGeneration/OpenApiToTypeScriptClientCommand.cs @@ -373,7 +373,7 @@ public string QueryNullValue set { Settings.QueryNullValue = value; } } - [Argument(Name = "UseAbortSignal", IsRequired = false, Description = "Specifies whether to use the AbortSignal (Fetch/Aurelia template only, default: false).")] + [Argument(Name = "UseAbortSignal", IsRequired = false, Description = "Specifies whether to use the AbortSignal (Aurelia/Axios/Fetch template only, default: false).")] public bool UseAbortSignal { get { return Settings.UseAbortSignal; } diff --git a/src/NSwagStudio/Views/CodeGenerators/SwaggerToTypeScriptClientGeneratorView.xaml b/src/NSwagStudio/Views/CodeGenerators/SwaggerToTypeScriptClientGeneratorView.xaml index b48e9826da..66cf7e4d0e 100644 --- a/src/NSwagStudio/Views/CodeGenerators/SwaggerToTypeScriptClientGeneratorView.xaml +++ b/src/NSwagStudio/Views/CodeGenerators/SwaggerToTypeScriptClientGeneratorView.xaml @@ -1,7 +1,7 @@  - @@ -38,53 +38,53 @@ - - - - - - - - - - @@ -95,11 +95,11 @@ - - - - - @@ -129,7 +129,7 @@ The operation name detection and replacement strategy of the {controller} placeholder in the Class Name setting: - @@ -138,7 +138,7 @@ - @@ -146,48 +146,48 @@ - - - - - - - - - @@ -218,71 +218,71 @@ - - - - - - - - - - - - - @@ -290,17 +290,17 @@ - - - - - @@ -350,7 +350,7 @@