fix(plugin-workflow): use promise to request (#1426)
This commit is contained in:
parent
a11c4bbe48
commit
3115134f3a
@ -15,12 +15,7 @@ export type RequestConfig = Pick<AxiosRequestConfig, 'url' | 'method' | 'params'
|
||||
ignoreFail: boolean;
|
||||
};
|
||||
|
||||
export default class implements Instruction {
|
||||
constructor(public plugin) {}
|
||||
|
||||
request = async (node: FlowNodeModel, job, processor: Processor) => {
|
||||
const config = processor.getParsedValue(node.config) as RequestConfig;
|
||||
|
||||
async function request(config) {
|
||||
// default headers
|
||||
const { url, method = 'POST', data, timeout = 5000 } = config;
|
||||
const headers = (config.headers ?? []).reduce((result, header) => {
|
||||
@ -34,8 +29,7 @@ export default class implements Instruction {
|
||||
// TODO(feat): only support JSON type for now, should support others in future
|
||||
headers['Content-Type'] = 'application/json';
|
||||
|
||||
try {
|
||||
const response = await axios.request({
|
||||
return axios.request({
|
||||
url,
|
||||
method,
|
||||
headers,
|
||||
@ -43,34 +37,43 @@ export default class implements Instruction {
|
||||
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) {
|
||||
export default class implements Instruction {
|
||||
constructor(public plugin) {}
|
||||
|
||||
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);
|
||||
|
Loading…
Reference in New Issue
Block a user