You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ju...@apache.org on 2020/05/31 03:55:23 UTC
[incubator-apisix-dashboard] branch next updated: feat: added
Plugin (#221)
This is an automated email from the ASF dual-hosted git repository.
juzhiyuan pushed a commit to branch next
in repository https://gitbox.apache.org/repos/asf/incubator-apisix-dashboard.git
The following commit(s) were added to refs/heads/next by this push:
new a301633 feat: added Plugin (#221)
a301633 is described below
commit a3016334b67557b53fad6a0340b30e7dc30d2fa6
Author: 琚致远 <ju...@apache.org>
AuthorDate: Sun May 31 11:55:15 2020 +0800
feat: added Plugin (#221)
* feat: added Plugin
* feat: refactor PluginSchema
* feat: update typing
* feat: added PluginDrawer
* feat: update desc
* feat: reset step
---
.../PluginForm/{index.tsx => PluginForm.tsx} | 21 ++++-----
src/components/PluginForm/index.ts | 2 +
src/components/PluginForm/service.ts | 7 +++
.../PluginForm/transformer.ts} | 8 ++--
src/components/PluginForm/typingd.d.ts | 50 +++++++++++++++++++++
src/pages/Routes/Create.tsx | 16 ++++---
.../Routes/components/CreateStep3/CreateStep3.tsx | 51 ++++++++++++++++++++++
.../Routes/components/CreateStep3/PluginCard.tsx | 17 ++++++++
.../Routes/components/CreateStep3/PluginDrawer.tsx | 44 +++++++++++++++++++
src/pages/Routes/components/CreateStep3/index.ts | 1 +
src/pages/Routes/components/PanelSection/index.tsx | 10 ++---
src/services/plugin.ts | 7 ---
src/typings.d.ts | 43 ------------------
src/utils/utils.less | 16 -------
src/utils/utils.test.ts | 37 ----------------
src/utils/utils.ts | 24 ----------
16 files changed, 199 insertions(+), 155 deletions(-)
diff --git a/src/components/PluginForm/index.tsx b/src/components/PluginForm/PluginForm.tsx
similarity index 91%
rename from src/components/PluginForm/index.tsx
rename to src/components/PluginForm/PluginForm.tsx
index a841cf7..361be91 100644
--- a/src/components/PluginForm/index.tsx
+++ b/src/components/PluginForm/PluginForm.tsx
@@ -4,14 +4,8 @@ import { useForm } from 'antd/es/form/util';
import { PlusOutlined, MinusCircleOutlined } from '@ant-design/icons';
import { useIntl } from 'umi';
-import { fetchPluginSchema } from '@/services/plugin';
-import { transformPropertyToRules } from '@/transforms/plugin';
-
-interface Props {
- name: string;
- initialData?: PluginSchema;
- onFinish(values: any): void;
-}
+import { fetchPluginSchema } from './service';
+import { transformPropertyToRules } from './transformer';
const formLayout = {
labelCol: { span: 10 },
@@ -23,7 +17,7 @@ interface RenderComponentProps {
}
const renderComponentByProperty = (
- propertyValue: PluginProperty,
+ propertyValue: PluginForm.PluginProperty,
restProps?: RenderComponentProps,
) => {
const { type, minimum, maximum } = propertyValue;
@@ -60,9 +54,9 @@ const renderComponentByProperty = (
};
interface ArrayComponentProps {
- schema: PluginSchema;
+ schema: PluginForm.PluginSchema;
propertyName: string;
- propertyValue: PluginProperty;
+ propertyValue: PluginForm.PluginProperty;
}
const ArrayComponent: React.FC<ArrayComponentProps> = ({
@@ -104,12 +98,13 @@ const ArrayComponent: React.FC<ArrayComponentProps> = ({
);
};
-const PluginForm: React.FC<Props> = ({ name, initialData = {}, onFinish }) => {
- const [schema, setSchema] = useState<PluginSchema>();
+const PluginForm: React.FC<PluginForm.Props> = ({ name, initialData = {}, onFinish }) => {
+ const [schema, setSchema] = useState<PluginForm.PluginSchema>();
const [form] = useForm();
useEffect(() => {
if (name) {
+ setSchema(undefined);
fetchPluginSchema(name).then((data) => {
setSchema(data);
diff --git a/src/components/PluginForm/index.ts b/src/components/PluginForm/index.ts
new file mode 100644
index 0000000..2f23710
--- /dev/null
+++ b/src/components/PluginForm/index.ts
@@ -0,0 +1,2 @@
+export { default } from './PluginForm';
+export { fetchList as fetchPluginList } from './service';
diff --git a/src/components/PluginForm/service.ts b/src/components/PluginForm/service.ts
new file mode 100644
index 0000000..447bd9a
--- /dev/null
+++ b/src/components/PluginForm/service.ts
@@ -0,0 +1,7 @@
+import { request } from 'umi';
+import { transformSchemaFromAPI } from './transformer';
+
+export const fetchList = () => request('/plugins/list');
+
+export const fetchPluginSchema = (name: string): Promise<PluginForm.PluginSchema> =>
+ request(`/schema/plugins/${name}`).then((data) => transformSchemaFromAPI(data, name));
diff --git a/src/transforms/plugin.ts b/src/components/PluginForm/transformer.ts
similarity index 87%
rename from src/transforms/plugin.ts
rename to src/components/PluginForm/transformer.ts
index 39b66f7..cf55c55 100644
--- a/src/transforms/plugin.ts
+++ b/src/components/PluginForm/transformer.ts
@@ -4,7 +4,7 @@ import { Rule } from 'antd/es/form';
/**
* Transform schema data from API for target plugin.
*/
-export const transformSchemaFromAPI = (schema: PluginSchema, pluginName: string): PluginSchema => {
+export const transformSchemaFromAPI = (schema: PluginForm.PluginSchema, pluginName: string): PluginForm.PluginSchema => {
if (pluginName === 'key-auth') {
return {
...schema,
@@ -37,7 +37,7 @@ export const transformSchemaFromAPI = (schema: PluginSchema, pluginName: string)
* Transform schema data to be compatible with API.
*/
// eslint-disable-next-line arrow-body-style
-export const transformSchemaToAPI = (schema: PluginSchema, pluginName: string) => {
+export const transformSchemaToAPI = (schema: PluginForm.PluginSchema, pluginName: string) => {
return { schema, pluginName };
};
@@ -45,9 +45,9 @@ export const transformSchemaToAPI = (schema: PluginSchema, pluginName: string) =
* Transform schema's property to rules.
*/
export const transformPropertyToRules = (
- schema: PluginSchema,
+ schema: PluginForm.PluginSchema,
propertyName: string,
- propertyValue: PluginProperty,
+ propertyValue: PluginForm.PluginProperty,
): Rule[] => {
if (!schema) {
return [];
diff --git a/src/components/PluginForm/typingd.d.ts b/src/components/PluginForm/typingd.d.ts
new file mode 100644
index 0000000..72a872f
--- /dev/null
+++ b/src/components/PluginForm/typingd.d.ts
@@ -0,0 +1,50 @@
+declare namespace PluginForm {
+ interface Props {
+ name?: string;
+ initialData?: PluginSchema;
+ onFinish(values: any): void;
+ }
+
+ interface PluginSchema {
+ type: 'object';
+ id?: string;
+ required?: string[];
+ additionalProperties?: boolean;
+ minProperties?: number;
+ oneOf?: Array<{
+ required: string[];
+ }>;
+ properties?: {
+ [propertyName: string]: PluginProperty;
+ };
+ }
+
+ interface PluginProperty {
+ type: 'number' | 'string' | 'integer' | 'array' | 'boolean' | 'object';
+ // the same as type
+ default?: any;
+ description?: string;
+ // NOTE: maybe 0.00001
+ minimum?: number;
+ maximum?: number;
+ minLength?: number;
+ maxLength?: number;
+ minItems?: number;
+ maxItems?: number;
+ // e.g "^/.*"
+ pattern?: string;
+ enum?: string[];
+ requried?: string[];
+ minProperties?: number;
+ additionalProperties?: boolean;
+ items?: {
+ type: string;
+ anyOf?: Array<{
+ type?: string;
+ description?: string;
+ enum?: string[];
+ pattern?: string;
+ }>;
+ };
+ }
+}
diff --git a/src/pages/Routes/Create.tsx b/src/pages/Routes/Create.tsx
index dc5ff9e..d4f9e3c 100644
--- a/src/pages/Routes/Create.tsx
+++ b/src/pages/Routes/Create.tsx
@@ -3,6 +3,7 @@ import { Card, Steps } from 'antd';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import Step1 from './components/Step1';
import styles from './Create.less';
+import CreateStep3 from './components/CreateStep3';
const { Step } = Steps;
@@ -32,16 +33,19 @@ const Create: React.FC = () => {
};
const renderStep = () => {
- return (
- <>
- {Boolean(currentStep === 0) && (
+ switch (currentStep) {
+ case 0:
+ return (
<Step1
data={data}
onChange={(params: RoutesModule.Step1DataProps) => handleChange(currentStep, params)}
/>
- )}
- </>
- );
+ );
+ case 2:
+ return <CreateStep3 />;
+ default:
+ return null;
+ }
};
return (
diff --git a/src/pages/Routes/components/CreateStep3/CreateStep3.tsx b/src/pages/Routes/components/CreateStep3/CreateStep3.tsx
new file mode 100644
index 0000000..38647b3
--- /dev/null
+++ b/src/pages/Routes/components/CreateStep3/CreateStep3.tsx
@@ -0,0 +1,51 @@
+import React, { useState, useEffect } from 'react';
+import { Card } from 'antd';
+import { SettingOutlined } from '@ant-design/icons';
+
+import { fetchPluginList } from '@/components/PluginForm';
+import PanelSection from '../PanelSection';
+import PluginDrawer from './PluginDrawer';
+import PluginCard from './PluginCard';
+
+const sectionStyle = {
+ display: 'grid',
+ gridTemplateColumns: 'repeat(3, 33.333%)',
+ gridRowGap: 10,
+};
+
+const CreateStep3: React.FC = () => {
+ const [pluginList, setPluginList] = useState<string[]>([]);
+ const [currentPlugin, setCurrentPlugin] = useState<string | undefined>();
+
+ useEffect(() => {
+ fetchPluginList().then((data) => setPluginList(data));
+ }, []);
+
+ // TODO: 设置插件黑名单(不展示的插件)
+ // TODO: 获取 Route 已启用插件
+ // TODO: 拆分已启用、未启用插件
+
+ return (
+ <>
+ <PanelSection title="已启用" style={sectionStyle}>
+ <Card
+ style={{ width: 300 }}
+ actions={[<SettingOutlined onClick={() => setCurrentPlugin('')} />]}
+ >
+ <Card.Meta title="插件名称" description="插件描述" />
+ </Card>
+ </PanelSection>
+ <PanelSection title="未启用" style={sectionStyle}>
+ {pluginList.map((name) => (
+ <PluginCard
+ name={name}
+ actions={[<SettingOutlined onClick={() => setCurrentPlugin(name)} />]}
+ />
+ ))}
+ </PanelSection>
+ <PluginDrawer name={currentPlugin} onFinish={() => {}} />
+ </>
+ );
+};
+
+export default CreateStep3;
diff --git a/src/pages/Routes/components/CreateStep3/PluginCard.tsx b/src/pages/Routes/components/CreateStep3/PluginCard.tsx
new file mode 100644
index 0000000..7dd1160
--- /dev/null
+++ b/src/pages/Routes/components/CreateStep3/PluginCard.tsx
@@ -0,0 +1,17 @@
+import React from 'react';
+import { Card } from 'antd';
+import { CardProps } from 'antd/lib/card';
+
+interface Props extends CardProps {
+ name: string;
+}
+
+const PluginCard: React.FC<Props> = ({ name, actions }) => {
+ return (
+ <Card style={{ width: 300 }} actions={actions}>
+ <Card.Meta title={name} description="插件描述" />
+ </Card>
+ );
+};
+
+export default PluginCard;
diff --git a/src/pages/Routes/components/CreateStep3/PluginDrawer.tsx b/src/pages/Routes/components/CreateStep3/PluginDrawer.tsx
new file mode 100644
index 0000000..18a6e7a
--- /dev/null
+++ b/src/pages/Routes/components/CreateStep3/PluginDrawer.tsx
@@ -0,0 +1,44 @@
+import React, { useState, useEffect } from 'react';
+import { Drawer, Button } from 'antd';
+
+import PluginForm from '@/components/PluginForm';
+
+interface Props extends PluginForm.Props {}
+
+const PluginDrawer: React.FC<Props> = ({ name, ...rest }) => {
+ const [visiable, setVisiable] = useState(false);
+
+ useEffect(() => {
+ setVisiable(Boolean(name));
+ }, [name]);
+
+ if (!name) {
+ return null;
+ }
+
+ return (
+ <Drawer
+ title="配置插件"
+ width={400}
+ visible={visiable}
+ destroyOnClose
+ footer={
+ <div
+ style={{
+ textAlign: 'right',
+ }}
+ >
+ <Button style={{ marginRight: 8 }} onClick={() => setVisiable(false)}>
+ 取消
+ </Button>
+ <Button type="primary">提交</Button>
+ </div>
+ }
+ onClose={() => setVisiable(false)}
+ >
+ <PluginForm name={name!} {...rest} />
+ </Drawer>
+ );
+};
+
+export default PluginDrawer;
diff --git a/src/pages/Routes/components/CreateStep3/index.ts b/src/pages/Routes/components/CreateStep3/index.ts
new file mode 100644
index 0000000..bb56257
--- /dev/null
+++ b/src/pages/Routes/components/CreateStep3/index.ts
@@ -0,0 +1 @@
+export { default } from './CreateStep3';
diff --git a/src/pages/Routes/components/PanelSection/index.tsx b/src/pages/Routes/components/PanelSection/index.tsx
index 93c8f04..153942c 100644
--- a/src/pages/Routes/components/PanelSection/index.tsx
+++ b/src/pages/Routes/components/PanelSection/index.tsx
@@ -1,14 +1,14 @@
-import React from 'react';
+import React, { CSSProperties } from 'react';
import { Divider } from 'antd';
const PanelSection: React.FC<{
title: string;
-}> = ({ title, children }) => {
+ style?: CSSProperties;
+}> = ({ title, style, children }) => {
return (
<>
- <div>{title}</div>
- <Divider />
- {children}
+ <Divider orientation="left">{title}</Divider>
+ <div style={style}>{children}</div>
</>
);
};
diff --git a/src/services/plugin.ts b/src/services/plugin.ts
deleted file mode 100644
index 6044df0..0000000
--- a/src/services/plugin.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import { request } from 'umi';
-import { transformSchemaFromAPI } from '@/transforms/plugin';
-
-export const fetchList = () => request('/api/plugins/list');
-
-export const fetchPluginSchema = (name: string): Promise<PluginSchema> =>
- request(`/api/schema/plugins/${name}`).then((data) => transformSchemaFromAPI(data, name));
diff --git a/src/typings.d.ts b/src/typings.d.ts
index b52e470..6a79cb1 100644
--- a/src/typings.d.ts
+++ b/src/typings.d.ts
@@ -38,46 +38,3 @@ declare let ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION: 'site' | undefine
declare const REACT_APP_ENV: 'test' | 'dev' | 'pre' | false;
type PageMode = 'CREATE' | 'EDIT' | 'VIEW';
-
-interface PluginProperty {
- type: 'number' | 'string' | 'integer' | 'array' | 'boolean' | 'object';
- // the same as type
- default?: any;
- description?: string;
- // NOTE: maybe 0.00001
- minimum?: number;
- maximum?: number;
- minLength?: number;
- maxLength?: number;
- minItems?: number;
- maxItems?: number;
- // e.g "^/.*"
- pattern?: string;
- enum?: string[];
- requried?: string[];
- minProperties?: number;
- additionalProperties?: boolean;
- items?: {
- type: string;
- anyOf?: Array<{
- type?: string;
- description?: string;
- enum?: string[];
- pattern?: string;
- }>;
- };
-}
-
-interface PluginSchema {
- type: 'object';
- id?: string;
- required?: string[];
- additionalProperties?: boolean;
- minProperties?: number;
- oneOf?: Array<{
- required: string[];
- }>;
- properties?: {
- [propertyName: string]: PluginProperty;
- };
-}
diff --git a/src/utils/utils.less b/src/utils/utils.less
deleted file mode 100644
index e9dfd5c..0000000
--- a/src/utils/utils.less
+++ /dev/null
@@ -1,16 +0,0 @@
-// mixins for clearfix
-// ------------------------
-.clearfix() {
- zoom: 1;
- &::before,
- &::after {
- display: table;
- content: ' ';
- }
- &::after {
- clear: both;
- height: 0;
- font-size: 0;
- visibility: hidden;
- }
-}
diff --git a/src/utils/utils.test.ts b/src/utils/utils.test.ts
deleted file mode 100644
index c2bb32d..0000000
--- a/src/utils/utils.test.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { isUrl } from './utils';
-
-describe('isUrl tests', (): void => {
- it('should return false for invalid and corner case inputs', (): void => {
- expect(isUrl([] as any)).toBeFalsy();
- expect(isUrl({} as any)).toBeFalsy();
- expect(isUrl(false as any)).toBeFalsy();
- expect(isUrl(true as any)).toBeFalsy();
- expect(isUrl(NaN as any)).toBeFalsy();
- expect(isUrl(null as any)).toBeFalsy();
- expect(isUrl(undefined as any)).toBeFalsy();
- expect(isUrl('')).toBeFalsy();
- });
-
- it('should return false for invalid URLs', (): void => {
- expect(isUrl('foo')).toBeFalsy();
- expect(isUrl('bar')).toBeFalsy();
- expect(isUrl('bar/test')).toBeFalsy();
- expect(isUrl('http:/example.com/')).toBeFalsy();
- expect(isUrl('ttp://example.com/')).toBeFalsy();
- });
-
- it('should return true for valid URLs', (): void => {
- expect(isUrl('http://example.com/')).toBeTruthy();
- expect(isUrl('https://example.com/')).toBeTruthy();
- expect(isUrl('http://example.com/test/123')).toBeTruthy();
- expect(isUrl('https://example.com/test/123')).toBeTruthy();
- expect(isUrl('http://example.com/test/123?foo=bar')).toBeTruthy();
- expect(isUrl('https://example.com/test/123?foo=bar')).toBeTruthy();
- expect(isUrl('http://www.example.com/')).toBeTruthy();
- expect(isUrl('https://www.example.com/')).toBeTruthy();
- expect(isUrl('http://www.example.com/test/123')).toBeTruthy();
- expect(isUrl('https://www.example.com/test/123')).toBeTruthy();
- expect(isUrl('http://www.example.com/test/123?foo=bar')).toBeTruthy();
- expect(isUrl('https://www.example.com/test/123?foo=bar')).toBeTruthy();
- });
-});
diff --git a/src/utils/utils.ts b/src/utils/utils.ts
deleted file mode 100644
index 71ed4ca..0000000
--- a/src/utils/utils.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-import { parse } from 'querystring';
-
-/* eslint no-useless-escape:0 import/prefer-default-export:0 */
-const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
-
-export const isUrl = (path: string): boolean => reg.test(path);
-
-export const isAntDesignPro = (): boolean => {
- if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
- return true;
- }
- return window.location.hostname === 'preview.pro.ant.design';
-};
-
-// 给官方演示站点用,用于关闭真实开发环境不需要使用的特性
-export const isAntDesignProOrDev = (): boolean => {
- const { NODE_ENV } = process.env;
- if (NODE_ENV === 'development') {
- return true;
- }
- return isAntDesignPro();
-};
-
-export const getPageQuery = () => parse(window.location.href.split('?')[1]);