diff --git a/.env b/.env new file mode 100644 index 00000000..b2b64a35 --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +EXTENSION_URL = http://106.12.149.147 +MOCK_URL = https://mockapi.eolink.com/ztBFKai20ee60c12871881565b5a6ddd718337df0e30979 diff --git a/package.json b/package.json index 5e71197d..0252c8c3 100644 --- a/package.json +++ b/package.json @@ -33,22 +33,22 @@ "dependencies": { "@bqy/node-module-alias": "^1.0.1", "@electron/remote": "2.0.8", + "content-disposition": "^0.5.4", "copyfiles": "2.4.1", + "crypto-js": "^4.1.1", "electron-log": "^4.4.8", "electron-store": "8.0.2", "electron-updater": "^5.0.5", "express": "4.18.1", "fix-path": "3.0.0", + "form-data": "^4.0.0", + "iconv-lite": "^0.6.3", + "jquery": "^3.3.0", + "jsdom": "^11.5.1", "npm": "6.14.17", "portfinder": "1.0.28", "resolve": "^1.22.1", "rxjs": "7.5.5", - "jquery": "^3.3.0", - "jsdom": "^11.5.1", - "content-disposition": "^0.5.4", - "crypto-js": "^4.1.1", - "form-data": "^4.0.0", - "iconv-lite": "^0.6.3", "xml2js": "^0.4.23" }, "devDependencies": { @@ -56,6 +56,8 @@ "@typescript-eslint/eslint-plugin": "5.29.0", "@typescript-eslint/parser": "5.29.0", "dmg-builder": "23.2.0", + "dotenv": "16.0.1", + "dotenv-webpack": "8.0.0", "electron": "19.0.6", "electron-builder": "23.1.0", "electron-notarize": "1.2.1", @@ -73,4 +75,4 @@ "node-module-alias": { "eo": "./out" } -} \ No newline at end of file +} diff --git a/src/app/electron-main/main.ts b/src/app/electron-main/main.ts index 6a42d2f9..00ad01ed 100644 --- a/src/app/electron-main/main.ts +++ b/src/app/electron-main/main.ts @@ -1,9 +1,10 @@ require('@bqy/node-module-alias/register'); +require('dotenv').config(); import { app, BrowserWindow, ipcMain, screen } from 'electron'; import { EoUpdater } from './updater'; import * as path from 'path'; import * as os from 'os'; -import {ModuleManager} from '../../platform/node/extension-manager/lib/manager'; +import { ModuleManager } from '../../platform/node/extension-manager/lib/manager'; import { ModuleManagerInterface } from '../../platform/node/extension-manager'; import { processEnv } from '../../platform/node/constant'; import { proxyOpenExternal } from '../../shared/common/browserView'; @@ -57,7 +58,7 @@ class EoBrowserWindow { }); } public loadURL() { - console.log('loadURL') + console.log('loadURL'); const file: string = processEnv === 'development' ? 'http://localhost:4200' diff --git a/src/platform/node/extension-manager/lib/handler.ts b/src/platform/node/extension-manager/lib/handler.ts index b93e7ef6..1f190feb 100644 --- a/src/platform/node/extension-manager/lib/handler.ts +++ b/src/platform/node/extension-manager/lib/handler.ts @@ -110,7 +110,7 @@ export class ModuleHandler extends CoreHandler { // https://www.npmjs.com/package/bin-links npmCli.load({ 'bin-links': false, verbose: true, prefix: this.baseDir }, (loaderr) => { const moduleList = modules.map((it) => it + '@latest'); - console.log('moduleList',command, moduleList); + console.log('moduleList', command, moduleList); let executeCommand = ['update', 'install', 'uninstall']; if (!executeCommand.includes(command)) return; npmCli.commands[command](moduleList, (err, data) => { @@ -133,7 +133,7 @@ export class ModuleHandler extends CoreHandler { args = args.concat(`--proxy=${this.proxy}`); } } - console.log(args) + console.log(args); const npm = spawn('npm', args, { cwd: this.baseDir }); // console.log('2==>', npm); let output = ''; diff --git a/src/workbench/browser/angular.webpack.js b/src/workbench/browser/angular.webpack.js index 3876073c..5a60e242 100644 --- a/src/workbench/browser/angular.webpack.js +++ b/src/workbench/browser/angular.webpack.js @@ -1,5 +1,6 @@ //Polyfill Node.js core modules in Webpack. This module is only needed for webpack 5+. const NodePolyfillPlugin = require('node-polyfill-webpack-plugin'); +const Dotenv = require('dotenv-webpack'); /** * Custom angular webpack configuration @@ -22,10 +23,11 @@ module.exports = (config, options) => { } config.plugins = [ ...config.plugins, + new Dotenv({ path: '../../../.env' }), new NodePolyfillPlugin({ excludeAliases: ['console'], }), ]; return config; -}; \ No newline at end of file +}; diff --git a/src/workbench/browser/src/app/app.module.ts b/src/workbench/browser/src/app/app.module.ts index 51b1b7a3..f77dcec9 100644 --- a/src/workbench/browser/src/app/app.module.ts +++ b/src/workbench/browser/src/app/app.module.ts @@ -32,6 +32,7 @@ import { registerLocaleData } from '@angular/common'; import { en_US, NZ_I18N, zh_CN } from 'ng-zorro-antd/i18n'; import en from '@angular/common/locales/en'; import zh from '@angular/common/locales/zh'; +import { ExtensionService } from 'eo/workbench/browser/src/app/pages/extension/extension.service'; registerLocaleData(en); registerLocaleData(zh); @NgModule({ @@ -49,6 +50,7 @@ registerLocaleData(zh); ], providers: [ SettingService, + ExtensionService, StorageService, RemoteService, MessageService, diff --git a/src/workbench/browser/src/app/app.service.ts b/src/workbench/browser/src/app/app.service.ts index a0e0fbce..87e909c2 100644 --- a/src/workbench/browser/src/app/app.service.ts +++ b/src/workbench/browser/src/app/app.service.ts @@ -42,7 +42,10 @@ export class AppService { const response = await this.matchApiData(1, req); sender.send('getMockApiList', response); } else { - sender.send('getMockApiList', { response: { message: $localize `No mock found with ID ${mockID}` }, url: req.url }); + sender.send('getMockApiList', { + response: { message: $localize`No mock found with ID ${mockID}` }, + url: req.url, + }); } }); } @@ -65,8 +68,14 @@ export class AppService { const apiList = await this.getAllApi(projectID); const { pathname } = new URL(req.url, this.remoteService.mockUrl); const apiData = apiList.find((n) => { - const uri = n.uri.trim().startsWith('/') ? n.uri.trim() : `/${n.uri.trim()}`; - return n.method === req.method && uri === pathname; + let uri = n.uri.trim(); + if (Array.isArray(n.restParams) && n.restParams.length > 0) { + const restMap = n.restParams.reduce((p, c) => ((p[c.name] = c.example), p), {}); + uri = uri.replace(/\{(.+?)\}/g, (match, p) => restMap[p] ?? match); + // console.log('restMap', restMap, n.uri, uri); + } + const uriReg = new RegExp(`/?${uri}/?`); + return n.method === req.method && uriReg.test(pathname); }); return apiData ? { response: this.generateResponse(apiData.responseBody) } : { statusCode: 404 }; } diff --git a/src/workbench/browser/src/app/pages/extension/extension.module.ts b/src/workbench/browser/src/app/pages/extension/extension.module.ts index 93f44de1..23136162 100644 --- a/src/workbench/browser/src/app/pages/extension/extension.module.ts +++ b/src/workbench/browser/src/app/pages/extension/extension.module.ts @@ -3,7 +3,6 @@ import { CommonModule } from '@angular/common'; import { FormsModule } from '@angular/forms'; import { ExtensionRoutingModule } from './extension-routing.module'; -import { ExtensionService } from './extension.service'; import { ExtensionComponent } from './extension.component'; import { ExtensionListComponent } from './list/extension-list.component'; import { ExtensionDetailComponent } from './detail/extension-detail.component'; @@ -37,7 +36,6 @@ import { SharedModule } from 'eo/workbench/browser/src/app/shared/shared.module' NzDropDownModule, NzSkeletonModule, ], - providers: [ExtensionService], declarations: [ExtensionComponent, ExtensionListComponent, ExtensionDetailComponent], }) export class ExtensionModule {} diff --git a/src/workbench/browser/src/app/pages/extension/extension.service.ts b/src/workbench/browser/src/app/pages/extension/extension.service.ts index 7de77397..66cfece4 100644 --- a/src/workbench/browser/src/app/pages/extension/extension.service.ts +++ b/src/workbench/browser/src/app/pages/extension/extension.service.ts @@ -6,7 +6,9 @@ import { ModuleInfo } from 'eo/platform/node/extension-manager/types/index'; import { TranslateService } from 'eo/platform/common/i18n'; import { LanguageService } from 'eo/workbench/browser/src/app/core/services/language/language.service'; -@Injectable() +@Injectable({ + providedIn: 'root', +}) export class ExtensionService { ignoreList = ['default']; extensionIDs: Array = []; @@ -15,9 +17,7 @@ export class ExtensionService { constructor(private http: HttpClient, private electron: ElectronService, private language: LanguageService) { this.localExtensions = this.getExtensions(); this.extensionIDs = this.updateExtensionIDs(); - this.HOST = this.electron.isElectron - ? 'http://106.12.149.147' - : 'https://mockapi.eolink.com/ztBFKai20ee60c12871881565b5a6ddd718337df0e30979'; + this.HOST = this.electron.isElectron ? process.env.EXTENSION_URL : process.env.MOCK_URL; } private getExtensions() { // Local extension diff --git a/src/workbench/browser/src/app/shared/services/storage/index.model.ts b/src/workbench/browser/src/app/shared/services/storage/index.model.ts index 59c37cbc..a1568695 100644 --- a/src/workbench/browser/src/app/shared/services/storage/index.model.ts +++ b/src/workbench/browser/src/app/shared/services/storage/index.model.ts @@ -116,7 +116,7 @@ export interface ApiTestHistoryResponse { /** * Inject Code println */ - reportList: string[] | {type:'throw'|'interrupt',content:string}[]; + reportList: string[] | { type: 'throw' | 'interrupt'; content: string }[]; } /** @@ -386,7 +386,7 @@ export interface ApiData extends StorageModel { * * @type {object[]} */ - restParams?: object[]; + restParams?: Record[]; /** * 返回头数据,数据用json存储 diff --git a/yarn.lock b/yarn.lock index 8c54b406..6ade4ed4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5043,16 +5043,40 @@ dot-prop@^6.0.1: dependencies: is-obj "^2.0.0" +dotenv-defaults@^2.0.2: + version "2.0.2" + resolved "https://registry.npmmirror.com/dotenv-defaults/-/dotenv-defaults-2.0.2.tgz#6b3ec2e4319aafb70940abda72d3856770ee77ac" + integrity sha512-iOIzovWfsUHU91L5i8bJce3NYK5JXeAwH50Jh6+ARUdLiiGlYWfGw6UkzsYqaXZH/hjE/eCd/PlfM/qqyK0AMg== + dependencies: + dotenv "^8.2.0" + dotenv-expand@^5.1.0: version "5.1.0" resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0" integrity sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA== +dotenv-webpack@8.0.0: + version "8.0.0" + resolved "https://registry.npmmirror.com/dotenv-webpack/-/dotenv-webpack-8.0.0.tgz#bde8750eebda8fd1c6d7134f02bf56ad1f865772" + integrity sha512-vsWj11yWbIxLUPcQDbifCGW1+Mp03XfApFHJTC+/Ag9g3D/AnxoaVZcp76LpuBmReRwIJ+YO1fVdhmpzh+LL1A== + dependencies: + dotenv-defaults "^2.0.2" + +dotenv@16.0.1: + version "16.0.1" + resolved "https://registry.npmmirror.com/dotenv/-/dotenv-16.0.1.tgz#8f8f9d94876c35dac989876a5d3a82a267fdce1d" + integrity sha512-1K6hR6wtk2FviQ4kEiSjFiH5rpzEVi8WW0x96aztHVMhEspNpc4DVOUTEHtEva5VThQ8IaBX1Pe4gSzpVVUsKQ== + dotenv@^5.0.1: version "5.0.1" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-5.0.1.tgz#a5317459bd3d79ab88cff6e44057a6a3fbb1fcef" integrity sha512-4As8uPrjfwb7VXC+WnLCbXK7y+Ueb2B3zgNCePYfhxS1PYeaO1YTeplffTEcbfLhvFNGLAz90VvJs9yomG7bow== +dotenv@^8.2.0: + version "8.6.0" + resolved "https://registry.npmmirror.com/dotenv/-/dotenv-8.6.0.tgz#061af664d19f7f4d8fc6e4ff9b584ce237adcb8b" + integrity sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g== + dotenv@^9.0.2: version "9.0.2" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-9.0.2.tgz#dacc20160935a37dea6364aa1bef819fb9b6ab05"