public string ConvertType(Type type, ILocalConvertContext context) { var data = new Data(); AddBase(data, type, context); AddProperties(data, type, context); GenerateDiscriminator(data, type, context); var name = context.Configuration.GetTypeName(type); if (type.GetTypeInfo().IsGenericTypeDefinition) { var args = type.GetTypeInfo().GetGenericArguments(); name += $"<{string.Join(", ", args.Select(x => x.Name))}>"; } AdditionalGeneration(data, type, context); var constructorArguments = GeneratePropertiesAndConstructor(context, data); var generated = $@" export{(type.GetTypeInfo().IsAbstract ? " abstract" : "")} class {name}{data.ClassSuffix} {{ constructor({constructorArguments}) {{{_.Foreach(data.ConstructorLines, line => $@" {line}")} }}{_.Foreach(data.Body, line => string.IsNullOrEmpty(line) ? @" " : $@" {line}")}
public string ConvertType(Type type, ILocalConvertContext context) { if (_responseType == DispatcherResponseType.Observable) { context.ExternalImports.Add(new [] { new BuiltInTypeScriptType("Observable") }, "rxjs/index"); } return($@"export interface {context.Configuration.GetTypeName(type)} {{ execute<TResponse>(request: { context.GetTypeScriptType(typeof(IHttpRequest<string>)).ToTypeScriptType() .Replace("string", "TResponse") }): {(_responseType == DispatcherResponseType.Promise ? "Promise<TResponse>" : "Observable<TResponse>")}; }}"); }
protected override void AdditionalGeneration(Data data, Type type, ILocalConvertContext context) { var attr = type.GetTypeInfo().GetCustomAttribute <HttpRequestAttribute>(); if (attr == null) { throw new ArgumentNullException("attr cannot be null"); } var parsed = new HttpRequestHandlerDefinition(attr, new RequestAndResponse(type)); var queryStringParameters = ParseProperties(data, parsed, BindingType.FromQuery); var routeParameters = ParseProperties(data, parsed, BindingType.FromRoute); var bodyParameters = ParseProperties(data, parsed, BindingType.FromBody); var httpRequestType = typeof(IHttpRequest <>).MakeGenericType(parsed.Definition.ResponseType); var replaceRouteArgs = _.Foreach(routeParameters, prop => $".replace('{{{prop.Original.PropertyName}}}', this.{prop.Parsed.Name} ? this.{prop.Parsed.Name}.toString() : '')"); var hasBody = parsed.HttpMethod == HttpMethod.Patch || parsed.HttpMethod == HttpMethod.Post || parsed.HttpMethod == HttpMethod.Put; var body = $@"{{{ _.Foreach(bodyParameters, prop => $@" {prop.Parsed.Name}: this.{prop.Parsed.Name},").TrimEnd(',') } }}"; var queryStringPropertyName = PropName(context, httpRequestType, nameof(IHttpRequest <object> .QueryString)); var methodPropertyName = PropName(context, httpRequestType, nameof(IHttpRequest <object> .Method)); var routePropertyName = PropName(context, httpRequestType, nameof(IHttpRequest <object> .Route)); var bodyPropertyName = PropName(context, httpRequestType, nameof(IHttpRequest <object> .Body)); var code = $@" public __name = '{context.Configuration.GetTypeName(type)}'; private __request = () => {{ const req: {context.GetTypeScriptType(httpRequestType).ToTypeScriptType()} = {{ {methodPropertyName}: '{parsed.HttpMethod.ToString().ToLower()}', {routePropertyName}: '{parsed.Route}'{replaceRouteArgs}, {bodyPropertyName}: {(hasBody ? body : "undefined")}, {queryStringPropertyName}: {{}} }}; {_getQueryStringLines(queryStringParameters.Select(x => x.Parsed), queryStringPropertyName, context)} return req; }} public execute = (dispatcher: { context.GetTypeScriptType(typeof(IRequestDispatcher)).ToTypeScriptType() }) => dispatcher.execute(this.__request());"; data.Body.AddRange(code.Replace("\r\n", "\n").Split('\n')); }
public string ConvertType(Type type, ILocalConvertContext context) { var nonNullable = GetNonNullable(type); var enumValues = Enum.GetNames(nonNullable).Select(x => new { Name = context.Configuration.GetEnumValueName(nonNullable, x), Value = Convert.ChangeType(Enum.Parse(nonNullable, x), System.Enum.GetUnderlyingType(nonNullable)) }); var generated = $@" export enum {context.Configuration.GetTypeName(nonNullable)} {{{_.Foreach(enumValues, val => $@" {val.Name} = {val.Value},").TrimEnd(',')} }}"; return(generated); }
public string ConvertType(Type type, ILocalConvertContext context) { return($@"export interface {context.Configuration.GetTypeName(type)}<TResponse> {{ {PropName(context, type, nameof(IHttpRequest<object>.Method))}: { context.GetTypeScriptType(typeof(string)).ToTypeScriptType() }; {PropName(context, type, nameof(IHttpRequest<object>.Route))}: { context.GetTypeScriptType(typeof(string)).ToTypeScriptType() }; {PropName(context, type, nameof(IHttpRequest<object>.Body))}: { context.GetTypeScriptType(typeof(object)).ToTypeScriptType() }; {PropName(context, type, nameof(IHttpRequest<object>.QueryString))}: {{ [key: string]: string | string[]; }}; }}"); }
private static string PropName(ILocalConvertContext context, Type httpRequestType, string name) { return(context.Configuration.GetPropertyName(httpRequestType, httpRequestType.GetProperty(name))); }
public static string GetQueryStringLines(IEnumerable <ClassConverter.Property> properties, string queryStringPropertyName, ILocalConvertContext context) { var queryStringBuilder = new StringBuilder(); foreach (var property in properties) { queryStringBuilder.AppendQueryStringLines(" ", property.PropertyInfo, context.Imports, context.Configuration.GetPropertyName, property.Name, queryStringPropertyName); } return(queryStringBuilder.ToString()); }