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

add axios abort signal #4282

Merged
merged 2 commits into from
May 3, 2023
Merged
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
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