diff --git a/src/app/eoui/editor/eo-editor/eo-editor.component.ts b/src/app/eoui/editor/eo-editor/eo-editor.component.ts index 2abfaaa7..e071d7a5 100644 --- a/src/app/eoui/editor/eo-editor/eo-editor.component.ts +++ b/src/app/eoui/editor/eo-editor/eo-editor.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, Output, EventEmitter, OnChanges, AfterViewInit, ViewChild } from '@angular/core'; +import { Component, Input, Output, EventEmitter, OnChanges, AfterViewInit, ViewChild, OnInit } from '@angular/core'; import { NzMessageService } from 'ng-zorro-antd/message'; import { AceConfigInterface, AceComponent, AceDirective } from 'ngx-ace-wrapper'; import { whatTextType } from '../../../utils'; @@ -44,7 +44,7 @@ const eventHash = new Map() templateUrl: './eo-editor.component.html', styleUrls: ['./eo-editor.component.scss'], }) -export class EoEditorComponent implements AfterViewInit, OnChanges { +export class EoEditorComponent implements AfterViewInit, OnInit, OnChanges { @Input() eventList: EventType[] = []; @Input() hiddenList: string[] = []; @Input() code: string; @@ -82,15 +82,7 @@ export class EoEditorComponent implements AfterViewInit, OnChanges { constructor(private message: NzMessageService) {} - ngAfterViewInit(): void { - // To get the Ace instance: - this.buttonList = this.eventList - .filter((it) => it !== 'type') - .map((it) => ({ - event: it, - ...eventHash.get(it), - })); - } + ngAfterViewInit(): void {} ngOnChanges() { // * update root type if (this.eventList.includes('type') && !this.hiddenList.includes('type')) { @@ -101,6 +93,15 @@ export class EoEditorComponent implements AfterViewInit, OnChanges { } } } + ngOnInit() { + // To get the Ace instance: + this.buttonList = this.eventList + .filter((it) => it !== 'type') + .map((it) => ({ + event: it, + ...eventHash.get(it), + })); + } log(event, txt) { console.log('ace event', event, txt); } diff --git a/src/app/pages/api/api.component.html b/src/app/pages/api/api.component.html index beb873c2..b2929d1a 100644 --- a/src/app/pages/api/api.component.html +++ b/src/app/pages/api/api.component.html @@ -20,15 +20,16 @@
- - - 文档 - - - 编辑 - - - 测试 + + + {{ tab.title }} diff --git a/src/app/pages/api/api.component.ts b/src/app/pages/api/api.component.ts index 2ae48323..6aa4cab6 100644 --- a/src/app/pages/api/api.component.ts +++ b/src/app/pages/api/api.component.ts @@ -1,9 +1,8 @@ -import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'; +import { Component, OnDestroy, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { NzFormatEmitEvent, NzTreeNode } from 'ng-zorro-antd/tree'; import { Message, MessageService } from '../../shared/services/message'; -import { ApiTabComponent } from './tab/api-tab.component'; import { GroupService } from '../../shared/services/group/group.service'; import { NzModalService } from 'ng-zorro-antd/modal'; import { ApiDataService } from '../../shared/services/api-data/api-data.service'; @@ -13,15 +12,15 @@ import { GroupApiDataModel, GroupTreeItem } from '../../shared/models'; import { ApiData } from '../../shared/services/api-data/api-data.model'; import { Group } from '../../shared/services/group/group.model'; -import { Subject } from 'rxjs'; +import { Subject } from 'rxjs'; import { takeUntil } from 'rxjs/operators'; +import { ApiTabService } from './tab/api-tab.service'; @Component({ selector: 'eo-api', templateUrl: './api.component.html', styleUrls: ['./api.component.scss'], }) export class ApiComponent implements OnInit, OnDestroy { - @ViewChild(ApiTabComponent) apiTabComponent: ApiTabComponent; /** * Default projectID. */ @@ -49,13 +48,28 @@ export class ApiComponent implements OnInit, OnDestroy { */ id: number; + TABS = [ + { + routerLink: 'detail', + title: '文档', + }, + { + routerLink: 'edit', + title: '编辑', + }, + { + routerLink: 'test', + title: '测试', + }, + ]; private destroy$: Subject = new Subject(); constructor( private route: ActivatedRoute, private groupService: GroupService, private apiDataService: ApiDataService, private messageService: MessageService, - private modalService: NzModalService + private modalService: NzModalService, + private tabSerive: ApiTabService ) {} ngOnInit(): void { @@ -93,7 +107,7 @@ export class ApiComponent implements OnInit, OnDestroy { } getApis() { this.apiDataService.loadAllByProjectID(this.projectID).subscribe((items: Array) => { - let apiItems={}; + let apiItems = {}; items.forEach((item) => { delete item.updatedAt; apiItems[item.uuid] = item; @@ -106,7 +120,7 @@ export class ApiComponent implements OnInit, OnDestroy { isLeaf: true, }); }); - this.apiDataItems=apiItems; + this.apiDataItems = apiItems; this.generateGroupTreeData(); }); } @@ -119,6 +133,9 @@ export class ApiComponent implements OnInit, OnDestroy { this.id = Number(params.get('uuid')); }); } + clickContentMenu(data) { + this.tabSerive.apiEvent$.next({ action: 'beforeChangeRouter', data: data }); + } /** * Event emit from group tree component. * @@ -132,27 +149,16 @@ export class ApiComponent implements OnInit, OnDestroy { case 'loadAllGroup': this.buildGroupTreeData(); break; - case 'testApi': - this.testApi(event.node); - break; - case 'detailApi': - this.detailApi(event.node); - break; - case 'editApi': - this.editApi(event.node); - break; case 'copyApi': this.copyApi(event.node); break; case 'deleteApi': this.deleteApi(event.node); break; - case 'newApi': - this.newApi(event.node); - break; - case 'newApiTest': - this.newApiTest(); + default: { + this.tabSerive.apiEvent$.next({ action: event.eventName, data: event.node }); break; + } } } @@ -165,42 +171,29 @@ export class ApiComponent implements OnInit, OnDestroy { .pipe(takeUntil(this.destroy$)) .subscribe((data: Message) => { switch (data.type) { - case 'apiAdd': - case 'editApi': + case 'addApi': + case 'editApi': + { + this.tabSerive.apiEvent$.next({ action: `${data.type}Finish`,data:data.data}); + this.buildGroupTreeData(); + break; + } case 'groupAdd': case 'groupEdit': case 'groupDelete': this.buildGroupTreeData(); break; case 'apiDelete': - this.watchApiDelete(data.data); + let tmpApi = data.data; + this.tabSerive.apiEvent$.next({ action: 'removeApiDataTabs', data: [tmpApi.uuid] }); + this.buildGroupTreeData(); break; case 'apiBatchDelete': - this.watchApiBatchDelete(data.data); + this.tabSerive.apiEvent$.next({ action: 'removeApiDataTabs', data: data.data.uuids }); break; } }); } - - /** - * Delete api data tabs after item removed. - * - * @param data object - */ - watchApiDelete(data) { - this.apiTabComponent.removeApiDataTabs([data.uuid]); - this.buildGroupTreeData(); - } - - /** - * Delete api data tabs after items removed. - * - * @param data object - */ - watchApiBatchDelete(data) { - this.apiTabComponent.removeApiDataTabs(data.uuids); - } - /** * Generate group tree nodes. */ @@ -209,51 +202,6 @@ export class ApiComponent implements OnInit, OnDestroy { this.treeNodes = []; listToTree(this.treeItems, this.treeNodes, 0); } - - /** - * Create a new tab of add new api data. - * - * @param item GroupTreeItem - */ - newApi(node?: NzTreeNode): void { - console.log('newApi',node) - this.apiTabComponent.appendTab('edit', node ? { groupID: node.key } : {}); - } - - /** - * Create a new tab of add new api test. - */ - newApiTest(): void { - this.apiTabComponent.appendTab('test'); - } - - /** - * Test api data. - * - * @param node NzTreeNode - */ - testApi(node: NzTreeNode): void { - this.apiTabComponent.appendTab('test', node.origin); - } - - /** - * View api data. - * - * @param node NzTreeNode - */ - detailApi(node: NzTreeNode): void { - this.apiTabComponent.appendTab('detail', node.origin); - } - - /** - * Edit api data. - * - * @param node NzTreeNode - */ - editApi(node: NzTreeNode): void { - this.apiTabComponent.appendTab('edit', node.origin); - } - /** * Copy api data. * @@ -264,13 +212,9 @@ export class ApiComponent implements OnInit, OnDestroy { delete data.uuid; delete data.createdAt; data.name += ' Copy'; - this.apiDataService.create(data).subscribe((result: ApiData) => { - this.buildGroupTreeData(); - this.apiTabComponent.appendTab('edit', { - title: result.name, - key: result.uuid, - method: result.method, - }); + window.sessionStorage.setItem('apiDataWillbeSave', JSON.stringify(data)); + this.tabSerive.apiEvent$.next({ + action: 'newApi', }); } diff --git a/src/app/pages/api/demo.json b/src/app/pages/api/demo.json deleted file mode 100644 index 8b137891..00000000 --- a/src/app/pages/api/demo.json +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/app/pages/api/detail/api-detail.component.html b/src/app/pages/api/detail/api-detail.component.html index 6ed51b2a..81ff1eb9 100644 --- a/src/app/pages/api/detail/api-detail.component.html +++ b/src/app/pages/api/detail/api-detail.component.html @@ -17,7 +17,7 @@

Rest 参数

-
+
Body 请求参数{{ CONST.BODY_TYPE[apiData.requestBodyType] }} 最外层结构为:{{ CONST.JSON_ROOT_TYPE[apiData.requestBodyJsonType] }} diff --git a/src/app/pages/api/edit/api-edit.component.html b/src/app/pages/api/edit/api-edit.component.html index 6c54fce1..106879ea 100644 --- a/src/app/pages/api/edit/api-edit.component.html +++ b/src/app/pages/api/edit/api-edit.component.html @@ -51,7 +51,9 @@ 请求头部 - {{ apiData.requestHeaders | apiParamsNum }} + {{ + apiData.requestHeaders | apiParamsNum + }} @@ -59,7 +61,14 @@ 请求体 - {{ apiData.requestBody | apiParamsNum }} + Query 参数 - {{ apiData.queryParams | apiParamsNum }} + {{ + apiData.queryParams | apiParamsNum + }} REST 参数 - {{ apiData.restParams | apiParamsNum }} + {{ + apiData.restParams | apiParamsNum + }} - + - - - {{ panel.nzActive ? '收缩' : '展开' }} - + + + {{ panelRes.nzActive ? '收缩' : '展开' }} + 返回头部 - {{ apiData.responseHeaders | apiParamsNum }} + {{ + apiData.responseHeaders | apiParamsNum + }} 返回结果 - {{ apiData.responseBody | apiParamsNum }} + ; private destroy$: Subject = new Subject(); private changeGroupID$: Subject = new Subject(); @@ -41,10 +42,10 @@ export class ApiEditComponent implements OnInit, OnDestroy { private storage: ApiDataService, private route: ActivatedRoute, private fb: FormBuilder, - private router: Router, private message: NzMessageService, private messageService: MessageService, - private groupService: GroupService + private groupService: GroupService, + private apiTab: ApiTabService ) {} getApiGroup() { this.groups = []; @@ -115,22 +116,37 @@ export class ApiEditComponent implements OnInit, OnDestroy { this.editApi(formData); } - + bindGetApiParamNum(params) { + return new ApiParamsNumPipe().transform(params); + } ngOnInit(): void { this.getApiGroup(); - this.resetApi(); + this.initApi(Number(this.route.snapshot.queryParams.uuid)); + this.watchTabChange(); + this.watchGroupIDChange(); + this.watchUri(); + } + + ngOnDestroy() { + this.destroy$.next(); + this.destroy$.complete(); + } + private initApi(id) { + this.resetForm(); this.initBasicForm(); - if (this.route.snapshot.queryParams.uuid) { - //Edit - this.watchQueryChange(); - } else { - let testData = window.sessionStorage.getItem('testDataToAPI'); - if (testData) { - //Add From Test - Object.assign( - this.apiData, - JSON.parse(testData) - ); + //recovery from tab + if (this.apiTab.currentTab && this.apiTab.tabCache[this.apiTab.tabID]) { + let tabData = this.apiTab.tabCache[this.apiTab.tabID]; + this.apiData = tabData.apiData; + return; + } + if (!id) { + let tmpApiData = window.sessionStorage.getItem('apiDataWillbeSave'); + if (tmpApiData) { + //Add From Test|Copy Api + window.sessionStorage.removeItem('apiDataWillbeSave'); + Object.assign(this.apiData, JSON.parse(tmpApiData)); + this.validateForm.patchValue(this.apiData); } else { //Add directly Object.assign(this.apiData, { @@ -146,70 +162,56 @@ export class ApiEditComponent implements OnInit, OnDestroy { responseBody: [], }); } + } else { + this.getApi(id); } - + } + private watchTabChange() { + this.apiTab.tabChange$ + .pipe( + pairwise(), + //actually change tab,not init tab + filter((data) => data[0].uuid !== data[1].uuid), + takeUntil(this.destroy$) + ) + .subscribe(([nowTab, nextTab]) => { + this.apiTab.saveTabData$.next({ + tab: nowTab, + data: { + apiData: this.apiData, + }, + }); + this.initApi(nextTab.key); + }); + } + private watchGroupIDChange() { this.changeGroupID$.pipe(debounceTime(500), take(1)).subscribe((id) => { this.apiData.groupID = (this.apiData.groupID === 0 ? -1 : this.apiData.groupID).toString(); this.expandGroup(); }); - this.validateForm - .get('uri') - .valueChanges.pipe(debounceTime(500), takeUntil(this.destroy$)) - .subscribe((url) => { - this.changeUri(url); - }); - } - - ngOnDestroy() { - this.destroy$.next(); - this.destroy$.complete(); - } - private watchQueryChange() { - this.api$ = this.route.queryParamMap.pipe( - switchMap((params) => { - const id = Number(params.get('uuid')); - if (!id) { - const groupID = params.get('groupID'); - if (groupID) { - return of({ - groupID, - }); - } - return of(); - } - return of({ id }); - }), - takeUntil(this.destroy$) - ); - this.api$.subscribe({ - next: (inArg: any = {}) => { - if (inArg.id) { - this.getApi(inArg.id); - } - if (inArg.groupID) { - this.apiData.groupID = inArg.groupID; - this.changeGroupID$.next(this.apiData.groupID); - } - }, - }); } /** * Generate Rest Param From Url */ - private changeUri(url) { - const rests = getRest(url); - rests.forEach((newRest) => { - if (this.apiData.restParams.find((val: ApiEditRest) => val.name === newRest)) { - return; - } - const restItem: ApiEditRest = { - name: newRest, - required: true, - example: '', - description: '', - }; - this.apiData.restParams.splice(this.apiData.restParams.length - 1, 0, restItem); - }); + private watchUri() { + this.validateForm + .get('uri') + .valueChanges.pipe(debounceTime(500), takeUntil(this.destroy$)) + .subscribe((url) => { + const rests = getRest(url); + rests.forEach((newRest) => { + if (this.apiData.restParams.find((val: ApiEditRest) => val.name === newRest)) { + return; + } + const restItem: ApiEditRest = { + name: newRest, + required: true, + example: '', + description: '', + }; + this.apiData.restParams.splice(this.apiData.restParams.length - 1, 0, restItem); + }); + }); } /** * Reset Group ID after group list load @@ -250,25 +252,24 @@ export class ApiEditComponent implements OnInit, OnDestroy { /** * Init API data structure */ - private resetApi() { + private resetForm() { this.apiData = { name: '', projectID: 1, uri: '/', - groupID: this.route.snapshot.queryParams.groupID||'-1', + groupID: this.route.snapshot.queryParams.groupID || '-1', protocol: RequestProtocol.HTTP, method: RequestMethod.POST, }; } private editApi(formData) { - const busEvent = formData.uuid ? 'editApi' : 'apiAdd'; + const busEvent = formData.uuid ? 'editApi' : 'addApi'; const title = busEvent === 'editApi' ? '编辑成功' : '新增成功'; this.storage[busEvent === 'editApi' ? 'update' : 'create'](formData, this.apiData.uuid).subscribe( (result: ApiData) => { this.message.success(title); this.messageService.send({ type: busEvent, data: result }); - this.router.navigate(['/home/api/detail'], { queryParams: { uuid: result.uuid } }); } ); } diff --git a/src/app/pages/api/edit/api-edit.service.ts b/src/app/pages/api/edit/api-edit.service.ts index a8a380d6..25585fb2 100644 --- a/src/app/pages/api/edit/api-edit.service.ts +++ b/src/app/pages/api/edit/api-edit.service.ts @@ -213,11 +213,12 @@ export class ApiEditService { { key: '插入', operateName: 'insert', - itemExpression: `ng-if="!($ctrl.mainObject.setting.munalHideOperateColumn&&$first)"`, + itemExpression: `ng-if="!($ctrl.mainObject.setting.munalHideOperateColumn&&$first)"` }, { key: '删除', operateName: 'delete', + itemExpression: 'ng-if="!($ctrl.mainObject.setting.munalHideOperateColumn&&$first)"' }, ], }, diff --git a/src/app/pages/api/group/tree/api-group-tree.component.html b/src/app/pages/api/group/tree/api-group-tree.component.html index 49900c31..b1dc2554 100644 --- a/src/app/pages/api/group/tree/api-group-tree.component.html +++ b/src/app/pages/api/group/tree/api-group-tree.component.html @@ -22,7 +22,7 @@ @@ -48,7 +48,7 @@
- {{ node.title }} + {{ node.title }}
- + @@ -45,7 +53,9 @@ 请求头部 - {{ apiData.requestHeaders | apiParamsNum }} + {{ + apiData.requestHeaders | apiParamsNum + }} @@ -53,7 +63,14 @@ 请求体 - {{ apiData.requestBody | apiParamsNum }} + Query 参数 - {{ apiData.queryParams | apiParamsNum }} + {{ + apiData.queryParams | apiParamsNum + }} REST 参数 - {{ apiData.restParams | apiParamsNum }} + {{ + apiData.restParams | apiParamsNum + }} @@ -85,7 +106,7 @@ diff --git a/src/app/pages/api/test/api-test.component.ts b/src/app/pages/api/test/api-test.component.ts index 677ba5b9..f7e9e928 100644 --- a/src/app/pages/api/test/api-test.component.ts +++ b/src/app/pages/api/test/api-test.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit, OnDestroy, ChangeDetectorRef, ViewChild } from '@angular/core'; -import { ActivatedRoute, Router } from '@angular/router'; +import { ActivatedRoute } from '@angular/router'; import { FormBuilder, FormGroup, Validators } from '@angular/forms'; import { Select } from '@ngxs/store'; @@ -8,7 +8,7 @@ import { ApiData, RequestMethod, RequestProtocol } from '../../../shared/service import { MessageService } from '../../../shared/services/message'; import { interval, Subscription, Observable, of, Subject } from 'rxjs'; -import { take, takeUntil, distinctUntilChanged, pairwise } from 'rxjs/operators'; +import { take, takeUntil, distinctUntilChanged, pairwise, filter } from 'rxjs/operators'; import { ApiTestHistoryComponent } from './history/api-test-history.component'; @@ -19,6 +19,7 @@ import { ApiTabService } from '../tab/api-tab.service'; import { objectToArray } from '../../../utils'; import { EnvState } from '../../../shared/store/env.state'; +import { ApiParamsNumPipe } from '../../../shared/pipes/api-param-num.pipe'; @Component({ selector: 'eo-api-test', @@ -47,18 +48,16 @@ export class ApiTestComponent implements OnInit, OnDestroy { private status$: Subject = new Subject(); private timer$: Subscription; - private api$: Observable; private destroy$: Subject = new Subject(); constructor( private fb: FormBuilder, - private router: Router, - private testServerService: TestServerService, private storage: ApiDataService, private route: ActivatedRoute, - private messageService: MessageService, private ref: ChangeDetectorRef, private apiTest: ApiTestService, - private apiTab: ApiTabService + private apiTab: ApiTabService, + private testServerService: TestServerService, + private messageService: MessageService ) { this.testServer = this.testServerService.getService(); this.testServer.init((message) => { @@ -105,8 +104,8 @@ export class ApiTestComponent implements OnInit, OnDestroy { history: this.testResult, testData: this.apiData, }); - window.sessionStorage.setItem('testDataToAPI', JSON.stringify(apiData)); - this.router.navigate(['/home/api/edit']); + window.sessionStorage.setItem('apiDataWillbeSave', JSON.stringify(apiData)); + this.apiTab.apiEvent$.next({action:'addApiFromTest',data:apiData}) } changeQuery() { this.apiData.uri = this.apiTest.transferUrlAndQuery(this.apiData.uri, this.apiData.queryParams, { @@ -120,6 +119,9 @@ export class ApiTestComponent implements OnInit, OnDestroy { replaceType: 'replace', }).query; } + bindGetApiParamNum(params) { + return new ApiParamsNumPipe().transform(params); + } ngOnInit(): void { this.initApi(Number(this.route.snapshot.queryParams.uuid)); this.watchTabChange(); @@ -215,16 +217,23 @@ export class ApiTestComponent implements OnInit, OnDestroy { }); } private watchTabChange() { - this.apiTab.tabChange$.pipe(pairwise(), takeUntil(this.destroy$)).subscribe(([nowTab, nextTab]) => { - this.apiTab.saveTabData$.next({ - tab: nowTab, - data: { - apiData: this.apiData, - testResult: this.testResult, - }, + this.apiTab.tabChange$ + .pipe( + pairwise(), + //actually change tab,not init tab + filter((data) => data[0].uuid !== data[1].uuid), + takeUntil(this.destroy$) + ) + .subscribe(([nowTab, nextTab]) => { + this.apiTab.saveTabData$.next({ + tab: nowTab, + data: { + apiData: this.apiData, + testResult: this.testResult, + }, + }); + this.initApi(nextTab.key); }); - this.initApi(nextTab.key); - }); } /** * Init API data structure diff --git a/src/app/pages/api/test/api-test.service.ts b/src/app/pages/api/test/api-test.service.ts index 8951741d..198496a3 100644 --- a/src/app/pages/api/test/api-test.service.ts +++ b/src/app/pages/api/test/api-test.service.ts @@ -146,7 +146,7 @@ export class ApiTestService { * @description Add query to URL and read query form url * @param {string} url - whole url include query * @param {object} query - ui query param - * @param {string} opts.priority - who's priority higher,url or query + * @param {string} opts.priority - which as priority higher,url or query * @param {string} opts.replaceType - replace means only keep replace array,merge means union * @returns {object} - {url:"",query:[]} */ @@ -252,7 +252,6 @@ export class ApiTestService { * @returns {ApiData} */ getApiFromTestData(inData) { - console.log('getApiFromTestData=>', inData); let testToEditParams = (arr) => { let result = []; arr.forEach((val) => { @@ -265,7 +264,7 @@ export class ApiTestService { }; let result = { ...inData.testData, - responseHeaders: [], + responseHeaders: inData.history.response.headers, responseBodyType: 'json', responseBodyJsonType: 'object', responseBody: [], @@ -281,7 +280,6 @@ export class ApiTestService { result.responseBodyType=bodyInfo.textType; result.responseBodyJsonType=bodyInfo.rootType; } - console.log('getApiFromTestData=>', result); return result; } getTestDataFromApi(inData) { diff --git a/src/app/shared/pipes/api-param-num.pipe.ts b/src/app/shared/pipes/api-param-num.pipe.ts index cb1acd30..ce701ac8 100644 --- a/src/app/shared/pipes/api-param-num.pipe.ts +++ b/src/app/shared/pipes/api-param-num.pipe.ts @@ -1,5 +1,4 @@ import { Pipe, PipeTransform } from '@angular/core'; - @Pipe({ name: 'apiParamsNum', pure: false, diff --git a/src/app/shared/services/api-test/apikit-node/test-connect.service.ts b/src/app/shared/services/api-test/apikit-node/test-connect.service.ts index f659f261..19f434f9 100644 --- a/src/app/shared/services/api-test/apikit-node/test-connect.service.ts +++ b/src/app/shared/services/api-test/apikit-node/test-connect.service.ts @@ -154,7 +154,6 @@ export class TestServerAPIKitService implements TestServer { env: formatEnv(opts.env), testTime: formatDate(new Date(), 'YYYY-MM-dd HH:mm:ss', this.locale), }; - console.log(data,result) return result; } /** diff --git a/src/app/utils/api.ts b/src/app/utils/api.ts new file mode 100644 index 00000000..cb2f9e4b --- /dev/null +++ b/src/app/utils/api.ts @@ -0,0 +1,21 @@ +/** + * get rest param from url,format like {restName} + * @param url + * @returns {Array[string]} + */ +export const getRest: (url: string) => string[] = (url) => { + return [...url.replace(/{{(.*?)}}/g, '').matchAll(/{(.*?)}/g)].map((val) => val[1]); +}; + +export const addEnvPrefix = (prefix, uri) => { + // * 需要先判断uri是否已经包含 http:// 前缀 + if (prefix == null) { + return uri; + } + const hasPrefix = /(http|https):\/{2}.+/.test(uri); + if (hasPrefix) { + return uri; + } + // * 添加前缀 + return prefix + uri; +}; diff --git a/src/app/utils/index.ts b/src/app/utils/index.ts index bd453938..9934bc09 100644 --- a/src/app/utils/index.ts +++ b/src/app/utils/index.ts @@ -1,25 +1,5 @@ export const uuid = (): string => Math.random().toString(36).slice(-8); -/** - * get rest param from url,format like {restName} - * @param url - * @returns {Array[string]} - */ -export const getRest: (url: string) => string[] = (url) => { - return [...url.replace(/{{(.*?)}}/g, '').matchAll(/{(.*?)}/g)].map((val) => val[1]); -}; -export const addEnvPrefix = (prefix, uri) => { - // * 需要先判断uri是否已经包含 http:// 前缀 - if (prefix == null) { - return uri; - } - const hasPrefix = /(http|https):\/{2}.+/.test(uri); - if (hasPrefix) { - return uri; - } - // * 添加前缀 - return prefix + uri; -}; // const DOMAIN_REGEX = // '(^((http|wss|ws|ftp|https)://))|(^(((http|wss|ws|ftp|https)://)|)(([\\w\\-_]+([\\w\\-\\.]*)?(\\.(' + diff --git a/src/app/utils/tree.ts b/src/app/utils/tree.ts index b314308f..1d5f2ba3 100644 --- a/src/app/utils/tree.ts +++ b/src/app/utils/tree.ts @@ -47,7 +47,7 @@ export const treeToListHasLevel = (tree, opts: { listDepth: number; mapItem?: (v val = opts.mapItem(val); } result.push(val); - if (val.children && val.children.length) { + if (val.children?.length) { result = result.concat( treeToListHasLevel(val.children, { listDepth: opts.listDepth + 1,