From adea8ff1b484e8653ae189312775cd0f34e321dd Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Fri, 28 Jul 2023 15:31:01 +0200 Subject: [PATCH] fix: improve webhooks panel correctness --- .../components/details/WebhooksPanel.svelte | 218 ++++++++++-------- 1 file changed, 120 insertions(+), 98 deletions(-) diff --git a/frontend/src/lib/components/details/WebhooksPanel.svelte b/frontend/src/lib/components/details/WebhooksPanel.svelte index 46677b140e..27a5f5ddda 100644 --- a/frontend/src/lib/components/details/WebhooksPanel.svelte +++ b/frontend/src/lib/components/details/WebhooksPanel.svelte @@ -44,21 +44,42 @@ requestType = 'hash' } - $: url = webhooks[webhookType][requestType] - let token = 'YOUR_TOKEN' - $: codeContent = `${ - requestType !== 'get_path' - ? 'const body = JSON.stringify(' + JSON.stringify(args, null, 2) + ');' - : '' + $: url = + webhooks[webhookType][requestType] + + (tokenType === 'query' + ? `?token=${token}${ + requestType === 'get_path' + ? `&payload=${encodeURIComponent(btoa(JSON.stringify(args)))}` + : '' + }` + : `${ + requestType === 'get_path' + ? `?payload=${encodeURIComponent(btoa(JSON.stringify(args)))}` + : '' + }`) + + function headers() { + const headers = {} + if (requestType != 'get_path') { + headers['Content-Type'] = 'application/json' + } + + if (tokenType === 'headers') { + headers['Authorization'] = `Bearer ${token}` + } + return headers } -fetch(${tokenType === 'query' ? `\`${url}?token=${token}\`` : `\`${url}\``}, { + function fetchCode() { + return `${ + requestType !== 'get_path' + ? 'let body = JSON.stringify(' + JSON.stringify(args, null, 2) + ');' + : '' + } +fetch(\`${url}\`, { method: '${requestType === 'get_path' ? 'GET' : 'POST'}', - headers: { - 'Content-Type': 'application/json', - ${tokenType === 'headers' ? `'Authorization': 'Bearer ${token}',` : ''} - }, + headers: ${JSON.stringify(headers(), null, 2)}, ${requestType !== 'get_path' ? `body: body` : ''} }).then( response => response.${webhookType === 'sync' ? 'json' : 'text'}() @@ -68,21 +89,55 @@ fetch(${tokenType === 'query' ? `\`${url}?token=${token}\`` : `\`${url}\``}, { ? 'console.log(data)' : `let UUID = data; let checkCompletion = setInterval(() => { - fetch(\`${$page.url.origin}/api/w/${$workspaceStore}/jobs_u/completed/get_result_maybe/\$\{UUID\}\`, { - method: 'GET', - headers: { - 'Authorization': 'Bearer ${token}' - } - }).then(response => response.json()) - .then(data => { - if (data.completed) { - console.log('Job result: ', data); - clearInterval(checkCompletion); - } - }); - }, 1000);` + fetch(\`${$page.url.origin}/api/w/${$workspaceStore}/jobs_u/completed/get_result_maybe/\$\{UUID\}\`, + { + method: 'GET', + headers: { + 'Authorization': 'Bearer ${token}' + } + }).then(response => response.json()) + .then(data => { + if (data.completed) { + console.log(data.result); + clearInterval(checkCompletion); + } + }); +}, 1000);` } });` + } + + function curlCode() { + return `${ + requestType !== 'get_path' + ? `TOKEN='${token}' +BODY='${JSON.stringify(args)}'` + : '' + } +URL='${url}' +${webhookType === 'sync' ? 'RESULT' : 'UUID'}=$(curl -s ${ + requestType != 'get_path' ? "-H 'Content-Type: application/json'" : '' + } ${tokenType === 'headers' ? `-H "Authorization: Bearer $TOKEN"` : ''} -X ${ + requestType === 'get_path' ? 'GET' : 'POST' + } ${requestType !== 'get_path' ? `-d "$BODY" ` : ''}$URL) + +${ + webhookType === 'sync' + ? 'echo $RESULT | jq' + : ` +URL="${$page.url.origin}/api/w/${$workspaceStore}/jobs_u/completed/get_result_maybe/$UUID" +while true; do + RESPONSE=$(curl -s -H "Authorization: Bearer $TOKEN" $URL) + COMPLETED=$(echo $RESPONSE | jq .completed) + if [ "$COMPLETED" = "true" ]; then + echo $RESPONSE | jq .result + break + else + sleep 1 + fi +done` +}` + } @@ -164,90 +219,57 @@ fetch(${tokenType === 'query' ? `\`${url}?token=${token}\`` : `\`${url}\``}, {
- + {#if requestType !== 'get_path'} {/if} - - + {#key requestType} + {#key tokenType} + + {/key} + {/key}
-
- - - +
+ {#key args} + {#key requestType} + {#key webhookType} + {#key tokenType} +
{ + e.preventDefault() + copyToClipboard(curlCode()) + }} + > + + +
+ {/key} + {/key} + {/key} + {/key}
-
{ - e.preventDefault() - copyToClipboard(codeContent) - }} - > - - -
+ {#key args} + {#key requestType} + {#key webhookType} + {#key tokenType} +
{ + e.preventDefault() + copyToClipboard(fetchCode()) + }} + > + + +
+ {/key}{/key}{/key} + {/key}