From bc5f109308bb2908edbe62d25314f0ef12746d32 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 9 Aug 2024 10:53:59 +0800 Subject: [PATCH] feat: http support body binary --- .../nodes/_base/components/variable/utils.ts | 9 +++---- .../nodes/http/components/edit-body/index.tsx | 24 ++++++++++++++++++- .../nodes/http/components/key-value/index.tsx | 2 +- .../components/workflow/nodes/http/default.ts | 3 +++ .../components/workflow/nodes/http/types.ts | 3 ++- web/i18n/en-US/workflow.ts | 1 + web/i18n/zh-Hans/workflow.ts | 1 + 7 files changed, 36 insertions(+), 7 deletions(-) diff --git a/web/app/components/workflow/nodes/_base/components/variable/utils.ts b/web/app/components/workflow/nodes/_base/components/variable/utils.ts index f5f6014ff0..d5b337cec7 100644 --- a/web/app/components/workflow/nodes/_base/components/variable/utils.ts +++ b/web/app/components/workflow/nodes/_base/components/variable/utils.ts @@ -39,10 +39,11 @@ export const isENV = (valueSelector: ValueSelector) => { } const inputVarTypeToVarType = (type: InputVarType): VarType => { - if (type === InputVarType.number) - return VarType.number - - return VarType.string + return ({ + [InputVarType.number]: VarType.number, + [InputVarType.singleFile]: VarType.file, + [InputVarType.multiFiles]: VarType.arrayFile, + } as any)[type] || VarType.string } const findExceptVarInObject = (obj: any, filterVar: (payload: Var, selector: ValueSelector) => boolean, value_selector: ValueSelector): Var => { diff --git a/web/app/components/workflow/nodes/http/components/edit-body/index.tsx b/web/app/components/workflow/nodes/http/components/edit-body/index.tsx index 0417c408cc..05ff070c2c 100644 --- a/web/app/components/workflow/nodes/http/components/edit-body/index.tsx +++ b/web/app/components/workflow/nodes/http/components/edit-body/index.tsx @@ -7,9 +7,10 @@ import { BodyType } from '../../types' import useKeyValueList from '../../hooks/use-key-value-list' import KeyValue from '../key-value' import useAvailableVarList from '../../../_base/hooks/use-available-var-list' +import VarReferencePicker from '../../../_base/components/variable/var-reference-picker' import cn from '@/utils/classnames' import InputWithVar from '@/app/components/workflow/nodes/_base/components/prompt/editor' -import type { Var } from '@/app/components/workflow/types' +import type { ValueSelector, Var } from '@/app/components/workflow/types' import { VarType } from '@/app/components/workflow/types' type Props = { @@ -77,6 +78,10 @@ const EditBody: FC = ({ onChange(newBody) }, type === BodyType.json) + const filterOnlyFileVariable = (varPayload: Var) => { + return varPayload.type === VarType.file + } + useEffect(() => { if (!isCurrentKeyValue) return @@ -99,6 +104,13 @@ const EditBody: FC = ({ onChange(newBody) }, [onChange, payload]) + const handleBinaryFileChange = useCallback((value: ValueSelector | string) => { + const newBody = produce(payload, (draft: Body) => { + draft.binaryFileVariable = value as ValueSelector + }) + onChange(newBody) + }, [onChange, payload]) + return (
{/* body type */} @@ -156,6 +168,16 @@ const EditBody: FC = ({ readOnly={readonly} /> )} + + {type === BodyType.binary && ( + + )}
) diff --git a/web/app/components/workflow/nodes/http/components/key-value/index.tsx b/web/app/components/workflow/nodes/http/components/key-value/index.tsx index b14f7d9dd1..e930114f32 100644 --- a/web/app/components/workflow/nodes/http/components/key-value/index.tsx +++ b/web/app/components/workflow/nodes/http/components/key-value/index.tsx @@ -10,7 +10,7 @@ type Props = { list: KeyValue[] onChange: (newList: KeyValue[]) => void onAdd: () => void - isSupportFile: boolean + isSupportFile?: boolean // toggleKeyValueEdit: () => void } diff --git a/web/app/components/workflow/nodes/http/default.ts b/web/app/components/workflow/nodes/http/default.ts index 4e797ed14a..71ed7478a0 100644 --- a/web/app/components/workflow/nodes/http/default.ts +++ b/web/app/components/workflow/nodes/http/default.ts @@ -40,6 +40,9 @@ const nodeDefault: NodeDefault = { if (!errorMessages && !payload.url) errorMessages = t('workflow.errorMsg.fieldRequired', { field: t('workflow.nodes.http.api') }) + if (!errorMessages && payload.body.type === BodyType.binary && !payload.body.binaryFileVariable) + errorMessages = t('workflow.errorMsg.fieldRequired', { field: t('workflow.nodes.http.binaryFileVariable') }) + return { isValid: !errorMessages, errorMessage: errorMessages, diff --git a/web/app/components/workflow/nodes/http/types.ts b/web/app/components/workflow/nodes/http/types.ts index 0495a74b95..ecdd29542d 100644 --- a/web/app/components/workflow/nodes/http/types.ts +++ b/web/app/components/workflow/nodes/http/types.ts @@ -1,4 +1,4 @@ -import type { CommonNodeType, Variable } from '@/app/components/workflow/types' +import type { CommonNodeType, ValueSelector, Variable } from '@/app/components/workflow/types' export enum Method { get = 'get', @@ -27,6 +27,7 @@ export type KeyValue = { export type Body = { type: BodyType data: string + binaryFileVariable?: ValueSelector } export enum AuthorizationType { diff --git a/web/i18n/en-US/workflow.ts b/web/i18n/en-US/workflow.ts index 01aea204b1..b510b5f889 100644 --- a/web/i18n/en-US/workflow.ts +++ b/web/i18n/en-US/workflow.ts @@ -323,6 +323,7 @@ const translation = { headers: 'Headers', params: 'Params', body: 'Body', + binaryFileVariable: 'Binary File Variable', outputVars: { body: 'Response Content', statusCode: 'Response Status Code', diff --git a/web/i18n/zh-Hans/workflow.ts b/web/i18n/zh-Hans/workflow.ts index eb28b5cb6f..db496a03ea 100644 --- a/web/i18n/zh-Hans/workflow.ts +++ b/web/i18n/zh-Hans/workflow.ts @@ -323,6 +323,7 @@ const translation = { headers: 'Headers', params: 'Params', body: 'Body', + binaryFileVariable: 'Binary 文件变量', outputVars: { body: '响应内容', statusCode: '响应状态码',