From 35fd32eb719d94b4e2fe1d127d329fa0566fd5f7 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 1 Dec 2023 10:53:58 -0500 Subject: [PATCH] - draft with url hoist implementation TS Signed-off-by: Vincent Biret --- .../Refiners/CommonLanguageRefiner.cs | 11 +++++++++-- src/Kiota.Builder/Refiners/TypeScriptRefiner.cs | 4 ++-- .../Writers/TypeScript/CodeMethodWriter.cs | 14 +++----------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs index 2eb5a77716..8220666d32 100644 --- a/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs +++ b/src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs @@ -1379,7 +1379,7 @@ mappingClass.Parent is CodeNamespace mappingNamespace && } CrawlTree(currentElement, RemoveDiscriminatorMappingsTargetingSubNamespaces); } - protected static void MoveRequestBuilderPropertiesToBaseType(CodeElement currentElement, CodeUsing baseTypeUsing, AccessModifier? accessModifier = null) + protected static void MoveRequestBuilderPropertiesToBaseType(CodeElement currentElement, CodeUsing baseTypeUsing, AccessModifier? accessModifier = null, bool addCurrentTypeAsGenericTypeParameter = false) { ArgumentNullException.ThrowIfNull(baseTypeUsing); if (currentElement is CodeClass currentClass && currentClass.IsOfKind(CodeClassKind.RequestBuilder)) @@ -1391,6 +1391,13 @@ protected static void MoveRequestBuilderPropertiesToBaseType(CodeElement current Name = baseTypeUsing.Name, IsExternal = true, }; + if (addCurrentTypeAsGenericTypeParameter) + { + currentClass.StartBlock.Inherits.GenericTypeParameterValues.Add(new CodeType + { + TypeDefinition = currentClass, + }); + } currentClass.AddUsing(baseTypeUsing); } @@ -1403,7 +1410,7 @@ protected static void MoveRequestBuilderPropertiesToBaseType(CodeElement current } } - CrawlTree(currentElement, x => MoveRequestBuilderPropertiesToBaseType(x, baseTypeUsing, accessModifier)); + CrawlTree(currentElement, x => MoveRequestBuilderPropertiesToBaseType(x, baseTypeUsing, accessModifier, addCurrentTypeAsGenericTypeParameter)); } protected static void RemoveRequestConfigurationClassesCommonProperties(CodeElement currentElement, CodeUsing baseTypeUsing) { diff --git a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs index 4b03ef4b4b..a87b3a90bf 100644 --- a/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs +++ b/src/Kiota.Builder/Refiners/TypeScriptRefiner.cs @@ -17,7 +17,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance return Task.Run(() => { cancellationToken.ThrowIfCancellationRequested(); - RemoveMethodByKind(generatedCode, CodeMethodKind.RawUrlConstructor); + RemoveMethodByKind(generatedCode, CodeMethodKind.RawUrlConstructor, CodeMethodKind.RawUrlBuilder); ReplaceReservedNames(generatedCode, new TypeScriptReservedNamesProvider(), static x => $"{x}Escaped"); ReplaceReservedExceptionPropertyNames(generatedCode, new TypeScriptExceptionsReservedNamesProvider(), static x => $"{x}Escaped"); MoveRequestBuilderPropertiesToBaseType(generatedCode, @@ -29,7 +29,7 @@ public override Task Refine(CodeNamespace generatedCode, CancellationToken cance Name = AbstractionsPackageName, IsExternal = true } - }); + }, addCurrentTypeAsGenericTypeParameter: true); ReplaceIndexersByMethodsWithParameter(generatedCode, false, static x => $"by{x.ToFirstCharacterUpperCase()}", diff --git a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs index 504ba0ac2b..468a172d77 100644 --- a/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs @@ -55,8 +55,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri WriteSetterBody(codeElement, writer, parentClass); break; case CodeMethodKind.RawUrlBuilder: - WriteRawUrlBuilderBody(parentClass, codeElement, writer); - break; + throw new InvalidOperationException("RawUrlBuilder is implemented in the base type in TypeScript."); case CodeMethodKind.ClientConstructor: WriteConstructorBody(parentClass, codeElement, writer, inherits); WriteApiConstructorBody(parentClass, codeElement, writer); @@ -81,14 +80,7 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri WriteDefaultMethodBody(codeElement, writer); break; } - writer.DecreaseIndent(); - writer.WriteLine("};"); - } - private void WriteRawUrlBuilderBody(CodeClass parentClass, CodeMethod codeElement, LanguageWriter writer) - { - var rawUrlParameter = codeElement.Parameters.OfKind(CodeParameterKind.RawUrl) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RawUrl parameter"); - var requestAdapterProperty = parentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter) ?? throw new InvalidOperationException("RawUrlBuilder method should have a RequestAdapter property"); - writer.WriteLine($"return new {parentClass.Name.ToFirstCharacterUpperCase()}({rawUrlParameter.Name.ToFirstCharacterLowerCase()}, this.{requestAdapterProperty.Name.ToFirstCharacterLowerCase()});"); + writer.CloseBlock(); } internal static void WriteDefensiveStatements(CodeMethod codeElement, LanguageWriter writer) @@ -192,7 +184,7 @@ private void WriteConstructorBody(CodeClass parentClass, CodeMethod currentMetho var pathParametersValue = "{}"; if (currentMethod.Parameters.OfKind(CodeParameterKind.PathParameters) is CodeParameter pathParametersParameter) pathParametersValue = pathParametersParameter.Name.ToFirstCharacterLowerCase(); - writer.WriteLine($"super({pathParametersValue}, {requestAdapterParameter.Name.ToFirstCharacterLowerCase()}, {urlTemplateProperty.DefaultValue});"); + writer.WriteLine($"super({pathParametersValue}, {requestAdapterParameter.Name.ToFirstCharacterLowerCase()}, {urlTemplateProperty.DefaultValue}, (x, y) => new {parentClass.Name.ToFirstCharacterUpperCase()}({(currentMethod.Kind is CodeMethodKind.Constructor ? "x, " : string.Empty)}y));"); } else writer.WriteLine("super();");