feat(接口测试): curl导入解析

This commit is contained in:
baiqi 2024-09-02 17:40:31 +08:00 committed by Craftsman
parent 6020b0781c
commit 2ad6cc78d0
6 changed files with 464 additions and 366 deletions

View File

@ -5,12 +5,14 @@ import {
GetPluginOptionsUrl, GetPluginOptionsUrl,
GetPluginScriptUrl, GetPluginScriptUrl,
GetProtocolListUrl, GetProtocolListUrl,
ImportCurlUrl,
LocalExecuteApiDebugUrl, LocalExecuteApiDebugUrl,
StopExecuteUrl, StopExecuteUrl,
StopLocalExecuteUrl, StopLocalExecuteUrl,
} from '@/api/requrls/api-test/common'; } from '@/api/requrls/api-test/common';
import { import {
type CurlParseResult,
ExecuteRequestParams, ExecuteRequestParams,
GetPluginOptionsParams, GetPluginOptionsParams,
PluginConfig, PluginConfig,
@ -61,3 +63,8 @@ export function stopLocalExecute(host: string, id: string | number, type?: Scena
export function stopExecute(id: string | number, type?: ScenarioStepType) { export function stopExecute(id: string | number, type?: ScenarioStepType) {
return MSR.get({ url: type ? `${StopExecuteUrl}/${type}/${id}` : `${StopExecuteUrl}/${id}` }); return MSR.get({ url: type ? `${StopExecuteUrl}/${type}/${id}` : `${StopExecuteUrl}/${id}` });
} }
// 导入curl
export function importByCurl(curl: string) {
return MSR.post<CurlParseResult>({ url: ImportCurlUrl, data: { curl } });
}

View File

@ -6,3 +6,4 @@ export const GetEnvListUrl = '/api/test/env-list'; // 获取接口测试环境
export const GetEnvironmentUrl = '/api/test/environment'; // 获取接口测试环境详情 export const GetEnvironmentUrl = '/api/test/environment'; // 获取接口测试环境详情
export const StopExecuteUrl = '/task/center/api/project/stop'; // 停止执行 export const StopExecuteUrl = '/task/center/api/project/stop'; // 停止执行
export const StopLocalExecuteUrl = '/api/stop'; // 停止本地执行 export const StopLocalExecuteUrl = '/api/stop'; // 停止本地执行
export const ImportCurlUrl = '/api/debug/import-curl'; // 导入curl

View File

@ -463,3 +463,11 @@ export type ApiCaseReportDetail = {
scriptIdentifier: string; scriptIdentifier: string;
content: RequestResult; content: RequestResult;
}; };
// curl解析结果
export interface CurlParseResult {
method: RequestMethods | string;
url: string;
headers: Record<string, any>;
body: string;
queryParams: Record<string, any>;
}

View File

@ -0,0 +1,82 @@
<template>
<a-modal
v-model:visible="visible"
title-align="start"
:width="680"
:ok-disabled="curlCode.trim() === ''"
:ok-text="t('common.import')"
:ok-button-props="{
disabled: curlCode.trim() === '',
}"
class="ms-modal-form"
@cancel="curlCode = ''"
@before-ok="handleCurlImportConfirm"
>
<template #title>
<a-tooltip position="right" :content="t('apiTestDebug.importByCURLTip')">
<span
>{{ t('apiTestDebug.importByCURL') }}
<icon-question-circle
class="ml-[4px] text-[var(--color-text-brand)] hover:text-[rgb(var(--primary-5))]"
size="16"
/>
</span>
</a-tooltip>
</template>
<div class="h-full">
<MsCodeEditor
v-if="visible"
v-model:model-value="curlCode"
theme="MS-text"
height="500px"
:language="LanguageEnum.PLAINTEXT"
:show-theme-change="false"
:show-full-screen="false"
>
</MsCodeEditor>
</div>
</a-modal>
</template>
<script setup lang="ts">
import { Message } from '@arco-design/web-vue';
import MsCodeEditor from '@/components/pure/ms-code-editor/index.vue';
import { LanguageEnum } from '@/components/pure/ms-code-editor/types';
import { importByCurl } from '@/api/modules/api-test/common';
import { useI18n } from '@/hooks/useI18n';
import type { CurlParseResult } from '@/models/apiTest/common';
const emit = defineEmits<{
(e: 'done', res: CurlParseResult): void;
}>();
const { t } = useI18n();
const visible = defineModel('visible', {
type: Boolean,
required: true,
});
const importLoading = ref(false);
const curlCode = ref('');
async function handleCurlImportConfirm(done: (closed: boolean) => void) {
try {
importLoading.value = true;
const res = await importByCurl(curlCode.value);
emit('done', res);
done(true);
Message.success(t('common.importSuccess'));
} catch (error) {
// eslint-disable-next-line no-console
console.log(error);
} finally {
importLoading.value = false;
}
}
</script>
<style lang="less" scoped></style>

View File

@ -9,7 +9,7 @@
@init="(val) => (folderTree = val)" @init="(val) => (folderTree = val)"
@new-api="addDebugTab" @new-api="addDebugTab"
@click-api-node="openApiTab" @click-api-node="openApiTab"
@import="importDrawerVisible = true" @import="importDialogVisible = true"
@update-api-node="handleApiUpdateFromModuleTree" @update-api-node="handleApiUpdateFromModuleTree"
@delete-finish="handleDeleteFinish" @delete-finish="handleDeleteFinish"
/> />
@ -63,38 +63,7 @@
</template> </template>
</MsSplitBox> </MsSplitBox>
</MsCard> </MsCard>
<MsDrawer <importCurlDialog v-model:visible="importDialogVisible" @done="handleImportCurlDone" />
v-model:visible="importDrawerVisible"
:width="680"
:ok-disabled="curlCode.trim() === ''"
disabled-width-drag
@cancel="curlCode = ''"
@confirm="handleCurlImportConfirm"
>
<template #title>
<a-tooltip position="right" :content="t('apiTestDebug.importByCURLTip')">
<span
>{{ t('apiTestDebug.importByCURL') }}
<icon-exclamation-circle
class="ml-[4px] text-[var(--color-text-brand)] hover:text-[rgb(var(--primary-5))]"
size="16"
/>
</span>
</a-tooltip>
</template>
<div class="h-full">
<MsCodeEditor
v-if="importDrawerVisible"
v-model:model-value="curlCode"
theme="MS-text"
height="100%"
:language="LanguageEnum.PLAINTEXT"
:show-theme-change="false"
:show-full-screen="false"
>
</MsCodeEditor>
</div>
</MsDrawer>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
@ -105,14 +74,12 @@
import { cloneDeep } from 'lodash-es'; import { cloneDeep } from 'lodash-es';
import MsCard from '@/components/pure/ms-card/index.vue'; import MsCard from '@/components/pure/ms-card/index.vue';
import MsCodeEditor from '@/components/pure/ms-code-editor/index.vue';
import { LanguageEnum } from '@/components/pure/ms-code-editor/types';
import MsDrawer from '@/components/pure/ms-drawer/index.vue';
import MsEditableTab from '@/components/pure/ms-editable-tab/index.vue'; import MsEditableTab from '@/components/pure/ms-editable-tab/index.vue';
import { TabItem } from '@/components/pure/ms-editable-tab/types'; import { TabItem } from '@/components/pure/ms-editable-tab/types';
import MsSplitBox from '@/components/pure/ms-split-box/index.vue'; import MsSplitBox from '@/components/pure/ms-split-box/index.vue';
import moduleTree from './components/moduleTree.vue'; import moduleTree from './components/moduleTree.vue';
import apiMethodName from '@/views/api-test/components/apiMethodName.vue'; import apiMethodName from '@/views/api-test/components/apiMethodName.vue';
import importCurlDialog from '@/views/api-test/components/importCurlDialog.vue';
import requestComposition, { RequestParam } from '@/views/api-test/components/requestComposition/index.vue'; import requestComposition, { RequestParam } from '@/views/api-test/components/requestComposition/index.vue';
import { localExecuteApiDebug } from '@/api/modules/api-test/common'; import { localExecuteApiDebug } from '@/api/modules/api-test/common';
@ -128,9 +95,9 @@
import { useI18n } from '@/hooks/useI18n'; import { useI18n } from '@/hooks/useI18n';
import useLeaveTabUnSaveCheck from '@/hooks/useLeaveTabUnSaveCheck'; import useLeaveTabUnSaveCheck from '@/hooks/useLeaveTabUnSaveCheck';
import useRequestCompositionStore from '@/store/modules/api/requestComposition'; import useRequestCompositionStore from '@/store/modules/api/requestComposition';
import { parseCurlScript } from '@/utils';
import { hasAnyPermission } from '@/utils/permission'; import { hasAnyPermission } from '@/utils/permission';
import { CurlParseResult } from '@/models/apiTest/common';
import { ModuleTreeNode } from '@/models/common'; import { ModuleTreeNode } from '@/models/common';
import { import {
ProtocolKeyEnum, ProtocolKeyEnum,
@ -151,8 +118,7 @@
const moduleTreeRef = ref<InstanceType<typeof moduleTree>>(); const moduleTreeRef = ref<InstanceType<typeof moduleTree>>();
const folderTree = ref<ModuleTreeNode[]>([]); const folderTree = ref<ModuleTreeNode[]>([]);
const importDrawerVisible = ref(false); const importDialogVisible = ref(false);
const curlCode = ref('');
const loading = ref(false); const loading = ref(false);
async function handleDebugAddDone() { async function handleDebugAddDone() {
@ -283,20 +249,21 @@
} }
} }
function handleCurlImportConfirm() { function handleImportCurlDone(res: CurlParseResult) {
const { url, headers, queryParameters, method } = parseCurlScript(curlCode.value); const { url, method, headers, queryParams } = res;
addDebugTab({ addDebugTab({
url, url,
method: method?.toUpperCase() || RequestMethods.GET, method: method?.toUpperCase() || RequestMethods.GET,
headers: headers:
headers?.map((e) => ({ Object.keys(headers)?.map((e) => ({
contentType: RequestContentTypeEnum.TEXT, contentType: RequestContentTypeEnum.TEXT,
description: '', description: '',
enable: true, enable: true,
...e, key: e,
value: headers[e],
})) || [], })) || [],
query: query:
queryParameters?.map((e) => ({ Object.keys(queryParams)?.map((e) => ({
paramType: RequestParamsType.STRING, paramType: RequestParamsType.STRING,
description: '', description: '',
required: false, required: false,
@ -304,11 +271,11 @@
minLength: undefined, minLength: undefined,
encode: false, encode: false,
enable: true, enable: true,
...e, key: e,
value: queryParams[e],
})) || [], })) || [],
}); });
curlCode.value = ''; importDialogVisible.value = false;
importDrawerVisible.value = false;
nextTick(() => { nextTick(() => {
handleActiveDebugChange(); handleActiveDebugChange();
}); });

View File

@ -1,4 +1,5 @@
<template> <template>
<div>
<MsDrawer <MsDrawer
v-model:visible="visible" v-model:visible="visible"
:width="960" :width="960"
@ -41,7 +42,6 @@
</a-tooltip> </a-tooltip>
<a-input <a-input
v-if="isShowEditStepNameInput" v-if="isShowEditStepNameInput"
ref="stepNameInputRef"
v-model:model-value="requestVModel.stepName" v-model:model-value="requestVModel.stepName"
class="flex-1" class="flex-1"
:placeholder="t('apiScenario.pleaseInputStepName')" :placeholder="t('apiScenario.pleaseInputStepName')"
@ -156,6 +156,14 @@
<template v-if="showEnvPrefix" #prefix> <template v-if="showEnvPrefix" #prefix>
{{ (appStore.currentEnvConfig as EnvConfig)?.httpConfig.find((e) => e.type === 'NONE')?.url }} {{ (appStore.currentEnvConfig as EnvConfig)?.httpConfig.find((e) => e.type === 'NONE')?.url }}
</template> </template>
<template #suffix>
<MsIcon
type="icon-icon_curl"
:size="18"
class="cursor-pointer hover:text-[rgb(var(--primary-5))]"
@click="importDialogVisible = true"
/>
</template>
</a-input> </a-input>
</a-input-group> </a-input-group>
</div> </div>
@ -339,6 +347,8 @@
</div> </div>
<!-- <addDependencyDrawer v-model:visible="showAddDependencyDrawer" :mode="addDependencyMode" /> --> <!-- <addDependencyDrawer v-model:visible="showAddDependencyDrawer" :mode="addDependencyMode" /> -->
</MsDrawer> </MsDrawer>
<importCurlDialog v-model:visible="importDialogVisible" @done="handleImportCurlDone" />
</div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
@ -356,6 +366,7 @@
import stepTypeVue from './stepType/stepType.vue'; import stepTypeVue from './stepType/stepType.vue';
import apiMethodName from '@/views/api-test/components/apiMethodName.vue'; import apiMethodName from '@/views/api-test/components/apiMethodName.vue';
import apiMethodSelect from '@/views/api-test/components/apiMethodSelect.vue'; import apiMethodSelect from '@/views/api-test/components/apiMethodSelect.vue';
import importCurlDialog from '@/views/api-test/components/importCurlDialog.vue';
import auth from '@/views/api-test/components/requestComposition/auth.vue'; import auth from '@/views/api-test/components/requestComposition/auth.vue';
import { TabErrorMessage } from '@/views/api-test/components/requestComposition/index.vue'; import { TabErrorMessage } from '@/views/api-test/components/requestComposition/index.vue';
import postcondition from '@/views/api-test/components/requestComposition/postcondition.vue'; import postcondition from '@/views/api-test/components/requestComposition/postcondition.vue';
@ -372,6 +383,7 @@
import { getLocalStorage, setLocalStorage } from '@/utils/local-storage'; import { getLocalStorage, setLocalStorage } from '@/utils/local-storage';
import { import {
CurlParseResult,
EnableKeyValueParam, EnableKeyValueParam,
ExecuteApiRequestFullParams, ExecuteApiRequestFullParams,
ExecuteBody, ExecuteBody,
@ -1268,6 +1280,27 @@
}); });
} }
const importDialogVisible = ref(false);
function handleImportCurlDone(res: CurlParseResult) {
const { url, method, headers, queryParams } = res;
requestVModel.value.url = url;
requestVModel.value.method = method;
requestVModel.value.headers = Object.keys(headers).map((e) => ({
...defaultHeaderParamsItem,
key: e,
value: headers[e],
}));
requestVModel.value.query = Object.keys(queryParams).map((e) => ({
...defaultRequestParamsItem,
key: e,
value: queryParams[e],
}));
importDialogVisible.value = false;
nextTick(() => {
handleActiveDebugChange();
});
}
watch( watch(
() => props.request?.uniqueId, () => props.request?.uniqueId,
() => { () => {