fix(plugin-workflow): use promise to request (#1426)

This commit is contained in:
Junyi 2023-02-04 16:03:25 +08:00 committed by GitHub
parent a11c4bbe48
commit 3115134f3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,62 +15,65 @@ export type RequestConfig = Pick<AxiosRequestConfig, 'url' | 'method' | 'params'
ignoreFail: boolean;
};
async function request(config) {
// 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';
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);