Skip to content

Commit

Permalink
fix: import openAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Jan 20, 2023
1 parent 805af93 commit bbfc226
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { EoNgFeedbackMessageService } from 'eo-ng-feedback';
import { old2new } from 'eo/workbench/browser/src/app/modules/extension-select/import-api/old2new';
import { FeatureInfo } from 'eo/workbench/browser/src/app/shared/models/extension-manager';
import { ExtensionService } from 'eo/workbench/browser/src/app/shared/services/extensions/extension.service';
import { StorageRes, StorageResStatus } from 'eo/workbench/browser/src/app/shared/services/storage/index.model';
Expand Down Expand Up @@ -93,7 +94,7 @@ export class ImportApiComponent implements OnInit {
const feature = this.featureMap.get(this.currentExtension);
const action = feature.action || null;
const module = await this.extensionService.getExtensionPackage(this.currentExtension);
const { name, content } = this.uploadData;
let { name, content } = this.uploadData;
try {
const [data, err] = module[action](content);
// console.log('import data', window.structuredClone?.(data));
Expand Down Expand Up @@ -124,6 +125,14 @@ export class ImportApiComponent implements OnInit {
// return obj;
// };
try {
const projectUuid = this.store.getCurrentProjectID;
const workSpaceUuid = this.store.getCurrentWorkspaceUuid;
console.log('content', content);
// TODO 兼容旧数据
if (Reflect.has(data, 'collections') && Reflect.has(data, 'environments')) {
content = old2new(data, projectUuid, workSpaceUuid);
console.log('new content', content);
}
if (this.store.isLocal) {
await this.effectService.projectImport('local', content);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { ImportProjectDto } from 'eo/workbench/browser/src/app/shared/services/storage/db/dto/project.dto';

import { convertApiData } from './../../../shared/services/storage/db/dataSource/convert';

export const old2new = (params, projectUuid, workSpaceUuid): ImportProjectDto => {
const { collections = [], environments } = params;

const environmentList = environments.map(n => ({
name: n.name,
hostUri: n.hostUri,
parameters: n.parameters,
projectUuid,
workSpaceUuid
}));

const genId = () => crypto.getRandomValues(new Uint32Array(1))[0];

const genGroup = (name, parentId?) => {
return {
name,
parentId,
id: genId(),
type: 1,
projectUuid,
workSpaceUuid,
children: []
};
};

const rootGroup = genGroup(collections[0].name);

const apiList = [];
const groupList = [rootGroup];

const formatData = (collections = [], parentGroup) => {
collections.forEach(item => {
// API
if (item.uri) {
const newApiData = convertApiData(item);
newApiData.groupId = parentGroup.id;
apiList.push(newApiData);
}
// 分组
else {
const group = genGroup(item.name, parentGroup.id);

parentGroup.children.push(group);

if (item.children?.length) {
formatData(item.children, group);
}
}
});
};

collections[0].id = genId();
formatData(collections[0]?.children, groupList[0]);

return {
name: 'Default',
apiList,
groupList,
environmentList
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class ProjectService extends BaseService<Project> {
.filter(n => n.depth !== 0)
.map(n => {
const { id, children, ...rest } = n;
rest.parentId ??= rootGroup[0].id;
return rest;
});
let remoteGroups = rootGroup;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ export class EffectService {
.filter(n => n.depth !== 0)
.map(n => {
const { id, children, ...rest } = n;
rest.parentId ??= rootGroup.id;
return rest;
});
const [remoteGroups] = groupFilters.length ? await this.remote.api_groupCreate(groupFilters) : [[rootGroup]];
Expand Down

0 comments on commit bbfc226

Please sign in to comment.