Browse Source

refactor: webhook handler

Ahmad Kholid 3 years ago
parent
commit
b4368eb08c

+ 15 - 27
src/background/workflow-engine/blocks-handler/handler-webhook.js

@@ -1,37 +1,25 @@
 import { getBlockConnection } from '../helper';
+import { isWhitespace } from '@/utils/helper';
 import { executeWebhook } from '@/utils/webhookUtil';
 
-function webhook({ data, outputs }) {
-  return new Promise((resolve, reject) => {
-    const nextBlockId = getBlockConnection({ outputs });
+export async function webhook({ data, outputs }) {
+  const nextBlockId = getBlockConnection({ outputs });
 
-    if (!data.url) {
-      const error = new Error('URL is empty');
-      error.nextBlockId = nextBlockId;
+  try {
+    if (isWhitespace(data.url)) throw new Error('url-empty');
+    if (!data.url.startsWith('http')) throw new Error('invalid-url');
 
-      reject(error);
-      return;
-    }
+    await executeWebhook(data);
 
-    if (!data.url.startsWith('http')) {
-      const error = new Error('URL is not valid');
-      error.nextBlockId = nextBlockId;
+    return {
+      data: '',
+      nextBlockId,
+    };
+  } catch (error) {
+    error.nextBlockId = nextBlockId;
 
-      reject(error);
-      return;
-    }
-
-    executeWebhook(data)
-      .then(() => {
-        resolve({
-          data: '',
-          nextBlockId: getBlockConnection({ outputs }),
-        });
-      })
-      .catch((error) => {
-        reject(error);
-      });
-  });
+    throw error;
+  }
 }
 
 export default webhook;

+ 3 - 0
src/locales/en/newtab.json

@@ -84,8 +84,11 @@
       "finish": "Finish"
     },
     "messages": {
+      "url-empty": "URL is empty",
+      "invalid-url": "URL is not valid",
       "conditions-empty": "Conditions is empty",
       "invalid-proxy-host": "Invalid proxy host",
+      "invalid-body": "Content body is not valid",
       "workflow-disabled": "Workflow is disabled",
       "selector-empty": "Element selector is empty",
       "empty-workflow": "You must select a workflow first",

+ 1 - 0
src/utils/reference-data.js

@@ -30,6 +30,7 @@ export function parseKey(key) {
   return { dataKey: 'dataColumns', path: dataPath };
 }
 export function replaceMustacheHandler(match, data) {
+  console.log(match, data);
   const key = match.slice(2, -2).replace(/\s/g, '');
 
   if (!key) return '';

+ 1 - 1
src/utils/shared.js

@@ -461,7 +461,7 @@ export const tasks = {
       contentType: 'json',
       timeout: 10000,
       headers: [{ name: '', value: '' }],
-      body: '{\n "key": {{ dataColumns@0.key }} \n}',
+      body: '{\n "key": "{{ dataColumns@0.key }}" \n}',
     },
   },
   'loop-data': {

+ 4 - 2
src/utils/webhookUtil.js

@@ -1,10 +1,12 @@
-import { isObject } from './helper';
+import { isObject, parseJSON } from './helper';
 
 const renderContent = (content, contentType) => {
   // 1. render the content
   // 2. if the content type is json then parse the json
   // 3. else parse to form data
-  const renderedJson = JSON.parse(content);
+  const renderedJson = parseJSON(content, new Error('invalid-body'));
+
+  if (renderedJson instanceof Error) throw renderedJson;
 
   if (contentType === 'form') {
     return Object.keys(renderedJson)