add code generation

This commit is contained in:
AceDataCloud committed 2023-01-23 19:46:11 +08:00
1 parent 7b8ac91183
commit 8f8e291188
14 files changed
+362 -35

No files matched your search

+111 -16
View File
@@ -1,11 +1,22 @@
<template>
<div class="wrapper">
<div class="options">
<div v-for="(option, optionKey) in options" :key="optionKey" class="option">
<div class="options mb-2">
<div
v-for="(option, optionKey) in options"
:key="optionKey"
:class="{
option: true,
active: lang === option.name
}"
@click="lang = option.name"
>
<el-image :src="option.icon" class="icon" />
<p class="name">{{ option.name }}</p>
</div>
</div>
<div v-if="code" class="code">
<code-snippet :code="code" :lang="lang" />
</div>
</div>
</template>
@@ -14,21 +25,41 @@ import { defineComponent } from 'vue';
import javascript from 'programming-languages-logos/src/javascript/javascript.svg';
import python from 'programming-languages-logos/src/python/python.svg';
import java from 'programming-languages-logos/src/java/java.svg';
import go from 'programming-languages-logos/src/go/go.svg';
import php from 'programming-languages-logos/src/php/php.svg';
import { IApi, IForm } from '@/operators/api/models';
import urlJoin from 'url-join';
import Mustache from 'mustache';
import pythonTemplate from '@/assets/templates/python.tpl';
import javascriptTemplate from '@/assets/templates/javascript.tpl';
import phpTemplate from '@/assets/templates/php.tpl';
import javaTemplate from '@/assets/templates/java.tpl';
import CodeSnippet from '../common/CodeSnippet.vue';
import queryString from 'query-string';
console.log('pythonTemplate', pythonTemplate);
interface IOption {
name: string;
name: ILang;
icon: string;
}
const LANG_PYTHON = 'Python';
const LANG_JAVA = 'Java';
const LANG_JAVASCRIPT = 'JavaScript';
const LANG_PHP = 'PHP';
type ILang = typeof LANG_PYTHON | typeof LANG_JAVA | typeof LANG_JAVASCRIPT | typeof LANG_PHP;
interface IData {
lang: ILang | undefined;
options: IOption[];
}
export default defineComponent({
name: 'ApiCode',
components: {
CodeSnippet
},
props: {
api: {
type: Object as () => IApi,
@@ -41,43 +72,106 @@ export default defineComponent({
},
data(): IData {
return {
lang: undefined,
options: [
{
name: 'Python',
name: LANG_PYTHON,
icon: python
},
{
name: 'JavaScript',
name: LANG_JAVASCRIPT,
icon: javascript
},
{
name: 'Java',
name: LANG_JAVA,
icon: java
},
{
name: 'Go',
icon: go
name: LANG_PHP,
icon: php
}
]
};
},
computed: {
method() {
return this.api.request.method;
},
url() {
return urlJoin(this.api?.endpoint, this.api?.path);
const getQueryString = () => {
if (this.form.queries) {
return `?${queryString.stringify(this.form?.queries)}`;
}
return '';
};
return urlJoin(this.api?.endpoint, this.api?.path, getQueryString());
},
contentType() {
return (this.form?.headers || {})['content-type'];
headers() {
let result = [];
for (const key in this.form.headers) {
result.push({
key,
value: this.form.headers[key]
});
}
if (result.length > 0) {
const last = result[result.length - 1];
result[result.length - 1] = {
...last,
last: true
};
}
return result;
},
pythonSample() {}
body() {
let result = [];
for (const key in this.form.body) {
result.push({
key,
value: this.form.body[key]
});
}
if (result.length > 0) {
const last = result[result.length - 1];
result[result.length - 1] = {
...last,
last: true
};
}
return result;
},
code(): string | undefined {
let template = undefined;
if (this.lang === LANG_PYTHON) {
template = pythonTemplate;
} else if (this.lang === LANG_JAVASCRIPT) {
template = javascriptTemplate;
} else if (this.lang === LANG_PHP) {
template = phpTemplate;
} else if (this.lang === LANG_JAVA) {
template = javaTemplate;
}
if (template)
return Mustache.render(template, {
headers: this.headers,
body: this.body,
url: this.url,
method: this.method
});
return undefined;
}
},
watch: {}
mounted() {
this.lang = this.options[0].name;
}
});
</script>
<style lang="scss">
<style lang="scss" scoped>
.wrapper {
width: 100%;
padding: 15px;
padding-bottom: 0;
overflow-x: scroll;
.options {
width: fit-content;
@@ -95,7 +189,8 @@ export default defineComponent({
flex-direction: column;
text-align: center;
align-items: center;
&:hover {
&:hover,
&.active {
border-color: #ccc;
cursor: pointer;
}
+2 -1
View File
@@ -1,6 +1,6 @@
<template>
<div class="wrapper">
<div class="operation">
<div class="operation mb-2">
<el-button class="btn-try" type="primary" :loading="loading" @click="onTry">{{
$t('common.button.try')
}}</el-button>
@@ -72,6 +72,7 @@ export default defineComponent({
<style lang="scss" scoped>
.wrapper {
padding: 15px;
padding-bottom: 0;
.operation {
margin-bottom: 10px;
.btn-try {
+1 -1
View File
@@ -119,7 +119,7 @@ export default defineComponent({
});
</script>
<style lang="scss">
<style lang="scss" scoped>
.item {
.title {
font-size: 26px;
+30 -9
View File
@@ -5,13 +5,12 @@
<script lang="ts">
import { Codemirror } from 'vue-codemirror';
import { json } from '@codemirror/lang-json';
import { python } from '@codemirror/lang-python';
import { javascript } from '@codemirror/lang-javascript';
import { php } from '@codemirror/lang-php';
import { java } from '@codemirror/lang-java';
import { oneDark } from '@codemirror/theme-one-dark';
import { defineComponent } from 'vue';
import { Extension } from '@codemirror/state';
interface IData {
extensions: Extension[];
}
export default defineComponent({
name: 'CodeSnippet',
@@ -26,12 +25,34 @@ export default defineComponent({
editable: {
type: Boolean,
required: false
},
lang: {
type: String,
required: false,
default: 'JSON'
}
},
data(): IData {
return {
extensions: [json(), oneDark]
};
computed: {
extensions() {
let result = [];
result.push(oneDark);
if (this.lang === 'JSON') {
result.push(json());
}
if (this.lang === 'Python') {
result.push(python());
}
if (this.lang === 'Java') {
result.push(java());
}
if (this.lang === 'JavaScript') {
result.push(javascript());
}
if (this.lang === 'PHP') {
result.push(php());
}
return result;
}
}
});
</script>