|
@@ -1,47 +1,36 @@
|
|
import { validator } from './utils'
|
|
import { validator } from './utils'
|
|
|
|
|
|
|
|
+import type { apiT } from './modules/api'
|
|
import type { demoT } from './modules/demo'
|
|
import type { demoT } from './modules/demo'
|
|
|
|
|
|
// 所有接口类型, 需要在modules下的ts文件中导出, 在ApiType上添加合并 type ApiType = demoT & xxx
|
|
// 所有接口类型, 需要在modules下的ts文件中导出, 在ApiType上添加合并 type ApiType = demoT & xxx
|
|
// 在vue文件中使用时具有提示作用
|
|
// 在vue文件中使用时具有提示作用
|
|
-type ApiType = demoT
|
|
|
|
|
|
+type ApiType = demoT & apiT
|
|
const map: Record<string, any> = {}
|
|
const map: Record<string, any> = {}
|
|
const isDev = import.meta.env.MODE === 'development'
|
|
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
|
|
// 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() {
|
|
export function useRequest() {
|
|
|
|
+ console.log(map)
|
|
|
|
+
|
|
return map as ApiType
|
|
return map as ApiType
|
|
}
|
|
}
|
|
|
|
|