From a726cab92cb9ab43f844a4be030d24b182e3804a Mon Sep 17 00:00:00 2001 From: Junyi Date: Thu, 5 Sep 2024 18:21:08 +0800 Subject: [PATCH] fix(plugin-workflow-request): fix params processing (#5204) --- .../src/server/RequestInstruction.ts | 9 +++++---- .../src/server/__tests__/instruction.test.ts | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-workflow-request/src/server/RequestInstruction.ts b/packages/plugins/@nocobase/plugin-workflow-request/src/server/RequestInstruction.ts index e7c65f091b..95e0bf0ea6 100644 --- a/packages/plugins/@nocobase/plugin-workflow-request/src/server/RequestInstruction.ts +++ b/packages/plugins/@nocobase/plugin-workflow-request/src/server/RequestInstruction.ts @@ -8,6 +8,7 @@ */ import axios, { AxiosRequestConfig } from 'axios'; +import { trim } from 'lodash'; import { Processor, Instruction, JOB_STATUS, FlowNodeModel } from '@nocobase/plugin-workflow'; @@ -38,14 +39,14 @@ async function request(config) { // default headers const { url, method = 'POST', contentType = 'application/json', data, timeout = 5000 } = config; const headers = (config.headers ?? []).reduce((result, header) => { - const name = header.name?.trim(); + const name = trim(header.name); if (name.toLowerCase() === 'content-type') { return result; } - return Object.assign(result, { [name]: header.value?.trim() }); + return Object.assign(result, { [name]: trim(header.value) }); }, {}); const params = (config.params ?? []).reduce( - (result, param) => Object.assign(result, { [param.name]: param.value?.trim() }), + (result, param) => Object.assign(result, { [param.name]: trim(param.value) }), {}, ); @@ -53,7 +54,7 @@ async function request(config) { headers['Content-Type'] = contentType; return axios.request({ - url: url?.trim(), + url: trim(url), method, headers, params, diff --git a/packages/plugins/@nocobase/plugin-workflow-request/src/server/__tests__/instruction.test.ts b/packages/plugins/@nocobase/plugin-workflow-request/src/server/__tests__/instruction.test.ts index 9761b3c854..4a8540f713 100644 --- a/packages/plugins/@nocobase/plugin-workflow-request/src/server/__tests__/instruction.test.ts +++ b/packages/plugins/@nocobase/plugin-workflow-request/src/server/__tests__/instruction.test.ts @@ -143,6 +143,26 @@ describe('workflow > instructions > request', () => { await app.destroy(); }); + describe('params processing', () => { + it('trim should not crash', async () => { + await workflow.createNode({ + type: 'request', + config: { + url: api.URL_DATA, + method: 'GET', + params: [{ name: 'id', value: '{{$context.data.id}}' }], + } as RequestConfig, + }); + + await PostRepo.create({ values: { title: 't1' } }); + + await sleep(500); + + const [execution] = await workflow.getExecutions(); + expect(execution.status).toBe(EXECUTION_STATUS.RESOLVED); + }); + }); + describe('request static app routes', () => { it('get data (legacy)', async () => { await workflow.createNode({