-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathWebhookCurl.vue
More file actions
146 lines (124 loc) · 4.05 KB
/
Copy pathWebhookCurl.vue
File metadata and controls
146 lines (124 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<template>
<div class="webhook-curl">
<div v-if="webhookTriggers.length > 0">
<KsFormItem :label="$t('webhook.payload')" class="payload">
<KsEditor
v-bind="editorBindings"
:options="{fullHeight: false, showScroll: true}"
:inline="true"
:navbar="false"
lang="json"
v-model="webhookPayload"
/>
</KsFormItem>
<div v-for="trigger in webhookTriggers" :key="trigger.id" class="trigger">
<div class="code">
<pre><code>{{ generateWebhookCurlCommand(trigger) }}</code></pre>
<CopyToClipboard :text="generateWebhookCurlCommand(trigger)" class="copy" />
</div>
</div>
<KsAlert type="info" :closable="false">
{{ $t('webhook.curl_note') }}
</KsAlert>
</div>
<div v-else>
<KsAlert type="warning" :closable="false">
{{ $t('webhook.no_triggers') }}
</KsAlert>
</div>
</div>
</template>
<script setup lang="ts">
import {computed, onMounted, ref} from "vue"
import CopyToClipboard from "../layout/CopyToClipboard.vue"
import {KsEditor} from "@kestra-io/design-system"
import {useEditorBindings} from "../../composables/useEditorBindings"
import {webhookUrl, WEBHOOK_TRIGGER_TYPE} from "../../utils/webhook"
import {useFlowStore} from "../../stores/flow"
interface Flow {
namespace: string;
id: string;
triggers?: Trigger[];
}
interface Trigger {
id: string;
type: string;
key?: string;
disabled?: boolean;
}
const props = defineProps<{
flow: Flow;
}>()
const editorBindings = useEditorBindings()
const webhookPayload = ref("{\"key1\":\"value1\",\"key2\":\"value2\"}")
const flowStore = useFlowStore()
const webhookTriggers = computed(() => {
const sourceFlow = flowStore.flow || props.flow
if (!sourceFlow?.triggers) {
return []
}
return sourceFlow.triggers.filter((trigger: Trigger) =>
trigger.type === WEBHOOK_TRIGGER_TYPE &&
(trigger.disabled === undefined || trigger.disabled === false),
)
})
const generateWebhookCurlCommand = (trigger: Trigger): string => {
if (!trigger.key) {
return "Webhook key not available"
}
const url = webhookUrl({namespace: props.flow.namespace, id: props.flow.id, key: trigger.key})
const command = [`curl -X POST ${url}`]
command.push("-H \"Content-Type: application/json\"")
if (webhookPayload.value.trim()) {
command.push(`-d '${webhookPayload.value}'`)
}
return toShell(command)
}
const toShell = (command: string[]): string => {
return command.join(" \\\n ")
}
onMounted(async () => {
if (props.flow?.namespace && props.flow?.id) {
try {
await flowStore.loadFlow({
namespace: props.flow.namespace,
id: props.flow.id,
})
} catch (error) {
// oxlint-disable-next-line preserve-caught-error
throw new Error(`Failed to load flow: ${error}`)
}
}
})
</script>
<style scoped lang="scss">
.webhook-curl {
position: relative;
border: 1px solid var(--ks-border-default);
padding: 1rem;
border-radius: 0.5rem;
.payload {
margin-bottom: 1rem;
:deep(.kel-form-item__label) {
font-size: var(--ks-font-size-sm);
color: var(--ks-text-secondary);
}
:deep(.editor-container) {
height: 50px;
max-height: 120px;
}
}
.code {
position: relative;
pre {
overflow-x: auto;
}
}
.copy {
position: absolute;
top: 8px;
right: 8px;
z-index: 10;
}
}
</style>