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}?"); } }
/// <summary> /// If the caller expect full uri path rather than relative path. /// </summary> /// <returns></returns> protected string GetFullUriText() { var jsUriQuery = UriQueryHelper.CreateUriQueryForTs(Description.RelativePath, Description.ParameterDescriptions); var hasArrayJoin = jsUriQuery != null && jsUriQuery.Contains(".join("); return(jsUriQuery == null ? $"this.baseUri + '{Description.RelativePath}'" : RemoveTrialEmptyString(hasArrayJoin ? $"this.baseUri + '{jsUriQuery}" : $"this.baseUri + '{jsUriQuery}'")); }
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(); //parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback")); Method.Parameters.AddRange(parameters.ToArray()); var jsUriQuery = UriQueryHelper.CreateUriQueryForTs(Description.RelativePath, Description.ParameterDescriptions); var uriText = jsUriQuery == null ? $"this.baseUri + '{Description.RelativePath}'" : RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'"); // var mapFunction = returnTypeText == NG2HttpResponse ? String.Empty : ".map(response=> response.json())"; if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethod}<{returnTypeText}>({uriText}, {{ headers: {{ 'Accept': 'application/json' }} }});")); 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; if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethod}<{returnTypeText}>({uriText}, '', {{ headers: {{ 'Accept': 'application/json' }} }});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethod}<{returnTypeText}>({uriText}, JSON.stringify({dataToPost}), {{ headers: {{ 'Content-Type': '{contentType}', 'Accept': 'application/json' }} }});")); } return; } Debug.Assert(false, "How come?"); }
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?"); }
protected override string CreateUriQueryForTs(string uriText, ParameterDescription[] parameterDescriptions) { return(UriQueryHelper.CreateUriQueryForTs(uriText, parameterDescriptions)); }
protected override void RenderImplementation() { string httpMethodName = HttpMethod.ToString().ToLower(); //Method is always uppercase. //deal with parameters CodeParameterDeclarationExpression[] parameters = ParameterDescriptions.Select(d => new CodeParameterDeclarationExpression(TypeMapper.MapCodeTypeReferenceToTsText(d.ParameterTypeReference), d.Name)) .ToArray(); Method.Parameters.AddRange(parameters); 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?")); } string jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions); string uriText = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" : RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'"); if (ReturnTypeReference != null && ReturnTypeReference.BaseType == "System.String" && ReturnTypeReference.ArrayElementType == null) //stringAsString is for .NET Core Web API { if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {OptionsForString}).then(d => d.data);")); //todo: type cast is not really needed. return; } if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {OptionsForString}).then(d => d.data);")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForString}).then(d => d.data);")); } return; } } else if (returnTypeText == AxiosHttpStringResponse) //translated from response to this { if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {OptionsForResponse});")); return; } if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {OptionsForResponse});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForResponse});")); } return; } } else if (returnTypeText == AxiosHttpResponse) // client should care about only status { if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {Options});")); return; } if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {Options});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForString});")); } return; } } else { string returnTypeCast = returnTypeText == null ? String.Empty : $"<{returnTypeText}>"; if (httpMethodName == "get" || httpMethodName == "delete") { if (returnTypeText == null) { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {OptionsForResponse});")); //only http response needed } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}{returnTypeCast}({uriText}, {Options}).then(d => d.data);")); } } else if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (returnTypeText == null) //http response { if (RequestBodyCodeTypeReference == null) //no content body { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {OptionsForResponse});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForResponse});")); } } else // type is returned { if (RequestBodyCodeTypeReference == null) // no body { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}{returnTypeCast}({uriText}, null, {Options}).then(d => d.data);")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}{returnTypeCast}({uriText}, JSON.stringify(requestBody), {OptionsWithContent}).then(d => d.data);")); } } } else { Debug.Assert(false, $"How come with {httpMethodName}?"); } } }
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(); //parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback")); Method.Parameters.AddRange(parameters.ToArray()); var jsUriQuery = UriQueryHelper.CreateUriQueryForTs(Description.RelativePath, Description.ParameterDescriptions); var uriText = jsUriQuery == null ? $"this.baseUri + '{Description.RelativePath}'" : RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'"); // var mapFunction = returnTypeText == ReactHttpResponse ? String.Empty : ".map(response=> response.json())"; if (ReturnType != null && TypeHelper.IsStringType(ReturnType) && this.stringAsString) //stringAsString is for .NET Core Web API { if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, {{ responseType: 'text' }}).then(d => d.data as {returnTypeText});")); 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; if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, null, {{headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }}).then(d => d.data as {returnTypeText});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, JSON.stringify({dataToPost}), {{ headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }}).then(d => d.data as {returnTypeText});")); } return; } } else if (returnTypeText == AxiostHttpBlobResponse) //translated from blobresponse to this { const string optionForStream = "{ responseType: 'blob' }"; if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, {optionForStream}).then(d => d.data as {returnTypeText});")); 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; //if (String.IsNullOrEmpty(contentType)) //{ // contentType = "application/json;charset=UTF-8"; //} if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, null, {optionForStream}).then(d => d.data as {returnTypeText});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, JSON.stringify({dataToPost}), {optionForStream}).then(d => d.data as {returnTypeText});")); } return; } } else if (returnTypeText == AxiosHttpStringResponse) //translated from response to this { const string optionForActionResult = "{ responseType: 'text' }"; if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, {optionForActionResult}).then(d => d.data as {returnTypeText});")); 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; if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, null, {optionForActionResult}).then(d => d.data as {returnTypeText});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, JSON.stringify({dataToPost}), {{ headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }}).then(d => d.data as {returnTypeText});")); } return; } } else if (returnTypeText == AxiosHttpResponse) // client should care about only status { const string optionForActionResult = "{ responseType: 'text' }"; if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, {optionForActionResult});")); 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; if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, null, {optionForActionResult});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, JSON.stringify({dataToPost}), {{ headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }});")); } return; } } else { if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}).then(d => d.data as {returnTypeText});")); 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; if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, null, {{ headers: {{ 'Content-Type': '{contentType}' }} }}).then(d => d.data as {returnTypeText});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethod}({uriText}, JSON.stringify({dataToPost}), {{ headers: {{ 'Content-Type': '{contentType}' }} }}).then(d => d.data as {returnTypeText});")); } return; } } Debug.Assert(false, "How come?"); }
protected override void RenderImplementation() { var httpMethodName = HttpMethod.ToString().ToLower(); //Method is always uppercase. //deal with parameters var parameters = ParameterDescriptions.Select(d => new CodeParameterDeclarationExpression(Poco2TsGen.TranslateToClientTypeReference(d.ParameterDescriptor.ParameterType), d.Name)) .ToArray(); //parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback")); Method.Parameters.AddRange(parameters); if (RequestBodyCodeTypeReference != null) { var p = new CodeParameterDeclarationExpression(RequestBodyCodeTypeReference, "requestBody"); Method.Parameters.Add(p); } var jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions); var uriText = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" : RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'"); // var mapFunction = returnTypeText == NG2HttpResponse ? String.Empty : ".map(response=> response.json())"; if (ReturnTypeReference != null && ReturnTypeReference.BaseType == "System.String" && ReturnTypeReference.ArrayElementType == null) //stringAsString is for .NET Core Web API { if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, {{ responseType: 'text' }});")); return; } if (httpMethodName == "post" || httpMethodName == "put") { if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, null, {{headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, JSON.stringify(requestBody), {{ headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }});")); } return; } } else if (returnTypeText == NG2HttpBlobResponse) //translated from blobresponse to this { const string optionForStream = "{ observe: 'response', responseType: 'blob' }"; if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, {optionForStream});")); return; } if (httpMethodName == "post" || httpMethodName == "put") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, null, {optionForStream});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, JSON.stringify(requestBody), {optionForStream});")); } return; } } else if (returnTypeText == NG2HttpStringResponse) //translated from response to this { const string optionForActionResult = "{ observe: 'response', responseType: 'text' }"; if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, {optionForActionResult});")); return; } if (httpMethodName == "post" || httpMethodName == "put") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, null, {optionForActionResult});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, JSON.stringify(requestBody), {{ headers: {{ 'Content-Type': '{contentType}' }}, observe: 'response', responseType: 'text' }});")); } return; } } else { var typeCast = returnTypeText == null ? "<Response>" : $"<{returnTypeText}>"; if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}{typeCast}({uriText});")); return; } if (httpMethodName == "post" || httpMethodName == "put") { if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}{typeCast}({uriText}, null, {{ headers: {{ 'Content-Type': '{contentType}' }} }});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}{typeCast}({uriText}, JSON.stringify(requestBody), {{ headers: {{ 'Content-Type': '{contentType}' }} }});")); } return; } } Debug.Assert(false, "How come?"); }
protected override void RenderImplementation() { var httpMethodName = HttpMethod.ToString().ToLower(); string contentOptionsWithHeadersHandlerForString = $"{{ method: '{httpMethodName}', headers: headersHandler ? Object.assign(headersHandler(), {{ 'Content-Type': '{contentType}' }}): {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}"; var ContentOptionsForString = settings.HandleHttpRequestHeaders ? contentOptionsWithHeadersHandlerForString : $"{{ method: '{httpMethodName}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}"; string contentOptionsWithHeadersHandlerForResponse = $"{{ method: '{httpMethodName}', headers: headersHandler ? Object.assign(headersHandler(), {{ 'Content-Type': '{contentType}' }}): {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}"; var ContentOptionsForResponse = settings.HandleHttpRequestHeaders ? contentOptionsWithHeadersHandlerForResponse : $"{{ method: '{httpMethodName}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}"; string optionsWithHeadersHandlerAndContent = $"{{ method: '{httpMethodName}', headers: headersHandler ? Object.assign(headersHandler(), {{ 'Content-Type': '{contentType}' }}): {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}"; var OptionsWithContent = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandlerAndContent : $"{{ method: '{httpMethodName}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}"; string optionsWithHeadersHandlerForString = $"{{ method: '{httpMethodName}', headers: headersHandler ? headersHandler() : undefined }}"; var OptionsForString = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandlerForString : $"{{ method: '{httpMethodName}' }}"; string optionsWithHeadersHandlerForResponse = $"{{ method: '{httpMethodName}', headers: headersHandler ? headersHandler() : undefined }}"; var OptionsForResponse = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandlerForResponse : $"{{ method: '{httpMethodName}' }}"; string optionsWithHeadersHandler = $"{{ method: '{httpMethodName}', headers: headersHandler ? headersHandler() : undefined }}"; var Options = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandler : $"{{ method: '{httpMethodName}' }}"; CodeParameterDeclarationExpression[] parameters = ParameterDescriptions.Select(d => new CodeParameterDeclarationExpression(TypeMapper.MapCodeTypeReferenceToTsText(d.ParameterTypeReference), d.Name)) .ToArray(); Method.Parameters.AddRange(parameters); 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?")); } string jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions); string uriText = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" : RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'"); if (ReturnTypeReference != null && ReturnTypeReference.BaseType == "System.String" && ReturnTypeReference.ArrayElementType == null) //stringAsString is for .NET Core Web API { if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForString}).then(d => d.text());")); //todo: type cast is not really needed. return; } if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForString}).then(d => d.text());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {ContentOptionsForString}).then(d => d.text());")); } return; } } //else if (returnTypeText == FetchHttpStringResponse)//translated from response to this //{ // if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") // { // Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForResponse}).then(d => d.text());")); // return; // } // if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") // { // if (RequestBodyCodeTypeReference == null) // { // Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, null, {OptionsForResponse}).then(d => d.text());")); // } // else // { // Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, JSON.stringify(requestBody), {ContentOptionsForResponse});")); // } // return; // } //} else if (returnTypeText == FetchHttpResponse) // client should care about only status { if (httpMethodName == "get" || httpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options});")); return; } if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (RequestBodyCodeTypeReference == null) { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsWithContent});")); } return; } } else { string returnTypeCast = returnTypeText == null ? String.Empty : $"<{returnTypeText}>"; if (httpMethodName == "get" || httpMethodName == "delete") { if (returnTypeText == null) { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForResponse});")); //only http response needed } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options}).then(d => d.json());")); } } else if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch") { if (returnTypeText == null) //http response { if (RequestBodyCodeTypeReference == null) //no content body { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForResponse});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {ContentOptionsForResponse});")); } } else // type is returned { if (RequestBodyCodeTypeReference == null) // no body { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options}).then(d => d.json());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsWithContent}).then(d => d.json());")); } } } else { Debug.Assert(false, $"How come with {httpMethodName}?"); } } }
protected override void RenderImplementation() { RenderMethodPrototype(); if (handleHttpRequestHeaders) { Method.Parameters.Add(new CodeParameterDeclarationExpression( "() => {[header: string]: string}", "headersHandler?")); } var jsUriQuery = UriQueryHelper.CreateUriQueryForTs(Description.RelativePath, Description.ParameterDescriptions); var hasArrayJoin = jsUriQuery != null && jsUriQuery.Contains(".join("); var uriText = jsUriQuery == null ? $"'{Description.RelativePath}'" : RemoveTrialEmptyString(hasArrayJoin ? $"'{jsUriQuery}" : $"'{jsUriQuery}'"); //Aurelia expect relative path. if (ReturnType != null && TypeHelper.IsStringType(ReturnType) && this.StringAsString) //stringAsString is for .NET Core Web API { if (HttpMethodName == "get" || HttpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}{OptionsForString}).then(d => d.text());")); //todo: type cast is not really needed. return; } if (HttpMethodName == "post" || HttpMethodName == "put" || HttpMethodName == "patch") { var dataToPost = GetDataToPost(); if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, null{OptionsForString}).then(d => d.text());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, JSON.stringify({dataToPost}), {ContentOptionsForString}).then(d => d.text());")); } return; } } else if (returnTypeText == AureliatHttpBlobResponse) //translated from blobresponse to this { if (HttpMethodName == "get" || HttpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}{OptionsForString}).then(d => d.blob());")); //todo: type cast is not really needed. return; } if (HttpMethodName == "post" || HttpMethodName == "put" || HttpMethodName == "patch") { var dataToPost = GetDataToPost(); if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, null{OptionsForString}).then(d => d.blob());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, JSON.stringify({dataToPost}), {ContentOptionsForString}).then(d => d.blob());")); } return; } } else if (returnTypeText == AureliaHttpStringResponse) //translated from response to this { if (HttpMethodName == "get" || HttpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}{OptionsForResponse}).then(d => d.json());")); return; } if (HttpMethodName == "post" || HttpMethodName == "put" || HttpMethodName == "patch") { var dataToPost = GetDataToPost(); if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, null{OptionsForResponse}).then(d => d.json());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, JSON.stringify({dataToPost}), {ContentOptionsForResponse}).then(d => d.json());")); } return; } } else if (returnTypeText == AureliaHttpResponse) // client should care about only status { if (HttpMethodName == "get" || HttpMethodName == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}{Options});")); return; } if (HttpMethodName == "post" || HttpMethodName == "put" || HttpMethodName == "patch") { var dataToPost = GetDataToPost(); if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, null{Options});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, JSON.stringify({dataToPost}), {OptionsWithContent});")); } return; } } else { if (HttpMethodName == "get" || HttpMethodName == "delete") { if (returnTypeText == null) { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}{OptionsForResponse});")); //only http response needed } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}{Options}).then(d => d.json());")); } } else if (HttpMethodName == "post" || HttpMethodName == "put" || HttpMethodName == "patch") { var dataToPost = GetDataToPost(); if (returnTypeText == null) //http response { if (dataToPost == "null") //no content body { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, null{OptionsForResponse});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, JSON.stringify({dataToPost}), {ContentOptionsForResponse}).then(d => d.json());")); } } else // type is returned { if (dataToPost == "null") // no body { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, null{Options}).then(d => d.json());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return this.http.{HttpMethodName}({uriText}, JSON.stringify({dataToPost}), {OptionsWithContent}).then(d => d.json());")); } } } else { Debug.Assert(false, $"How come with {HttpMethodName}?"); } } }
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(); 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 (ReturnType != null && TypeHelper.IsStringType(ReturnType) && this.StringAsString) //stringAsString is for .NET Core Web API { if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}}).then(d => d.text());")); 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; if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}', headers: {{ 'Content-Type': '{contentType}' }} }}).then(d => d.text());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify({dataToPost}) }}).then(d => d.text());")); } return; } } else if (returnTypeText == AureliatHttpBlobResponse) //translated from blobresponse to this { if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}}).then(d => d.blob());")); 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; if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}}).then(d => d.blob());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}', body: JSON.stringify({dataToPost})}}).then(d => d.blob());")); } return; } } else if (returnTypeText == AureliaHttpResponse) // client should care about only status { if (httpMethod == "get" || httpMethod == "delete") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}});")); 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; if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}});")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify({dataToPost}) }});")); } return; } } else { if (httpMethod == "get" || httpMethod == "delete") { if (hasArrayJoin) { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}}).then(d => d.json());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}'}}).then(d => d.json());")); } 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; if (String.IsNullOrEmpty(contentType)) { contentType = "application/json;charset=UTF-8"; } if (dataToPost == "null") { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}', headers: {{ 'Content-Type': '{contentType}' }} }}).then(d => d.json());")); } else { Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {{method: '{httpMethod}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify({dataToPost}) }}).then(d => d.json());")); } return; } } Debug.Assert(false, "How come?"); }