“頭疼”
自己在用Angular做項目時,前端要請求后端數據時的代碼如下
this.http.get("url/xxx")
這是請求一個URL地址數據最簡單的代碼,但是如此簡單的代碼還會遇到一些頭疼的問題
- URL地址拼寫錯誤,有可能折騰半天才發現😑
- 后端修改了地址沒通知到前端,測試不到位就炸了,不是所有項目都有那么規范的接口變更流程😑
- 查看代碼的時候,看着URL地址,這個地址用途是哈?還要翻接口文檔😑
“吃葯”
為了解決這個問題,我們需要一個包含所有接口的文件
import { environment } from 'src/environments/environment';
export const WebAPI = {
/** 授權控制器 */
Auth: {
/** */
Controller: `${environment.host}/api/Auth`,
/** GET 獲得用戶 */
GetUser: `${environment.host}/api/Auth/GetUser`,
/** POST 登陸 */
Login: `${environment.host}/api/Auth/Login`,
},
}
那么請求代碼就可以改成
this.http.get(WebAPI.Auth.GetUser)
但是維護這個文件是件吃力的事情,作為一個時刻想着如何偷懶的程序員,吃力的事情就讓計算機去干,所以寫一個代碼生成工具。
工具代碼
public static class BuildWebApiToTS
{
public static string Build(Assembly assembly, string prefix = "api/")
{
List<Controller> controllers = GetApis(assembly);
string code = CreateCode(controllers);
return code.ToString();
}
public static void BuildToFile(Assembly assembly, string path, string prefix = "api/")
{
var code = Build(assembly, prefix);
string existsCode = "";
if (System.IO.File.Exists(path) == true)
existsCode = System.IO.File.ReadAllText(path);
if (existsCode != code)
System.IO.File.WriteAllText(path, code);
}
#region 構造代碼
public static string CreateCode(List<Controller> controllers, string prefix = "api/")
{
StringBuilder code = new StringBuilder();
code.AppendLine("import { environment } from 'src/environments/environment';");
code.AppendLine("export const WebAPI = {");
foreach (var coll in controllers.OrderBy(x => x.Name))
{
code.AppendLine($" /** {coll.ApiComments?.Title} */");
code.AppendLine($" {coll.Name}: {{");
code.AppendLine($" Controller: `${{environment.host}}/{prefix}{coll.Name}`,");
foreach (var action in coll.Actions.OrderBy(x => x.Name))
{
code.AppendLine($" /** {action.Type} {action.ApiComments?.Title} */");
code.AppendLine($" {action.Name}: `${{environment.host}}/{prefix}{coll.Name}/{action.Name}`,");
}
code.AppendLine($" }},");
}
code.AppendLine($"}};");
return code.ToString();
}
#endregion
#region 獲得接口清單
public static List<Controller> GetApis(Assembly assembly)
{
List<Controller> controllers = new List<Controller>();
var collTypes = assembly.GetTypes().Where(x => x.GetCustomAttributes(typeof(ApiControllerAttribute), false).Count() > 0);
foreach (var collType in collTypes)
{
var controller = new Controller(collType.Name.Replace("Controller", ""));
controller.ApiComments = collType.GetCustomAttribute<ApiCommentsAttribute>();
controllers.Add(controller);
controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpGetAttribute), "GET"));
controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpPostAttribute), "POST"));
controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpPutAttribute), "PUT"));
controller.Actions.AddRange(GetTypeMembers(collType, typeof(HttpDeleteAttribute), "DELETE"));
}
return controllers;
}
private static List<Action> GetTypeMembers(Type type, Type whereType, string saveType)
{
var actonTypes = type.GetMembers().Where(x => x.GetCustomAttributes(whereType, false).Count() > 0);
List<Action> actons = new List<Action>();
foreach (var actonType in actonTypes)
{
var action = new Action(saveType, actonType.Name);
action.ApiComments = actonType.GetCustomAttribute<ApiCommentsAttribute>();
actons.Add(action);
}
return actons;
}
public record Controller(string Name)
{
public ApiCommentsAttribute ApiComments { get; set; }
public List<Action> Actions { get; set; } = new List<Action>();
}
public record Action(string Type, string Name)
{
public ApiCommentsAttribute ApiComments { get; set; }
}
#endregion
}
public class ApiCommentsAttribute : Attribute
{
public string Title { get; set; }
public ApiCommentsAttribute(string title)
{
Title = title;
}
}
使用代碼
#if DEBUG
BuildWebApiToTS.BuildToFile(typeof(Program).Assembly, "ClientApp/src/app/web-api.ts");
#endif
上面代碼大概的流程就是
- 利用反射讀取程序集中包含
ApiControllerAttribute
特性的類 - 利用發射讀取
HttpGetAttribute
、HttpPostAttribute
、HttpPutAttribute
、HttpDeleteAttribute
特性的方法 - 根據需要的格式生成代碼
- 將代碼文件寫入
ClientApp/src/app/web-api.ts
有了這個東西帶來以下好處
- URL地址拼寫錯誤問題,不存在的🎉
- 后端修改了接口,前端直接編譯不過,立即發現問題🎉
- 鼠標停留,接口注釋就顯示了🎉