@creekjs/umi-plugins 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/creek-layout/index.d.ts.map +1 -1
- package/dist/creek-layout/index.js +3 -1
- package/dist/creek-layout/index.js.map +2 -2
- package/dist/creek-layout/template/layout.tpl +9 -0
- package/dist/creek-layout/template/type.tpl +4 -13
- package/package.json +1 -1
- package/dist/template/locale/i18N-package.tpl +0 -16
- package/dist/template/locale/i18N.tpl +0 -15
- package/dist/template/locale/index.tpl +0 -2
- package/dist/template/request/index.tpl +0 -8
- package/dist/template/request/request.tpl +0 -580
- package/dist/template/request/runtime-config.tpl +0 -6
- package/dist/template/request/type.tpl +0 -6
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/creek-layout/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAkC,MAAM,YAAY,CAAC;8BA8E7C,IAAI;AAAzB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/creek-layout/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAkC,MAAM,YAAY,CAAC;8BA8E7C,IAAI;AAAzB,wBAiIE"}
|
|
@@ -128,7 +128,8 @@ var creek_layout_default = (api) => {
|
|
|
128
128
|
});
|
|
129
129
|
api.onGenerateFiles(() => {
|
|
130
130
|
var _a, _b;
|
|
131
|
-
const hasInitialStatePlugin = api.config.initialState;
|
|
131
|
+
const hasInitialStatePlugin = !!api.config.initialState;
|
|
132
|
+
const hasLocalePlugin = !!api.config.locale;
|
|
132
133
|
const iconsInfo = getIconsInfoForRoutes(api);
|
|
133
134
|
const iconFontCNs = Array.isArray((_a = api.userConfig.creekLayout) == null ? void 0 : _a.iconFontCNs) ? (_b = api.userConfig.creekLayout) == null ? void 0 : _b.iconFontCNs : [];
|
|
134
135
|
api.writeTmpFile({
|
|
@@ -137,6 +138,7 @@ var creek_layout_default = (api) => {
|
|
|
137
138
|
context: {
|
|
138
139
|
creekWebComponentsPath,
|
|
139
140
|
hasInitialStatePlugin,
|
|
141
|
+
hasLocalePlugin,
|
|
140
142
|
access: api.config.access,
|
|
141
143
|
creekLocaleConfig: api.config.creekLocaleConfig ? JSON.stringify(api.config.glocale, null, 2) : "undefined",
|
|
142
144
|
userConfig: JSON.stringify(api.config.creekLayout, null, 2)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/creek-layout/index.tsx"],
|
|
4
|
-
"sourcesContent": ["import { IApi, IRoute, RUNTIME_TYPE_FILE_NAME } from '@umijs/max';\nimport { lodash, winPath } from '@umijs/max/plugin-utils';\nimport { existsSync, readFileSync } from 'fs';\nimport { dirname, join } from 'path';\n\nimport { withTmpPath } from '../utils';\n\nconst getPkgHasDep = (api: IApi, depList: string[]) => {\n const hasDependency = (pkg: Record<string, any>, dep: string) => pkg.dependencies?.[dep] || pkg.devDependencies?.[dep];\n const { pkg } = api;\n return depList.find((dep) => hasDependency(pkg, dep));\n};\n\nconst getPkgPath = (api: IApi, pkgName: string, depList?: string[]) => {\n const _depList = depList && depList.length ? depList : [pkgName];\n const pkgHasDep = getPkgHasDep(api, _depList);\n\n if (pkgHasDep) {\n const nodeModulesPath = join(api.cwd, 'node_modules', pkgHasDep, 'package.json');\n\n if (existsSync(nodeModulesPath)) {\n return join(api.cwd, 'node_modules', pkgHasDep);\n }\n\n const cwd = process.cwd();\n if (api.cwd !== cwd) {\n const altNodeModulesPath = join(cwd, 'node_modules', pkgHasDep, 'package.json');\n if (existsSync(altNodeModulesPath)) {\n return join(cwd, 'node_modules', pkgHasDep);\n }\n }\n }\n\n // 如果项目中没有去找插件依赖的\n return dirname(require.resolve(`${pkgName}/package.json`));\n};\n\nconst getIconsInfoForRoutes = (api: IApi) => {\n // 获取所有 icons\n const antIconsPath = winPath(getPkgPath(api, '@ant-design/icons'));\n\n const getAllIcons = () => {\n // 读取 index.d.ts\n const iconTypePath = join(antIconsPath, './lib/icons/index.d.ts');\n const iconTypeContent = readFileSync(iconTypePath, 'utf-8');\n\n // 截取 default as ${iconName}, 然后获取 iconName 转换为 map\n return [...iconTypeContent.matchAll(/default as (\\w+)/g)].reduce((memo: Record<string, boolean>, cur) => {\n memo[cur[1]] = true;\n return memo;\n }, {});\n };\n\n const allIcons: Record<string, boolean> = getAllIcons();\n\n const iconsMap = Object.keys(api.appData.routes).reduce<Record<string, boolean>>((memo, id) => {\n const { icon } = api.appData.routes[id];\n if (icon) {\n const upperIcon = lodash.upperFirst(lodash.camelCase(icon));\n // @ts-ignore\n if (allIcons[upperIcon]) {\n memo[upperIcon] = true;\n }\n // @ts-ignore\n if (allIcons[`${upperIcon}Outlined`]) {\n memo[`${upperIcon}Outlined`] = true;\n }\n }\n return memo;\n }, {});\n const icons = Object.keys(iconsMap);\n\n return {\n icons,\n antIconsPath,\n };\n};\n\nexport default (api: IApi) => {\n const TEMPLATE_DIR = join(__dirname, 'template');\n\n const creekWebComponentsPath = winPath(getPkgPath(api, '@creekjs/web-components'));\n const creekIconPath = require.resolve(`${creekWebComponentsPath}/dist/creek-icon`);\n\n api.describe({\n key: 'creekLayout',\n config: {\n schema({ zod }) {\n return zod\n .object({\n title: zod.string(),\n iconFontCNs: zod.string().array(),\n })\n .partial();\n }, \n onChange: api.ConfigChangeType.regenerateTmpFiles,\n },\n enableBy: api.EnableBy.config,\n });\n\n api.modifyAppData((memo) => {\n const version = require(`${creekWebComponentsPath}/package.json`).version;\n memo.pluginLayout = {\n creekWebComponentsPath,\n version,\n };\n return memo;\n });\n\n // 新增404页面\n api.modifyConfig((memo) => {\n const name = require(`${creekWebComponentsPath}/package.json`).name;\n memo.alias[name] = creekWebComponentsPath;\n\n const routes = memo.routes as IRoute[];\n const hasNotFoundPage = routes?.find((item) => [item.name, item.path].includes('404'));\n const defaultRoutes = [\n {\n name: '404',\n path: '*',\n hideInMenu: true,\n component: require.resolve(`${creekWebComponentsPath}/dist/creek-layout/Exception/NotFoundPage`),\n },\n ];\n\n if (!hasNotFoundPage) {\n memo.routes = [...routes, ...defaultRoutes];\n }\n\n return memo;\n });\n\n api.onGenerateFiles(() => {\n const hasInitialStatePlugin = api.config.initialState;\n const iconsInfo = getIconsInfoForRoutes(api);\n\n const iconFontCNs = Array.isArray(api.userConfig.creekLayout?.iconFontCNs) ? api.userConfig.creekLayout?.iconFontCNs : [];\n\n api.writeTmpFile({\n path: 'Layout.tsx',\n tplPath: join(TEMPLATE_DIR, '/layout.tpl'),\n context: {\n creekWebComponentsPath,\n hasInitialStatePlugin,\n access: api.config.access,\n creekLocaleConfig: api.config.creekLocaleConfig ? JSON.stringify(api.config.glocale, null, 2) : 'undefined',\n userConfig: JSON.stringify(api.config.creekLayout, null, 2),\n },\n });\n // 写入类型, RunTimeLayoutConfig 是 app.tsx 中 layout 配置的类型\n api.writeTmpFile({\n path: 'types.d.ts',\n tplPath: join(TEMPLATE_DIR, '/type.tpl'),\n context: {\n creekWebComponentsPath,\n hasInitialStatePlugin,\n access: api.config.access,\n },\n });\n\n api.writeTmpFile({\n path: RUNTIME_TYPE_FILE_NAME,\n tplPath: join(TEMPLATE_DIR, '/runtime-config.type.tpl'),\n context: {},\n });\n\n api.writeTmpFile({\n path: 'icons.tsx',\n tplPath: join(TEMPLATE_DIR, '/icons.tpl'),\n context: {\n icons: iconsInfo.icons,\n antIconsPath: iconsInfo.antIconsPath,\n creekIconPath,\n },\n });\n\n // runtime.tsx\n api.writeTmpFile({\n path: 'runtime.tsx',\n tplPath: join(TEMPLATE_DIR, '/runtime.tpl'),\n context: {\n creekWebComponentsPath,\n hasIconFontCNs: iconFontCNs.length > 0,\n iconFontCNs: JSON.stringify(iconFontCNs),\n },\n });\n });\n\n api.addLayouts(() => {\n return [\n {\n id: 'ant-design-pro-layout',\n file: withTmpPath({ api, path: 'Layout.tsx' }),\n test: (route: any) => {\n return route.layout !==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AAAA,IACA,UAAU,IAAI,SAAS;AAAA,EACzB,CAAC;AAED,MAAI,cAAc,CAAC,SAAS;AAC1B,UAAM,UAAU,QAAQ,GAAG,qCAAqC,EAAE;AAClE,SAAK,eAAe;AAAA,MAClB;AAAA,MACA;AAAA,IACF;AACA,WAAO;AAAA,EACT,CAAC;AAGD,MAAI,aAAa,CAAC,SAAS;AACzB,UAAM,OAAO,QAAQ,GAAG,qCAAqC,EAAE;AAC/D,SAAK,MAAM,IAAI,IAAI;AAEnB,UAAM,SAAS,KAAK;AACpB,UAAM,kBAAkB,iCAAQ,KAAK,CAAC,SAAS,CAAC,KAAK,MAAM,KAAK,IAAI,EAAE,SAAS,KAAK;AACpF,UAAM,gBAAgB;AAAA,MACpB;AAAA,QACE,MAAM;AAAA,QACN,MAAM;AAAA,QACN,YAAY;AAAA,QACZ,WAAW,QAAQ,QAAQ,GAAG,iEAAiE;AAAA,MACjG;AAAA,IACF;AAEA,QAAI,CAAC,iBAAiB;AACpB,WAAK,SAAS,CAAC,GAAG,QAAQ,GAAG,aAAa;AAAA,IAC5C;AAEA,WAAO;AAAA,EACT,CAAC;AAED,MAAI,gBAAgB,MAAM;AApI5B;AAqII,UAAM,wBAAwB,IAAI,OAAO;
|
|
4
|
+
"sourcesContent": ["import { IApi, IRoute, RUNTIME_TYPE_FILE_NAME } from '@umijs/max';\nimport { lodash, winPath } from '@umijs/max/plugin-utils';\nimport { existsSync, readFileSync } from 'fs';\nimport { dirname, join } from 'path';\n\nimport { withTmpPath } from '../utils';\n\nconst getPkgHasDep = (api: IApi, depList: string[]) => {\n const hasDependency = (pkg: Record<string, any>, dep: string) => pkg.dependencies?.[dep] || pkg.devDependencies?.[dep];\n const { pkg } = api;\n return depList.find((dep) => hasDependency(pkg, dep));\n};\n\nconst getPkgPath = (api: IApi, pkgName: string, depList?: string[]) => {\n const _depList = depList && depList.length ? depList : [pkgName];\n const pkgHasDep = getPkgHasDep(api, _depList);\n\n if (pkgHasDep) {\n const nodeModulesPath = join(api.cwd, 'node_modules', pkgHasDep, 'package.json');\n\n if (existsSync(nodeModulesPath)) {\n return join(api.cwd, 'node_modules', pkgHasDep);\n }\n\n const cwd = process.cwd();\n if (api.cwd !== cwd) {\n const altNodeModulesPath = join(cwd, 'node_modules', pkgHasDep, 'package.json');\n if (existsSync(altNodeModulesPath)) {\n return join(cwd, 'node_modules', pkgHasDep);\n }\n }\n }\n\n // 如果项目中没有去找插件依赖的\n return dirname(require.resolve(`${pkgName}/package.json`));\n};\n\nconst getIconsInfoForRoutes = (api: IApi) => {\n // 获取所有 icons\n const antIconsPath = winPath(getPkgPath(api, '@ant-design/icons'));\n\n const getAllIcons = () => {\n // 读取 index.d.ts\n const iconTypePath = join(antIconsPath, './lib/icons/index.d.ts');\n const iconTypeContent = readFileSync(iconTypePath, 'utf-8');\n\n // 截取 default as ${iconName}, 然后获取 iconName 转换为 map\n return [...iconTypeContent.matchAll(/default as (\\w+)/g)].reduce((memo: Record<string, boolean>, cur) => {\n memo[cur[1]] = true;\n return memo;\n }, {});\n };\n\n const allIcons: Record<string, boolean> = getAllIcons();\n\n const iconsMap = Object.keys(api.appData.routes).reduce<Record<string, boolean>>((memo, id) => {\n const { icon } = api.appData.routes[id];\n if (icon) {\n const upperIcon = lodash.upperFirst(lodash.camelCase(icon));\n // @ts-ignore\n if (allIcons[upperIcon]) {\n memo[upperIcon] = true;\n }\n // @ts-ignore\n if (allIcons[`${upperIcon}Outlined`]) {\n memo[`${upperIcon}Outlined`] = true;\n }\n }\n return memo;\n }, {});\n const icons = Object.keys(iconsMap);\n\n return {\n icons,\n antIconsPath,\n };\n};\n\nexport default (api: IApi) => {\n const TEMPLATE_DIR = join(__dirname, 'template');\n\n const creekWebComponentsPath = winPath(getPkgPath(api, '@creekjs/web-components'));\n const creekIconPath = require.resolve(`${creekWebComponentsPath}/dist/creek-icon`);\n\n api.describe({\n key: 'creekLayout',\n config: {\n schema({ zod }) {\n return zod\n .object({\n title: zod.string(),\n iconFontCNs: zod.string().array(),\n })\n .partial();\n }, \n onChange: api.ConfigChangeType.regenerateTmpFiles,\n },\n enableBy: api.EnableBy.config,\n });\n\n api.modifyAppData((memo) => {\n const version = require(`${creekWebComponentsPath}/package.json`).version;\n memo.pluginLayout = {\n creekWebComponentsPath,\n version,\n };\n return memo;\n });\n\n // 新增404页面\n api.modifyConfig((memo) => {\n const name = require(`${creekWebComponentsPath}/package.json`).name;\n memo.alias[name] = creekWebComponentsPath;\n\n const routes = memo.routes as IRoute[];\n const hasNotFoundPage = routes?.find((item) => [item.name, item.path].includes('404'));\n const defaultRoutes = [\n {\n name: '404',\n path: '*',\n hideInMenu: true,\n component: require.resolve(`${creekWebComponentsPath}/dist/creek-layout/Exception/NotFoundPage`),\n },\n ];\n\n if (!hasNotFoundPage) {\n memo.routes = [...routes, ...defaultRoutes];\n }\n\n return memo;\n });\n\n api.onGenerateFiles(() => {\n const hasInitialStatePlugin = !!api.config.initialState;\n const hasLocalePlugin = !!api.config.locale;\n const iconsInfo = getIconsInfoForRoutes(api);\n\n const iconFontCNs = Array.isArray(api.userConfig.creekLayout?.iconFontCNs) ? api.userConfig.creekLayout?.iconFontCNs : [];\n\n api.writeTmpFile({\n path: 'Layout.tsx',\n tplPath: join(TEMPLATE_DIR, '/layout.tpl'),\n context: {\n creekWebComponentsPath,\n hasInitialStatePlugin,\n hasLocalePlugin,\n access: api.config.access,\n creekLocaleConfig: api.config.creekLocaleConfig ? JSON.stringify(api.config.glocale, null, 2) : 'undefined',\n userConfig: JSON.stringify(api.config.creekLayout, null, 2),\n },\n });\n // 写入类型, RunTimeLayoutConfig 是 app.tsx 中 layout 配置的类型\n api.writeTmpFile({\n path: 'types.d.ts',\n tplPath: join(TEMPLATE_DIR, '/type.tpl'),\n context: {\n creekWebComponentsPath,\n hasInitialStatePlugin,\n access: api.config.access,\n },\n });\n\n api.writeTmpFile({\n path: RUNTIME_TYPE_FILE_NAME,\n tplPath: join(TEMPLATE_DIR, '/runtime-config.type.tpl'),\n context: {},\n });\n\n api.writeTmpFile({\n path: 'icons.tsx',\n tplPath: join(TEMPLATE_DIR, '/icons.tpl'),\n context: {\n icons: iconsInfo.icons,\n antIconsPath: iconsInfo.antIconsPath,\n creekIconPath,\n },\n });\n\n // runtime.tsx\n api.writeTmpFile({\n path: 'runtime.tsx',\n tplPath: join(TEMPLATE_DIR, '/runtime.tpl'),\n context: {\n creekWebComponentsPath,\n hasIconFontCNs: iconFontCNs.length > 0,\n iconFontCNs: JSON.stringify(iconFontCNs),\n },\n });\n });\n\n api.addLayouts(() => {\n return [\n {\n id: 'ant-design-pro-layout',\n file: withTmpPath({ api, path: 'Layout.tsx' }),\n test: (route: any) => {\n return route.layout !== false;\n },\n },\n ];\n });\n\n api.addRuntimePluginKey(() => ['layout']);\n\n api.addRuntimePlugin(() => {\n return [withTmpPath({ api, path: 'runtime.tsx' })];\n });\n};\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,iBAAqD;AACrD,0BAAgC;AAChC,gBAAyC;AACzC,kBAA8B;AAE9B,mBAA4B;AAE5B,IAAM,eAAe,CAAC,KAAW,YAAsB;AACrD,QAAM,gBAAgB,CAACA,MAA0B,QAAa;AARhE;AAQmE,kBAAAA,KAAI,iBAAJ,mBAAmB,WAAQ,KAAAA,KAAI,oBAAJ,mBAAsB;AAAA;AAClH,QAAM,EAAE,IAAI,IAAI;AAChB,SAAO,QAAQ,KAAK,CAAC,QAAQ,cAAc,KAAK,GAAG,CAAC;AACtD;AAEA,IAAM,aAAa,CAAC,KAAW,SAAiB,YAAuB;AACrE,QAAM,WAAW,WAAW,QAAQ,SAAS,UAAU,CAAC,OAAO;AAC/D,QAAM,YAAY,aAAa,KAAK,QAAQ;AAE5C,MAAI,WAAW;AACb,UAAM,sBAAkB,kBAAK,IAAI,KAAK,gBAAgB,WAAW,cAAc;AAE/E,YAAI,sBAAW,eAAe,GAAG;AAC/B,iBAAO,kBAAK,IAAI,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,QACJ,UAAM,0BAAY,EAAE,KAAK,MAAM,aAAa,CAAC;AAAA,QAC7C,MAAM,CAAC,UAAe;AACpB,iBAAO,MAAM,WAAW;AAAA,QAC1B;AAAA,MACF;AAAA,IACF;AAAA,EACF,CAAC;AAED,MAAI,oBAAoB,MAAM,CAAC,QAAQ,CAAC;AAExC,MAAI,iBAAiB,MAAM;AACzB,WAAO,KAAC,0BAAY,EAAE,KAAK,MAAM,cAAc,CAAC,CAAC;AAAA,EACnD,CAAC;AACH;",
|
|
6
6
|
"names": ["pkg"]
|
|
7
7
|
}
|
|
@@ -3,6 +3,13 @@ import { useMemo } from 'react';
|
|
|
3
3
|
import _ from 'lodash';
|
|
4
4
|
import { CreekLayout, CreekLayoutProps } from '{{{creekWebComponentsPath}}}';
|
|
5
5
|
|
|
6
|
+
{{#hasLocalePlugin}}
|
|
7
|
+
import { useIntl } from 'umi';
|
|
8
|
+
{{/hasLocalePlugin}}
|
|
9
|
+
{{^hasLocalePlugin}}
|
|
10
|
+
const useIntl = () => ({ formatMessage: undefined });
|
|
11
|
+
{{/hasLocalePlugin}}
|
|
12
|
+
|
|
6
13
|
{{#hasInitialStatePlugin}}
|
|
7
14
|
import { useModel } from '@@/plugin-model';
|
|
8
15
|
{{/hasInitialStatePlugin}}
|
|
@@ -74,6 +81,7 @@ const Layout = (props: CreekLayoutProps) => {
|
|
|
74
81
|
loading: false,
|
|
75
82
|
setInitialState: null,
|
|
76
83
|
};
|
|
84
|
+
const { formatMessage } = useIntl();
|
|
77
85
|
const userConfig = {{{userConfig}}};
|
|
78
86
|
const creekLocaleConfig = {{{creekLocaleConfig}}};
|
|
79
87
|
const runtimeConfig = pluginManager.applyPlugins({
|
|
@@ -114,6 +122,7 @@ const Layout = (props: CreekLayoutProps) => {
|
|
|
114
122
|
navigate={navigate}
|
|
115
123
|
userConfig={userConfig}
|
|
116
124
|
route={route}
|
|
125
|
+
formatMessage={formatMessage}
|
|
117
126
|
initialInfo={initialInfo}
|
|
118
127
|
{...layoutRestProps}
|
|
119
128
|
children={
|
|
@@ -13,24 +13,15 @@
|
|
|
13
13
|
type InitDataType = any;
|
|
14
14
|
{{/hasInitialStatePlugin}}
|
|
15
15
|
import type { IConfigFromPlugins } from '@@/core/pluginConfig';
|
|
16
|
+
import { CreekKeepAliveProps } from '{{{creekWebComponentsPath}}}';
|
|
16
17
|
|
|
17
18
|
export type RunTimeLayoutConfig = (initData: InitDataType) => ProLayoutProps & {
|
|
18
|
-
dropdownProps?: DropDownProps;
|
|
19
19
|
showDarkButton?: boolean;
|
|
20
|
-
showFooter?: boolean;
|
|
21
20
|
provider?: React.ReactNode;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
childrenRender?: (dom: JSX.Element, props: ProLayoutProps) => React.ReactNode;
|
|
21
|
+
keepAlive?: boolean | CreekKeepAliveProps;
|
|
22
|
+
extraActions?: React.ReactNode[];
|
|
23
|
+
showLocaleButton?: boolean;
|
|
26
24
|
unAccessible?: JSX.Element;
|
|
27
|
-
logout?: (initialState: InitDataType['initialState']) => Promise<void> | void;
|
|
28
25
|
iconFontCNs?: string[];
|
|
29
|
-
rightRender?: (
|
|
30
|
-
initialState: InitDataType['initialState'],
|
|
31
|
-
setInitialState: InitDataType['setInitialState'],
|
|
32
|
-
runtimeConfig: RunTimeLayoutConfig,
|
|
33
|
-
) => JSX.Element;
|
|
34
|
-
|
|
35
26
|
};
|
|
36
27
|
|
package/package.json
CHANGED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import KiwiIntl from "{{{kiwiIntlPath}}}";
|
|
2
|
-
|
|
3
|
-
import { languageStorage, LANGUAGE_LIST, CURRENT_LANGUAGE_KEY, IS_EN_US,
|
|
4
|
-
IS_ZH_CN, LanguageEnum } from "{{{constantsPath}}}";
|
|
5
|
-
|
|
6
|
-
import en_US from "{{{en_US}}}";
|
|
7
|
-
import zh_CN from "{{{zh_CN}}}";
|
|
8
|
-
|
|
9
|
-
let I18N = KiwiIntl.init(languageStorage.get() || "zh-CN", {
|
|
10
|
-
"en-US": en_US,
|
|
11
|
-
"zh-CN": zh_CN,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
window.{{{packageName}}} = I18N;
|
|
15
|
-
export { I18N, languageStorage, LANGUAGE_LIST, CURRENT_LANGUAGE_KEY, IS_EN_US,
|
|
16
|
-
IS_ZH_CN, LanguageEnum };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import KiwiIntl from "{{{kiwiIntlPath}}}";
|
|
2
|
-
|
|
3
|
-
import { languageStorage, LANGUAGE_LIST, CURRENT_LANGUAGE_KEY, IS_EN_US,
|
|
4
|
-
IS_ZH_CN, LanguageEnum } from "{{{constantsPath}}}";
|
|
5
|
-
|
|
6
|
-
import en_US from "{{{en_US}}}";
|
|
7
|
-
import zh_CN from "{{{zh_CN}}}";
|
|
8
|
-
|
|
9
|
-
let I18N = KiwiIntl.init(languageStorage.get() || "zh-CN", {
|
|
10
|
-
"en-US": en_US,
|
|
11
|
-
"zh-CN": zh_CN,
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
export { I18N, languageStorage, LANGUAGE_LIST, CURRENT_LANGUAGE_KEY, IS_EN_US,
|
|
15
|
-
IS_ZH_CN, LanguageEnum };
|
|
@@ -1,580 +0,0 @@
|
|
|
1
|
-
// @ts-nocheck
|
|
2
|
-
import { notification } from 'antd';
|
|
3
|
-
import { ApplyPluginsType } from 'umi';
|
|
4
|
-
import { ICacheLike, cacheAdapterEnhancer } from '{{{axiosExtensionsPath}}}';
|
|
5
|
-
import {CURRENT_LANGUAGE_KEY, LanguageEnum} from '{{{constantsPath}}}'
|
|
6
|
-
import axios, {
|
|
7
|
-
type AxiosError,
|
|
8
|
-
type AxiosInstance,
|
|
9
|
-
type AxiosRequestConfig,
|
|
10
|
-
type AxiosResponse,
|
|
11
|
-
} from '{{{axiosPath}}}';
|
|
12
|
-
import { useRequest as useUmiRequest } from '{{{umiRequestPath}}}';
|
|
13
|
-
import { getPluginManager } from '../core/plugin';
|
|
14
|
-
|
|
15
|
-
import {
|
|
16
|
-
Options,
|
|
17
|
-
Service,
|
|
18
|
-
Result
|
|
19
|
-
} from '{{{umiRequestPath}}}/es/useRequest/src/types';
|
|
20
|
-
|
|
21
|
-
type OptionsWithNoFullApiData<TData, TParams> = Options<TData, TParams> & {
|
|
22
|
-
formatResult?: (data: TData['result'], params: TParams) => TData['result'],
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
type OptionsWithFullApiData<TData, TParams> = Options<TData, TParams> & {
|
|
26
|
-
formatResult?: (data: TData, params: TParams) => TData,
|
|
27
|
-
needFullApiData: true;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function useRequest<
|
|
31
|
-
TData,
|
|
32
|
-
TParams extends any[]
|
|
33
|
-
>(
|
|
34
|
-
service:Service<TData, TParams>,
|
|
35
|
-
options?: OptionsWithFullApiData<TData, TParams>,
|
|
36
|
-
plugins?: Plugin<TData, TParams>[]
|
|
37
|
-
):Result<TData, TParams>;
|
|
38
|
-
|
|
39
|
-
function useRequest<
|
|
40
|
-
TData,
|
|
41
|
-
TParams extends any[]
|
|
42
|
-
>(
|
|
43
|
-
service:Service<TData, TParams>,
|
|
44
|
-
options?: OptionsWithNoFullApiData<TData, TParams>,
|
|
45
|
-
plugins?: Plugin<TData, TParams>[]
|
|
46
|
-
):Result<TData{{{resultDataField}}}, TParams>;
|
|
47
|
-
|
|
48
|
-
function useRequest<TData, TParams extends any[]>( service: Service<TData, TParams>,options?: Options<TData, TParams> = {}) {
|
|
49
|
-
const resquestResult = useUmiRequest(service, {
|
|
50
|
-
...options,
|
|
51
|
-
});
|
|
52
|
-
let needFullApiData = options['needFullApiData'] ;
|
|
53
|
-
if(options['formatResult']){
|
|
54
|
-
resquestResult.data = options['formatResult'](resquestResult.data, options);
|
|
55
|
-
}
|
|
56
|
-
if(needFullApiData){
|
|
57
|
-
resquestResult.data = resquestResult.data;
|
|
58
|
-
}else{
|
|
59
|
-
resquestResult.data = resquestResult.data? resquestResult.data['result'] : resquestResult.data ;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return resquestResult;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
interface IRequestOptions extends AxiosRequestConfig {
|
|
66
|
-
skipErrorHandler?: boolean;
|
|
67
|
-
requestInterceptors?: IRequestInterceptorTuple[];
|
|
68
|
-
responseInterceptors?: IResponseInterceptorTuple[];
|
|
69
|
-
closeMessage?: boolean;
|
|
70
|
-
setCustomMessage?: (error?: AxiosResponse<any, any> & {message: string}, opts?: IRequestOptions) => string;
|
|
71
|
-
cache?: boolean | {maxCount?: number, maxAge?: number};
|
|
72
|
-
[key: string]: any;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
interface IRequestOptionsWithResponse extends IRequestOptions {
|
|
76
|
-
getResponse: true;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
interface IRequestOptionsWithoutResponse extends IRequestOptions{
|
|
80
|
-
getResponse: false;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface ApiResponse<T> {
|
|
84
|
-
code: string,
|
|
85
|
-
msg: string,
|
|
86
|
-
totalRows?: number;
|
|
87
|
-
totalPages?: number;
|
|
88
|
-
result?: T,
|
|
89
|
-
records?: T,
|
|
90
|
-
pageSize?: number,
|
|
91
|
-
currentPage?: number
|
|
92
|
-
success: boolean,
|
|
93
|
-
data: T{{{resultDataField}}} & T{{{resultDataField}}}['records'] & T['records'] & T,
|
|
94
|
-
total: number
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export interface AntdApiResponse<T> {
|
|
98
|
-
success: boolean,
|
|
99
|
-
data: T,
|
|
100
|
-
total: number
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
interface IRequest{
|
|
105
|
-
<T = any>(url: string, opts: IRequestOptionsWithResponse): Promise<AxiosResponse<ApiResponse<T>>>;
|
|
106
|
-
<T = any>(url: string, opts: IRequestOptionsWithoutResponse): Promise<ApiResponse<T>>;
|
|
107
|
-
<T = any>(url: string, opts: IRequestOptions): Promise<ApiResponse<T>>;
|
|
108
|
-
<T = any>(url: string): Promise<ApiResponse<T>>;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
type RequestError = AxiosError | Error
|
|
112
|
-
|
|
113
|
-
interface IErrorHandler {
|
|
114
|
-
(error: AxiosResponse<any, any> & {message: string}, opts: IRequestOptions): void;
|
|
115
|
-
}
|
|
116
|
-
type IRequestInterceptorAxios = (config: IRequestOptions) => IRequestOptions | Promise<IRequestOptions>;
|
|
117
|
-
type IRequestInterceptorUmiRequest = (url: string, config : IRequestOptions) => { url: string, options: RequestOptions } | Promise<{ url: string, options: RequestOptions }>;
|
|
118
|
-
type IRequestInterceptor = IRequestInterceptorAxios | IRequestInterceptorUmiRequest;
|
|
119
|
-
type IErrorInterceptor = (error: Error) => Promise<Error>;
|
|
120
|
-
type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => AxiosResponse<T> ;
|
|
121
|
-
type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [ IRequestInterceptor ] | IRequestInterceptor;
|
|
122
|
-
type IResponseInterceptorTuple = [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor;
|
|
123
|
-
|
|
124
|
-
enum HttpStatusEnum {
|
|
125
|
-
CONTINUE = 100,
|
|
126
|
-
SWITCHING_PROTOCOLS = 101,
|
|
127
|
-
PROCESSING = 102,
|
|
128
|
-
SUCCESS = 200,
|
|
129
|
-
CREATED = 201,
|
|
130
|
-
ACCEPTED = 202,
|
|
131
|
-
NON_AUTHORITATIVE_INFORMATION = 203,
|
|
132
|
-
NO_CONTENT = 204,
|
|
133
|
-
RESET_CONTENT = 205,
|
|
134
|
-
PARTIAL_CONTENT = 206,
|
|
135
|
-
AMBIGUOUS = 300,
|
|
136
|
-
MOVED_PERMANENTLY = 301,
|
|
137
|
-
FOUND = 302,
|
|
138
|
-
SEE_OTHER = 303,
|
|
139
|
-
NOT_MODIFIED = 304,
|
|
140
|
-
TEMPORARY_REDIRECT = 307,
|
|
141
|
-
PERMANENT_REDIRECT = 308,
|
|
142
|
-
BAD_REQUEST = 400,
|
|
143
|
-
UNAUTHORIZED = 401,
|
|
144
|
-
PAYMENT_REQUIRED = 402,
|
|
145
|
-
FORBIDDEN = 403,
|
|
146
|
-
NOT_FOUND = 404,
|
|
147
|
-
METHOD_NOT_ALLOWED = 405,
|
|
148
|
-
NOT_ACCEPTABLE = 406,
|
|
149
|
-
PROXY_AUTHENTICATION_REQUIRED = 407,
|
|
150
|
-
REQUEST_TIMEOUT = 408,
|
|
151
|
-
CONFLICT = 409,
|
|
152
|
-
GONE = 410,
|
|
153
|
-
LENGTH_REQUIRED = 411,
|
|
154
|
-
PRECONDITION_FAILED = 412,
|
|
155
|
-
PAYLOAD_TOO_LARGE = 413,
|
|
156
|
-
URI_TOO_LONG = 414,
|
|
157
|
-
UNSUPPORTED_MEDIA_TYPE = 415,
|
|
158
|
-
REQUESTED_RANGE_NOT_SATISFIABLE = 416,
|
|
159
|
-
EXPECTATION_FAILED = 417,
|
|
160
|
-
I_AM_A_TEAPOT = 418,
|
|
161
|
-
UNPROCESSABLE_ENTITY = 422,
|
|
162
|
-
TOO_MANY_REQUESTS = 429,
|
|
163
|
-
INTERNAL_SERVER_ERROR = 500,
|
|
164
|
-
NOT_IMPLEMENTED = 501,
|
|
165
|
-
BAD_GATEWAY = 502,
|
|
166
|
-
SERVICE_UNAVAILABLE = 503,
|
|
167
|
-
GATEWAY_TIMEOUT = 504,
|
|
168
|
-
HTTP_VERSION_NOT_SUPPORTED = 505,
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
enum HttpMessageLocalEnum {
|
|
172
|
-
BAD_GATEWAY = {
|
|
173
|
-
[LanguageEnum.zhCn]: '网关错误',
|
|
174
|
-
[LanguageEnum.enUs]: 'gateway error',
|
|
175
|
-
},
|
|
176
|
-
SERVICE_UNAVAILABLE = {
|
|
177
|
-
[LanguageEnum.zhCn]: '服务不可用,服务器暂时过载或维护',
|
|
178
|
-
[LanguageEnum.enUs]: 'Service unavailable, server temporarily overloaded or maintenance gateway error',
|
|
179
|
-
},
|
|
180
|
-
GATEWAY_TIMEOUT = {
|
|
181
|
-
[LanguageEnum.zhCn]: '网关超时',
|
|
182
|
-
[LanguageEnum.enUs]: 'gateway timeout',
|
|
183
|
-
},
|
|
184
|
-
NOT_FOUND = {
|
|
185
|
-
[LanguageEnum.zhCn]: '路径错误',
|
|
186
|
-
[LanguageEnum.enUs]: 'wrong path',
|
|
187
|
-
},
|
|
188
|
-
UNAUTHORIZED = {
|
|
189
|
-
[LanguageEnum.zhCn]: '认证失败',
|
|
190
|
-
[LanguageEnum.enUs]: 'Authentication failed',
|
|
191
|
-
},
|
|
192
|
-
BAD_REQUEST = {
|
|
193
|
-
[LanguageEnum.zhCn]: '错误的请求',
|
|
194
|
-
[LanguageEnum.enUs]: 'bad Request',
|
|
195
|
-
},
|
|
196
|
-
INTERNAL_SERVER_ERROR = {
|
|
197
|
-
[LanguageEnum.zhCn]: '错误的请求',
|
|
198
|
-
[LanguageEnum.enUs]: 'bad Request',
|
|
199
|
-
},
|
|
200
|
-
ERROR_HTTP = {
|
|
201
|
-
[LanguageEnum.zhCn]: '服务器错误',
|
|
202
|
-
[LanguageEnum.enUs]: 'Server Error',
|
|
203
|
-
},
|
|
204
|
-
FORBIDDEN = {
|
|
205
|
-
[LanguageEnum.zhCn]: '无此权限',
|
|
206
|
-
[LanguageEnum.enUs]: 'no permission',
|
|
207
|
-
},
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
enum HttpMessageEnum {
|
|
211
|
-
BAD_GATEWAY = HttpMessageLocalEnum["BAD_GATEWAY"][CURRENT_LANGUAGE_KEY],
|
|
212
|
-
SERVICE_UNAVAILABLE = HttpMessageLocalEnum["SERVICE_UNAVAILABLE"][CURRENT_LANGUAGE_KEY],
|
|
213
|
-
GATEWAY_TIMEOUT = HttpMessageLocalEnum["GATEWAY_TIMEOUT"][CURRENT_LANGUAGE_KEY],
|
|
214
|
-
NOT_FOUND = HttpMessageLocalEnum["NOT_FOUND"][CURRENT_LANGUAGE_KEY],
|
|
215
|
-
UNAUTHORIZED = HttpMessageLocalEnum["UNAUTHORIZED"][CURRENT_LANGUAGE_KEY],
|
|
216
|
-
BAD_REQUEST = HttpMessageLocalEnum["BAD_REQUEST"][CURRENT_LANGUAGE_KEY],
|
|
217
|
-
INTERNAL_SERVER_ERROR=HttpMessageLocalEnum["INTERNAL_SERVER_ERROR"][CURRENT_LANGUAGE_KEY],
|
|
218
|
-
ERROR_HTTP =HttpMessageLocalEnum["ERROR_HTTP"][CURRENT_LANGUAGE_KEY],
|
|
219
|
-
FORBIDDEN = HttpMessageLocalEnum["FORBIDDEN"][CURRENT_LANGUAGE_KEY],
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export interface RequestConfig<T = any> extends AxiosRequestConfig {
|
|
223
|
-
errorConfig?: {
|
|
224
|
-
errorHandler?: IErrorHandler;
|
|
225
|
-
customErrorShow?: IErrorHandler;
|
|
226
|
-
};
|
|
227
|
-
requestInterceptors?: IRequestInterceptorTuple[];
|
|
228
|
-
responseInterceptors?: IResponseInterceptorTuple[];
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* 判断如果是 Promise, 则从 Promise 中取出并执行剩余操作, 此时返回 Promise
|
|
233
|
-
* 如果不是 Promise, 则直接执行剩余操作, 此时直接返回结果
|
|
234
|
-
*/
|
|
235
|
-
const getPromiseValue = <T>(value: T | Promise<T>, callback: (val: T) => any): T | Promise<T> => {
|
|
236
|
-
if (value instanceof Promise) {
|
|
237
|
-
return value.then(callback);
|
|
238
|
-
}
|
|
239
|
-
return callback(value);
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
let requestInstance: AxiosInstance;
|
|
243
|
-
let config: RequestConfig;
|
|
244
|
-
const getConfig = (): RequestConfig => {
|
|
245
|
-
if (config) return config;
|
|
246
|
-
config = getPluginManager().applyPlugins({
|
|
247
|
-
key: 'request',
|
|
248
|
-
type: ApplyPluginsType.modify,
|
|
249
|
-
initialValue: {},
|
|
250
|
-
});
|
|
251
|
-
return config;
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
const dealRequestData = (response: AxiosResponse<any, any>, opts: IRequestOptions) => {
|
|
255
|
-
const { data, status, message } = response;
|
|
256
|
-
const regex = /^0+$/;
|
|
257
|
-
let httpMessage = null;
|
|
258
|
-
if (Number(status) === 200) {
|
|
259
|
-
if (data?.code && !regex.test(data?.code)) {
|
|
260
|
-
if (!opts?.closeMessage) {
|
|
261
|
-
config?.errorConfig?.customErrorShow ?
|
|
262
|
-
config?.errorConfig?.customErrorShow(response, opts) :
|
|
263
|
-
notification.error({
|
|
264
|
-
message: data?.code,
|
|
265
|
-
description: opts.setCustomMessage ? opts.setCustomMessage(response, opts): data?.msg
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
if (config?.errorConfig?.errorHandler) {
|
|
269
|
-
config?.errorConfig?.errorHandler(response, opts)
|
|
270
|
-
}
|
|
271
|
-
return Promise.reject(response);
|
|
272
|
-
}
|
|
273
|
-
} else {
|
|
274
|
-
switch (status) {
|
|
275
|
-
case HttpStatusEnum.BAD_GATEWAY:
|
|
276
|
-
httpMessage = HttpMessageEnum.BAD_GATEWAY;
|
|
277
|
-
break;
|
|
278
|
-
case HttpStatusEnum.SERVICE_UNAVAILABLE:
|
|
279
|
-
httpMessage = HttpMessageEnum.SERVICE_UNAVAILABLE;
|
|
280
|
-
break;
|
|
281
|
-
case HttpStatusEnum.GATEWAY_TIMEOUT:
|
|
282
|
-
httpMessage = HttpMessageEnum.GATEWAY_TIMEOUT;
|
|
283
|
-
break;
|
|
284
|
-
case HttpStatusEnum.NOT_FOUND:
|
|
285
|
-
httpMessage = HttpMessageEnum.NOT_FOUND;
|
|
286
|
-
break;
|
|
287
|
-
case HttpStatusEnum.UNAUTHORIZED:
|
|
288
|
-
httpMessage = HttpMessageEnum.UNAUTHORIZED;
|
|
289
|
-
break;
|
|
290
|
-
case HttpStatusEnum.FORBIDDEN:
|
|
291
|
-
httpMessage = HttpMessageEnum.FORBIDDEN;
|
|
292
|
-
break;
|
|
293
|
-
case HttpStatusEnum.BAD_REQUEST:
|
|
294
|
-
httpMessage = HttpMessageEnum.BAD_REQUEST;
|
|
295
|
-
break;
|
|
296
|
-
default:
|
|
297
|
-
httpMessage = HttpMessageEnum.ERROR_HTTP;
|
|
298
|
-
break;
|
|
299
|
-
}
|
|
300
|
-
if (!opts?.closeMessage && status !== HttpStatusEnum.UNAUTHORIZED) {
|
|
301
|
-
config?.errorConfig?.customErrorShow ?
|
|
302
|
-
config?.errorConfig?.customErrorShow(response, opts) :
|
|
303
|
-
notification.error({
|
|
304
|
-
message: status ? `${status}(${httpMessage})`: httpMessage,
|
|
305
|
-
description: opts.setCustomMessage ? opts.setCustomMessage(response, opts) : (message || "服务器错误")
|
|
306
|
-
})
|
|
307
|
-
}
|
|
308
|
-
if (config?.errorConfig?.errorHandler) {
|
|
309
|
-
config?.errorConfig?.errorHandler(response, opts)
|
|
310
|
-
}
|
|
311
|
-
return Promise.reject(response);
|
|
312
|
-
}
|
|
313
|
-
return response;
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
const getRequestInstance = (opts): AxiosInstance => {
|
|
317
|
-
if (requestInstance) return requestInstance;
|
|
318
|
-
const config = getConfig();
|
|
319
|
-
requestInstance = axios.create({
|
|
320
|
-
...config,
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
const configCache = opts.cache;
|
|
324
|
-
const finalCache = {
|
|
325
|
-
enabledByDefault: configCache === true || !!configCache?.maxAge || !!configCache?.maxCount,
|
|
326
|
-
maxCount: configCache?.maxCount,
|
|
327
|
-
maxAge: configCache?.maxAge
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
const cache = cacheAdapterEnhancer(finalCache);
|
|
332
|
-
const _request = requestInstance.request;
|
|
333
|
-
requestInstance.request = (config) => cache(_request, config);
|
|
334
|
-
|
|
335
|
-
config?.requestInterceptors?.forEach((interceptor) => {
|
|
336
|
-
if(interceptor instanceof Array){
|
|
337
|
-
requestInstance.interceptors.request.use((config) => {
|
|
338
|
-
const { url } = config;
|
|
339
|
-
if(interceptor[0].length === 2){
|
|
340
|
-
const res = interceptor[0](url, config);
|
|
341
|
-
return getPromiseValue(res, (val) => {
|
|
342
|
-
const { url: newUrl, options = {} } = val
|
|
343
|
-
return { ...options, url: newUrl }
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
return interceptor[0](config);
|
|
347
|
-
}, interceptor[1]);
|
|
348
|
-
} else {
|
|
349
|
-
requestInstance.interceptors.request.use((config) => {
|
|
350
|
-
const { url } = config;
|
|
351
|
-
if(interceptor.length === 2){
|
|
352
|
-
const res = interceptor(url, config);
|
|
353
|
-
return getPromiseValue(res, (val) => {
|
|
354
|
-
const { url: newUrl, options = {} } = val
|
|
355
|
-
return { ...options, url: newUrl }
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
return interceptor(config);
|
|
359
|
-
})
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
|
|
363
|
-
config?.responseInterceptors?.forEach((interceptor) => {
|
|
364
|
-
interceptor instanceof Array ?
|
|
365
|
-
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
366
|
-
requestInstance.interceptors.response.use(interceptor);
|
|
367
|
-
});
|
|
368
|
-
|
|
369
|
-
requestInstance.interceptors.response.use((response) => {
|
|
370
|
-
return dealRequestData(response, opts);
|
|
371
|
-
})
|
|
372
|
-
return requestInstance;
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
const changeRequestData = (opts: IRequestOptions) => {
|
|
376
|
-
const optsData = opts?.data || {};
|
|
377
|
-
const optsParams = opts?.params || {};
|
|
378
|
-
const contentType = opts?.headers? opts?.headers['Content-Type']: null;
|
|
379
|
-
let finalOpts = {...opts};
|
|
380
|
-
if (optsData && optsData?.current) {
|
|
381
|
-
optsData.currentPage = optsData.current;
|
|
382
|
-
delete optsData.current;
|
|
383
|
-
finalOpts.data = {...optsData};
|
|
384
|
-
}
|
|
385
|
-
if(optsParams && optsParams?.current){
|
|
386
|
-
optsParams.currentPage = optsParams.current;
|
|
387
|
-
delete optsParams.current;
|
|
388
|
-
finalOpts.params = {...optsParams};
|
|
389
|
-
}
|
|
390
|
-
if( contentType && contentType.includes('multipart/form-data') ){
|
|
391
|
-
let formData = new FormData();
|
|
392
|
-
let userData = opts?.params || opts.data || {};
|
|
393
|
-
Object.keys(userData).forEach(key => {
|
|
394
|
-
formData.append(key, userData[key]);
|
|
395
|
-
})
|
|
396
|
-
finalOpts.data = formData;
|
|
397
|
-
if(finalOpts.params){
|
|
398
|
-
delete finalOpts.params;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
finalOpts.headers = {
|
|
402
|
-
...finalOpts.headers,
|
|
403
|
-
"Accept-Language": CURRENT_LANGUAGE_KEY
|
|
404
|
-
}
|
|
405
|
-
return finalOpts;
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
const changeResponseData = (response: AxiosResponse<any, any>, getResponse: boolean) => {
|
|
409
|
-
const currentResponseData = response.data;
|
|
410
|
-
let finalResponse = currentResponseData;
|
|
411
|
-
if (currentResponseData?.code) {
|
|
412
|
-
const { code, result } = currentResponseData;
|
|
413
|
-
const regex = /^0+$/;
|
|
414
|
-
if (result) {
|
|
415
|
-
finalResponse = {
|
|
416
|
-
...currentResponseData,
|
|
417
|
-
success: regex.test(code),
|
|
418
|
-
data: Array.isArray(result) ? result : result?.records,
|
|
419
|
-
total: Array.isArray(result) ? result.length : result?.totalRows
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
response.data = { ...finalResponse };
|
|
423
|
-
}
|
|
424
|
-
return getResponse ? response : finalResponse;
|
|
425
|
-
};
|
|
426
|
-
|
|
427
|
-
const request: IRequest = (url: string, opts: IRequestOptions = { method: 'GET' }) => {
|
|
428
|
-
const requestInstance = getRequestInstance(opts);
|
|
429
|
-
const config = getConfig();
|
|
430
|
-
const finalOpts = changeRequestData(opts);
|
|
431
|
-
const { getResponse = false, requestInterceptors, responseInterceptors } = finalOpts;
|
|
432
|
-
const requestInterceptorsToEject = requestInterceptors?.map((interceptor) => {
|
|
433
|
-
if(interceptor instanceof Array){
|
|
434
|
-
return requestInstance.interceptors.request.use((config) => {
|
|
435
|
-
const { url } = config;
|
|
436
|
-
if(interceptor[0].length === 2){
|
|
437
|
-
const res = interceptor[0](url, config);
|
|
438
|
-
return getPromiseValue(res, (val) => {
|
|
439
|
-
const { url: newUrl, options = {} } = val
|
|
440
|
-
return { ...options, url: newUrl }
|
|
441
|
-
});
|
|
442
|
-
}
|
|
443
|
-
return interceptor[0](config);
|
|
444
|
-
}, interceptor[1]);
|
|
445
|
-
} else {
|
|
446
|
-
return requestInstance.interceptors.request.use((config) => {
|
|
447
|
-
const { url } = config;
|
|
448
|
-
if(interceptor.length === 2){
|
|
449
|
-
const res = interceptor(url, config);
|
|
450
|
-
return getPromiseValue(res, (val) => {
|
|
451
|
-
const { url: newUrl, options = {} } = val
|
|
452
|
-
return { ...options, url: newUrl }
|
|
453
|
-
});
|
|
454
|
-
}
|
|
455
|
-
return interceptor(config);
|
|
456
|
-
})
|
|
457
|
-
}
|
|
458
|
-
});
|
|
459
|
-
const responseInterceptorsToEject = responseInterceptors?.map((interceptor) => {
|
|
460
|
-
return interceptor instanceof Array ?
|
|
461
|
-
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
462
|
-
requestInstance.interceptors.response.use(interceptor);
|
|
463
|
-
});
|
|
464
|
-
return new Promise((resolve, reject)=>{
|
|
465
|
-
requestInstance
|
|
466
|
-
.request({...finalOpts, url})
|
|
467
|
-
.then((res)=>{
|
|
468
|
-
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
469
|
-
requestInstance.interceptors.request.eject(interceptor);
|
|
470
|
-
});
|
|
471
|
-
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
472
|
-
requestInstance.interceptors.response.eject(interceptor);
|
|
473
|
-
});
|
|
474
|
-
resolve(changeResponseData(res,getResponse ))
|
|
475
|
-
})
|
|
476
|
-
.catch((error)=>{
|
|
477
|
-
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
478
|
-
requestInstance.interceptors.request.eject(interceptor);
|
|
479
|
-
});
|
|
480
|
-
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
481
|
-
requestInstance.interceptors.response.eject(interceptor);
|
|
482
|
-
});
|
|
483
|
-
const {response, message} = error;
|
|
484
|
-
if(response){
|
|
485
|
-
dealRequestData({
|
|
486
|
-
...response,
|
|
487
|
-
message
|
|
488
|
-
}, opts);
|
|
489
|
-
}
|
|
490
|
-
reject(error);
|
|
491
|
-
})
|
|
492
|
-
})
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
// interface PageParams {
|
|
496
|
-
// pageSize?: number;
|
|
497
|
-
// current?: number;
|
|
498
|
-
// keyword?: string;
|
|
499
|
-
// }
|
|
500
|
-
|
|
501
|
-
// type WidthPageParams<T> = T & PageParams;
|
|
502
|
-
|
|
503
|
-
// type TableRequest<T, U> = ProTableProps<T, U>['request']
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
type ApiSort = {
|
|
507
|
-
field?: string;
|
|
508
|
-
direction?: string;
|
|
509
|
-
asc?: boolean;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
type SortOrder = Record<string, "descend" | "ascend" | null>
|
|
513
|
-
|
|
514
|
-
function sortFilterProTable<P, D>(
|
|
515
|
-
fn: (
|
|
516
|
-
params: P & {
|
|
517
|
-
sort?: ApiSort[];
|
|
518
|
-
}
|
|
519
|
-
) => Promise<ApiResponse<D>>
|
|
520
|
-
) {
|
|
521
|
-
return (
|
|
522
|
-
params: P & {
|
|
523
|
-
pageSize?: number;
|
|
524
|
-
current?: number;
|
|
525
|
-
keyword?: string;
|
|
526
|
-
},
|
|
527
|
-
sort: SortOrder,
|
|
528
|
-
filter: Record<string, (string | number)[] | null>
|
|
529
|
-
) => {
|
|
530
|
-
let finalSort: ApiSort = {};
|
|
531
|
-
let finalParams: P & {
|
|
532
|
-
pageSize?: number;
|
|
533
|
-
current?: number;
|
|
534
|
-
keyword?: string;
|
|
535
|
-
sort?: ApiSort[]
|
|
536
|
-
} = {...params};
|
|
537
|
-
|
|
538
|
-
if(sort){
|
|
539
|
-
Object.keys(sort).forEach((key) => {
|
|
540
|
-
if (sort[key]) {
|
|
541
|
-
finalSort.field = key;
|
|
542
|
-
finalSort.asc = sort[key] === "ascend" ? true : false;
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
};
|
|
546
|
-
|
|
547
|
-
if(filter){
|
|
548
|
-
Object.keys(filter).forEach((key) => {
|
|
549
|
-
if (filter[key]) {
|
|
550
|
-
finalParams[key] = filter[key];
|
|
551
|
-
}
|
|
552
|
-
});
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
if(finalSort.field){
|
|
556
|
-
finalParams.sort = [finalSort];
|
|
557
|
-
}
|
|
558
|
-
return fn(finalParams);
|
|
559
|
-
};
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
export {
|
|
563
|
-
useRequest,
|
|
564
|
-
request,
|
|
565
|
-
getRequestInstance,
|
|
566
|
-
sortFilterProTable,
|
|
567
|
-
HttpStatusEnum,
|
|
568
|
-
HttpMessageEnum
|
|
569
|
-
};
|
|
570
|
-
export type {
|
|
571
|
-
AxiosInstance,
|
|
572
|
-
AxiosRequestConfig,
|
|
573
|
-
AxiosResponse,
|
|
574
|
-
AxiosError,
|
|
575
|
-
RequestError,
|
|
576
|
-
IResponseInterceptor as ResponseInterceptor,
|
|
577
|
-
IRequestOptions as RequestOptions,
|
|
578
|
-
IRequest as Request,
|
|
579
|
-
};
|
|
580
|
-
|