8000 Add support for axios cancellation token by MariuszKogut · Pull Request #2846 · RicoSuter/NSwag · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for axios cancellation token #2846

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

Merged
merged 1 commit into from
May 15, 2020
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: 26 additions & 3 deletions src/NSwag.CodeGeneration.TypeScript.Tests/AxiosTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public async Task When_export_types_is_true_then_add_export_before_classes()
//// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
{
Template = TypeScriptTemplate.Fetch,
Template = TypeScriptTemplate.Axios,
GenerateClientInterfaces = true,
TypeScriptGeneratorSettings =
{
Expand All @@ -66,7 +66,7 @@ public async Task When_export_types_is_false_then_dont_add_export_before_classes
//// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
{
Template = TypeScriptTemplate.Fetch,
Template = TypeScriptTemplate.Axios,
GenerateClientInterfaces = true,
TypeScriptGeneratorSettings =
{
Expand All @@ -92,7 +92,7 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques
//// Act
var codeGen = new TypeScriptClientGenerator(document, new TypeScriptClientGeneratorSettings
{
Template = TypeScriptTemplate.Fetch,
Template = TypeScriptTemplate.Axios,
TypeScriptGeneratorSettings =
{
TypeScriptVersion = 2.0m
Expand All @@ -105,5 +105,28 @@ public async Task When_consumes_is_url_encoded_then_construct_url_encoded_reques
Assert.DoesNotContain("FormData", code);
Assert.Contains("\"Content-Type\": \"application/x-www-form-urlencoded\"", code);
}

[Fact]
public async Task Add_cancel_token_to_every_call()
{
//// 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,
TypeScriptGeneratorSettings =
{
TypeScriptVersion = 2.0m
}
});
var code = codeGen.GenerateFile();

//// Assert
Assert.Contains("cancelToken?: CancelToken | undefined", code);
}
}
}
10000
5 changes: 3 additions & 2 deletions src/NSwag.CodeGeneration.TypeScript/Templates/AxiosClient.liquid
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 %}): 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 operation.Parameters.size > 0 %},{%endif%} cancelToken?: CancelToken | undefined): Promise<{{ operation.ResultType }}> {
{% template Client.RequestUrl %}

{% if operation.HasBody -%}
Expand All @@ -58,7 +58,8 @@
{% if operation.HasResultType and operation.HasAcceptHeaderParameterParameter == false -%}
"Accept": "{{ operation.Produces }}"
{% endif -%}
}
},
cancelToken
};

{% if UseTransformOptionsMethod -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ function blobToText(blob: Blob, q: ng.IQService): ng.IPromise<string> {
}

{% elseif Framework.IsAxios -%}
function isAxiosError(obj: any): obj is AxiosError {
return obj.isAxiosError === true;
function isAxiosError(obj: any | undefined): obj is AxiosError {
return obj && obj.isAxiosError === true;
}

{% endif -%}
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.TypeScript/Templates/File.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import * as ng from 'angular';
{% endif -%}
{% if Framework.IsAxios -%}

import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, CancelToken } from 'axios';
{% endif -%}
{% if Framework.IsKnockout -%}

Expand Down
0