api-client/components/http/CodegenModal.vue

131 lines
3.4 KiB
Vue
Raw Normal View History

<template>
<SmartModal
v-if="show"
2021-08-26 07:32:53 +00:00
:title="t('request.generate_code')"
@close="hideModal"
>
<template #body>
2021-07-17 17:40:28 +00:00
<div class="flex flex-col px-2">
<label for="requestType" class="px-4 pb-4">
2021-08-04 05:50:23 +00:00
{{ $t("request.choose_language") }}
2021-07-09 17:19:45 +00:00
</label>
<tippy ref="options" interactive trigger="click" theme="popover" arrow>
<template #trigger>
<span class="select-wrapper">
<ButtonSecondary
:label="codegens.find((x) => x.id === codegenType).name"
outline
class="flex-1 pr-8"
/>
</span>
</template>
<SmartItem
v-for="(gen, index) in codegens"
:key="`gen-${index}`"
:label="gen.name"
:info-icon="gen.id === codegenType ? 'done' : ''"
:active-info-icon="gen.id === codegenType"
@click.native="
2021-08-26 05:32:05 +00:00
() => {
codegenType = gen.id
options.tippy().hide()
}
"
/>
</tippy>
2021-07-17 17:40:28 +00:00
<div class="flex flex-1 justify-between">
<label for="generatedCode" class="px-4 pt-4 pb-4">
2021-08-26 07:32:53 +00:00
{{ t("request.generated_code") }}
2021-07-09 17:19:45 +00:00
</label>
2020-12-11 10:29:03 +00:00
</div>
<div
v-if="codegenType"
2021-07-09 17:19:45 +00:00
ref="generatedCode"
class="w-full border border-dividerLight rounded block"
></div>
2020-12-11 10:29:03 +00:00
</div>
</template>
<template #footer>
<ButtonPrimary
ref="copyRequestCode"
2021-08-26 07:32:53 +00:00
:label="t('action.copy')"
2021-08-28 00:17:33 +00:00
:svg="copyIcon"
@click.native="copyRequestCode"
/>
2021-08-26 07:32:53 +00:00
<ButtonSecondary :label="t('action.dismiss')" @click.native="hideModal" />
</template>
</SmartModal>
</template>
2021-08-26 05:32:05 +00:00
<script setup lang="ts">
import { computed, ref, useContext, watch } from "@nuxtjs/composition-api"
import { codegens, generateCodegenContext } from "~/helpers/codegen/codegen"
import { useCodemirror } from "~/helpers/editor/codemirror"
2021-08-26 05:32:05 +00:00
import { copyToClipboard } from "~/helpers/utils/clipboard"
2021-07-18 04:33:52 +00:00
import { getEffectiveRESTRequest } from "~/helpers/utils/EffectiveURL"
import { getCurrentEnvironment } from "~/newstore/environments"
2021-08-26 05:32:05 +00:00
import { getRESTRequest } from "~/newstore/RESTSession"
2021-08-26 05:32:05 +00:00
const props = defineProps<{
show: boolean
}>()
const emit = defineEmits<{
(e: "hide-modal"): void
}>()
const {
$toast,
app: { i18n },
} = useContext()
2021-08-26 07:32:53 +00:00
const t = i18n.t.bind(i18n)
2021-08-26 05:32:05 +00:00
const options = ref<any | null>(null)
2021-08-26 05:32:05 +00:00
const request = ref(getRESTRequest())
const codegenType = ref("curl")
2021-08-28 00:17:33 +00:00
const copyIcon = ref("copy")
2021-08-26 05:32:05 +00:00
const requestCode = computed(() => {
const effectiveRequest = getEffectiveRESTRequest(
request.value as any,
getCurrentEnvironment()
)
return codegens
.find((x) => x.id === codegenType.value)!
.generator(generateCodegenContext(effectiveRequest))
2021-07-18 04:33:52 +00:00
})
2021-08-26 05:32:05 +00:00
const generatedCode = ref<any | null>(null)
useCodemirror(generatedCode, requestCode, {
extendedEditorConfig: {
mode: "text/plain",
readOnly: true,
},
linter: null,
completer: null,
})
2021-08-26 05:32:05 +00:00
watch(
() => props.show,
(goingToShow) => {
if (goingToShow) {
request.value = getRESTRequest()
}
}
)
const hideModal = () => emit("hide-modal")
const copyRequestCode = () => {
copyToClipboard(requestCode.value)
2021-08-28 00:17:33 +00:00
copyIcon.value = "check"
2021-08-26 07:32:53 +00:00
$toast.success(t("state.copied_to_clipboard").toString(), {
2021-08-26 05:32:05 +00:00
icon: "content_paste",
})
2021-08-28 00:17:33 +00:00
setTimeout(() => (copyIcon.value = "copy"), 1000)
2021-08-26 05:32:05 +00:00
}
</script>