add code generation
This commit is contained in:
1 parent
7b8ac91183
commit
8f8e291188
14 files changed
+362
-35
No files matched your search
+111
-16
@@ -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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user