protected override CodeMemberMethod CreateMethodName()
        {
            //Debug.Assert(ReturnType==null || ReturnType.Name != "DayOfWeek[]");
            var returnTypeReference = Poco2TsGen.TranslateToClientTypeReference(ReturnType);

            returnTypeText = TypeMapper.MapCodeTypeReferenceToTsText(returnTypeReference);
            if (returnTypeText == "any" || returnTypeText == "void")
            {
                returnTypeText = NG2HttpStringResponse;
            }
            else if (returnTypeText == "response")
            {
                returnTypeText = NG2HttpStringResponse;
            }
            else if (returnTypeText == "blobresponse")
            {
                returnTypeText = NG2HttpBlobResponse;
            }

            var callbackTypeText = $"Observable<{returnTypeText}>";

            Debug.WriteLine("callback: " + callbackTypeText);
            var returnTypeReferenceWithObservable = new CodeSnipetTypeReference(callbackTypeText);

            return(new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name = MethodName,
                ReturnType = returnTypeReferenceWithObservable,
            });
        }
Ejemplo n.º 2
0
        protected override CodeMemberMethod CreateMethodName()
        {
            returnTypeText = TypeMapper.MapCodeTypeReferenceToTsText(ReturnTypeReference);
            if (returnTypeText == "any" || returnTypeText == "void")
            {
                returnTypeText = AxiosHttpResponse;
            }
            else if (returnTypeText == "response")
            {
                returnTypeText = AxiosHttpStringResponse;
            }
            else if (returnTypeText == "blobresponse")
            {
                returnTypeText = AxiostHttpBlobResponse;
            }

            typeCast = returnTypeText == null ? "AxiosResponse<string>" : $"{returnTypeText}";

            string callbackTypeText = $"Promise<{typeCast}>";

            Debug.WriteLine("callback: " + callbackTypeText);
            CodeSnipetTypeReference returnTypeReferenceWithObservable = new CodeSnipetTypeReference(callbackTypeText);

            return(new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name = ActionName,
                ReturnType = returnTypeReferenceWithObservable,
            });
        }
        protected override void RenderImplementation()
        {
            string httpMethodName = HttpMethod.ToString().ToLower();             //Method is always uppercase.
            //deal with parameters
            var parameters = ParameterDescriptions.Select(d =>
                                                          new CodeParameterDeclarationExpression(TypeMapper.MapCodeTypeReferenceToTsText(d.ParameterTypeReference), d.Name))
                             .ToList();

            var returnTypeText = TypeMapper.MapCodeTypeReferenceToTsText(ReturnTypeReference);

            if (returnTypeText == "any" || String.IsNullOrEmpty(returnTypeText) || returnTypeText == "response")
            {
                returnTypeText = "void";
            }
            var callbackTypeText = String.Format("(data : {0}) => any", returnTypeText);

            Debug.WriteLine("callback: " + callbackTypeText);
            var callbackTypeReference = new CodeSnipetTypeReference(callbackTypeText);

            parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback"));
            Method.Parameters.AddRange(parameters.ToArray());

            if (RequestBodyCodeTypeReference != null)
            {
                CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(RequestBodyCodeTypeReference, "requestBody");
                Method.Parameters.Add(p);
            }

            if (settings.HandleHttpRequestHeaders)
            {
                Method.Parameters.Add(new CodeParameterDeclarationExpression(
                                          "() => {[header: string]: string}", "headersHandler?"));
            }

            var    jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions);
            string uriText    = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" :
                                RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'");

            string headerHandlerCall = settings.HandleHttpRequestHeaders ? ", headersHandler" : String.Empty;

            if (httpMethodName == "get" || httpMethodName == "delete")
            {
                Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethodName}({uriText}, callback, this.error, this.statusCode{headerHandlerCall});"));
            }
            else if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
            {
                if (RequestBodyCodeTypeReference == null)                 // no body
                {
                    Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethodName}({uriText}, null, callback, this.error, this.statusCode, '{contentType}'{headerHandlerCall});"));
                }
                else
                {
                    Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethodName}({uriText}, requestBody, callback, this.error, this.statusCode, '{contentType}'{headerHandlerCall});"));
                }
            }
            else
            {
                Debug.Assert(false, $"How come with {httpMethodName}?");
            }
        }
Ejemplo n.º 4
0
        public void TestTupleCallbackSnipet()
        {
            var callbackTypeText               = "(data : [string, string, string, string, string, number]) => any";
            var callbackTypeReference          = new CodeSnipetTypeReference(callbackTypeText);
            var parameterDeclarationExpression = new CodeParameterDeclarationExpression(callbackTypeReference, "callback");
            var s = TypeMapper.MapCodeTypeReferenceToTsText(parameterDeclarationExpression.Type);

            Assert.Equal(callbackTypeText, s);
        }
        protected override void RenderImplementation()
        {
            var httpMethod = Description.HttpMethod.ToLower();             //Method is always uppercase.
            //deal with parameters
            var parameters = Description.ParameterDescriptions.Select(d =>
                                                                      new CodeParameterDeclarationExpression(Poco2TsGen.TranslateToClientTypeReference(d.ParameterDescriptor.ParameterType), d.Name)
                                                                      ).ToList();

            var returnTypeReference = Poco2TsGen.TranslateToClientTypeReference(ReturnType);

            if (returnTypeReference.BaseType == "response")            //response is for NG2 with better built-in support for typing, and get translated to HttpResponse<Blob>
            {
                returnTypeReference.BaseType = "any";
            }

            var callbackTypeText = String.Format("(data : {0}) => any", TypeMapper.MapCodeTypeReferenceToTsText(returnTypeReference));

            Debug.WriteLine("callback: " + callbackTypeText);
            var callbackTypeReference = new CodeSnipetTypeReference(callbackTypeText);

            parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback"));

            Method.Parameters.AddRange(parameters.ToArray());

            var jsUriQuery   = UriQueryHelper.CreateUriQueryForTs(Description.RelativePath, Description.ParameterDescriptions);
            var hasArrayJoin = jsUriQuery != null && jsUriQuery.Contains(".join(");
            var uriText      = jsUriQuery == null ? $"this.baseUri + '{Description.RelativePath}'" :
                               RemoveTrialEmptyString(hasArrayJoin ? $"this.baseUri + '{jsUriQuery}" : $"this.baseUri + '{jsUriQuery}'");

            if (httpMethod == "get" || httpMethod == "delete")
            {
                Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethod}({uriText}, callback, this.error, this.statusCode);"));
                return;
            }

            if (httpMethod == "post" || httpMethod == "put")
            {
                var fromBodyParameterDescriptions = Description.ParameterDescriptions.Where(d => d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromBody ||
                                                                                            (TypeHelper.IsComplexType(d.ParameterDescriptor.ParameterType) && (!(d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromUri) ||
                                                                                                                                                               (d.ParameterDescriptor.ParameterBinder == ParameterBinder.None)))).ToArray();
                if (fromBodyParameterDescriptions.Length > 1)
                {
                    throw new InvalidOperationException(String.Format("This API function {0} has more than 1 FromBody bindings in parameters", Description.ActionDescriptor.ActionName));
                }
                var singleFromBodyParameterDescription = fromBodyParameterDescriptions.FirstOrDefault();

                var dataToPost = singleFromBodyParameterDescription == null ? "null" : singleFromBodyParameterDescription.ParameterDescriptor.ParameterName;

                Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethod}({uriText}, {dataToPost}, callback, this.error, this.statusCode);"));
                return;
            }

            Debug.Assert(false, "How come?");
        }
Ejemplo n.º 6
0
        protected override CodeMemberMethod CreateMethodName()
        {
            var returnTypeReference = Poco2TsGen.TranslateToClientTypeReference(ReturnType);

            returnTypeText = TypeMapper.MapCodeTypeReferenceToTsText(returnTypeReference);
            if (returnTypeText == "any" || returnTypeText == "void")
            {
                returnTypeText = NG2HttpResponse;
            }
            var callbackTypeText = $"Observable<{returnTypeText}>";

            Debug.WriteLine("callback: " + callbackTypeText);
            var returnTypeReferenceWithObservable = new CodeSnipetTypeReference(callbackTypeText);

            return(new CodeMemberMethod()
            {
                Attributes = MemberAttributes.Public | MemberAttributes.Final,
                Name = NethodName,
                ReturnType = returnTypeReferenceWithObservable,
            });
        }
Ejemplo n.º 7
0
        protected override void RenderImplementation()
        {
            var parameters = Description.ParameterDescriptions.Where(p => p.ParameterDescriptor.ParameterBinder == ParameterBinder.FromUri ||
                                                                     p.ParameterDescriptor.ParameterBinder == ParameterBinder.FromQuery || p.ParameterDescriptor.ParameterBinder == ParameterBinder.FromBody ||
                                                                     p.ParameterDescriptor.ParameterBinder == ParameterBinder.None).Select(d =>
                                                                                                                                           new CodeParameterDeclarationExpression(Poco2TsGen.TranslateToClientTypeReference(d.ParameterDescriptor.ParameterType), d.Name)
                                                                                                                                           ).ToList();

            var returnTypeReference = Poco2TsGen.TranslateToClientTypeReference(ReturnType);

            if (returnTypeReference.BaseType == "response")            //response is for NG2 with better built-in support for typing, and get translated to HttpResponse<Blob>
            {
                returnTypeReference.BaseType = "any";
            }

            var callbackTypeText = String.Format("(data : {0}) => any", TypeMapper.MapCodeTypeReferenceToTsText(returnTypeReference));

            Debug.WriteLine("callback: " + callbackTypeText);
            var callbackTypeReference = new CodeSnipetTypeReference(callbackTypeText);

            parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback"));

            Method.Parameters.AddRange(parameters.ToArray());

            if (handleHttpRequestHeaders)
            {
                Method.Parameters.Add(new CodeParameterDeclarationExpression(
                                          "() => {[header: string]: string}", "headersHandler?"));
            }

            var uriText = GetFullUriText();

            string headerHandlerCall = handleHttpRequestHeaders ? ", headersHandler" : String.Empty;

            if (HttpMethodName == "get" || HttpMethodName == "delete")
            {
                Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{HttpMethodName}({uriText}, callback, this.error, this.statusCode{headerHandlerCall});"));
            }
            else if (HttpMethodName == "post" || HttpMethodName == "put" || HttpMethodName == "patch")
            {
                var fromBodyParameterDescriptions = Description.ParameterDescriptions.Where(d => d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromBody ||
                                                                                            (TypeHelper.IsComplexType(d.ParameterDescriptor.ParameterType) && (!(d.ParameterDescriptor.ParameterBinder == ParameterBinder.FromUri) ||
                                                                                                                                                               (d.ParameterDescriptor.ParameterBinder == ParameterBinder.None)))).ToArray();
                if (fromBodyParameterDescriptions.Length > 1)
                {
                    throw new InvalidOperationException(String.Format("This API function {0} has more than 1 FromBody bindings in parameters", Description.ActionDescriptor.ActionName));
                }
                var singleFromBodyParameterDescription = fromBodyParameterDescriptions.FirstOrDefault();

                var dataToPost = singleFromBodyParameterDescription == null ? "null" : singleFromBodyParameterDescription.ParameterDescriptor.ParameterName;

                if (dataToPost == "null")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{HttpMethodName}({uriText}, null, callback, this.error, this.statusCode, '{contentType}'{headerHandlerCall});"));
                }
                else
                {
                    Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{HttpMethodName}({uriText}, {dataToPost}, callback, this.error, this.statusCode, '{contentType}'{headerHandlerCall});"));
                }
            }
            else
            {
                Debug.Assert(false, $"How come with {HttpMethodName}?");
            }
        }