tachybase_todo/packages/plugins/api-keys/src/client/Configuration/schema.tsx
Dunqing 6cfd586175
feat(plugin-api-keys): support fetch api via api-keys (#2136)
* feat(plugin-api-keys): support fetch api via api-keys

* feat: full support

* fix: missing parseToken in auth

* feat: add created at column

* feat: configure snippet

* fix: remove unused code

* fix: revert

* chore: update deps

* feat: improve role

* fix: avoid create api key without not exist role

* feat: improve select roles

* refactor: when no X-Role is found, roles should not be randomly assigned

* feat: improve code

* feat: improve current role

* fix: revert

* fix: revert apilicent

* fix: revert auth

* feat: improve currentRole logic

* feat: use resourcer.use instead it

* refactor: remove api-keys-auth

* fix: type

* refactor: move jwt to authManager

* refactor: remove unused code

* refactor: remove protected

* Revert "refactor: remove unused code"

This reverts commit 8f81535ab7e9c412bdc4d4bc05abad64ff60ba3f.

* feat: remove unused code

* feat: improve code

* fix: test error

* test: update test

* test: add test cases

* docs: update

* chore: update X-Role

* fix: token's roleName not work

* docs: update usage

* fix: i18n Add APi key

* docs: update capital

* docs: update

* feat: clean

* Update package.json

* Update roles.ts

* fix: api key

---------

Co-authored-by: chenos <chenlinxh@gmail.com>
2023-06-30 11:20:35 +08:00

263 lines
8.3 KiB
TypeScript

import { ISchema, useForm } from '@formily/react';
import { uid } from '@formily/shared';
import { useActionContext, useBlockRequestContext, useRecord } from '@nocobase/client';
import { Alert, Modal, Space, Typography } from 'antd';
import React from 'react';
import { generateNTemplate, useTranslation } from '../locale';
const { useModal } = Modal;
const useCreateAction = () => {
const form = useForm();
const { setVisible } = useActionContext();
const { resource, service } = useBlockRequestContext();
const { t } = useTranslation();
const [modalIns, element] = useModal();
return {
async run() {
await form.submit();
const response = await resource.create({
values: form.values,
});
modalIns.success({
title: t('API key created successfully'),
onOk: () => {
form.reset();
setVisible(false);
},
content: (
<Space direction="vertical">
<Alert
message={t('Make sure to copy your personal access key now as you will not be able to see this again.')}
type="warning"
></Alert>
<Typography.Text copyable>{response.data?.data?.token}</Typography.Text>
</Space>
),
});
service?.refresh();
},
element,
};
};
const useDestroyAction = () => {
const record = useRecord();
const { resource, service } = useBlockRequestContext();
return {
async run() {
await resource.destroy({
filterByTk: record.id,
});
service.refresh();
},
};
};
export const configurationSchema: ISchema = {
type: 'object',
properties: {
configuration: {
type: 'void',
'x-decorator': 'TableBlockProvider',
'x-decorator-props': {
collection: 'apiKeys',
resource: 'apiKeys',
action: 'list',
params: {
pageSize: 20,
appends: ['role'],
sort: ['-createdAt'],
},
rowKey: 'name',
showIndex: true,
},
'x-component': 'CardItem',
properties: {
actions: {
type: 'void',
'x-component': 'ActionBar',
'x-component-props': {
style: {
marginBottom: 'var(--nb-spacing)',
},
},
properties: {
create: {
type: 'void',
'x-action': 'create',
title: generateNTemplate('Add API key'),
'x-component': 'Action',
'x-component-props': {
openMode: 'drawer',
type: 'primary',
},
properties: {
drawer: {
type: 'void',
title: generateNTemplate('Add API key'),
'x-decorator': 'Form',
'x-component': 'Action.Modal',
'x-component-props': {
style: {
maxWidth: '520px',
width: '100%',
},
},
properties: {
name: {
type: 'string',
title: generateNTemplate('Key name'),
required: true,
'x-component': 'CollectionField',
'x-decorator': 'FormItem',
},
role: {
type: 'string',
title: generateNTemplate('Role'),
required: true,
'x-collection-field': 'apiKeys.role',
'x-component': 'CollectionField',
'x-decorator': 'FormItem',
},
expiresIn: {
type: 'string',
title: generateNTemplate('Expiration'),
required: true,
'x-component': 'CollectionField',
'x-decorator': 'FormItem',
default: '30d',
},
footer: {
type: 'void',
'x-component': 'Action.Modal.Footer',
properties: {
cancel: {
title: '{{t("Cancel")}}',
'x-component': 'Action',
'x-component-props': {
useAction: '{{ cm.useCancelAction }}',
},
},
submit: {
title: '{{t("Submit")}}',
'x-component': 'Action',
'x-component-props': {
type: 'primary',
useAction: useCreateAction,
},
},
},
},
},
},
},
'x-align': 'right',
},
},
},
[uid()]: {
type: 'array',
'x-component': 'TableV2',
'x-component-props': {
rowKey: 'id',
useProps: '{{ useTableBlockProps }}',
},
properties: {
column1: {
type: 'void',
'x-decorator': 'TableV2.Column.Decorator',
'x-component': 'TableV2.Column',
title: generateNTemplate('Key name'),
properties: {
name: {
type: 'string',
'x-component': 'CollectionField',
'x-read-pretty': true,
},
},
},
column2: {
type: 'void',
'x-decorator': 'TableV2.Column.Decorator',
'x-component': 'TableV2.Column',
title: generateNTemplate('Role'),
properties: {
role: {
type: 'object',
'x-collection-field': 'apiKeys.role',
'x-component': 'CollectionField',
'x-component-props': {
enableLink: false,
},
'x-read-pretty': true,
},
},
},
column3: {
type: 'void',
'x-decorator': 'TableV2.Column.Decorator',
'x-component': 'TableV2.Column',
title: generateNTemplate('Expiration'),
properties: {
expiresIn: {
type: 'string',
'x-component': 'CollectionField',
'x-read-pretty': true,
},
},
},
column4: {
type: 'void',
'x-decorator': 'TableV2.Column.Decorator',
'x-component': 'TableV2.Column',
title: generateNTemplate('Created at'),
properties: {
createdAt: {
type: 'date',
// 'x-component': 'CollectionField',
'x-component': 'DatePicker',
'x-component-props': {
format: 'YYYY-MM-DD HH:mm:ss',
},
'x-read-pretty': true,
},
},
},
actionColumn: {
type: 'void',
title: '{{ t("Actions") }}',
'x-action-column': 'actions',
'x-decorator': 'TableV2.Column.ActionBar',
'x-component': 'TableV2.Column',
properties: {
columnActions: {
type: 'void',
'x-component': 'Space',
'x-component-props': {
split: '|',
},
properties: {
delete: {
type: 'void',
title: '{{ t("Delete") }}',
'x-component': 'Action.Link',
'x-component-props': {
confirm: {
title: generateNTemplate('Delete API key'),
content: "{{t('Are you sure you want to delete it?')}}",
},
useAction: useDestroyAction,
},
},
},
},
},
},
},
},
},
},
},
};