fix: 优化类型提示

This commit is contained in:
winixt 2022-06-22 12:29:18 +08:00
parent 8a67774e46
commit 54d435538c
25 changed files with 290 additions and 271 deletions

View File

@ -3,6 +3,5 @@ import { name } from '../package.json';
export default function (api) {
api.addConfigType(() => ({
source: name,
build: ['ViteBuildConfig'],
}));
}

View File

@ -4,11 +4,12 @@ import {Options as PolyfillOptions } from '@vitejs/plugin-legacy'
import createPlugin from '@vitejs/plugin-vue-jsx'
import {createHtmlPlugin} from 'vite-plugin-html'
export interface ViteBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
viteOption?: UserConfig;
viteVuePlugin?: Options;
viteVueJsx?: Parameters<typeof createPlugin>[0];
viteLegacy?: PolyfillOptions;
viteHtml?: Parameters<typeof createHtmlPlugin>[0]
}
}

View File

@ -3,6 +3,5 @@ import { name } from '../../package.json';
export default function (api) {
api.addConfigType(() => ({
source: name,
build: ['WebpackBuildConfig'],
}));
}

View File

@ -7,7 +7,8 @@ interface CopyFileType {
to: string;
}
export interface WebpackBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
analyze?: {
analyzerMode?: 'server' | 'static' | 'disabled';
analyzerHost?: string;
@ -53,5 +54,5 @@ export interface WebpackBuildConfig {
};
postcssLoader?: Record<string, any>;
vueLoader?: object;
}
}

View File

@ -56,7 +56,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
runtime: ['AccessRuntimeConfig'],
build: ['AccessBuildConfig'],
}));
};

View File

@ -1,14 +1,16 @@
import { Router, NavigationGuard } from 'vue-router';
export interface AccessBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
access: {
rules: Record<string, []>;
};
}
}
export interface AccessRuntimeConfig {
interface PluginRuntimeConfig {
access: {
noFoundHandler: (param: { router: Router } & NavigationGuard) => void;
unAccessHandler: (param: { router: Router } & NavigationGuard) => void;
};
}
}

View File

@ -46,6 +46,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
build: ['EnumsBuildConfig'],
}));
};

View File

@ -1,8 +1,10 @@
export interface EnumsBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
enums: {
[key: string]: [string | number, string | number][]
}
}
}

View File

@ -78,7 +78,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
runtime: ['LayoutRuntimeConfig'],
build: ['LayoutBuildConfig'],
}));
};

View File

@ -10,7 +10,8 @@ interface Menu {
children?: Menu[]
}
export interface LayoutBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
layout: {
title: string;
footer: string;
@ -22,11 +23,8 @@ export interface LayoutBuildConfig {
sideWidth: number;
menus: Menu[];
};
}
export interface LayoutRuntimeConfig {
}
interface PluginRuntimeConfig {
layout: {
header: boolean;
sidebar: boolean;
@ -35,4 +33,5 @@ export interface LayoutRuntimeConfig {
noFoundHandler: (param: { router: Router } & NavigationGuard) => void;
unAccessHandler: (param: { router: Router } & NavigationGuard) => void;
};
}
}

View File

@ -85,6 +85,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
build: ['LocalBuildConfig'],
}));
};

View File

@ -1,8 +1,10 @@
export interface LocalBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
locale: {
locale: string;
fallbackLocale: string;
baseNavigator: boolean;
legacy: boolean;
};
}
}

View File

@ -89,6 +89,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
build: ['MonacoEditorBuildConfig'],
}));
};

View File

@ -1,7 +1,8 @@
import type { EditorLanguage } from 'monaco-editor-webpack-plugin/out/languages';
import type { EditorFeature } from 'monaco-editor-webpack-plugin/out/features';
export interface MonacoEditorBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
monacoEditor: {
filename: string;
publicPath: string;
@ -9,4 +10,5 @@ export interface MonacoEditorBuildConfig {
features: EditorFeature[];
globalAPI: boolean;
};
}
}

View File

@ -6,7 +6,9 @@ interface AppOption {
props: Record<string, any>;
}
export interface QiankunBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
qiankun: {
main: {
apps: AppOption[];
@ -15,5 +17,5 @@ export interface QiankunBuildConfig {
};
micro: {}
};
}
}

View File

@ -26,6 +26,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
runtime: ['RequestRuntimeConfig'],
}));
};

View File

@ -1,18 +1,27 @@
import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
import {Ref} from 'vue';
type RequestInterceptor = (value: AxiosRequestConfig) => AxiosRequestConfig | [(value: AxiosRequestConfig) => AxiosRequestConfig, (error: any) => any];
type ResponseInterceptor = (value: AxiosResponse) => AxiosResponse | [(value: AxiosResponse) => AxiosResponse, (error: any) => any];
interface RequestPluginOption {
mergeRequest: boolean;
cache: boolean | {
type: 'ram' | 'sessionStorage' | 'localStorage',
cacheTime: number;
}
}
export interface RequestRuntimeConfig {
declare module "@fesjs/fes" {
interface PluginRuntimeConfig {
request: {
responseDataAdaptor?<T>(data: T): T;
closeResDataCheck?: boolean;
dataHandler?(data: any, response: AxiosResponse): any;
errorHandler?(error: AxiosError | {type: string, msg: string, [key: string]: string}): void;
requestInterceptors?: RequestInterceptor[];
responseInterceptors?: ResponseInterceptor[];
errorHandler?: {
[key: string]: (error: { response: AxiosResponse } | AxiosResponse) => void;
};
} & AxiosRequestConfig;
}
export function request(url: string, data: null | Record<string, any>, options: AxiosRequestConfig & RequestPluginOption ): Promise<any>
export function useRequest(url: string, data: null | Record<string, any>, options: AxiosRequestConfig & RequestPluginOption ): {loadingRef: Ref<boolean>; errorRef: Ref<Error>; dataRef: Ref<any>}
}

View File

@ -61,6 +61,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
build: ['VuexBuildConfig'],
}));
};

View File

@ -1,9 +1,12 @@
export interface VuexBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
vuex: {
strict: boolean;
devtools: boolean;
}
}
}

View File

@ -69,6 +69,5 @@ export default (api) => {
api.addConfigType(() => ({
source: name,
build: ['WindicssBuildConfig'],
}));
};

View File

@ -1,7 +1,9 @@
import type { Config } from 'windicss/types/interfaces';
export interface WindicssBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
windicss: {
config: Config
}
}
}

View File

@ -1,14 +1,12 @@
function importsToStr(imports) {
return imports.map((imp) => {
const { source, build = [], runtime = [] } = imp;
return `import {${build.concat(runtime).join(', ')}} from '${source}';`;
const { source } = imp;
return `import '${source}';`;
});
}
function genTypeContent(imports) {
return {
RUNTIME_TYPES: imports.reduce((previousValue, currentValue) => previousValue.concat(currentValue.runtime || []), []).join(' & '),
BUILD_TYPES: imports.reduce((previousValue, currentValue) => previousValue.concat(currentValue.build || []), []).join(' & '),
imports: importsToStr(imports).join('\n'),
};
}
@ -21,9 +19,6 @@ export default function (api) {
api.onGenerateFiles(async () => {
const typeTpl = `
{{{ imports }}}
export type PluginRuntimeConfig = {{{RUNTIME_TYPES}}};
export type PluginBuildConfig = {{{BUILD_TYPES}}};
`;
const importSources = await api.applyPlugins({
key: 'addConfigType',

View File

@ -18,18 +18,9 @@ interface ClientRenderOption {
type RenderFunc = () => Promise<App>
export interface InnerRuntimeConfig {
beforeRender?: (option: BeforeRenderConfig) => BeforeRenderConfig;
patchRoutes?: ({ routes }: { routes: RouteRecordRaw[] }) => void;
modifyClientRenderOpts?: (option: ClientRenderOption) => ClientRenderOption;
rootContainer?: (component: DefineComponent, option: { routes: RouteRecordRaw[], plugin: Plugin }) => DefineComponent;
onAppCreated?: ({ app, routes }: { app: App, routes: RouteRecordRaw[] }) => void;
render?: (defaultRender: RenderFunc) => RenderFunc;
onRouterCreated?: ({ router }: { router: Router }) => void;
}
export interface InnerBuildConfig {
declare module "@fesjs/fes" {
interface PluginBuildConfig {
alias?: Record<string, string>,
autoprefixer?: {
/** environment for `Browserslist` */
@ -89,4 +80,16 @@ export interface InnerBuildConfig {
targets?: object;
terserOptions?: object;
title?: string;
}
interface PluginRuntimeConfig {
beforeRender?: (option: BeforeRenderConfig) => BeforeRenderConfig;
patchRoutes?: ({ routes }: { routes: RouteRecordRaw[] }) => void;
modifyClientRenderOpts?: (option: ClientRenderOption) => ClientRenderOption;
rootContainer?: (component: DefineComponent, option: { routes: RouteRecordRaw[], plugin: Plugin }) => DefineComponent;
onAppCreated?: ({ app, routes }: { app: App, routes: RouteRecordRaw[] }) => void;
render?: (defaultRender: RenderFunc) => RenderFunc;
onRouterCreated?: ({ router }: { router: Router }) => void;
}
export function getRouter(): Router;
}

View File

@ -1,42 +1,46 @@
import { defineRuntimeConfig } from '@fesjs/fes';
export default defineRuntimeConfig({
request: {
baseURL: '/ras-mas',
dataHandler(data) {
if (data?.code !== '0') {
if (data.code === '10000') {
console.log('code', data.code);
}
if (data?.code === '20000') {
console.log('code', data.code);
}
throw new Error(data);
}
return data.result ? data.result : data;
},
errorHandler(error) {
if (error.response) {
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// 请求已经成功发起,但没有收到响应
// `error.request` 在浏览器中是 XMLHttpRequest 的实例,
// 而在node.js中是 http.ClientRequest 的实例
console.log(error.request);
} else if (error.type) {
// 插件异常
console.log(error.msg);
} else {
// 发送请求时出了点问题
console.log('Error', error.message);
}
console.log(error.config);
},
},
patchRoutes: () => {
console.log('patchRoutes');
},
request: {},
});
// export default defineRuntimeConfig({
// request: {
// baseURL: '/ras-mas',
// dataHandler(data) {
// if (data?.code !== '0') {
// if (data.code === '10000') {
// console.log('code', data.code);
// }
// if (data?.code === '20000') {
// console.log('code', data.code);
// }
// throw new Error(data);
// }
// return data.result ? data.result : data;
// },
// errorHandler(error) {
// if (error.response) {
// // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
// console.log(error.response.data);
// console.log(error.response.status);
// console.log(error.response.headers);
// } else if (error.request) {
// // 请求已经成功发起,但没有收到响应
// // `error.request` 在浏览器中是 XMLHttpRequest 的实例,
// // 而在node.js中是 http.ClientRequest 的实例
// console.log(error.request);
// } else if (error.type) {
// // 插件异常
// console.log(error.msg);
// } else {
// // 发送请求时出了点问题
// console.log('Error', error.message);
// }
// console.log(error.config);
// },
// },
// patchRoutes: () => {
// console.log('patchRoutes');
// },
// });

View File

@ -1,17 +1,21 @@
// @ts-ignore
import type { PluginRuntimeConfig, PluginBuildConfig } from '@@/configType';
import '@@/configType';
// @ts-ignore
export * from '@@/core/coreExports';
// @ts-ignore
export * from '@@/core/pluginExports';
// // @ts-ignore
// export * from '@@/core/coreExports';
// // @ts-ignore
// export * from '@@/core/pluginExports';
interface RouteMeta {
export interface RouteMeta {
name?: string;
title?: string;
layout?: boolean | { sidebar?: boolean; header?: boolean; logo?: boolean };
}
export interface PluginRuntimeConfig {}
export interface PluginBuildConfig {}
export declare function defineRouteMeta(routeMeta: RouteMeta): RouteMeta;
export function defineBuildConfig(config: PluginBuildConfig ): PluginBuildConfig;