From 239a4e5964b4defe9d76f6d86dc2c60c773e2959 Mon Sep 17 00:00:00 2001 From: godakid Date: Mon, 9 Sep 2024 22:44:38 +0800 Subject: [PATCH] config: inject rewrite --- example/.simple-mock.yaml | 1 - example/apis/api/index.option.json | 11 -------- package.json | 1 + pnpm-lock.yaml | 39 ++++++++++++++++++++++++++++ src/__test__/index.test.ts | 1 - src/core/config.ts | 2 +- src/core/startup.ts | 4 +-- src/presets/plugins/swagger/index.ts | 1 - 8 files changed, 43 insertions(+), 17 deletions(-) delete mode 100644 example/apis/api/index.option.json diff --git a/example/.simple-mock.yaml b/example/.simple-mock.yaml index 53168af..53be620 100644 --- a/example/.simple-mock.yaml +++ b/example/.simple-mock.yaml @@ -13,7 +13,6 @@ plugins: path: /swagger documentation: title: "Test" - rewrites: - path: '' test: ^(index)$ # rewrite index for '' diff --git a/example/apis/api/index.option.json b/example/apis/api/index.option.json deleted file mode 100644 index c6191f9..0000000 --- a/example/apis/api/index.option.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "list|1-10": [ - { - "id|+1": 1 - } - ] -} - - - - diff --git a/package.json b/package.json index dea4228..f957daf 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "mustache" ], "devDependencies": { + "@happy-dom/global-registrator": "^15.7.3", "@types/bun": "latest", "@types/dotenv": "^8.2.0", "@types/mustache": "^4.2.5", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f4eb74d..5e0edba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -60,6 +60,9 @@ importers: specifier: ^2.5.1 version: 2.5.1 devDependencies: + '@happy-dom/global-registrator': + specifier: ^15.7.3 + version: 15.7.3 '@types/bun': specifier: latest version: 1.1.8 @@ -102,6 +105,10 @@ packages: elysia: ^1.0.0-rc.11 typescript: ^5.3.3 + '@happy-dom/global-registrator@15.7.3': + resolution: {integrity: sha512-pxhR1xWFSU/Jtycar/ErXOFoheasVKLm7iFJb9zVwz8eGQ0dr3UBvjrtzloQEwXxomGGXUNgTOhGwPnkiCq2Ag==} + engines: {node: '>=18.0.0'} + '@ptkdev/logger@1.8.0': resolution: {integrity: sha512-gwg0pleMUyzsZIErDtzz2OP4F2Q3nzRGjhUTP+831/eogq17LpG7PgbHo1n1HZ/dlz/v2xvotfUcPLO3IzwEVQ==} engines: {node: '>=10.0.0'} @@ -216,6 +223,10 @@ packages: typescript: optional: true + entities@4.5.0: + resolution: {integrity: sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==} + engines: {node: '>=0.12'} + fast-decode-uri-component@1.0.1: resolution: {integrity: sha512-WKgKWg5eUxvRZGwW8FvfbaH7AXSh2cL+3j5fMGzUMCxWBJ3dV3a7Wz8y2f/uQ0e3B6WmodD3oS54jTQ9HVTIIg==} @@ -226,6 +237,10 @@ packages: graceful-fs@4.2.11: resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==} + happy-dom@15.7.3: + resolution: {integrity: sha512-w3RUaYNXFJX5LiNVhOJLK4GqCB1bFj1FvELtpon3HrN8gUpS09V0Vvm4/BBRRj7mLUE1+ch8PKv1JxEp/0IHjA==} + engines: {node: '>=18.0.0'} + has-flag@4.0.0: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} engines: {node: '>=8'} @@ -319,6 +334,14 @@ packages: resolution: {integrity: sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==} engines: {node: '>= 10.0.0'} + webidl-conversions@7.0.0: + resolution: {integrity: sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==} + engines: {node: '>=12'} + + whatwg-mimetype@3.0.0: + resolution: {integrity: sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==} + engines: {node: '>=12'} + which@2.0.2: resolution: {integrity: sha512-BLI3Tl1TW3Pvl70l3yq3Y64i+awpwXqsGBYWkkqMtnbXgrMD+yj7rhW0kuEDxzJaYXGjEW5ogapKNMEKNMjibA==} engines: {node: '>= 8'} @@ -352,6 +375,10 @@ snapshots: picocolors: 1.1.0 typescript: 5.5.4 + '@happy-dom/global-registrator@15.7.3': + dependencies: + happy-dom: 15.7.3 + '@ptkdev/logger@1.8.0': dependencies: chalk: 4.1.2 @@ -456,6 +483,8 @@ snapshots: openapi-types: 12.1.3 typescript: 5.5.4 + entities@4.5.0: {} + fast-decode-uri-component@1.0.1: {} fs-extra@10.1.0: @@ -466,6 +495,12 @@ snapshots: graceful-fs@4.2.11: {} + happy-dom@15.7.3: + dependencies: + entities: 4.5.0 + webidl-conversions: 7.0.0 + whatwg-mimetype: 3.0.0 + has-flag@4.0.0: {} is-promise@2.2.2: {} @@ -540,6 +575,10 @@ snapshots: universalify@2.0.1: {} + webidl-conversions@7.0.0: {} + + whatwg-mimetype@3.0.0: {} + which@2.0.2: dependencies: isexe: 2.0.0 diff --git a/src/__test__/index.test.ts b/src/__test__/index.test.ts index 3d0d90d..9ad85a3 100644 --- a/src/__test__/index.test.ts +++ b/src/__test__/index.test.ts @@ -1,6 +1,5 @@ // test/index.test.ts import {describe, expect, it} from 'bun:test'; -import Elysia from "elysia"; const baseURL = `http://localhost:3000/`; describe('Elysia', () => { diff --git a/src/core/config.ts b/src/core/config.ts index 7283e99..0ecd9d3 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -40,8 +40,8 @@ export const cwd = (...p: string[]) => path.resolve(command.getOptionValue('cwd' const parsedConfig = yaml.parse(await safeRun(() => { const configFilePath = cwd(DEFAULT_CONFIG.root_dir, configFile); if (!fs.existsSync(configFilePath)) { + console.warn(`Not Found config file,Create ${configFilePath}`); fs.writeFileSync(configFilePath, DEFAULT_MOCK_YAML_CONFIG); - return ''; } return fs.readFileSync(configFilePath).toString(); }, diff --git a/src/core/startup.ts b/src/core/startup.ts index 1fe0ea2..9d1d5ed 100644 --- a/src/core/startup.ts +++ b/src/core/startup.ts @@ -32,9 +32,9 @@ export async function startup(config, cwd) { const glob = new Glob("./**/*.*"); // 启动服务 const app = new Elysia(); - const rewrites = config.rewrites.map(({path, test}) => { + const rewrites = config?.rewrites?.map(({path, test}) => { return [new RegExp(test), path]; - }) as [[RegExp, string]]; + }) as [[RegExp, string]] ?? []; const urlRewrite = (url: string, method: string) => { for (let [reg, final] of rewrites) { diff --git a/src/presets/plugins/swagger/index.ts b/src/presets/plugins/swagger/index.ts index 9cad2e5..cb6d5da 100644 --- a/src/presets/plugins/swagger/index.ts +++ b/src/presets/plugins/swagger/index.ts @@ -1,7 +1,6 @@ import {swagger} from '@elysiajs/swagger'; export default function ({app, option}) { - console.log(option); app.use(swagger({ ...(option ?? {}), }));