api-client/components/http/CodegenModal.vue

150 lines
4.1 KiB
Vue
Raw Normal View History

<template>
<SmartModal
v-if="show"
: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="
codegenType = gen.id
$refs.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-04 05:50:23 +00:00
{{ $t("request.generated_code") }}
2021-07-09 17:19:45 +00:00
</label>
2020-12-11 10:29:03 +00:00
</div>
2021-07-09 17:19:45 +00:00
<SmartAceEditor
v-if="codegenType"
2021-07-09 17:19:45 +00:00
ref="generatedCode"
:value="requestCode"
:lang="codegens.find((x) => x.id === codegenType).language"
2021-07-09 17:19:45 +00:00
:options="{
maxLines: 16,
minLines: 8,
2021-07-09 17:19:45 +00:00
autoScrollEditorIntoView: true,
readOnly: true,
showPrintMargin: false,
useWorker: false,
}"
2021-08-14 18:16:03 +00:00
styles="border rounded border-dividerLight"
2021-07-09 17:19:45 +00:00
/>
2020-12-11 10:29:03 +00:00
</div>
</template>
<template #footer>
<ButtonPrimary
ref="copyRequestCode"
2021-08-02 15:27:18 +00:00
:label="$t('action.copy')"
:icon="copyIcon"
@click.native="copyRequestCode"
/>
2021-08-02 15:27:18 +00:00
<ButtonSecondary
:label="$t('action.dismiss')"
@click.native="hideModal"
/>
</template>
</SmartModal>
</template>
2021-07-18 04:33:52 +00:00
<script lang="ts">
import { defineComponent } from "@nuxtjs/composition-api"
import { codegens } from "~/helpers/codegen/codegen"
2021-07-18 04:33:52 +00:00
import { getRESTRequest } from "~/newstore/RESTSession"
import { getEffectiveRESTRequest } from "~/helpers/utils/EffectiveURL"
import { getCurrentEnvironment } from "~/newstore/environments"
import { copyToClipboard } from "~/helpers/utils/clipboard"
2021-07-18 04:33:52 +00:00
export default defineComponent({
props: {
show: Boolean,
},
data() {
return {
codegens,
2021-06-29 11:34:02 +00:00
copyIcon: "content_copy",
2021-07-18 04:33:52 +00:00
request: getRESTRequest(),
codegenType: "curl",
}
},
computed: {
requestCode(): string {
const effectiveRequest = getEffectiveRESTRequest(
this.request,
getCurrentEnvironment()
)
const urlObj = new URL(effectiveRequest.effectiveFinalURL)
const baseURL = urlObj.origin
const path = urlObj.pathname
// TODO: Solidify
// TODO: Implement updated auth stuff
return codegens
.find((x) => x.id === this.codegenType)!
.generator({
auth: "None",
httpUser: null,
httpPassword: null,
method: effectiveRequest.method,
url: baseURL,
pathName: path,
queryString: urlObj.searchParams.toString(),
bearerToken: null,
headers: effectiveRequest.effectiveFinalHeaders,
rawInput: null,
rawParams: null,
rawRequestBody: "",
contentType: effectiveRequest.effectiveFinalHeaders.find(
(x) => x.key === "content-type"
),
})
},
},
2021-07-18 04:33:52 +00:00
watch: {
show(goingToShow) {
if (goingToShow) {
this.request = getRESTRequest()
}
},
},
methods: {
hideModal() {
this.$emit("hide-modal")
},
handleImport() {
this.$emit("handle-import")
},
copyRequestCode() {
copyToClipboard(this.requestCode)
2021-06-29 11:34:02 +00:00
this.copyIcon = "done"
this.$toast.success(this.$t("state.copied_to_clipboard").toString(), {
2021-06-29 11:34:02 +00:00
icon: "done",
})
setTimeout(() => (this.copyIcon = "content_copy"), 1000)
},
},
2021-07-18 04:33:52 +00:00
})
</script>