Skip to content

Commit

Permalink
make methods virtual
Browse files Browse the repository at this point in the history
  • Loading branch information
fakefeik committed Jan 11, 2024
1 parent 18dd4b8 commit e9cee58
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Changelog
## v2.1 - 2024.01.11
## v2.1.2 - 2024.01.11
- refactor ApiControllerTypeBuildingContext
- use string templates with custom tag for urls in generated apis

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,43 @@ namespace SkbKontur.TypeScript.ContractGenerator.TypeBuilders.ApiController
{
public class DefaultApiCustomization : IApiCustomization
{
public TypeLocation GetApiBase(ITypeInfo type) => new TypeLocation
public virtual TypeLocation GetApiBase(ITypeInfo type) => new TypeLocation
{
Name = "ApiBase",
Location = "ApiBase/ApiBase",
};

public TypeLocation GetUrlTag(ITypeInfo type) => new TypeLocation
public virtual TypeLocation GetUrlTag(ITypeInfo type) => new TypeLocation
{
Name = "url",
Location = "ApiBase/ApiBase",
};

public string GetApiClassName(ITypeInfo type) => new Regex("(Api)?Controller").Replace(type.Name, "Api");
public string GetApiInterfaceName(ITypeInfo type) => "I" + GetApiClassName(type);
public virtual string GetApiClassName(ITypeInfo type) => new Regex("(Api)?Controller").Replace(type.Name, "Api");
public virtual string GetApiInterfaceName(ITypeInfo type) => "I" + GetApiClassName(type);

public string GetMethodName(IMethodInfo methodInfo) => IsUrlMethod(methodInfo)
? $"urlFor{methodInfo.Name}"
: methodInfo.Name.ToLowerCamelCase();
public virtual string GetMethodName(IMethodInfo methodInfo) => IsUrlMethod(methodInfo)
? $"urlFor{methodInfo.Name}"
: methodInfo.Name.ToLowerCamelCase();

public IMethodInfo[] GetApiMethods(ITypeInfo type) =>
public virtual IMethodInfo[] GetApiMethods(ITypeInfo type) =>
type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
.Where(m => m.GetAttributes(inherit : false).Any(a => KnownTypeNames.HttpAttributeNames.Any(a.HasName)))
.ToArray();

public IParameterInfo[] GetMethodParameters(IMethodInfo methodInfo) =>
public virtual IParameterInfo[] GetMethodParameters(IMethodInfo methodInfo) =>
methodInfo.GetParameters()
.Where(x => !x.ParameterType.Equals(TypeInfo.From<CancellationToken>()))
.ToArray();

public bool IsUrlMethod(IMethodInfo methodInfo)
public virtual bool IsUrlMethod(IMethodInfo methodInfo)
{
return GetMethodVerb(methodInfo) == "makeGetRequest" && ResolveReturnType(methodInfo.ReturnType).Equals(TypeInfo.From(typeof(void)));
}

public bool IsAsyncMethod(IMethodInfo methodInfo) => false;
public virtual bool IsAsyncMethod(IMethodInfo methodInfo) => false;

public string GetMethodVerb(IMethodInfo methodInfo)
public virtual string GetMethodVerb(IMethodInfo methodInfo)
{
var attributes = methodInfo.GetAttributes(inherit : false);

Expand All @@ -74,7 +74,7 @@ public string GetMethodVerb(IMethodInfo methodInfo)
throw new NotSupportedException($"Unresolved http verb for method {methodInfo.Name} at controller {methodInfo.DeclaringType?.Name}");
}

public string GetMethodRoute(IMethodInfo methodInfo)
public virtual string GetMethodRoute(IMethodInfo methodInfo)
{
var path = RouteTemplateHelper.GetRouteTemplate(methodInfo.DeclaringType!, methodInfo);

Expand All @@ -97,14 +97,14 @@ public string GetMethodRoute(IMethodInfo methodInfo)
return $"{path}{query}";
}

public TypeScriptType GetMethodResultType(IMethodInfo methodInfo, Func<ITypeInfo, TypeScriptType> buildAndImportType)
public virtual TypeScriptType GetMethodResultType(IMethodInfo methodInfo, Func<ITypeInfo, TypeScriptType> buildAndImportType)
{
return IsUrlMethod(methodInfo)
? (TypeScriptType)new TypeScriptTypeReference("string")
: new TypeScriptPromiseOfType(buildAndImportType(ResolveReturnType(methodInfo.ReturnType)));
}

public TypeScriptExpression? GetMethodBodyExpression(IMethodInfo methodInfo)
public virtual TypeScriptExpression? GetMethodBodyExpression(IMethodInfo methodInfo)
{
var parameter = GetMethodParameters(methodInfo).SingleOrDefault(IsFromBody);
return parameter == null
Expand Down

0 comments on commit e9cee58

Please sign in to comment.