Explorar o código

fix🐛: 修复api

gitboyzcf hai 2 semanas
pai
achega
b7d699d408
Modificáronse 1 ficheiros con 17 adicións e 28 borrados
  1. 17 28
      src/api/index.ts

+ 17 - 28
src/api/index.ts

@@ -1,47 +1,36 @@
 import { validator } from './utils'
 
+import type { apiT } from './modules/api'
 import type { demoT } from './modules/demo'
 
 // 所有接口类型, 需要在modules下的ts文件中导出, 在ApiType上添加合并 type ApiType = demoT & xxx
 // 在vue文件中使用时具有提示作用
-type ApiType = demoT
+type ApiType = demoT & apiT
 const map: Record<string, any> = {}
 const isDev = import.meta.env.MODE === 'development'
 
-const generators = (files = import.meta.glob('./modules/**/*.ts')) => {
-  return new Promise<ApiType[]>((resolve) => {
-    const promises = []
-    const def: ApiType[] = []
-    for (const path in files) {
-      promises.push(files[path]())
-    }
-    Promise.all(promises).then((modules) => {
-      modules.forEach((module: any) => def.push(module.default))
-      resolve(def)
-    })
+const generators = (files = import.meta.glob('./modules/**/*.ts', { eager: true })) => {
+  return Object.keys(files).map((key) => {
+    return (files as Record<string, any>)[key].default
   })
 }
-
-generators().then((gens) => {
-  gens.forEach((generator: any) => {
-    for (const key in generator) {
+generators().forEach((generator: any) => {
+  for (const key in generator) {
+    // eslint-disable-next-line @typescript-eslint/no-unused-expressions
+    isDev && validator(key)
+    if (!Object.prototype.hasOwnProperty.call(map, key)) {
+      map[key] = generator[key]
+    } else {
       // eslint-disable-next-line @typescript-eslint/no-unused-expressions
-      isDev && validator(key);
-      if (!Object.prototype.hasOwnProperty.call(map, key)) {
-        map[key] = generator[key];
-      } else {
-        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
-        isDev && console.error(new Error(`API modules模块下 ${key} 接口名重复`));
-        continue;
-      }
+      isDev && console.error(new Error(`API modules模块下 ${key} 接口名重复`))
+      continue
     }
-  });
-  console.log(map);
-  
+  }
 })
 
-
 export function useRequest() {
+  console.log(map)
+
   return map as ApiType
 }