add api try

This commit is contained in:
AceDataCloud committed 2023-01-21 12:40:45 +08:00
1 parent bdfaf84b15
commit 983dff4ab3
7 files changed
+159 -62

No files matched your search

+38
View File
@@ -0,0 +1,38 @@
<template>
<div class="authentication">
<div class="title">
{{ $t('api.entity.requestHeader') }}
</div>
<el-input v-model="apiKeyValue" placeholder="x-api-key">
<template #prepend>x-api-key</template>
</el-input>
</div>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
export default defineComponent({
name: 'ApiTry',
props: {
apiKey: {
type: String,
required: false,
default: ''
}
},
emits: ['update:api-key'],
data() {
return {
apiKeyValue: this.apiKey
};
},
watch: {
apiKeyValue: {
handler(val) {
this.$emit('update:api-key', val);
}
}
}
});
</script>
+2 -5
View File
@@ -36,7 +36,7 @@ export default defineComponent({
required: true
}
},
emits: ['change'],
emits: ['update:form'],
data(): IData {
return {
value: {}
@@ -46,7 +46,7 @@ export default defineComponent({
value: {
handler() {
console.log('change', this.value);
this.$emit('change', { ...this.value });
this.$emit('update:form', { ...this.value });
},
deep: true
}
@@ -55,9 +55,6 @@ export default defineComponent({
onChange(key: string, val: string) {
console.log('key', key, val);
this.value[key] = val;
this.$emit('change', {
...this.value
});
}
}
});
+39 -16
View File
@@ -1,38 +1,61 @@
<template>
<div class="authentication">
<div class="title">
{{ $t('api.entity.requestHeader') }}
<div class="wrapper">
<div class="operation">
<el-button class="btn-try" type="primary" @click="onTry">{{ $t('common.button.try') }}</el-button>
</div>
<div class="result">
<div class="title">{{ $t('api.entity.response') }}</div>
</div>
<el-input v-model="apiKeyValue" placeholder="x-api-key">
<template #prepend>x-api-key</template>
</el-input>
</div>
</template>
<script lang="ts">
import { IApi, IForm, IRequest } from '@/operators/api/models';
import { defineComponent } from 'vue';
import axios, { Method } from 'axios';
import urlJoin from 'url-join';
export default defineComponent({
name: 'ApiTry',
props: {
apiKey: {
type: String,
required: false,
default: ''
api: {
type: Object as () => IApi,
required: true
},
form: {
type: Object as () => IForm,
required: true
}
},
emits: ['update:api-key'],
data() {
return {
apiKeyValue: this.apiKey
};
},
watch: {
apiKeyValue: {
handler(val) {
this.$emit('update:api-key', val);
}
methods: {
onTry() {
console.log('try', this.api, this.form);
const config = {
method: this.api?.request?.method as Method,
url: urlJoin(this.api?.endpoint, this.api?.path),
params: { ...this.form.queries },
data: { ...this.form.body }
};
console.debug('config', config);
axios(config).then((response) => {
console.log('response', response);
// response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'));
});
}
}
});
</script>
<style lang="scss" scoped>
.wrapper {
padding: 15px;
.btn-try {
width: 100%;
}
}
</style>
+25 -34
View File
@@ -18,15 +18,15 @@
<el-divider />
<div class="queries">
<h2 class="title">{{ $t('api.entity.requestQueries') }}</h2>
<api-form :schema="api?.request.queries" />
<api-form v-model:form="formValue.queries" :schema="api?.request.queries" />
</div>
<div class="headers">
<h2 class="title">{{ $t('api.entity.requestHeaders') }}</h2>
<api-form :schema="api?.request.headers" />
<api-form v-model:form="formValue.headers" :schema="api?.request.headers" />
</div>
<div class="body">
<h2 class="title">{{ $t('api.entity.requestBody') }}</h2>
<api-form :schema="api?.request.body" />
<api-form v-model:form="formValue.body" :schema="api?.request.body" />
</div>
<div v-if="api?.responses" class="responses">
<h2 class="title">{{ $t('api.entity.response') }}</h2>
@@ -37,15 +37,13 @@
<script lang="ts">
import { defineComponent } from 'vue';
import { IApi, IApiDetailResponse } from '@/operators/api/models';
import { apiOperator } from '@/operators/api/operator';
import { IApi, IApiDetailResponse, IForm } from '@/operators/api/models';
import ApiForm from './ApiForm.vue';
import ApiResult from './ApiResult.vue';
import urlJoin from 'url-join';
export interface IData {
api: IApi | undefined;
loading: boolean;
formValue: IForm | undefined;
}
export default defineComponent({
@@ -55,43 +53,36 @@ export default defineComponent({
ApiResult
},
props: {
id: {
type: String,
api: {
type: Object as () => IApi,
required: true
},
loading: {
type: Boolean,
required: true
},
form: {
type: Object as () => IForm,
required: true
}
},
data(): IData {
emits: ['update:form'],
data() {
return {
api: undefined,
loading: false
formValue: this.form
};
},
watch: {
id: {
handler() {
this.getApi(this.id);
}
formValue: {
handler(val) {
console.log('formValue changeddd');
this.$emit('update:form', { ...val });
},
deep: true
}
},
mounted() {
this.getApi(this.id);
},
methods: {
urlJoin,
getApi(id: string) {
this.loading = true;
apiOperator
.get(id)
.then(({ data: data }: { data: IApiDetailResponse }) => {
this.loading = false;
this.api = data;
console.log('api', this.api);
})
.catch((error) => {
this.loading = false;
console.error(error);
});
}
urlJoin
}
});
</script>
+2 -1
View File
@@ -15,5 +15,6 @@ export default {
profile: '个人信息',
console: '控制台',
learn: '学习',
buy: '购买'
buy: '购买',
try: '测试'
};
+12
View File
@@ -1,3 +1,15 @@
export interface IForm {
queries?: {
[key: string]: string;
};
headers?: {
[key: string]: string;
};
body?: {
[key: string]: string | object | number | object[];
};
}
export interface ISchema {
properties: {
[key: string]: ISchema;
+41 -6
View File
@@ -4,11 +4,11 @@
<div class="left">
<api-list />
</div>
<div class="main">
<api-usage :id="id" />
<div v-if="api" class="main">
<api-usage v-model:form="form" :api="api" :loading="loading" />
</div>
<div class="right">
<api-try v-model:api-key="apiKey" />
<api-try :form="form" :api="api" />
</div>
</el-col>
</el-row>
@@ -19,6 +19,14 @@ import { defineComponent } from 'vue';
import ApiList from '@/components/document/ApiList.vue';
import ApiUsage from '@/components/document/ApiUsage.vue';
import ApiTry from '@/components/document/ApiTry.vue';
import { apiOperator } from '@/operators/api/operator';
import { IApi, IApiDetailResponse, IForm } from '@/operators/api/models';
export interface IData {
api: IApi | undefined;
loading: boolean;
form: IForm;
}
export default defineComponent({
components: {
@@ -26,9 +34,11 @@ export default defineComponent({
ApiUsage,
ApiTry
},
data() {
data(): IData {
return {
apiKey: ''
api: undefined,
loading: false,
form: {}
};
},
computed: {
@@ -36,7 +46,32 @@ export default defineComponent({
return this.$route.params.id.toString();
}
},
methods: {}
watch: {
id: {
handler() {
this.getApi(this.id);
}
}
},
mounted() {
this.getApi(this.id);
},
methods: {
getApi(id: string) {
this.loading = true;
apiOperator
.get(id)
.then(({ data: data }: { data: IApiDetailResponse }) => {
this.loading = false;
this.api = data;
console.log('api', this.api);
})
.catch((error) => {
this.loading = false;
console.error(error);
});
}
}
});
</script>