示例#1
0
        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'));
        }
示例#2
0
        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[];
    }};
}}");
        }
        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>")};
}}");
        }