api-client/packages/hoppscotch-app/components/http/CodegenModal.vue

197 lines
5.2 KiB
Vue
Raw Normal View History

<template>
<SmartModal
v-if="show"
2021-09-18 10:39:58 +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">
{{ 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="
CodegenDefinitions.find((x) => x.name === codegenType).caption
"
outline
class="flex-1 pr-8"
/>
</span>
</template>
<div class="flex flex-col space-y-2">
<div class="sticky top-0">
<input
v-model="searchQuery"
type="search"
autocomplete="off"
class="flex w-full p-4 py-2 !bg-popover input"
:placeholder="`${t('action.search')}`"
/>
</div>
2022-03-01 06:41:53 +00:00
<div class="flex flex-col" role="menu">
<SmartItem
v-for="codegen in filteredCodegenDefinitions"
:key="codegen.name"
:label="codegen.caption"
:info-icon="codegen.name === codegenType ? 'done' : ''"
:active-info-icon="codegen.name === codegenType"
@click.native="
() => {
codegenType = codegen.name
options.tippy().hide()
}
"
/>
</div>
</div>
</tippy>
2021-12-31 14:35:39 +00:00
<div class="flex justify-between flex-1">
<label for="generatedCode" class="p-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
2022-01-01 10:45:21 +00:00
v-if="errorState"
2022-01-31 14:14:40 +00:00
class="w-full px-4 py-2 overflow-auto font-mono text-red-400 whitespace-normal rounded bg-primaryLight"
>
{{ t("error.something_went_wrong") }}
</div>
<div
v-else-if="codegenType"
2021-07-09 17:19:45 +00:00
ref="generatedCode"
2021-12-31 14:35:39 +00:00
class="border rounded border-dividerLight"
></div>
2020-12-11 10:29:03 +00:00
</div>
</template>
<template #footer>
2021-09-11 02:56:54 +00:00
<span class="flex">
<ButtonPrimary
2021-09-18 10:39:58 +00:00
:label="`${t('action.copy')}`"
2021-09-11 02:56:54 +00:00
:svg="copyIcon"
@click.native="copyRequestCode"
/>
<ButtonSecondary
2021-09-18 10:39:58 +00:00
:label="`${t('action.dismiss')}`"
2021-09-11 02:56:54 +00:00
@click.native="hideModal"
/>
</span>
</template>
</SmartModal>
</template>
2021-08-26 05:32:05 +00:00
<script setup lang="ts">
import { computed, ref, watch } from "@nuxtjs/composition-api"
import * as O from "fp-ts/Option"
import { Environment, makeRESTRequest } from "@hoppscotch/data"
import { useCodemirror } from "~/helpers/editor/codemirror"
2021-08-26 05:32:05 +00:00
import { copyToClipboard } from "~/helpers/utils/clipboard"
import {
getEffectiveRESTRequest,
resolvesEnvsInBody,
} from "~/helpers/utils/EffectiveURL"
import { getAggregateEnvs } from "~/newstore/environments"
2021-08-26 05:32:05 +00:00
import { getRESTRequest } from "~/newstore/RESTSession"
import { useI18n, useToast } from "~/helpers/utils/composables"
import {
CodegenDefinitions,
CodegenName,
generateCode,
} from "~/helpers/new-codegen"
const t = useI18n()
2021-08-26 05:32:05 +00:00
const props = defineProps<{
show: boolean
}>()
const emit = defineEmits<{
(e: "hide-modal"): void
}>()
const toast = useToast()
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<CodegenName>("shell-curl")
2021-08-28 00:17:33 +00:00
const copyIcon = ref("copy")
2022-01-01 10:45:21 +00:00
const errorState = ref(false)
2021-08-26 05:32:05 +00:00
const requestCode = computed(() => {
2022-01-03 05:58:49 +00:00
const aggregateEnvs = getAggregateEnvs()
const env: Environment = {
name: "Env",
variables: aggregateEnvs,
}
const effectiveRequest = getEffectiveRESTRequest(request.value, env)
2021-08-26 05:32:05 +00:00
if (!props.show) return ""
2022-01-03 05:18:18 +00:00
const result = generateCode(
codegenType.value,
makeRESTRequest({
...effectiveRequest,
body: resolvesEnvsInBody(effectiveRequest.body, env),
2022-01-03 05:18:18 +00:00
headers: effectiveRequest.effectiveFinalHeaders.map((header) => ({
...header,
active: true,
})),
params: effectiveRequest.effectiveFinalParams.map((param) => ({
...param,
active: true,
})),
endpoint: effectiveRequest.effectiveFinalURL,
})
)
if (O.isSome(result)) {
2022-01-01 10:45:21 +00:00
errorState.value = false
return result.value
} else {
2022-01-01 10:45:21 +00:00
errorState.value = true
return ""
}
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,
environmentHighlights: false,
})
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"
toast.success(`${t("state.copied_to_clipboard")}`)
2021-08-28 00:17:33 +00:00
setTimeout(() => (copyIcon.value = "copy"), 1000)
2021-08-26 05:32:05 +00:00
}
const searchQuery = ref("")
const filteredCodegenDefinitions = computed(() => {
return CodegenDefinitions.filter((obj) =>
Object.values(obj).some((val) => val.includes(searchQuery.value))
)
})
</script>