Skip to content

Commit

Permalink
fix: the first time the default mock is not shown in the api document
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Jun 6, 2022
1 parent ef81576 commit a9a64e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
StorageHandleResult,
StorageHandleStatus,
} from 'eo/platform/browser/IndexedDB';
import { treeToListHasLevel } from '../../../utils/tree/tree.utils';
import { tree2obj, treeToListHasLevel } from '../../../utils/tree/tree.utils';
import { reverseObj } from '../../../utils';
import { StorageService } from '../../../shared/services/storage';

Expand Down Expand Up @@ -44,13 +44,26 @@ export class ApiDetailComponent implements OnInit {
}
getApiByUuid(id: number) {
this.storage.run('apiDataLoad', [id], (result: StorageHandleResult) => {
this.apiData = result.data;
if (result.status === StorageHandleStatus.success) {
// 如果没有mock,则生成系统默认mock
if ((window.eo?.getMockUrl && !Array.isArray(this.apiData.mockList)) || this.apiData.mockList?.length === 0) {
const url = new URL(this.apiData.uri, window.eo.getMockUrl());
this.apiData.mockList = [
{
name: '系统默认期望',
url: url.toString(),
response: JSON.stringify(tree2obj([].concat(this.apiData.responseBody))),
isDefault: true,
},
];
}

['requestBody', 'responseBody'].forEach((tableName) => {
if (['xml', 'json'].includes(result.data[`${tableName}Type`])) {
result.data[tableName] = treeToListHasLevel(result.data[tableName]);
if (['xml', 'json'].includes(this.apiData[`${tableName}Type`])) {
this.apiData[tableName] = treeToListHasLevel(this.apiData[tableName]);
}
});
this.apiData = result.data;
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/workbench/browser/src/app/utils/tree/tree.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ export const getExpandGroupByKey = (component, key) => {
* @param opts
* @returns
*/
export const tree2obj = (list: any[], opts: TreeToObjOpts = {}, initObj = {}) => {
export const tree2obj = (list: any[] = [], opts: TreeToObjOpts = {}, initObj = {}) => {
const { key = 'name', valueKey = 'description', childKey = 'children' } = opts;

return list.reduce((prev, curr) => {
return list?.reduce?.((prev, curr) => {
prev[curr[key]] = curr[valueKey];
if (Array.isArray(curr[childKey]) && curr[childKey].length > 0) {
tree2obj(curr[childKey], opts, (prev[curr[key]] = {}));
Expand Down

0 comments on commit a9a64e2

Please sign in to comment.