From 95c750db15afd34f127627bb877907102f06a1c8 Mon Sep 17 00:00:00 2001 From: SemmyWong <67748948+semmywong@users.noreply.github.com> Date: Sun, 19 Jun 2022 14:06:31 +0800 Subject: [PATCH] fix(custom-request): support string/json templates (#514) * fix: request api support dynamic custom * fix: scope undefined will not work * fix: use formily compile * fix: user may be null * fix: export add context --- .../client/src/block-provider/hooks/index.ts | 25 ++++++++++++++++--- .../src/schema-settings/SchemaSettings.tsx | 3 +++ packages/core/utils/src/index.ts | 1 - .../export/src/server/actions/export-xlsx.ts | 1 + 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/packages/core/client/src/block-provider/hooks/index.ts b/packages/core/client/src/block-provider/hooks/index.ts index 65c5755b75..eede036b75 100644 --- a/packages/core/client/src/block-provider/hooks/index.ts +++ b/packages/core/client/src/block-provider/hooks/index.ts @@ -1,5 +1,7 @@ +import { Schema as SchemaCompiler } from '@formily/json-schema'; import { useField, useFieldSchema, useForm } from '@formily/react'; import { message, Modal } from 'antd'; +import get from 'lodash/get'; import { useTranslation } from 'react-i18next'; import { useHistory } from 'react-router-dom'; import { useAPIClient } from '../../api-client'; @@ -20,6 +22,13 @@ export const usePickActionProps = () => { }; }; +function renderTemplate(str: string, data: any) { + const re = /\{\{\s*((\w+\.?)+)\s*\}\}/g; + return str.replace(re, function (_, key) { + return get(data, key) || ''; + }); +} + function isURL(string) { let url; @@ -215,6 +224,9 @@ export const useCustomizeRequestActionProps = () => { const form = useForm(); const { fields, getField } = useCollection(); const { field, resource } = useBlockRequestContext(); + const currentRecord = useRecord(); + const currentUserContext = useCurrentUserContext(); + const currentUser = currentUserContext?.data?.data; return { async onClick() { const { skipValidator, onSuccess, requestSettings } = actionSchema?.['x-action-settings'] ?? {}; @@ -234,11 +246,16 @@ export const useCustomizeRequestActionProps = () => { const values = getFormValues(filterByTk, field, form, fieldNames, getField, resource); Object.assign(data, values); } + const requestBody = { + url: renderTemplate(requestSettings['url'], { currentRecord, currentUser }), + method: requestSettings['method'], + headers: SchemaCompiler.compile(headers, { currentRecord, currentUser }), + params: SchemaCompiler.compile(params, { currentRecord, currentUser }), + data: SchemaCompiler.compile(data, { currentRecord, currentUser }), + }; + await apiClient.request({ - ...requestSettings, - headers, - params, - data, + ...requestBody, }); if (!onSuccess?.successMessage) { diff --git a/packages/core/client/src/schema-settings/SchemaSettings.tsx b/packages/core/client/src/schema-settings/SchemaSettings.tsx index 334134ba39..72e5b961cd 100644 --- a/packages/core/client/src/schema-settings/SchemaSettings.tsx +++ b/packages/core/client/src/schema-settings/SchemaSettings.tsx @@ -520,6 +520,9 @@ SchemaSettings.ActionModalItem = React.memo((props: any) => { onClick={(e) => { e.stopPropagation(); }} + onKeyDown={(e) => { + e.stopPropagation(); + }} > collection.fields.get(col.dataIndex[0])); const { rows, ranges } = await render({ columns, fields: collectionFields, data }, ctx);