Skip to content

Commit

Permalink
feat: save data from test requestBody json/xml to form
Browse files Browse the repository at this point in the history
  • Loading branch information
scarqin committed Aug 24, 2022
1 parent d230860 commit ad4c943
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { whatType } from 'eo/workbench/browser/src/app/utils';
import { transferUrlAndQuery } from 'eo/workbench/browser/src/app/utils/api';
import { listToTreeHasLevel } from 'eo/workbench/browser/src/app/utils/tree/tree.utils';
import { ApiTestHeaders, ApiTestQuery, ContentTypeByAbridge } from '../../../shared/services/api-test/api-test.model';
import { ApiTestHeaders, ContentTypeByAbridge } from '../../../shared/services/api-test/api-test.model';
import { ApiBodyType, ApiData, ApiTestData, ApiTestHistory } from '../../../shared/services/storage/index.model';
import { eoDeepCopy } from '../../../utils';
import { uiData2Json, text2UiData, json2XML } from '../../../utils/data-transfer/data-transfer.utils';
Expand Down Expand Up @@ -217,6 +217,18 @@ export class ApiTestUtilService {
});
return result;
}
private text2Body(keyName, inData = '') {
const result = {};
const bodyInfo = text2UiData(inData);
if (bodyInfo.textType !== 'raw') {
result[`${keyName}`] = listToTreeHasLevel(bodyInfo.data);
} else {
result[`${keyName}`] = bodyInfo.data;
}
result[`${keyName}Type`] = bodyInfo.textType;
result[`${keyName}JsonType`] = bodyInfo.rootType;
return result;
}
/**
* Transfer test data/test history to api data
*
Expand All @@ -233,19 +245,17 @@ export class ApiTestUtilService {
responseBody: [],
};
delete result.uuid;
if (result.requestBodyType === ApiBodyType.Raw) {
Object.assign(result, this.text2Body('requestBody', result.requestBody));
}
['requestHeaders', 'requestBody', 'restParams', 'queryParams'].forEach((keyName) => {
if (!result[keyName] || typeof result[keyName] !== 'object') {
return;
}
result[keyName] = this.testTableData2ApiBody(result[keyName]);
});
if (inData.history.response.responseType === 'text') {
console.log('inData.history.response.body', inData.history.response.body);
const bodyInfo = text2UiData(inData.history.response.body);
console.log('bodyInfo', bodyInfo);
result.responseBody = listToTreeHasLevel(bodyInfo.data);
result.responseBodyType = bodyInfo.textType;
result.responseBodyJsonType = bodyInfo.rootType;
Object.assign(result, this.text2Body('responseBody', inData.history.response.body));
}
return result;
}
Expand Down Expand Up @@ -273,15 +283,15 @@ export class ApiTestUtilService {
case ApiBodyType.JSON: {
inData.requestBody = JSON.stringify(
uiData2Json(inData.requestBody, {
defaultValueKey: 'example',
rootType: inData.requestBodyJsonType,
})
);
break;
}
case ApiBodyType.XML: {
inData.requestBody = json2XML(
uiData2Json(inData.requestBody, {
defaultValueKey: 'example',
rootType: inData.requestBodyJsonType,
})
);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { TranslateService } from 'eo/platform/common/i18n';
import { LanguageService } from 'eo/workbench/browser/src/app/core/services/language/language.service';
import { APP_CONFIG } from 'eo/workbench/browser/src/environments/environment';


@Injectable({
providedIn: 'root',
})
Expand All @@ -32,7 +31,9 @@ export class ExtensionService {
private translateModule(module: ModuleInfo) {
const lang = this.language.systemLanguage;
const locale = module.i18n?.find((val) => val.locale === lang)?.package;
if (!locale) {return module;}
if (!locale) {
return module;
}
module = new TranslateService(module, locale).translate();
return module;
}
Expand All @@ -48,12 +49,13 @@ export class ExtensionService {
return result;
}
async getDetail(id, name): Promise<any> {
const result = {};
let result = {} as ModuleInfo;
const { code, data }: any = await this.requestDetail(name);
Object.assign(result, data);
if (this.localExtensions.has(id)) {
Object.assign(result, this.localExtensions.get(id), { installed: true });
}
result = this.translateModule(result);
return result;
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ export class EoMonacoEditorComponent implements AfterViewInit, OnInit, OnChanges
}

private setCode(val: string) {
console.log(val === this.$$code);
if (val === this.$$code) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ export const xml2json = (tmpl) => {
};

type uiData = {
textType: ApiBodyType | string;
rootType: JsonRootType | string;
textType: ApiBodyType;
rootType: JsonRootType;
data: ApiEditBody | any;
};

Expand All @@ -185,7 +185,7 @@ export const xml2UiData = (text) => {
* @returns
*/
export const json2XML: (o: object, tab?) => string = (o, tab) => {
const toXml = function (v, name, ind) {
const toXml = function(v, name, ind) {
let xml = '';
if (v instanceof Array) {
for (let i = 0, n = v.length; i < n; i++) {
Expand Down Expand Up @@ -236,26 +236,21 @@ export const json2XML: (o: object, tab?) => string = (o, tab) => {
*/
export const text2UiData: (text: string) => uiData = (text) => {
const result: uiData = {
textType: 'raw',
rootType: 'object',
textType: ApiBodyType.Raw,
rootType: JsonRootType.Object,
data: text,
};
const textType = whatTextType(text);
result.textType = ['xml', 'json'].includes(textType) ? textType : 'raw';
result.textType = ['xml', 'json'].includes(textType) ? textType as ApiBodyType : ApiBodyType.Raw;
switch (result.textType) {
case 'xml': {
result.data = xml2UiData(text);
result.data = flatData(Object.keys(result.data).map((it) => parseTree(it, result.data[it])));
break;
}
case 'json': {
try {
result.data = JSON.parse(result.data);
result.data = flatData(Object.keys(result.data).map((it) => parseTree(it, result.data[it])));
} catch (error) {
console.error('text2UiData', error);
result.textType = 'raw';
}
result.data = JSON.parse(result.data);
result.data = flatData(Object.keys(result.data).map((it) => parseTree(it, result.data[it])));
break;
}
default: {
Expand All @@ -273,9 +268,9 @@ export const text2UiData: (text: string) => uiData = (text) => {
* @param inputOptions
* @returns
*/
export const uiData2Json = function (eoapiArr: ApiEditBody[], inputOptions) {
export const uiData2Json = function(eoapiArr: ApiEditBody[], inputOptions) {
inputOptions = inputOptions || {};
const result = {};
let result = {};
const loopFun = (inputArr, inputObject) => {
if (inputOptions.checkXmlAttr) {
inputObject['@eo_attr'] = inputObject['@eo_attr'] || {};
Expand All @@ -287,10 +282,9 @@ export const uiData2Json = function (eoapiArr: ApiEditBody[], inputOptions) {
if (!val.required && !inputOptions.ignoreCheckbox) {
continue;
}
const tmpKey = val.nameHtml || val.name;
const tmpKey = val.name;
if (inputOptions.checkXmlAttr) {
if (val.isErrorXmlAttr) {
// $rootScope.InfoModal('请填写正确格式的XML属性列表再进行转换', 'warning');
throw new Error('errorXmlAttr');
}
if (inputObject['@eo_attr'].hasOwnProperty(tmpKey)) {
Expand All @@ -302,45 +296,15 @@ export const uiData2Json = function (eoapiArr: ApiEditBody[], inputOptions) {
inputObject['@eo_attr'][tmpKey] = (val.attribute || '').replace(/\s+/, ' ');
}
}
if (inputOptions.defaultValueKey) {
inputObject[tmpKey] = val[inputOptions.defaultValueKey];
} else {
inputObject[tmpKey] = val.paramInfo;
}
inputObject[tmpKey] = val.example;
if (val.children && val.children.length > 0) {
switch (val.type) {
case 'array': {
const tmp_child_zero_item = val.children[0];
if (tmp_child_zero_item.isArrItem) {
// 判断是否为新数组格式
if (inputOptions.checkXmlAttr) {
inputObject['@eo_attr'][tmpKey] = [];
}
inputObject[tmpKey] = [];
val.children.forEach((tmp_arr_item) => {
if (!(tmp_arr_item.checkbox || tmp_arr_item.paramNotNull || inputOptions.ignoreCheckbox)) {
return;
}
if (inputOptions.checkXmlAttr) {
const tmp_attr = typeof tmp_arr_item.attribute === 'string' ? tmp_arr_item.attribute : '';
inputObject['@eo_attr'][tmpKey].push(tmp_attr.replace(/\s+/, ' '));
}
let tmp_result_arr_item = {};
if (tmp_arr_item.type === 'array' || !(tmp_arr_item.children && tmp_arr_item.children.length > 0)) {
loopFun([tmp_arr_item], tmp_result_arr_item);
tmp_result_arr_item = tmp_result_arr_item[tmp_arr_item.nameHtml || tmp_arr_item.name];
} else {
loopFun(tmp_arr_item.children, tmp_result_arr_item);
}
inputObject[tmpKey].push(tmp_result_arr_item);
});
} else {
if (inputOptions.checkXmlAttr) {
inputObject['@eo_attr'][tmpKey] = [inputObject['@eo_attr'][tmpKey]];
}
inputObject[tmpKey] = [{}];
loopFun(val.children, inputObject[tmpKey][0]);
if (inputOptions.checkXmlAttr) {
inputObject['@eo_attr'][tmpKey] = [inputObject['@eo_attr'][tmpKey]];
}
inputObject[tmpKey] = [{}];
loopFun(val.children, inputObject[tmpKey][0]);
break;
}
default: {
Expand Down Expand Up @@ -379,5 +343,8 @@ export const uiData2Json = function (eoapiArr: ApiEditBody[], inputOptions) {
}
};
loopFun(eoapiArr, result);
if (inputOptions.rootType === JsonRootType.Array) {
result = [result];
}
return result;
};
16 changes: 11 additions & 5 deletions src/workbench/browser/src/app/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ export const whatType = (data: any): string => {
*
* @returns textType - xml|json|html|text
*/
export const whatTextType = (tmpText) => {
export const whatTextType = (tmpText): 'xml' | 'json' | 'html' | 'text' => {
// TODO it can be better
const tmpCompareText = tmpText.replace(/\s/g, '');
if (/^({|\[)(.*)(}|])$/.test(tmpCompareText) && JSON.parse(tmpCompareText)) {
return 'json';
} else if (/^(<)(.*)(>)$/.test(tmpCompareText)) {
if (/^({|\[)(.*)(}|])$/.test(tmpCompareText)) {
try {
JSON.parse(tmpCompareText);
return 'json';
} catch (e) {}
}
if (/^(<)(.*)(>)$/.test(tmpCompareText)) {
if (/^(<!DOCTYPEhtml>)|(html)/i.test(tmpCompareText)) {
return 'html';
} else {
Expand Down Expand Up @@ -191,7 +195,9 @@ export function throttle(fn, gap) {
}

export const eoDeepCopy = (obj) => {
if (structuredClone) {return structuredClone(obj);}
if (structuredClone) {
return structuredClone(obj);
}
let copy;

// Handle the 3 simple types, and null or undefined
Expand Down

0 comments on commit ad4c943

Please sign in to comment.