eps.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { merge } from "lodash-es";
  2. import { service } from "../service";
  3. import { Module } from "../types";
  4. import { path2Obj } from "../utils";
  5. import { config, isDev } from "/@/config";
  6. export function createEps(modules: Module[]) {
  7. // 本地模块的数据
  8. const s = path2Obj(
  9. modules.reduce((a, b) => {
  10. return a.concat(...((b.services as any[]) || []));
  11. }, [])
  12. );
  13. // 合并数据
  14. merge(service, s);
  15. // 开发环境下,生成本地 service 的类型文件
  16. if (isDev && config.test.eps) {
  17. const list: any[] = [];
  18. // 模拟 eps 数据
  19. function deep(s: any) {
  20. if (s.namespace) {
  21. const api = Array.from(
  22. new Set([
  23. ...Object.getOwnPropertyNames(s.constructor.prototype),
  24. "page",
  25. "list",
  26. "info",
  27. "delete",
  28. "update",
  29. "add"
  30. ])
  31. )
  32. .filter((e) => !["constructor", "namespace"].includes(e))
  33. .map((e) => {
  34. return {
  35. path: `/${e}`
  36. };
  37. });
  38. list.push({
  39. api,
  40. module: s.namespace.split("/")[0],
  41. name: s.constructor.name + "Entity",
  42. prefix: `/admin/${s.namespace}`
  43. });
  44. } else {
  45. for (const i in s) {
  46. deep(s[i]);
  47. }
  48. }
  49. }
  50. deep(s);
  51. // 生成文件
  52. service.request({
  53. url: "/__cool_eps",
  54. method: "POST",
  55. proxy: false,
  56. data: {
  57. list
  58. }
  59. });
  60. }
  61. }