Skip to content

Commit

Permalink
add axios abort signal (#4282)
Browse files Browse the repository at this point in the history
* add axios abort signal

* unit test
  • Loading branch information
fmg-lydonchandra committed May 3, 2023
1 parent 480d1e2 commit 73ac020
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 67 deletions.
29 changes: 27 additions & 2 deletions src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void AddMessage([FromBody]Foo message)
{
}
}

public class UrlEncodedRequestConsumingController: Controller
{
[HttpPost]
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand All @@ -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<UrlEncodedRequestConsumingController>();
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand All @@ -59,7 +59,11 @@
"Accept": "{{ operation.Produces }}"
{% endif -%}
},
{%- if UseAbortSignal -%}
signal
{%- else -%}
cancelToken
{%- endif -%}
};

{% if UseTransformOptionsMethod -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public TypeScriptClientGeneratorSettings()
/// <summary>Gets or sets the name of the exception class (default 'ApiException').</summary>
public string ExceptionClass { get; set; }

/// <summary>Gets or sets a value indicating whether to use the AbortSignal (Fetch/Aurelia template only, default: false).</summary>
/// <summary>Gets or sets a value indicating whether to use the AbortSignal (Aurelia/Axios/Fetch template only, default: false).</summary>
public bool UseAbortSignal { get; set; } = false;

// TODO: Angular specific => move
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Loading

0 comments on commit 73ac020

Please sign in to comment.