add code generation
This commit is contained in:
1 parent
7b8ac91183
commit
8f8e291188
14 files changed
+362
-35
No files matched your search
@@ -0,0 +1,17 @@
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
{{#body}}
|
||||
jsonObject.put("{{{key}}}", "{{{value}}}");
|
||||
{{/body}}
|
||||
MediaType mediaType = "application/json; charset=utf-8".toMediaType();
|
||||
RequestBody body = jsonObject.toString().toRequestBody(mediaType);
|
||||
Request request = new Request.Builder()
|
||||
.url("{{{url}}}")
|
||||
.{{{method}}}(body)
|
||||
{{#headers}}
|
||||
.addHeader("{{{key}}}", "{{{value}}}")
|
||||
{{/headers}}
|
||||
.build();
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Response response = client.newCall(request).execute();
|
||||
System.out.print(response.body!!.string())
|
||||
@@ -0,0 +1,18 @@
|
||||
const options = {
|
||||
method: '{{{method}}}',
|
||||
headers: {
|
||||
{{#headers}}
|
||||
'{{{key}}}': '{{{value}}}'{{^last}},{{/last}}
|
||||
{{/headers}}
|
||||
},
|
||||
body: JSON.stringify({
|
||||
{{#body}}
|
||||
{{{key}}}: '{{{value}}}'{{^last}},{{/last}}
|
||||
{{/body}}
|
||||
})
|
||||
};
|
||||
|
||||
fetch('{{{url}}}', options)
|
||||
.then(response => response.json())
|
||||
.then(response => console.log(response))
|
||||
.catch(err => console.error(err));
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
require_once('vendor/autoload.php');
|
||||
|
||||
$client = new \GuzzleHttp\Client();
|
||||
|
||||
$response = $client->request('{{{method}}}', '{{{url}}}', [
|
||||
'body' => '{
|
||||
{{#body}}
|
||||
"{{{key}}}": "{{{value}}}"{{^last}},{{/last}}
|
||||
{{/body}}
|
||||
}',
|
||||
'headers' => [
|
||||
{{#headers}}
|
||||
'{{{key}}}': '{{{value}}}'{{^last}},{{/last}}
|
||||
{{/headers}}
|
||||
],
|
||||
]);
|
||||
|
||||
echo $response->getBody();
|
||||
@@ -0,0 +1,18 @@
|
||||
import requests
|
||||
|
||||
url = "{{{url}}}"
|
||||
|
||||
headers = {
|
||||
{{#headers}}
|
||||
'{{{key}}}': '{{{value}}}'{{^last}},{{/last}}
|
||||
{{/headers}}
|
||||
}
|
||||
|
||||
payload = {
|
||||
{{#body}}
|
||||
'{{{key}}}': '{{{value}}}'{{^last}},{{/last}}
|
||||
{{/body}}
|
||||
}
|
||||
|
||||
response = requests.{{{method}}}}(url, json=payload, headers=headers)
|
||||
print(response.text)
|
||||
+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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -119,7 +119,7 @@ export default defineComponent({
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss">
|
||||
<style lang="scss" scoped>
|
||||
.item {
|
||||
.title {
|
||||
font-size: 26px;
|
||||
|
||||
@@ -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>
|
||||
Vendored
+4
@@ -2,6 +2,10 @@
|
||||
|
||||
declare module 'element-ui/*';
|
||||
declare module '*.mjs';
|
||||
declare module '*.tpl' {
|
||||
const value: string;
|
||||
export default value;
|
||||
}
|
||||
|
||||
declare module '*.vue' {
|
||||
import { DefineComponent } from 'vue';
|
||||
|
||||
+1
-1
@@ -4,8 +4,8 @@ import router from './router/index';
|
||||
import store from './store/index';
|
||||
import ElementPlus from 'element-plus';
|
||||
import i18n from './i18n/index';
|
||||
import './assets/scss/style.scss';
|
||||
import './assets/css/tailwind.css';
|
||||
import './assets/scss/style.scss';
|
||||
import dayjs from './plugins/dayjs';
|
||||
import './plugins/font-awesome';
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="document?.type === 'API' && document?.api" class="right">
|
||||
<api-try :form="form" :api="document?.api" />
|
||||
<api-code :form="form" :api="document?.api" />
|
||||
<api-try :form="form" :api="document?.api" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in new issue
Block a user