Skip to content

Commit

Permalink
config: inject rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
couriourc committed Sep 9, 2024
1 parent e928959 commit 239a4e5
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 17 deletions.
1 change: 0 additions & 1 deletion example/.simple-mock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ plugins:
path: /swagger
documentation:
title: "Test"

rewrites:
- path: ''
test: ^(index)$ # rewrite index for ''
11 changes: 0 additions & 11 deletions example/apis/api/index.option.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
39 changes: 39 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export const cwd = (...p: string[]) => path.resolve(command.getOptionValue('cwd'
const parsedConfig = yaml.parse<IConfigParameter>(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();
},
Expand Down
4 changes: 2 additions & 2 deletions src/core/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 0 additions & 1 deletion src/presets/plugins/swagger/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {swagger} from '@elysiajs/swagger';

export default function ({app, option}) {
console.log(option);
app.use(swagger({
...(option ?? {}),
}));
Expand Down

0 comments on commit 239a4e5

Please sign in to comment.