From 3115134f3ab568561ce797a87f6c6a23ce691b5d Mon Sep 17 00:00:00 2001 From: Junyi Date: Sat, 4 Feb 2023 16:03:25 +0800 Subject: [PATCH] fix(plugin-workflow): use promise to request (#1426) --- .../src/server/instructions/request.ts | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/packages/plugins/workflow/src/server/instructions/request.ts b/packages/plugins/workflow/src/server/instructions/request.ts index 0cd5ea334..a11417cd2 100644 --- a/packages/plugins/workflow/src/server/instructions/request.ts +++ b/packages/plugins/workflow/src/server/instructions/request.ts @@ -15,62 +15,65 @@ export type RequestConfig = Pick { + if (header.name.toLowerCase() === 'content-type') { + return result; + } + return Object.assign(result, { [header.name]: header.value }); + }, {}); + const params = (config.params ?? []).reduce((result, param) => Object.assign(result, { [param.name]: param.value }), {}); + + // TODO(feat): only support JSON type for now, should support others in future + headers['Content-Type'] = 'application/json'; + + return axios.request({ + url, + method, + headers, + params, + data, + timeout, + }); +}; + export default class implements Instruction { constructor(public plugin) {} - request = async (node: FlowNodeModel, job, processor: Processor) => { - const config = processor.getParsedValue(node.config) as RequestConfig; - - // default headers - const { url, method = 'POST', data, timeout = 5000 } = config; - const headers = (config.headers ?? []).reduce((result, header) => { - if (header.name.toLowerCase() === 'content-type') { - return result; - } - return Object.assign(result, { [header.name]: header.value }); - }, {}); - const params = (config.params ?? []).reduce((result, param) => Object.assign(result, { [param.name]: param.value }), {}); - - // TODO(feat): only support JSON type for now, should support others in future - headers['Content-Type'] = 'application/json'; - - try { - const response = await axios.request({ - url, - method, - headers, - params, - data, - timeout, - }); - job.set({ - status: JOB_STATUS.RESOLVED, - result: response.data - }); - } catch (error) { - job.set({ - status: JOB_STATUS.REJECTED, - result: error.isAxiosError ? error.toJSON() : error.message - }); - } - - return this.plugin.resume(job); - }; - - async run(node, input, processor) { + async run(node: FlowNodeModel, input, processor: Processor) { const job = await processor.saveJob({ status: JOB_STATUS.PENDING, nodeId: node.id, }); - setTimeout(() => { - this.request(node, job, processor); - }); + const config = processor.getParsedValue(node.config) as RequestConfig; + + request(config) + .then(response => { + job.set({ + status: JOB_STATUS.RESOLVED, + result: response.data + }); + }) + .catch(error => { + job.set({ + status: JOB_STATUS.REJECTED, + result: error.isAxiosError ? error.toJSON() : error.message + }); + }) + .finally(() => { + this.plugin.app.logger.info(`[Workflow] request (#${node.id}) response received, status: ${job.get('status')}`); + this.plugin.resume(job); + }); + + this.plugin.app.logger.info(`[Workflow] request (#${node.id}) sent to "${config.url}", waiting for response...`); return job; } - async resume(node, job, processor) { + async resume(node: FlowNodeModel, job, processor: Processor) { const { ignoreFail } = node.config as RequestConfig; if (ignoreFail) { job.set('status', JOB_STATUS.RESOLVED);