befly 3.9.40 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -8
- package/befly.config.ts +19 -2
- package/checks/checkApi.ts +79 -77
- package/checks/checkHook.ts +48 -0
- package/checks/checkMenu.ts +168 -0
- package/checks/checkPlugin.ts +48 -0
- package/checks/checkTable.ts +137 -183
- package/docs/README.md +1 -1
- package/docs/api/api.md +1 -1
- package/docs/guide/quickstart.md +16 -9
- package/docs/hooks/hook.md +2 -2
- package/docs/hooks/rateLimit.md +1 -1
- package/docs/infra/redis.md +7 -7
- package/docs/plugins/plugin.md +23 -21
- package/docs/quickstart.md +16 -9
- package/docs/reference/addon.md +12 -1
- package/docs/reference/config.md +13 -30
- package/docs/reference/sync.md +62 -193
- package/docs/reference/table.md +27 -29
- package/hooks/auth.ts +3 -4
- package/hooks/cors.ts +4 -6
- package/hooks/parser.ts +3 -4
- package/hooks/permission.ts +3 -4
- package/hooks/validator.ts +3 -4
- package/lib/cacheHelper.ts +89 -153
- package/lib/cacheKeys.ts +1 -1
- package/lib/connect.ts +9 -13
- package/lib/dbDialect.ts +285 -0
- package/lib/dbHelper.ts +179 -507
- package/lib/dbUtils.ts +450 -0
- package/lib/logger.ts +41 -5
- package/lib/redisHelper.ts +1 -0
- package/lib/sqlBuilder.ts +358 -58
- package/lib/sqlCheck.ts +136 -0
- package/lib/validator.ts +1 -1
- package/loader/loadApis.ts +23 -126
- package/loader/loadHooks.ts +31 -46
- package/loader/loadPlugins.ts +37 -52
- package/main.ts +58 -19
- package/package.json +24 -25
- package/paths.ts +14 -14
- package/plugins/cache.ts +12 -6
- package/plugins/cipher.ts +2 -2
- package/plugins/config.ts +6 -8
- package/plugins/db.ts +14 -19
- package/plugins/jwt.ts +6 -7
- package/plugins/logger.ts +7 -9
- package/plugins/redis.ts +8 -10
- package/plugins/tool.ts +3 -4
- package/router/api.ts +3 -2
- package/router/static.ts +7 -5
- package/sync/syncApi.ts +80 -235
- package/sync/syncCache.ts +16 -0
- package/sync/syncDev.ts +167 -202
- package/sync/syncMenu.ts +230 -444
- package/sync/syncTable.ts +1247 -0
- package/tests/_mocks/mockSqliteDb.ts +204 -0
- package/tests/addonHelper-cache.test.ts +32 -0
- package/tests/apiHandler-routePath-only.test.ts +32 -0
- package/tests/cacheHelper.test.ts +16 -51
- package/tests/checkApi-routePath-strict.test.ts +166 -0
- package/tests/checkMenu.test.ts +346 -0
- package/tests/checkTable-smoke.test.ts +157 -0
- package/tests/dbDialect-cache.test.ts +23 -0
- package/tests/dbDialect.test.ts +46 -0
- package/tests/dbHelper-advanced.test.ts +1 -1
- package/tests/dbHelper-all-array-types.test.ts +15 -15
- package/tests/dbHelper-batch-write.test.ts +90 -0
- package/tests/dbHelper-columns.test.ts +36 -54
- package/tests/dbHelper-execute.test.ts +26 -26
- package/tests/dbHelper-joins.test.ts +85 -176
- package/tests/fixtures/scanFilesAddon/node_modules/@befly-addon/demo/apis/sub/b.ts +3 -0
- package/tests/fixtures/scanFilesApis/a.ts +3 -0
- package/tests/fixtures/scanFilesApis/sub/b.ts +3 -0
- package/tests/loadPlugins-order-smoke.test.ts +75 -0
- package/tests/logger.test.ts +6 -6
- package/tests/redisHelper.test.ts +6 -1
- package/tests/scanFiles-routePath.test.ts +46 -0
- package/tests/smoke-sql.test.ts +24 -0
- package/tests/sqlBuilder-advanced.test.ts +18 -5
- package/tests/sqlBuilder.test.ts +24 -0
- package/tests/sync-init-guard.test.ts +105 -0
- package/tests/syncApi-insBatch-fields-consistent.test.ts +61 -0
- package/tests/syncApi-obsolete-records.test.ts +69 -0
- package/tests/syncApi-type-compat.test.ts +72 -0
- package/tests/syncDev-permissions.test.ts +81 -0
- package/tests/syncMenu-disableMenus-hard-delete.test.ts +88 -0
- package/tests/syncMenu-duplicate-path.test.ts +122 -0
- package/tests/syncMenu-obsolete-records.test.ts +161 -0
- package/tests/syncMenu-parentPath-from-tree.test.ts +75 -0
- package/tests/syncMenu-paths.test.ts +0 -9
- package/tests/{syncDb-apply.test.ts → syncTable-apply.test.ts} +14 -24
- package/tests/{syncDb-array-number.test.ts → syncTable-array-number.test.ts} +31 -31
- package/tests/syncTable-constants.test.ts +101 -0
- package/tests/syncTable-db-integration.test.ts +237 -0
- package/tests/{syncDb-ddl.test.ts → syncTable-ddl.test.ts} +67 -53
- package/tests/{syncDb-helpers.test.ts → syncTable-helpers.test.ts} +12 -26
- package/tests/syncTable-schema.test.ts +99 -0
- package/tests/syncTable-testkit.test.ts +25 -0
- package/tests/syncTable-types.test.ts +122 -0
- package/tests/tableRef-and-deserialize.test.ts +67 -0
- package/tsconfig.json +1 -1
- package/types/api.d.ts +1 -1
- package/types/befly.d.ts +13 -12
- package/types/cache.d.ts +2 -2
- package/types/context.d.ts +1 -1
- package/types/database.d.ts +0 -5
- package/types/hook.d.ts +1 -10
- package/types/plugin.d.ts +2 -96
- package/types/sync.d.ts +19 -25
- package/utils/convertBigIntFields.ts +38 -0
- package/utils/disableMenusGlob.ts +85 -0
- package/utils/importDefault.ts +21 -0
- package/utils/isDirentDirectory.ts +23 -0
- package/utils/loadMenuConfigs.ts +145 -0
- package/utils/processFields.ts +25 -0
- package/utils/scanAddons.ts +72 -0
- package/utils/scanFiles.ts +129 -21
- package/utils/scanSources.ts +64 -0
- package/utils/sortModules.ts +137 -0
- package/checks/checkApp.ts +0 -55
- package/hooks/rateLimit.ts +0 -276
- package/sync/syncAll.ts +0 -35
- package/sync/syncDb/apply.ts +0 -192
- package/sync/syncDb/constants.ts +0 -119
- package/sync/syncDb/ddl.ts +0 -251
- package/sync/syncDb/helpers.ts +0 -84
- package/sync/syncDb/schema.ts +0 -202
- package/sync/syncDb/sqlite.ts +0 -48
- package/sync/syncDb/table.ts +0 -207
- package/sync/syncDb/tableCreate.ts +0 -163
- package/sync/syncDb/types.ts +0 -132
- package/sync/syncDb/version.ts +0 -69
- package/sync/syncDb.ts +0 -168
- package/tests/rateLimit-hook.test.ts +0 -477
- package/tests/syncDb-constants.test.ts +0 -130
- package/tests/syncDb-schema.test.ts +0 -179
- package/tests/syncDb-types.test.ts +0 -139
- package/utils/addonHelper.ts +0 -90
- package/utils/modules.ts +0 -98
- package/utils/route.ts +0 -23
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.0",
|
|
4
|
+
"gitHead": "faa8189c7d23cf45885c03d1425cba8f5bf45df9",
|
|
4
5
|
"private": false,
|
|
5
6
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
6
7
|
"keywords": [
|
|
@@ -16,26 +17,10 @@
|
|
|
16
17
|
"typescript"
|
|
17
18
|
],
|
|
18
19
|
"homepage": "https://chensuiyi.me",
|
|
19
|
-
"author": "chensuiyi <bimostyle@qq.com>",
|
|
20
20
|
"license": "Apache-2.0",
|
|
21
|
-
"
|
|
22
|
-
"main": "main.ts",
|
|
23
|
-
"exports": {
|
|
24
|
-
".": {
|
|
25
|
-
"types": "./main.ts",
|
|
26
|
-
"default": "./main.ts"
|
|
27
|
-
},
|
|
28
|
-
"./lib/*": "./lib/*.ts",
|
|
29
|
-
"./types/*": "./types/*.d.ts",
|
|
30
|
-
"./utils/*": "./utils/*.ts"
|
|
31
|
-
},
|
|
32
|
-
"types": "main.ts",
|
|
21
|
+
"author": "chensuiyi <bimostyle@qq.com>",
|
|
33
22
|
"files": [
|
|
34
23
|
".npmrc",
|
|
35
|
-
".prettierignore",
|
|
36
|
-
".prettierrc",
|
|
37
|
-
"LICENSE",
|
|
38
|
-
"README.md",
|
|
39
24
|
"befly.config.ts",
|
|
40
25
|
"bunfig.toml",
|
|
41
26
|
"checks",
|
|
@@ -43,11 +28,13 @@
|
|
|
43
28
|
"docs",
|
|
44
29
|
"hooks",
|
|
45
30
|
"lib",
|
|
31
|
+
"LICENSE",
|
|
46
32
|
"loader",
|
|
47
33
|
"main.ts",
|
|
48
34
|
"package.json",
|
|
49
35
|
"paths.ts",
|
|
50
36
|
"plugins",
|
|
37
|
+
"README.md",
|
|
51
38
|
"router",
|
|
52
39
|
"sync",
|
|
53
40
|
"tests",
|
|
@@ -55,11 +42,28 @@
|
|
|
55
42
|
"types",
|
|
56
43
|
"utils"
|
|
57
44
|
],
|
|
45
|
+
"type": "module",
|
|
46
|
+
"main": "main.ts",
|
|
47
|
+
"types": "main.ts",
|
|
48
|
+
"exports": {
|
|
49
|
+
".": {
|
|
50
|
+
"types": "./main.ts",
|
|
51
|
+
"default": "./main.ts"
|
|
52
|
+
},
|
|
53
|
+
"./lib/*": "./lib/*.ts",
|
|
54
|
+
"./types/*": "./types/*.d.ts",
|
|
55
|
+
"./utils/*": "./utils/*.ts"
|
|
56
|
+
},
|
|
57
|
+
"publishConfig": {
|
|
58
|
+
"access": "public",
|
|
59
|
+
"registry": "https://registry.npmjs.org"
|
|
60
|
+
},
|
|
58
61
|
"scripts": {
|
|
59
62
|
"test": "bun test",
|
|
60
|
-
"
|
|
63
|
+
"typecheck": "bunx tsgo --noEmit"
|
|
61
64
|
},
|
|
62
65
|
"dependencies": {
|
|
66
|
+
"befly-shared": "1.0.0",
|
|
63
67
|
"chalk": "^5.6.2",
|
|
64
68
|
"es-toolkit": "^1.43.0",
|
|
65
69
|
"fast-jwt": "^6.1.0",
|
|
@@ -74,10 +78,5 @@
|
|
|
74
78
|
},
|
|
75
79
|
"engines": {
|
|
76
80
|
"bun": ">=1.3.0"
|
|
77
|
-
}
|
|
78
|
-
"publishConfig": {
|
|
79
|
-
"access": "public",
|
|
80
|
-
"registry": "https://registry.npmjs.org"
|
|
81
|
-
},
|
|
82
|
-
"gitHead": "0ba53d2bbdc6a7f2659ef60256cda7302fe585b0"
|
|
81
|
+
}
|
|
83
82
|
}
|
package/paths.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
* 路径分类:
|
|
8
8
|
* - root* 系列:Core 框架内部路径(packages/core/*)
|
|
9
|
-
* -
|
|
9
|
+
* - app* 系列:用户项目路径(process.cwd()/*)
|
|
10
10
|
*
|
|
11
11
|
*/
|
|
12
12
|
|
|
@@ -68,46 +68,46 @@ export const coreTableDir = join(__dirname, "tables");
|
|
|
68
68
|
* @description process.cwd()
|
|
69
69
|
* @usage 用户项目的根目录
|
|
70
70
|
*/
|
|
71
|
-
export const
|
|
71
|
+
export const appDir = process.cwd();
|
|
72
72
|
|
|
73
73
|
/**
|
|
74
74
|
* 项目检查目录
|
|
75
|
-
* @description {
|
|
75
|
+
* @description {appDir}/checks/
|
|
76
76
|
* @usage 存放用户自定义启动检查模块
|
|
77
77
|
*/
|
|
78
|
-
export const
|
|
78
|
+
export const appCheckDir = join(appDir, "checks");
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
81
|
* 项目插件目录
|
|
82
|
-
* @description {
|
|
82
|
+
* @description {appDir}/plugins/
|
|
83
83
|
* @usage 存放用户自定义插件
|
|
84
84
|
*/
|
|
85
|
-
export const
|
|
85
|
+
export const appPluginDir = join(appDir, "plugins");
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
88
|
* 项目钩子目录
|
|
89
|
-
* @description {
|
|
89
|
+
* @description {appDir}/hooks/
|
|
90
90
|
* @usage 存放用户自定义钩子
|
|
91
91
|
*/
|
|
92
|
-
export const
|
|
92
|
+
export const appHookDir = join(appDir, "hooks");
|
|
93
93
|
|
|
94
94
|
/**
|
|
95
95
|
* 项目 API 目录
|
|
96
|
-
* @description {
|
|
96
|
+
* @description {appDir}/apis/
|
|
97
97
|
* @usage 存放用户业务 API 接口
|
|
98
98
|
*/
|
|
99
|
-
export const
|
|
99
|
+
export const appApiDir = join(appDir, "apis");
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
102
|
* 项目表定义目录
|
|
103
|
-
* @description {
|
|
103
|
+
* @description {appDir}/tables/
|
|
104
104
|
* @usage 存放用户业务表定义(JSON 格式)
|
|
105
105
|
*/
|
|
106
|
-
export const
|
|
106
|
+
export const appTableDir = join(appDir, "tables");
|
|
107
107
|
|
|
108
108
|
/**
|
|
109
109
|
* 项目组件目录
|
|
110
|
-
* @description {
|
|
110
|
+
* @description {appDir}/addons/
|
|
111
111
|
* @usage 存放本地组件(优先级高于 node_modules 中的组件)
|
|
112
112
|
*/
|
|
113
|
-
export const
|
|
113
|
+
export const appAddonsDir = join(appDir, "addons");
|
package/plugins/cache.ts
CHANGED
|
@@ -11,11 +11,17 @@ import { CacheHelper } from "../lib/cacheHelper.js";
|
|
|
11
11
|
/**
|
|
12
12
|
* 缓存插件
|
|
13
13
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
export default {
|
|
15
|
+
deps: ["logger", "redis", "db"],
|
|
16
16
|
async handler(befly: BeflyContext): Promise<CacheHelper> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
17
|
+
if (!(befly as any).db) {
|
|
18
|
+
throw new Error("缓存初始化失败:ctx.db 未初始化(Db 插件未加载或注入失败)");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!(befly as any).redis) {
|
|
22
|
+
throw new Error("缓存初始化失败:ctx.redis 未初始化(Redis 插件未加载或注入失败)");
|
|
23
|
+
}
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
return new CacheHelper({ db: befly.db, redis: befly.redis });
|
|
26
|
+
}
|
|
27
|
+
} satisfies Plugin;
|
package/plugins/cipher.ts
CHANGED
package/plugins/config.ts
CHANGED
|
@@ -2,14 +2,12 @@
|
|
|
2
2
|
* 配置插件
|
|
3
3
|
* 提供访问项目配置的能力
|
|
4
4
|
*/
|
|
5
|
+
import type { BeflyContext } from "../types/befly.js";
|
|
5
6
|
import type { Plugin } from "../types/plugin.js";
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return beflyConfig;
|
|
8
|
+
export default {
|
|
9
|
+
deps: [],
|
|
10
|
+
handler: (context: BeflyContext) => {
|
|
11
|
+
return context.config;
|
|
12
12
|
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export default plugin;
|
|
13
|
+
} satisfies Plugin;
|
package/plugins/db.ts
CHANGED
|
@@ -7,39 +7,34 @@ import type { BeflyContext } from "../types/befly.js";
|
|
|
7
7
|
import type { Plugin } from "../types/plugin.js";
|
|
8
8
|
|
|
9
9
|
import { Connect } from "../lib/connect.js";
|
|
10
|
+
import { getDialectByName } from "../lib/dbDialect.js";
|
|
10
11
|
import { DbHelper } from "../lib/dbHelper.js";
|
|
11
12
|
import { Logger } from "../lib/logger.js";
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* 数据库插件
|
|
15
16
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
export default {
|
|
18
|
+
deps: ["logger", "redis"],
|
|
18
19
|
async handler(befly: BeflyContext): Promise<DbHelper> {
|
|
19
|
-
|
|
20
|
+
if (!(befly as any).redis) {
|
|
21
|
+
throw new Error("数据库初始化失败:ctx.redis 未初始化(Redis 插件未加载或注入失败)");
|
|
22
|
+
}
|
|
20
23
|
|
|
21
24
|
try {
|
|
22
|
-
sql =
|
|
25
|
+
const sql = Connect.getSql();
|
|
26
|
+
|
|
27
|
+
const rawDbType = befly.config && befly.config.db ? befly.config.db.type : undefined;
|
|
28
|
+
const resolvedDbType = rawDbType === "postgres" ? "postgresql" : rawDbType;
|
|
29
|
+
const dialect = getDialectByName(resolvedDbType === "postgresql" || resolvedDbType === "sqlite" ? resolvedDbType : "mysql");
|
|
23
30
|
|
|
24
|
-
//
|
|
25
|
-
const dbManager = new DbHelper(befly, sql);
|
|
31
|
+
// 创建数据库管理器实例
|
|
32
|
+
const dbManager = new DbHelper({ redis: befly.redis, sql: sql, dialect: dialect });
|
|
26
33
|
|
|
27
34
|
return dbManager;
|
|
28
35
|
} catch (error: any) {
|
|
29
36
|
Logger.error({ err: error }, "数据库初始化失败");
|
|
30
|
-
|
|
31
|
-
// 清理资源
|
|
32
|
-
if (sql) {
|
|
33
|
-
try {
|
|
34
|
-
await sql.close();
|
|
35
|
-
} catch (cleanupError: any) {
|
|
36
|
-
Logger.error({ err: cleanupError }, "清理连接池失败");
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
37
|
throw error;
|
|
41
38
|
}
|
|
42
39
|
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export default dbPlugin;
|
|
40
|
+
} satisfies Plugin;
|
package/plugins/jwt.ts
CHANGED
|
@@ -2,15 +2,14 @@
|
|
|
2
2
|
* JWT 插件
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import type { BeflyContext } from "../types/befly.js";
|
|
5
6
|
import type { Plugin } from "../types/plugin.js";
|
|
6
7
|
|
|
7
|
-
import { beflyConfig } from "../befly.config.js";
|
|
8
8
|
import { Jwt } from "../lib/jwt.js";
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
export default {
|
|
11
|
+
deps: [],
|
|
12
|
+
handler: (context: BeflyContext) => {
|
|
13
|
+
return new Jwt(context.config ? context.config.auth : undefined);
|
|
13
14
|
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export default jwtPlugin;
|
|
15
|
+
} satisfies Plugin;
|
package/plugins/logger.ts
CHANGED
|
@@ -3,23 +3,21 @@
|
|
|
3
3
|
* 提供全局日志功能
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import type { BeflyContext } from "../types/befly.js";
|
|
6
7
|
import type { Plugin } from "../types/plugin.js";
|
|
7
8
|
|
|
8
|
-
import { beflyConfig } from "../befly.config.js";
|
|
9
9
|
import { Logger } from "../lib/logger.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* 日志插件
|
|
13
13
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
async handler(): Promise<typeof Logger> {
|
|
14
|
+
export default {
|
|
15
|
+
deps: [],
|
|
16
|
+
async handler(context: BeflyContext): Promise<typeof Logger> {
|
|
17
17
|
// 配置 Logger
|
|
18
|
-
if (
|
|
19
|
-
Logger.configure(
|
|
18
|
+
if (context.config && context.config.logger) {
|
|
19
|
+
Logger.configure(context.config.logger);
|
|
20
20
|
}
|
|
21
21
|
return Logger;
|
|
22
22
|
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export default loggerPlugin;
|
|
23
|
+
} satisfies Plugin;
|
package/plugins/redis.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* 初始化 Redis 连接和助手工具
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import type { BeflyContext } from "../types/befly.js";
|
|
6
7
|
import type { Plugin } from "../types/plugin.js";
|
|
7
8
|
|
|
8
|
-
import { beflyConfig } from "../befly.config.js";
|
|
9
9
|
import { Connect } from "../lib/connect.js";
|
|
10
10
|
import { Logger } from "../lib/logger.js";
|
|
11
11
|
import { RedisHelper } from "../lib/redisHelper.js";
|
|
@@ -13,13 +13,13 @@ import { RedisHelper } from "../lib/redisHelper.js";
|
|
|
13
13
|
/**
|
|
14
14
|
* Redis 插件
|
|
15
15
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
async handler(): Promise<RedisHelper | Record<string, never>> {
|
|
19
|
-
const redisConfig =
|
|
16
|
+
export default {
|
|
17
|
+
deps: ["logger"],
|
|
18
|
+
async handler(context: BeflyContext): Promise<RedisHelper | Record<string, never>> {
|
|
19
|
+
const redisConfig = context.config && context.config.redis ? context.config.redis : {};
|
|
20
20
|
try {
|
|
21
|
-
//
|
|
22
|
-
|
|
21
|
+
// 启动期已建立 Redis 连接;这里仅校验连接存在
|
|
22
|
+
Connect.getRedis();
|
|
23
23
|
|
|
24
24
|
// 返回 RedisHelper 实例
|
|
25
25
|
return new RedisHelper(redisConfig.prefix);
|
|
@@ -28,6 +28,4 @@ const redisPlugin: Plugin = {
|
|
|
28
28
|
throw error;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export default redisPlugin;
|
|
31
|
+
} satisfies Plugin;
|
package/plugins/tool.ts
CHANGED
|
@@ -113,7 +113,8 @@ export function Raw(ctx: RequestContext, data: Record<string, any> | string, opt
|
|
|
113
113
|
});
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
|
|
116
|
+
export default {
|
|
117
|
+
deps: [],
|
|
117
118
|
handler: () => {
|
|
118
119
|
return {
|
|
119
120
|
Yes: Yes,
|
|
@@ -121,6 +122,4 @@ const plugin: Plugin = {
|
|
|
121
122
|
Raw: Raw
|
|
122
123
|
};
|
|
123
124
|
}
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
export default plugin;
|
|
125
|
+
} satisfies Plugin;
|
package/router/api.ts
CHANGED
|
@@ -15,7 +15,6 @@ import { Logger } from "../lib/logger.js";
|
|
|
15
15
|
import { genShortId } from "../utils/genShortId.js";
|
|
16
16
|
import { getClientIp } from "../utils/getClientIp.js";
|
|
17
17
|
import { FinalResponse } from "../utils/response.js";
|
|
18
|
-
import { makeRouteKey } from "../utils/route.js";
|
|
19
18
|
|
|
20
19
|
/**
|
|
21
20
|
* API处理器工厂函数
|
|
@@ -30,7 +29,9 @@ export function apiHandler(apis: Map<string, ApiRoute>, hooks: Hook[], context:
|
|
|
30
29
|
|
|
31
30
|
// 2. 创建请求上下文
|
|
32
31
|
const url = new URL(req.url);
|
|
33
|
-
|
|
32
|
+
// 只用接口路径做存在性判断与匹配:不要把 method 拼进 key
|
|
33
|
+
// 说明:apisMap 的 key 来源于 scanFiles/loadApis 生成的 routePath(例如 /api/core/xxx)
|
|
34
|
+
const apiPath = url.pathname || "/";
|
|
34
35
|
|
|
35
36
|
const clientIp = getClientIp(req, server);
|
|
36
37
|
|
package/router/static.ts
CHANGED
|
@@ -3,25 +3,27 @@
|
|
|
3
3
|
* 处理 /* 路径的静态文件请求
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
// 类型导入
|
|
7
|
+
import type { CorsConfig } from "../types/befly.js";
|
|
8
|
+
|
|
6
9
|
// 外部依赖
|
|
7
10
|
import { join } from "pathe";
|
|
8
11
|
|
|
9
|
-
import { beflyConfig } from "../befly.config.js";
|
|
10
12
|
import { Logger } from "../lib/logger.js";
|
|
11
13
|
// 相对导入
|
|
12
|
-
import {
|
|
14
|
+
import { appDir } from "../paths.js";
|
|
13
15
|
import { setCorsOptions } from "../utils/cors.js";
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
18
|
* 静态文件处理器工厂
|
|
17
19
|
*/
|
|
18
|
-
export function staticHandler() {
|
|
20
|
+
export function staticHandler(corsConfig: CorsConfig | undefined = undefined) {
|
|
19
21
|
return async (req: Request): Promise<Response> => {
|
|
20
22
|
// 设置 CORS 响应头
|
|
21
|
-
const corsHeaders = setCorsOptions(req,
|
|
23
|
+
const corsHeaders = setCorsOptions(req, corsConfig);
|
|
22
24
|
|
|
23
25
|
const url = new URL(req.url);
|
|
24
|
-
const filePath = join(
|
|
26
|
+
const filePath = join(appDir, "public", url.pathname);
|
|
25
27
|
|
|
26
28
|
try {
|
|
27
29
|
// OPTIONS预检请求
|