Skip to content

Commit

Permalink
- draft with url hoist implementation TS
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Dec 1, 2023
1 parent 0b49383 commit 35fd32e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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);
}

Expand All @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Refiners/TypeScriptRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()}",
Expand Down
14 changes: 3 additions & 11 deletions src/Kiota.Builder/Writers/TypeScript/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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();");
Expand Down

0 comments on commit 35fd32e

Please sign in to comment.