feat: initial attempt at restoring codegen

This commit is contained in:
Andrew Bastin
2021-07-19 05:59:22 -04:00
parent 2ace83bcaf
commit 1573ddad1b

View File

@@ -35,7 +35,7 @@
focus:outline-none focus:outline-none
" "
> >
{{ codegens.find((x) => x.id === requestType).name }} {{ codegens.find((x) => x.id === codegenType).name }}
</span> </span>
</template> </template>
<SmartItem <SmartItem
@@ -43,7 +43,7 @@
:key="`gen-${index}`" :key="`gen-${index}`"
:label="gen.name" :label="gen.name"
@click.native=" @click.native="
requestType = gen.id codegenType = gen.id
$refs.options.tippy().hide() $refs.options.tippy().hide()
" "
/> />
@@ -66,10 +66,10 @@
/> />
</div> </div>
<SmartAceEditor <SmartAceEditor
v-if="requestType" v-if="codegenType"
ref="generatedCode" ref="generatedCode"
:value="requestCode" :value="requestCode"
:lang="codegens.find((x) => x.id === requestType).language" :lang="codegens.find((x) => x.id === codegenType).language"
:options="{ :options="{
maxLines: '10', maxLines: '10',
minLines: '10', minLines: '10',
@@ -96,23 +96,46 @@ import { getCurrentEnvironment } from "~/newstore/environments"
export default defineComponent({ export default defineComponent({
props: { props: {
show: Boolean, show: Boolean,
requestTypeProp: { type: String, default: "curl" },
}, },
data() { data() {
return { return {
codegens, codegens,
copyIcon: "content_copy", copyIcon: "content_copy",
request: getRESTRequest(), request: getRESTRequest(),
codegenType: "curl",
} }
}, },
computed: { computed: {
requestType: { requestCode(): string {
get(): string { const effectiveRequest = getEffectiveRESTRequest(
return this.requestTypeProp this.request,
}, getCurrentEnvironment()
set(val: string) { )
this.$emit("set-request-type", val)
}, const urlObj = new URL(effectiveRequest.effectiveFinalURL)
const baseURL = urlObj.origin
const path = urlObj.pathname
// TODO: Solidify
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"
),
})
}, },
}, },
watch: { watch: {
@@ -123,9 +146,6 @@ export default defineComponent({
}, },
}, },
methods: { methods: {
requestCode() {
return getEffectiveRESTRequest(this.request, getCurrentEnvironment())
},
hideModal() { hideModal() {
this.$emit("hide-modal") this.$emit("hide-modal")
}, },
@@ -133,15 +153,16 @@ export default defineComponent({
this.$emit("handle-import") this.$emit("handle-import")
}, },
copyRequestCode() { copyRequestCode() {
;(this.$refs.generatedCode as any).editor const editor: any = this.$refs.generatedCode
.selectAll()(this.$refs.generatedCode as any)
.editor.focus() editor.editor.selectAll()
editor.editor.focus()
document.execCommand("copy") document.execCommand("copy")
this.copyIcon = "done" this.copyIcon = "done"
this.$toast.success(this.$t("copied_to_clipboard").toString(), { this.$toast.success(this.$t("copied_to_clipboard").toString(), {
icon: "done", icon: "done",
}) })
setTimeout(() => (this.copyIcon = "content_copy"), 1000)
}, },
}, },
}) })