public override string Generate() { TSBuilder builder = new TSBuilder(); builder.DocumentationComment(comment => { comment.Interface(); string description = Documentation; if (string.IsNullOrEmpty(description)) { description = $"An interface representing the {Name}."; } comment.Description(description); comment.Summary(Summary); comment.Extends($"Array{ConstructTSItemTypeName()}"); }); builder.ExportInterface(Name, $"Array{ConstructTSItemTypeName()}", tsInterface => { foreach (Property property in InterfaceProperties) { if (!(property.Name.ToLowerInvariant() == "value" || property.Name.ToLowerInvariant() == "values")) { tsInterface.DocumentationComment(property.Documentation); string propertyType = property.IsPolymorphicDiscriminator ? $"\"{SerializedName}\"" : property.ModelType.TSType(true); bool isReadonly = property.IsReadOnly; bool isOptional = !property.IsRequired && (!(CodeModel?.HeaderTypes.Contains(this) == true) || CodeModelTS.Settings.OptionalResponseHeaders); tsInterface.Property(property.Name, propertyType, optional: isOptional, isReadonly: isReadonly); } } }); return(builder.ToString()); }
public void DocumentationComment(params string[] documentationCommentLines) { if (builder.AnyCommentLines(documentationCommentLines)) { SetCurrentState(State.DocumentationComment); builder.DocumentationComment(documentationCommentLines); } }
public void GenerateResponseType(TSBuilder builder) { builder.DocumentationComment($"Contains response data for the {Name} operation."); builder.ExportIntersectionType(HttpResponseName, type => { if (ReturnType.Body is DictionaryTypeTS dictionaryBody) { type.ObjectType(dict => { dict.DocumentationComment("The response body properties."); dict.IndexSignature(dictionaryBody.ValueType.TSType(inModelsModule: true)); }); } else if (ReturnType.Body is SequenceTypeTS sequenceBody) { type.NamedType($"Array<{sequenceBody.ElementType.TSType(inModelsModule: true)}>"); } else if (ReturnType.Body is CompositeTypeTS compositeBody) { type.NamedType(compositeBody.TSType(inModelsModule: true)); } if (ReturnType.Headers != null) { type.NamedType(ReturnType.Headers.TSType(inModelsModule: true)); } type.ObjectType(iface => { if (HasStreamResponseType()) { iface.DocumentationComment( "BROWSER ONLY", "", "The response body as a browser Blob.", "Always undefined in node.js."); iface.Property("blobBody", "Promise<Blob>", optional: true); iface.DocumentationComment( "NODEJS ONLY", "", "The response body as a node.js Readable stream.", "Always undefined in the browser."); iface.Property("readableStreamBody", "NodeJS.ReadableStream", optional: true); } else if (ReturnType.Body != null && !(ReturnType.Body is CompositeTypeTS || ReturnType.Body is SequenceTypeTS || ReturnType.Body is DictionaryTypeTS)) { iface.DocumentationComment("The parsed response body."); iface.Property(primitiveHttpBodyPropertyName, ReturnType.Body.TSType(inModelsModule: true)); } iface.DocumentationComment("The underlying HTTP response."); iface.Property(rawHttpResponsePropertyName, GenerateHttpOperationResponseType); }); }); }
public string GenerateConstructorComment(string className) { TSBuilder builder = new TSBuilder(); builder.DocumentationComment(comment => { comment.Description($"Initializes a new instance of the {className} class."); IEnumerable <Property> requiredParameters = Properties.Where(p => p.IsRequired && !p.IsConstant && string.IsNullOrEmpty(p.DefaultValue)); foreach (Property requiredParameter in requiredParameters) { comment.Parameter(requiredParameter.Name, requiredParameter.Documentation); } comment.Parameter("options", "The parameter options", isOptional: true); }); return(builder.ToString()); }
public void GenerateResponseType(TSBuilder builder) { builder.DocumentationComment($"Contains response data for the {Name} operation."); builder.ExportIntersectionType(HttpResponseName, type => { if (ReturnType.Body is DictionaryTypeTS dictionaryBody) { type.ObjectType(dict => { dict.DocumentationComment("The response body properties."); dict.IndexSignature(dictionaryBody.ValueType.TSType(inModelsModule: true)); }); } else if (ReturnType.Body is SequenceTypeTS sequenceBody) { type.NamedType($"Array<{sequenceBody.ElementType.TSType(inModelsModule: true)}>"); } else if (ReturnType.Body is CompositeTypeTS compositeBody) { type.NamedType(compositeBody.TSType(inModelsModule: true)); } if (ReturnType.Headers != null) { type.NamedType(ReturnType.Headers.TSType(inModelsModule: true)); } if (HasStreamResponseType()) { type.ObjectType(iface => { iface.DocumentationComment("The response body as a node.js Readable stream."); iface.Property("body", "NodeJS.ReadableStream", optional: true); }); } type.ObjectType(iface => { string statusCodeValue = string.Join(" | ", Responses.Keys.Select(val => ((int)val).ToString())); iface.DocumentationComment("The response status code."); iface.Property("statusCode", statusCodeValue, optional: false); }); }); }
public string Generate(bool emitEnumType) { TSBuilder builder = new TSBuilder(); builder.DocumentationComment(comment => { comment.Description($"Defines values for {Name}."); comment.Description(ExtendedDocumentation); comment.ReadOnly(); comment.Enum(CodeNamer.Instance.CamelCase(UnderlyingType.Name)); }); if (emitEnumType) { builder.ExportEnum(Name, tsEnum => { foreach (EnumValue value in Values) { tsEnum.DocumentationComment(value.Description); string valueName = CodeNamer.Instance.GetEnumMemberName(value.MemberName); string valueValue = CodeNamerTS.GetEnumValueName(value.SerializedName, UnderlyingType); if (valueValue == null || valueValue == "null") { valueValue = "\"null\""; } tsEnum.Value(valueName, valueValue); } }); } else { builder.ExportUnionType(Name, Values.Select(v => CodeNamerTS.GetEnumValueName(v.SerializedName, UnderlyingType))); } return(builder.ToString()); }
public void DocumentationComment(string comment) { builder.DocumentationComment(comment); }
public virtual string Generate() { TSBuilder builder = new TSBuilder(); if (ImmediatePolymorphicSubtypes.Any()) { builder.DocumentationComment($"Contains the possible cases for {Name}."); List <string> unionTypeValues = new List <string>() { Name }; unionTypeValues.AddRange(ImmediatePolymorphicSubtypes.Select(m => m.UnionTypeName)); builder.ExportUnionType($"{Name}Union", unionTypeValues); builder.Line(); } builder.DocumentationComment(comment => { string description = Documentation; if (string.IsNullOrEmpty(description)) { description = $"An interface representing {Name}."; } comment.Description(description); comment.Summary(Summary); }); string baseTypeName = null; if (BaseModelType != null && !BaseIsPolymorphic) { baseTypeName = BaseModelType.Name; if (baseTypeName == "RequestOptionsBase") { // baseTypeName = $"coreHttp.{baseTypeName}"; baseTypeName = null; } } builder.ExportInterface(Name, baseTypeName, tsInterface => { ISet <string> addedPropertyNames = new HashSet <string>(); foreach (Property property in InterfaceProperties) { string propertyName = property.Name; if (!addedPropertyNames.Contains(propertyName)) { addedPropertyNames.Add(propertyName); string propertyDescription = $"{property.Summary.EnsureEndsWith(".")} {property.Documentation}".Trim(); if (!property.DefaultValue.IsNullOrEmpty()) { propertyDescription = $"{propertyDescription.EnsureEndsWith(".")} Default value: {property.DefaultValue}.".Trim(); } tsInterface.DocumentationComment(propertyDescription); string propertyType = property.IsPolymorphicDiscriminator ? $"\"{SerializedName}\"" : property.ModelType.TSType(true); bool isReadonly = property.IsReadOnly; bool isOptional = !property.IsRequired && (!(CodeModel?.HeaderTypes.Contains(this) == true) || CodeModelTS.Settings.OptionalResponseHeaders); bool isNullable = property.IsXNullable ?? false; tsInterface.Property(property.Name, propertyType, optional: isOptional, isReadonly: isReadonly, isNullable: isNullable); } } if (AdditionalProperties != null) { tsInterface.DocumentationComment(AdditionalPropertiesDocumentation()); tsInterface.Property("[property: string]", AdditionalPropertiesTSType()); } }); return(builder.ToString()); }