Skip to content

Commit

Permalink
- uses symbols for error mappings in typescript
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Jan 15, 2024
1 parent d0bbd55 commit b420de2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Kiota.Builder/Writers/TypeScript/CodeConstantWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ private static void WriteNavigationMetadataEntry(CodeNamespace parentNamespace,
if (pathParameters is { Length: > 0 })
writer.WriteLine($"pathParametersMappings: [{string.Join(", ", pathParameters)}],");
}
private static string GetErrorMappingKey(string original) =>
original.Equals("4XX", StringComparison.OrdinalIgnoreCase) || original.Equals("5XX", StringComparison.OrdinalIgnoreCase) ?
$"_{original.ToUpperInvariant()}" : // to avoid emitting strings that can't be minified
original.ToUpperInvariant();

private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWriter writer)
{
Expand All @@ -105,9 +109,9 @@ private void WriteRequestsMetadataConstant(CodeConstant codeElement, LanguageWri
writer.StartBlock("errorMappings: {");
foreach (var errorMapping in executorMethod.ErrorMappings)
{
writer.WriteLine($"\"{errorMapping.Key.ToUpperInvariant()}\": {GetFactoryMethodName(errorMapping.Value, codeElement, writer)},");
writer.WriteLine($"{GetErrorMappingKey(errorMapping.Key)}: {GetFactoryMethodName(errorMapping.Value, codeElement, writer)} as ParsableFactory<Parsable>,");
}
writer.CloseBlock("} as Record<string, ParsableFactory<Parsable>>,");
writer.CloseBlock("},");
}
writer.WriteLine($"adapterMethodName: \"{GetSendRequestMethodName(isVoid, isStream, executorMethod.ReturnType.IsCollection, returnTypeWithoutCollectionSymbol)}\",");
if (!isVoid)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,9 @@ public void WritesRequestExecutorBody()
writer.Write(constant);
var result = tw.ToString();
Assert.Contains("errorMappings: {", result);
Assert.Contains("4XX\": createError4XXFromDiscriminatorValue", result);
Assert.Contains("5XX\": createError5XXFromDiscriminatorValue", result);
Assert.Contains("403\": createError403FromDiscriminatorValue", result);
Assert.Contains(" as Record<string, ParsableFactory<Parsable>>", result);
Assert.Contains("_4XX: createError4XXFromDiscriminatorValue as ParsableFactory<Parsable>", result);
Assert.Contains("_5XX: createError5XXFromDiscriminatorValue as ParsableFactory<Parsable>", result);
Assert.Contains("403: createError403FromDiscriminatorValue as ParsableFactory<Parsable>", result);
}
[Fact]
public void WritesIndexer()
Expand Down

0 comments on commit b420de2

Please sign in to comment.