@dangao/bun-server 3.2.0 → 3.3.1

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.
Files changed (139) hide show
  1. package/dist/ai/types.d.ts +4 -0
  2. package/dist/ai/types.d.ts.map +1 -1
  3. package/dist/auth/types.d.ts +4 -0
  4. package/dist/auth/types.d.ts.map +1 -1
  5. package/dist/cache/interceptors.d.ts +3 -3
  6. package/dist/cache/interceptors.d.ts.map +1 -1
  7. package/dist/cache/service.d.ts +6 -6
  8. package/dist/cache/service.d.ts.map +1 -1
  9. package/dist/cache/types.d.ts +12 -12
  10. package/dist/cache/types.d.ts.map +1 -1
  11. package/dist/config/service.d.ts +6 -4
  12. package/dist/config/service.d.ts.map +1 -1
  13. package/dist/core/application.d.ts +4 -0
  14. package/dist/core/application.d.ts.map +1 -1
  15. package/dist/core/context.d.ts +4 -2
  16. package/dist/core/context.d.ts.map +1 -1
  17. package/dist/database/sqlite-adapter.d.ts +4 -2
  18. package/dist/database/sqlite-adapter.d.ts.map +1 -1
  19. package/dist/di/container.d.ts +2 -0
  20. package/dist/di/container.d.ts.map +1 -1
  21. package/dist/di/module-registry.d.ts.map +1 -1
  22. package/dist/di/module.d.ts +11 -1
  23. package/dist/di/module.d.ts.map +1 -1
  24. package/dist/di/types.d.ts +1 -1
  25. package/dist/di/types.d.ts.map +1 -1
  26. package/dist/error/handler.d.ts.map +1 -1
  27. package/dist/error/http-exception.d.ts +8 -8
  28. package/dist/error/http-exception.d.ts.map +1 -1
  29. package/dist/error/index.d.ts +1 -0
  30. package/dist/error/index.d.ts.map +1 -1
  31. package/dist/events/service.d.ts +2 -2
  32. package/dist/events/service.d.ts.map +1 -1
  33. package/dist/events/types.d.ts +12 -3
  34. package/dist/events/types.d.ts.map +1 -1
  35. package/dist/index.js +63 -25
  36. package/dist/index.node.mjs +63 -25
  37. package/dist/interceptor/base-interceptor.d.ts +3 -3
  38. package/dist/interceptor/base-interceptor.d.ts.map +1 -1
  39. package/dist/interceptor/builtin/log-interceptor.d.ts +1 -1
  40. package/dist/interceptor/builtin/log-interceptor.d.ts.map +1 -1
  41. package/dist/interceptor/builtin/permission-interceptor.d.ts +1 -1
  42. package/dist/interceptor/builtin/permission-interceptor.d.ts.map +1 -1
  43. package/dist/interceptor/interceptor-chain.d.ts +1 -1
  44. package/dist/interceptor/interceptor-chain.d.ts.map +1 -1
  45. package/dist/interceptor/interceptor-registry.d.ts +3 -1
  46. package/dist/interceptor/interceptor-registry.d.ts.map +1 -1
  47. package/dist/interceptor/types.d.ts +6 -1
  48. package/dist/interceptor/types.d.ts.map +1 -1
  49. package/dist/microservice/service-client/types.d.ts +1 -0
  50. package/dist/microservice/service-client/types.d.ts.map +1 -1
  51. package/dist/microservice/tracing/tracer.d.ts +1 -0
  52. package/dist/microservice/tracing/tracer.d.ts.map +1 -1
  53. package/dist/middleware/builtin/file-upload.d.ts +2 -0
  54. package/dist/middleware/builtin/file-upload.d.ts.map +1 -1
  55. package/dist/middleware/builtin/rate-limit.d.ts +9 -1
  56. package/dist/middleware/builtin/rate-limit.d.ts.map +1 -1
  57. package/dist/queue/service.d.ts +2 -2
  58. package/dist/queue/service.d.ts.map +1 -1
  59. package/dist/queue/types.d.ts +25 -1
  60. package/dist/queue/types.d.ts.map +1 -1
  61. package/dist/router/decorators.d.ts +1 -2
  62. package/dist/router/decorators.d.ts.map +1 -1
  63. package/dist/security/guards/types.d.ts +1 -0
  64. package/dist/security/guards/types.d.ts.map +1 -1
  65. package/dist/security/types.d.ts +1 -1
  66. package/dist/security/types.d.ts.map +1 -1
  67. package/dist/session/types.d.ts +8 -0
  68. package/dist/session/types.d.ts.map +1 -1
  69. package/dist/swagger/decorators.d.ts +1 -1
  70. package/dist/swagger/decorators.d.ts.map +1 -1
  71. package/dist/swagger/types.d.ts +1 -1
  72. package/dist/swagger/types.d.ts.map +1 -1
  73. package/dist/testing/harness.d.ts +1 -1
  74. package/dist/testing/harness.d.ts.map +1 -1
  75. package/dist/testing/test-client.d.ts +1 -1
  76. package/dist/testing/test-client.d.ts.map +1 -1
  77. package/dist/testing/testing-module.d.ts +2 -2
  78. package/dist/testing/testing-module.d.ts.map +1 -1
  79. package/dist/validation/errors.d.ts +5 -1
  80. package/dist/validation/errors.d.ts.map +1 -1
  81. package/docs/microservice-config-center.md +2 -1
  82. package/docs/microservice-nacos.md +32 -5
  83. package/docs/microservice-service-registry.md +3 -6
  84. package/docs/microservice.md +0 -3
  85. package/docs/zh/microservice-config-center.md +2 -1
  86. package/docs/zh/microservice-nacos.md +32 -5
  87. package/docs/zh/microservice-service-registry.md +3 -6
  88. package/docs/zh/microservice.md +0 -3
  89. package/package.json +4 -4
  90. package/src/ai/types.ts +5 -0
  91. package/src/auth/types.ts +4 -1
  92. package/src/cache/interceptors.ts +6 -6
  93. package/src/cache/service-proxy.ts +2 -2
  94. package/src/cache/service.ts +17 -8
  95. package/src/cache/types.ts +12 -12
  96. package/src/config/service.ts +8 -6
  97. package/src/core/application.ts +7 -1
  98. package/src/core/context.ts +6 -3
  99. package/src/database/sqlite-adapter.ts +4 -3
  100. package/src/di/container.ts +13 -0
  101. package/src/di/module-registry.ts +2 -3
  102. package/src/di/module.ts +21 -2
  103. package/src/di/types.ts +1 -2
  104. package/src/error/handler.ts +3 -4
  105. package/src/error/http-exception.ts +11 -11
  106. package/src/error/index.ts +1 -1
  107. package/src/events/service.ts +4 -2
  108. package/src/events/types.ts +14 -3
  109. package/src/interceptor/base-interceptor.ts +5 -6
  110. package/src/interceptor/builtin/log-interceptor.ts +2 -3
  111. package/src/interceptor/builtin/permission-interceptor.ts +2 -3
  112. package/src/interceptor/interceptor-chain.ts +6 -7
  113. package/src/interceptor/interceptor-registry.ts +5 -3
  114. package/src/interceptor/types.ts +9 -4
  115. package/src/microservice/service-client/types.ts +1 -1
  116. package/src/microservice/tracing/tracer.ts +15 -3
  117. package/src/middleware/builtin/file-upload.ts +3 -2
  118. package/src/middleware/builtin/rate-limit.ts +22 -5
  119. package/src/queue/service.ts +1 -1
  120. package/src/queue/types.ts +40 -1
  121. package/src/router/decorators.ts +2 -3
  122. package/src/security/guards/types.ts +2 -1
  123. package/src/security/types.ts +1 -2
  124. package/src/session/service.ts +1 -1
  125. package/src/session/types.ts +10 -0
  126. package/src/swagger/decorators.ts +15 -4
  127. package/src/swagger/generator.ts +2 -3
  128. package/src/swagger/types.ts +1 -2
  129. package/src/testing/harness.ts +1 -2
  130. package/src/testing/test-client.ts +2 -2
  131. package/src/testing/testing-module.ts +5 -5
  132. package/src/validation/errors.ts +6 -2
  133. package/tests/bun-test-shim.d.ts +11 -0
  134. package/tests/controller/context-decorator.test.ts +1 -2
  135. package/tests/di/module.test.ts +199 -1
  136. package/tests/events/event-emitter.test.ts +2 -2
  137. package/tests/global.d.ts +30 -0
  138. package/tests/queue/queue-service.test.ts +14 -0
  139. package/tests/testing/testing-module.test.ts +20 -0
@@ -5,6 +5,7 @@ This document introduces how to use Nacos as a configuration center and service
5
5
  ## Table of Contents
6
6
 
7
7
  - [Overview](#overview)
8
+ - [Namespace](#namespace)
8
9
  - [Nacos Installation](#nacos-installation)
9
10
  - [Configuration Center Integration](#configuration-center-integration)
10
11
  - [Service Registry Integration](#service-registry-integration)
@@ -20,6 +21,26 @@ Bun Server Framework provides Nacos 3.X support through the `@dangao/nacos-clien
20
21
  - **Service Discovery**: Service instance querying and watching
21
22
  - **Open API**: Based on Nacos 3.X Open API implementation
22
23
 
24
+ ## Namespace
25
+
26
+ The default namespace shown as **public** in the Nacos console maps to an **empty string** `""` in the Open API, not the literal `"public"`.
27
+
28
+ `@dangao/nacos-client` **0.1.2+** automatically sends `namespaceId=` (empty value) when `namespaceId` is not configured, for both configuration and service discovery. Versions 0.1.1 and earlier omitted the parameter entirely, causing Nacos 3.x to return `code=20004`.
29
+
30
+ **Recommended**: omit `namespaceId` in client config for the public namespace:
31
+
32
+ ```typescript
33
+ nacos: {
34
+ client: {
35
+ serverList: ['http://localhost:8848'],
36
+ username: 'nacos',
37
+ password: 'nacos',
38
+ },
39
+ },
40
+ ```
41
+
42
+ For a custom namespace, pass the ID from the console (usually a UUID). If you patched `@dangao/nacos-client@0.1.1` with `bun patch`, remove `patchedDependencies` and the patch file after upgrading to 0.1.2+.
43
+
23
44
  ## Nacos Installation
24
45
 
25
46
  ### Docker Installation (Recommended)
@@ -48,7 +69,6 @@ app.registerModule(
48
69
  nacos: {
49
70
  client: {
50
71
  serverList: ['http://localhost:8848'],
51
- namespaceId: 'public',
52
72
  username: 'nacos',
53
73
  password: 'nacos',
54
74
  },
@@ -125,7 +145,6 @@ app.registerModule(
125
145
  nacos: {
126
146
  client: {
127
147
  serverList: ['http://localhost:8848'],
128
- namespaceId: 'public',
129
148
  username: 'nacos',
130
149
  password: 'nacos',
131
150
  },
@@ -197,7 +216,6 @@ import {
197
216
 
198
217
  const instances = await serviceRegistry.getInstances('user-service', {
199
218
  healthyOnly: true,
200
- namespaceId: 'public',
201
219
  });
202
220
 
203
221
  // Watch for service instance changes
@@ -226,7 +244,6 @@ ConfigCenterModule.forRoot({
226
244
  nacos: {
227
245
  client: {
228
246
  serverList: ['http://localhost:8848'],
229
- namespaceId: 'public',
230
247
  username: 'nacos',
231
248
  password: 'nacos',
232
249
  },
@@ -239,7 +256,6 @@ ServiceRegistryModule.forRoot({
239
256
  nacos: {
240
257
  client: {
241
258
  serverList: ['http://localhost:8848'],
242
- namespaceId: 'public',
243
259
  username: 'nacos',
244
260
  password: 'nacos',
245
261
  },
@@ -337,6 +353,17 @@ await app.listen(3000);
337
353
  - Check if configuration watch callback is correctly implemented
338
354
  - Check logs to confirm if configuration changes are detected
339
355
 
356
+ ### 6. Public Namespace Returns 20004
357
+
358
+ **Issue**: `getConfig` or service registration/discovery returns `code=20004` when `namespaceId` is not configured
359
+
360
+ **Cause**: Nacos 3.x requires `namespaceId=` (empty value) in requests; `@dangao/nacos-client` 0.1.1 and earlier omitted the parameter
361
+
362
+ **Solutions**:
363
+ - Upgrade `@dangao/nacos-client` to **0.1.2+** (pulled in via framework dependency, or pin explicitly in downstream projects)
364
+ - Remove any `bun patch` workaround
365
+ - Do not set `namespaceId` to the literal `"public"`; public maps to an empty string—omit the config instead
366
+
340
367
  ## Reference Resources
341
368
 
342
369
  - [Nacos Official Documentation](https://nacos.io/docs/latest/)
@@ -22,6 +22,8 @@ Service registration and discovery provides core capabilities in microservice ar
22
22
  - **Health Check**: Automatically update service health status based on health check results
23
23
  - **Instance Watching**: Watch for service instance changes
24
24
 
25
+ > **Namespace**: Omit `namespaceId` for the Nacos public namespace. See [Nacos Integration - Namespace](./microservice-nacos.md#namespace).
26
+
25
27
  ## Quick Start
26
28
 
27
29
  ### 1. Register Service Registry Module
@@ -39,7 +41,6 @@ app.registerModule(
39
41
  nacos: {
40
42
  client: {
41
43
  serverList: ['http://localhost:8848'],
42
- namespaceId: 'public',
43
44
  username: 'nacos',
44
45
  password: 'nacos',
45
46
  },
@@ -95,7 +96,6 @@ The simplest way is to use the `@ServiceRegistry` decorator:
95
96
  region: 'us-east',
96
97
  },
97
98
  clusterName: 'default',
98
- namespaceId: 'public',
99
99
  groupName: 'DEFAULT_GROUP',
100
100
  })
101
101
  @Controller('/api/users')
@@ -113,7 +113,7 @@ class UserController {
113
113
  - `healthy`: Initial health status
114
114
  - `metadata`: Service metadata (version, region, etc.)
115
115
  - `clusterName`: Cluster name
116
- - `namespaceId`: Namespace
116
+ - `namespaceId`: Namespace ID; omit for public (empty string). See [Nacos Integration - Namespace](./microservice-nacos.md#namespace)
117
117
  - `groupName`: Group name
118
118
 
119
119
  ### Manual Registration
@@ -182,7 +182,6 @@ import type { ServiceInstance } from '@dangao/bun-server';
182
182
  class MyService {
183
183
  @ServiceDiscovery('user-service', {
184
184
  healthyOnly: true, // Only get healthy instances
185
- namespaceId: 'public',
186
185
  groupName: 'DEFAULT_GROUP',
187
186
  })
188
187
  public instances: ServiceInstance[] = [];
@@ -200,7 +199,6 @@ class MyService {
200
199
  // Get service instance list
201
200
  const instances = await serviceRegistry.getInstances('user-service', {
202
201
  healthyOnly: true,
203
- namespaceId: 'public',
204
202
  groupName: 'DEFAULT_GROUP',
205
203
  clusterName: 'default',
206
204
  });
@@ -214,7 +212,6 @@ const unsubscribe = serviceRegistry.watchInstances(
214
212
  updateLocalCache(newInstances);
215
213
  },
216
214
  {
217
- namespaceId: 'public',
218
215
  groupName: 'DEFAULT_GROUP',
219
216
  },
220
217
  );
@@ -40,7 +40,6 @@ app.registerModule(
40
40
  nacos: {
41
41
  client: {
42
42
  serverList: ['http://localhost:8848'],
43
- namespaceId: 'public',
44
43
  username: 'nacos',
45
44
  password: 'nacos',
46
45
  },
@@ -55,7 +54,6 @@ app.registerModule(
55
54
  nacos: {
56
55
  client: {
57
56
  serverList: ['http://localhost:8848'],
58
- namespaceId: 'public',
59
57
  username: 'nacos',
60
58
  password: 'nacos',
61
59
  },
@@ -230,7 +228,6 @@ class MyService {
230
228
  ```typescript
231
229
  const instances = await serviceRegistry.getInstances('user-service', {
232
230
  healthyOnly: true,
233
- namespaceId: 'public',
234
231
  });
235
232
 
236
233
  // Watch for service instance changes
@@ -21,6 +21,8 @@
21
21
  - **配置优先级**:配置中心配置 > 环境变量 > 默认配置
22
22
  - **配置监听**:监听配置变更,自动刷新应用配置
23
23
 
24
+ > **命名空间**:使用 Nacos public 命名空间(控制台默认)时,省略 `namespaceId` 即可。请勿设为字面量 `"public"`。详见 [Nacos 集成文档 - 命名空间](./microservice-nacos.md#命名空间)。
25
+
24
26
  ## 快速开始
25
27
 
26
28
  ### 1. 注册配置中心模块
@@ -37,7 +39,6 @@ app.registerModule(
37
39
  nacos: {
38
40
  client: {
39
41
  serverList: ['http://localhost:8848'],
40
- namespaceId: 'public',
41
42
  username: 'nacos',
42
43
  password: 'nacos',
43
44
  },
@@ -5,6 +5,7 @@
5
5
  ## 目录
6
6
 
7
7
  - [概述](#概述)
8
+ - [命名空间](#命名空间)
8
9
  - [Nacos 安装](#nacos-安装)
9
10
  - [配置中心集成](#配置中心集成)
10
11
  - [服务注册中心集成](#服务注册中心集成)
@@ -20,6 +21,26 @@ Bun Server Framework 通过 `@dangao/nacos-client` 包提供 Nacos 3.X 支持,
20
21
  - **服务发现**:服务实例查询和监听
21
22
  - **Open API**:基于 Nacos 3.X Open API 实现
22
23
 
24
+ ## 命名空间
25
+
26
+ Nacos 控制台默认命名空间显示为 **public**,但其 Open API 中的 ID 是**空字符串** `""`,不是字面量 `"public"`。
27
+
28
+ `@dangao/nacos-client` **0.1.2+** 在未配置 `namespaceId` 时会自动在请求中附带 `namespaceId=`(空值),适用于配置读取与服务发现。0.1.1 及更早版本会完全省略该参数,导致 Nacos 3.x 返回 `code=20004`。
29
+
30
+ **推荐做法**:使用 public 命名空间时,省略客户端配置中的 `namespaceId`:
31
+
32
+ ```typescript
33
+ nacos: {
34
+ client: {
35
+ serverList: ['http://localhost:8848'],
36
+ username: 'nacos',
37
+ password: 'nacos',
38
+ },
39
+ },
40
+ ```
41
+
42
+ 自定义命名空间时传入控制台中的 ID(通常为 UUID)。若曾对 `@dangao/nacos-client@0.1.1` 使用 `bun patch`,升级至 0.1.2+ 后请移除 `patchedDependencies` 及对应 patch 文件。
43
+
23
44
  ## Nacos 安装
24
45
 
25
46
  ### Docker 安装(推荐)
@@ -48,7 +69,6 @@ app.registerModule(
48
69
  nacos: {
49
70
  client: {
50
71
  serverList: ['http://localhost:8848'],
51
- namespaceId: 'public',
52
72
  username: 'nacos',
53
73
  password: 'nacos',
54
74
  },
@@ -125,7 +145,6 @@ app.registerModule(
125
145
  nacos: {
126
146
  client: {
127
147
  serverList: ['http://localhost:8848'],
128
- namespaceId: 'public',
129
148
  username: 'nacos',
130
149
  password: 'nacos',
131
150
  },
@@ -197,7 +216,6 @@ import {
197
216
 
198
217
  const instances = await serviceRegistry.getInstances('user-service', {
199
218
  healthyOnly: true,
200
- namespaceId: 'public',
201
219
  });
202
220
 
203
221
  // 监听服务实例变更
@@ -226,7 +244,6 @@ ConfigCenterModule.forRoot({
226
244
  nacos: {
227
245
  client: {
228
246
  serverList: ['http://localhost:8848'],
229
- namespaceId: 'public',
230
247
  username: 'nacos',
231
248
  password: 'nacos',
232
249
  },
@@ -239,7 +256,6 @@ ServiceRegistryModule.forRoot({
239
256
  nacos: {
240
257
  client: {
241
258
  serverList: ['http://localhost:8848'],
242
- namespaceId: 'public',
243
259
  username: 'nacos',
244
260
  password: 'nacos',
245
261
  },
@@ -337,6 +353,17 @@ await app.listen(3000);
337
353
  - 检查配置监听回调是否正确实现
338
354
  - 查看日志确认配置变更是否被检测到
339
355
 
356
+ ### 6. public 命名空间返回 20004
357
+
358
+ **问题**:未配置 `namespaceId` 时,`getConfig` / 服务注册或发现返回 `code=20004`
359
+
360
+ **原因**:Nacos 3.x 要求请求携带 `namespaceId=`(空值);0.1.1 及更早的 `@dangao/nacos-client` 会完全省略该参数
361
+
362
+ **解决方案**:
363
+ - 升级 `@dangao/nacos-client` 至 **0.1.2+**(框架依赖会自动带入,也可在下游项目中显式升级)
364
+ - 移除 `bun patch`(如有)
365
+ - 不要将 `namespaceId` 设为字面量 `"public"`;public 对应空字符串,推荐直接省略配置
366
+
340
367
  ## 参考资源
341
368
 
342
369
  - [Nacos 官方文档](https://nacos.io/docs/latest/)
@@ -22,6 +22,8 @@
22
22
  - **健康检查**:自动根据健康检查状态更新服务健康状态
23
23
  - **实例监听**:监听服务实例变更
24
24
 
25
+ > **命名空间**:使用 Nacos public 命名空间时省略 `namespaceId`。详见 [Nacos 集成文档 - 命名空间](./microservice-nacos.md#命名空间)。
26
+
25
27
  ## 快速开始
26
28
 
27
29
  ### 1. 注册服务注册中心模块
@@ -39,7 +41,6 @@ app.registerModule(
39
41
  nacos: {
40
42
  client: {
41
43
  serverList: ['http://localhost:8848'],
42
- namespaceId: 'public',
43
44
  username: 'nacos',
44
45
  password: 'nacos',
45
46
  },
@@ -94,7 +95,6 @@ await app.listen(3000);
94
95
  region: 'us-east',
95
96
  },
96
97
  clusterName: 'default',
97
- namespaceId: 'public',
98
98
  groupName: 'DEFAULT_GROUP',
99
99
  })
100
100
  @Controller('/api/users')
@@ -112,7 +112,7 @@ class UserController {
112
112
  - `healthy`:初始健康状态
113
113
  - `metadata`:服务元数据(版本、区域等)
114
114
  - `clusterName`:集群名
115
- - `namespaceId`:命名空间
115
+ - `namespaceId`:命名空间 ID;省略表示 public(空字符串)。详见 [Nacos 集成文档 - 命名空间](./microservice-nacos.md#命名空间)
116
116
  - `groupName`:分组名
117
117
 
118
118
  ### 手动注册
@@ -181,7 +181,6 @@ import type { ServiceInstance } from '@dangao/bun-server';
181
181
  class MyService {
182
182
  @ServiceDiscovery('user-service', {
183
183
  healthyOnly: true, // 只获取健康实例
184
- namespaceId: 'public',
185
184
  groupName: 'DEFAULT_GROUP',
186
185
  })
187
186
  public instances: ServiceInstance[] = [];
@@ -199,7 +198,6 @@ class MyService {
199
198
  // 获取服务实例列表
200
199
  const instances = await serviceRegistry.getInstances('user-service', {
201
200
  healthyOnly: true,
202
- namespaceId: 'public',
203
201
  groupName: 'DEFAULT_GROUP',
204
202
  clusterName: 'default',
205
203
  });
@@ -213,7 +211,6 @@ const unsubscribe = serviceRegistry.watchInstances(
213
211
  updateLocalCache(newInstances);
214
212
  },
215
213
  {
216
- namespaceId: 'public',
217
214
  groupName: 'DEFAULT_GROUP',
218
215
  },
219
216
  );
@@ -40,7 +40,6 @@ app.registerModule(
40
40
  nacos: {
41
41
  client: {
42
42
  serverList: ['http://localhost:8848'],
43
- namespaceId: 'public',
44
43
  username: 'nacos',
45
44
  password: 'nacos',
46
45
  },
@@ -55,7 +54,6 @@ app.registerModule(
55
54
  nacos: {
56
55
  client: {
57
56
  serverList: ['http://localhost:8848'],
58
- namespaceId: 'public',
59
57
  username: 'nacos',
60
58
  password: 'nacos',
61
59
  },
@@ -230,7 +228,6 @@ class MyService {
230
228
  ```typescript
231
229
  const instances = await serviceRegistry.getInstances('user-service', {
232
230
  healthyOnly: true,
233
- namespaceId: 'public',
234
231
  });
235
232
 
236
233
  // 监听服务实例变更
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dangao/bun-server",
3
- "version": "3.2.0",
3
+ "version": "3.3.1",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -48,7 +48,7 @@
48
48
  "test:bun": "bun test tests/platform/bun",
49
49
  "test:node": "vitest run tests/platform/node",
50
50
  "test:platform": "bun run test:bun && bun run test:node",
51
- "type-check": "tsc --noEmit",
51
+ "typecheck": "tsc -p tsconfig.build.json --noEmit && tsc -p tsconfig.test.json --noEmit",
52
52
  "clean": "rm -rf dist",
53
53
  "bundle": "bun build src/index.ts --target=bun --format=esm --outfile=dist/index.js --external reflect-metadata --external @dangao/logsmith --external @dangao/nacos-client --external @vscode/sqlite3",
54
54
  "bundle:node": "bun build src/index.ts --target=node --packages=external --outfile=dist/index.node.mjs",
@@ -79,7 +79,7 @@
79
79
  },
80
80
  "dependencies": {
81
81
  "@dangao/logsmith": "0.2.0",
82
- "@dangao/nacos-client": "0.1.1",
82
+ "@dangao/nacos-client": "0.1.2",
83
83
  "json5": "^2.2.3",
84
84
  "jsonc-parser": "^3.3.1",
85
85
  "marked": "^18.0.0",
@@ -88,4 +88,4 @@
88
88
  "postgres": "^3.4.9",
89
89
  "reflect-metadata": "^0.2.2"
90
90
  }
91
- }
91
+ }
package/src/ai/types.ts CHANGED
@@ -144,6 +144,11 @@ export const AI_SERVICE_TOKEN = Symbol('@dangao/bun-server:ai:service');
144
144
  export const AI_MODULE_OPTIONS_TOKEN = Symbol('@dangao/bun-server:ai:options');
145
145
  export const AI_TOOL_REGISTRY_TOKEN = Symbol('@dangao/bun-server:ai:tool-registry');
146
146
 
147
+ /**
148
+ * Metadata key for AiModule options.
149
+ */
150
+ export const MODULE_METADATA_KEY = '@dangao/bun-server:ai:module';
151
+
147
152
  /**
148
153
  * Metadata key for @AiTool decorator
149
154
  */
package/src/auth/types.ts CHANGED
@@ -93,6 +93,10 @@ export interface OAuth2Client {
93
93
  * 允许的授权类型
94
94
  */
95
95
  grantTypes: ('authorization_code' | 'refresh_token')[];
96
+ /**
97
+ * 允许的 OAuth2 scope 列表
98
+ */
99
+ scopes?: string[];
96
100
  }
97
101
 
98
102
  /**
@@ -245,4 +249,3 @@ export interface AuthContext {
245
249
  */
246
250
  isAuthenticated: boolean;
247
251
  }
248
-
@@ -91,8 +91,8 @@ export class CacheableInterceptor extends BaseInterceptor {
91
91
  public async execute<T>(
92
92
  target: unknown,
93
93
  propertyKey: string | symbol,
94
- originalMethod: (...args: unknown[]) => T | Promise<T>,
95
- args: unknown[],
94
+ originalMethod: (...args: any[]) => T | Promise<T>,
95
+ args: any[],
96
96
  container: Container,
97
97
  context?: Context,
98
98
  ): Promise<T> {
@@ -164,8 +164,8 @@ export class CacheEvictInterceptor extends BaseInterceptor {
164
164
  public async execute<T>(
165
165
  target: unknown,
166
166
  propertyKey: string | symbol,
167
- originalMethod: (...args: unknown[]) => T | Promise<T>,
168
- args: unknown[],
167
+ originalMethod: (...args: any[]) => T | Promise<T>,
168
+ args: any[],
169
169
  container: Container,
170
170
  context?: Context,
171
171
  ): Promise<T> {
@@ -238,8 +238,8 @@ export class CachePutInterceptor extends BaseInterceptor {
238
238
  public async execute<T>(
239
239
  target: unknown,
240
240
  propertyKey: string | symbol,
241
- originalMethod: (...args: unknown[]) => T | Promise<T>,
242
- args: unknown[],
241
+ originalMethod: (...args: any[]) => T | Promise<T>,
242
+ args: any[],
243
243
  container: Container,
244
244
  context?: Context,
245
245
  ): Promise<T> {
@@ -87,9 +87,9 @@ export class CacheServiceProxy {
87
87
 
88
88
  // 返回包装后的方法
89
89
  // 使用原型方法以确保拦截器能获取到正确的元数据
90
- const originalMethod = prototypeMethod as (...args: unknown[]) => unknown;
90
+ const originalMethod = prototypeMethod as (...args: any[]) => unknown;
91
91
 
92
- return async function (this: T, ...args: unknown[]): Promise<unknown> {
92
+ return async function (this: T, ...args: any[]): Promise<unknown> {
93
93
  // 按优先级执行拦截器:CacheEvict (beforeInvocation) -> Cacheable/CachePut -> CacheEvict (afterInvocation)
94
94
 
95
95
  // 如果有 @CacheEvict 且配置了 beforeInvocation
@@ -13,11 +13,14 @@ export class CacheService {
13
13
  private keyPrefix: string;
14
14
 
15
15
  public constructor(
16
- @Inject(CACHE_OPTIONS_TOKEN) options: CacheModuleOptions,
16
+ @Inject(CACHE_OPTIONS_TOKEN) options: CacheModuleOptions | CacheStore,
17
17
  ) {
18
- this.store = options.store!;
19
- this.defaultTtl = options.defaultTtl ?? 3600000; // 1 小时
20
- this.keyPrefix = options.keyPrefix ?? '';
18
+ const resolvedOptions: CacheModuleOptions = isCacheStore(options)
19
+ ? { store: options }
20
+ : options;
21
+ this.store = resolvedOptions.store!;
22
+ this.defaultTtl = resolvedOptions.defaultTtl ?? 3600000; // 1 小时
23
+ this.keyPrefix = resolvedOptions.keyPrefix ?? '';
21
24
  }
22
25
 
23
26
  /**
@@ -25,7 +28,7 @@ export class CacheService {
25
28
  * @param key - 缓存键
26
29
  * @returns 缓存值,如果不存在则返回 undefined
27
30
  */
28
- public async get<T = unknown>(key: string): Promise<T | undefined> {
31
+ public async get<T = any>(key: string): Promise<T | undefined> {
29
32
  return this.store.get<T>(this.getKey(key));
30
33
  }
31
34
 
@@ -36,7 +39,7 @@ export class CacheService {
36
39
  * @param ttl - 过期时间(毫秒),0 表示永不过期,undefined 使用默认 TTL
37
40
  * @returns 是否设置成功
38
41
  */
39
- public async set<T = unknown>(
42
+ public async set<T = any>(
40
43
  key: string,
41
44
  value: T,
42
45
  ttl?: number,
@@ -76,7 +79,7 @@ export class CacheService {
76
79
  * @param keys - 缓存键数组
77
80
  * @returns 缓存值映射
78
81
  */
79
- public async getMany<T = unknown>(
82
+ public async getMany<T = any>(
80
83
  keys: string[],
81
84
  ): Promise<Map<string, T>> {
82
85
  const prefixedKeys = keys.map((k) => this.getKey(k));
@@ -97,7 +100,7 @@ export class CacheService {
97
100
  * @param ttl - 过期时间(毫秒),0 表示永不过期,undefined 使用默认 TTL
98
101
  * @returns 是否设置成功
99
102
  */
100
- public async setMany<T = unknown>(
103
+ public async setMany<T = any>(
101
104
  entries: Array<{ key: string; value: T }>,
102
105
  ttl?: number,
103
106
  ): Promise<boolean> {
@@ -149,3 +152,9 @@ export class CacheService {
149
152
  return this.keyPrefix ? `${this.keyPrefix}${key}` : key;
150
153
  }
151
154
  }
155
+
156
+ function isCacheStore(value: CacheModuleOptions | CacheStore): value is CacheStore {
157
+ return typeof (value as CacheStore).get === 'function' &&
158
+ typeof (value as CacheStore).set === 'function' &&
159
+ typeof (value as CacheStore).delete === 'function';
160
+ }
@@ -7,7 +7,7 @@ export interface CacheStore {
7
7
  * @param key - 缓存键
8
8
  * @returns 缓存值,如果不存在则返回 undefined
9
9
  */
10
- get<T = unknown>(key: string): Promise<T | undefined>;
10
+ get<T = any>(key: string): Promise<T | undefined>;
11
11
 
12
12
  /**
13
13
  * 设置缓存值
@@ -16,7 +16,7 @@ export interface CacheStore {
16
16
  * @param ttl - 过期时间(毫秒),0 表示永不过期
17
17
  * @returns 是否设置成功
18
18
  */
19
- set<T = unknown>(key: string, value: T, ttl?: number): Promise<boolean>;
19
+ set<T = any>(key: string, value: T, ttl?: number): Promise<boolean>;
20
20
 
21
21
  /**
22
22
  * 删除缓存
@@ -43,7 +43,7 @@ export interface CacheStore {
43
43
  * @param keys - 缓存键数组
44
44
  * @returns 缓存值映射
45
45
  */
46
- getMany<T = unknown>(keys: string[]): Promise<Map<string, T>>;
46
+ getMany<T = any>(keys: string[]): Promise<Map<string, T>>;
47
47
 
48
48
  /**
49
49
  * 设置多个缓存值
@@ -51,7 +51,7 @@ export interface CacheStore {
51
51
  * @param ttl - 过期时间(毫秒),0 表示永不过期
52
52
  * @returns 是否设置成功
53
53
  */
54
- setMany<T = unknown>(
54
+ setMany<T = any>(
55
55
  entries: Array<{ key: string; value: T }>,
56
56
  ttl?: number,
57
57
  ): Promise<boolean>;
@@ -84,7 +84,7 @@ export class MemoryCacheStore implements CacheStore {
84
84
  }
85
85
  }
86
86
 
87
- public async get<T = unknown>(key: string): Promise<T | undefined> {
87
+ public async get<T = any>(key: string): Promise<T | undefined> {
88
88
  const entry = this.store.get(key);
89
89
  if (!entry) {
90
90
  return undefined;
@@ -99,7 +99,7 @@ export class MemoryCacheStore implements CacheStore {
99
99
  return entry.value as T;
100
100
  }
101
101
 
102
- public async set<T = unknown>(
102
+ public async set<T = any>(
103
103
  key: string,
104
104
  value: T,
105
105
  ttl?: number,
@@ -133,7 +133,7 @@ export class MemoryCacheStore implements CacheStore {
133
133
  return true;
134
134
  }
135
135
 
136
- public async getMany<T = unknown>(
136
+ public async getMany<T = any>(
137
137
  keys: string[],
138
138
  ): Promise<Map<string, T>> {
139
139
  const result = new Map<string, T>();
@@ -154,7 +154,7 @@ export class MemoryCacheStore implements CacheStore {
154
154
  return result;
155
155
  }
156
156
 
157
- public async setMany<T = unknown>(
157
+ public async setMany<T = any>(
158
158
  entries: Array<{ key: string; value: T }>,
159
159
  ttl?: number,
160
160
  ): Promise<boolean> {
@@ -239,7 +239,7 @@ export class RedisCacheStore implements CacheStore {
239
239
  return `${this.keyPrefix}${key}`;
240
240
  }
241
241
 
242
- public async get<T = unknown>(key: string): Promise<T | undefined> {
242
+ public async get<T = any>(key: string): Promise<T | undefined> {
243
243
  const value = await this.client.get(this.getKey(key));
244
244
  if (value === null) {
245
245
  return undefined;
@@ -251,7 +251,7 @@ export class RedisCacheStore implements CacheStore {
251
251
  }
252
252
  }
253
253
 
254
- public async set<T = unknown>(
254
+ public async set<T = any>(
255
255
  key: string,
256
256
  value: T,
257
257
  ttl?: number,
@@ -296,7 +296,7 @@ export class RedisCacheStore implements CacheStore {
296
296
  }
297
297
  }
298
298
 
299
- public async getMany<T = unknown>(
299
+ public async getMany<T = any>(
300
300
  keys: string[],
301
301
  ): Promise<Map<string, T>> {
302
302
  const result = new Map<string, T>();
@@ -325,7 +325,7 @@ export class RedisCacheStore implements CacheStore {
325
325
  return result;
326
326
  }
327
327
 
328
- public async setMany<T = unknown>(
328
+ public async setMany<T = any>(
329
329
  entries: Array<{ key: string; value: T }>,
330
330
  ttl?: number,
331
331
  ): Promise<boolean> {
@@ -67,12 +67,12 @@ export class ConfigService<TConfig extends Record<string, unknown> = Record<stri
67
67
  * 更新配置(用于配置中心动态刷新)
68
68
  * @param newConfig - 新配置对象
69
69
  */
70
- public updateConfig(newConfig: TConfig): void {
71
- this.config = newConfig;
70
+ public updateConfig(newConfig: TConfig | Record<string, unknown>): void {
71
+ this.config = newConfig as TConfig;
72
72
  // 通知所有监听器
73
73
  for (const listener of this.configUpdateListeners) {
74
74
  try {
75
- listener(newConfig);
75
+ listener(this.config);
76
76
  } catch (error) {
77
77
  console.error('[ConfigService] Error in config update listener:', error);
78
78
  }
@@ -83,7 +83,7 @@ export class ConfigService<TConfig extends Record<string, unknown> = Record<stri
83
83
  * 合并配置(用于配置中心增量更新)
84
84
  * @param partialConfig - 部分配置对象
85
85
  */
86
- public mergeConfig(partialConfig: Partial<TConfig>): void {
86
+ public mergeConfig(partialConfig: Partial<TConfig> | Record<string, unknown>): void {
87
87
  this.config = {
88
88
  ...this.config,
89
89
  ...partialConfig,
@@ -125,6 +125,8 @@ export class ConfigService<TConfig extends Record<string, unknown> = Record<stri
125
125
  * @param key - 配置键(如 "db.host")
126
126
  * @param defaultValue - 默认值(可选)
127
127
  */
128
+ public get(key: string): any;
129
+ public get<T>(key: string, defaultValue?: T): T | undefined;
128
130
  public get<T = unknown>(key: string, defaultValue?: T): T | undefined {
129
131
  const namespacedKey = this.applyNamespace(key);
130
132
  const value = this.getValueByPath(this.config, namespacedKey);
@@ -138,6 +140,8 @@ export class ConfigService<TConfig extends Record<string, unknown> = Record<stri
138
140
  * 获取必需的配置值,如果不存在则抛出错误
139
141
  * @param key - 配置键
140
142
  */
143
+ public getRequired(key: string): any;
144
+ public getRequired<T>(key: string): T;
141
145
  public getRequired<T = unknown>(key: string): T {
142
146
  const value = this.get<T>(key);
143
147
  if (value === undefined) {
@@ -191,5 +195,3 @@ export class ConfigService<TConfig extends Record<string, unknown> = Record<stri
191
195
  return current;
192
196
  }
193
197
  }
194
-
195
-