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 WrapResponses to Angular client #894

Merged
merged 9 commits into from
Aug 25, 2017
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ src/packages/**

**.sln.ide/**
**.vs/**
.vs/**

[Bb]in/
[Oo]bj/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public TypeScriptClientTemplateModel(
Operations = operations;

BaseClass = _settings.ClientBaseClass?.Replace("{controller}", controllerName);

ResponseClass = settings.ResponseClass;
//ResponseClass = settings.ResponseClass.Replace("{controller}", controllerName);
WrapResponses = settings.WrapResponses;
}

/// <summary>Gets the class name.</summary>
Expand Down Expand Up @@ -112,5 +116,11 @@ public TypeScriptClientTemplateModel(

/// <summary>Gets or sets a value indicating whether DTO exceptions are wrapped in a SwaggerException instance.</summary>
public bool WrapDtoExceptions => _settings.WrapDtoExceptions;

/// <summary>Gets a value indicating whether to wrap success responses to allow full response access.</summary>
public bool WrapResponses { get; set; }

/// <summary>Gets the response class name.</summary>
public string ResponseClass { get; protected set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ public TypeScriptFileTemplateModel(
/// <summary>Gets or sets a value indicating whether DTO exceptions are wrapped in a SwaggerException instance.</summary>
public bool WrapDtoExceptions => _settings.WrapDtoExceptions;

/// <summary>Gets or sets a value indicating whether to wrap success responses to allow full response access.</summary>
public bool WrapResponses => _settings.WrapResponses;

///// <summary>Gets the response class name.</summary>
//public string ResponseClass => this._settings.ResponseClass;

/// <summary>Gets or sets a value indicating whether to generate the response class (only applied when WrapResponses == true, default: true).</summary>
public bool GenerateResponseClasses => _settings.GenerateResponseClasses;

/// <summary>Gets the response class names.</summary>
public IEnumerable<string> ResponseClassNames {
get {
if (_settings.OperationNameGenerator.SupportsMultipleClients) {
return _document.Operations
.GroupBy(o => _settings.OperationNameGenerator.GetClientName(_document, o.Path, o.Method, o.Operation))
.Select(g => _settings.ResponseClass.Replace("{controller}", g.Key))
.Where(a => _settings.TypeScriptGeneratorSettings.ExcludedTypeNames?.Contains(a) != true)
.Distinct();
}

return new[] { _settings.ResponseClass.Replace("{controller}", string.Empty) };
}
}

/// <summary>Gets a value indicating whether MomentJS is required.</summary>
public bool RequiresMomentJS => _settings.TypeScriptGeneratorSettings.DateTimeType == TypeScriptDateTimeType.MomentJS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace NSwag.CodeGeneration.TypeScript.Models
{
/// <summary>The TypeScript operation model.</summary>
public class TypeScriptOperationModel : OperationModelBase<TypeScriptParameterModel, TypeScriptResponseModel>
{
{
private readonly SwaggerToTypeScriptClientGeneratorSettings _settings;
private readonly SwaggerToTypeScriptClientGenerator _generator;
private readonly SwaggerOperation _operation;
Expand All @@ -31,7 +31,7 @@ public TypeScriptOperationModel(
SwaggerToTypeScriptClientGenerator generator,
ITypeResolver resolver)
: base(null, operation, resolver, generator, settings)
{
{
_operation = operation;
_settings = settings;
_generator = generator;
Expand All @@ -55,8 +55,22 @@ public TypeScriptOperationModel(
public string ActualOperationNameUpper => ConversionUtilities.ConvertToUpperCamelCase(OperationName, false);

/// <summary>Gets or sets the type of the result.</summary>
public override string ResultType => SupportsStrictNullChecks && UnwrappedResultType != "void" && UnwrappedResultType != "null" ?
UnwrappedResultType + " | null" : UnwrappedResultType;
public override string ResultType {
get {
var resultType = SupportsStrictNullChecks && UnwrappedResultType != "void" && UnwrappedResultType != "null"
? UnwrappedResultType + " | null"
: UnwrappedResultType;
if (_settings != null && _settings.WrapResponses && _settings.TemplateSupportsWrapResponses) {
resultType = resultType == "void"
? _settings.ResponseClass + "Base"
: _settings.ResponseClass + "<" + resultType.Replace("{controller}", ControllerName) + ">";
}
return resultType;
}
}

public bool WrapResponses => this._settings.WrapResponses;
public string ResponseClass => this._settings.ResponseClass;

/// <summary>Gets a value indicating whether the operation requires mappings for DTO generation.</summary>
public bool RequiresMappings => Responses.Any(r => r.HasType && r.ActualResponseSchema.UsesComplexObjectSchema());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public SwaggerToTypeScriptClientGeneratorSettings()
};
BaseUrlTokenName = "API_BASE_URL";
ImportRequiredTypes = true;
ResponseClass = "SwaggerResponse";
this.GenerateResponseClasses = true;
}

/// <summary>Gets or sets the TypeScript generator settings.</summary>
Expand All @@ -41,6 +43,8 @@ public SwaggerToTypeScriptClientGeneratorSettings()
/// <summary>Gets or sets the output template.</summary>
public TypeScriptTemplate Template { get; set; }

public bool TemplateSupportsWrapResponses => Template == TypeScriptTemplate.Angular;

/// <summary>Gets or sets the promise type.</summary>
public PromiseType PromiseType { get; set; }

Expand Down Expand Up @@ -71,6 +75,15 @@ public SwaggerToTypeScriptClientGeneratorSettings()
/// <summary>Gets or sets a value indicating whether to use the 'getBaseUrl(defaultUrl: string)' from the base class (default: false).</summary>
public bool UseGetBaseUrlMethod { get; set; }

/// <summary>Gets or sets a value indicating whether to wrap success responses to allow full response access (experimental).</summary>
public bool WrapResponses { get; set; }

/// <summary>Gets or sets a value indicating whether to generate the response classes (only needed when WrapResponses == true, default: true).</summary>
public bool GenerateResponseClasses { get; set; }

/// <summary>Gets or sets the name of the response class (supports the '{controller}' placeholder).</summary>
public string ResponseClass { get; set; }

internal ITemplate CreateTemplate(object model)
{
if (Template == TypeScriptTemplate.Aurelia)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,15 +873,15 @@ public virtual string TransformText()
this.Write(" } catch (e) {\r\n return <Observable<");

#line 102 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\AngularClientTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(operation.UnwrappedResultType));
this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType));

#line default
#line hidden
this.Write(">><any>Observable.throw(e);\r\n }\r\n } else\r\n " +
" return <Observable<");

#line 105 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.TypeScript\Templates\AngularClientTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(operation.UnwrappedResultType));
this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType));

#line default
#line hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ export class <#=Model.Class#> <#if(Model.HasBaseClass){#>extends <#=Model.BaseCl
return this.process<#=operation.ActualOperationNameUpper#>(response_);
<#}#>
} catch (e) {
return <Observable<<#=operation.UnwrappedResultType#>>><any>Observable.throw(e);
return <Observable<<#=operation.ResultType#>>><any>Observable.throw(e);
}
} else
return <Observable<<#=operation.UnwrappedResultType#>>><any>Observable.throw(response_);
return <Observable<<#=operation.ResultType#>>><any>Observable.throw(response_);
});
}

Expand Down
Loading