@taicode/common-server 4.0.0-beta.1 → 4.0.0-beta.10

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 (292) hide show
  1. package/output/app/index.d.ts +18 -15
  2. package/output/app/index.d.ts.map +1 -1
  3. package/output/app/index.js +330 -18
  4. package/output/auth/index.d.ts +73 -0
  5. package/output/auth/index.d.ts.map +1 -0
  6. package/output/auth/index.js +196 -0
  7. package/output/core/assemble.d.ts +64 -0
  8. package/output/core/assemble.d.ts.map +1 -0
  9. package/output/core/assemble.js +88 -0
  10. package/output/core/boot/context.d.ts +109 -0
  11. package/output/core/boot/context.d.ts.map +1 -0
  12. package/output/core/boot/context.js +145 -0
  13. package/output/core/boot/lifecycle.d.ts +206 -0
  14. package/output/core/boot/lifecycle.d.ts.map +1 -0
  15. package/output/core/boot/lifecycle.js +255 -0
  16. package/output/core/boot/shutdown.d.ts +34 -0
  17. package/output/core/boot/shutdown.d.ts.map +1 -0
  18. package/output/core/boot/shutdown.js +50 -0
  19. package/output/core/boot/signal.d.ts +13 -0
  20. package/output/core/boot/signal.d.ts.map +1 -0
  21. package/output/{app → core/boot}/signal.js +3 -2
  22. package/output/core/boot/steps.d.ts +138 -0
  23. package/output/core/boot/steps.d.ts.map +1 -0
  24. package/output/core/boot/steps.js +259 -0
  25. package/output/core/entries.d.ts +151 -0
  26. package/output/core/entries.d.ts.map +1 -0
  27. package/output/core/entries.js +242 -0
  28. package/output/core/entries.test.d.ts +2 -0
  29. package/output/core/entries.test.d.ts.map +1 -0
  30. package/output/core/entries.test.js +458 -0
  31. package/output/core/env.d.ts +83 -0
  32. package/output/core/env.d.ts.map +1 -0
  33. package/output/core/env.js +124 -0
  34. package/output/{app → core}/env.test.d.ts.map +1 -1
  35. package/output/core/env.test.js +105 -0
  36. package/output/core/modules/auth-provider/index.d.ts +10 -0
  37. package/output/core/modules/auth-provider/index.d.ts.map +1 -0
  38. package/output/core/modules/auth-provider/index.js +9 -0
  39. package/output/core/modules/auth-provider/types.d.ts +71 -0
  40. package/output/core/modules/auth-provider/types.d.ts.map +1 -0
  41. package/output/core/modules/context/index.d.ts +18 -0
  42. package/output/core/modules/context/index.d.ts.map +1 -0
  43. package/output/core/modules/context/index.js +23 -0
  44. package/output/core/modules/context/index.test.d.ts.map +1 -0
  45. package/output/core/modules/cookie/index.d.ts +21 -0
  46. package/output/core/modules/cookie/index.d.ts.map +1 -0
  47. package/output/{app/core/cookie.js → core/modules/cookie/index.js} +1 -6
  48. package/output/core/modules/identity/index.d.ts +4 -0
  49. package/output/core/modules/identity/index.d.ts.map +1 -0
  50. package/output/{app/core/identity.js → core/modules/identity/index.js} +1 -1
  51. package/output/core/modules/logging/index.d.ts +5 -0
  52. package/output/core/modules/logging/index.d.ts.map +1 -0
  53. package/output/{app/core/logging.js → core/modules/logging/index.js} +2 -2
  54. package/output/core/modules/openapi/index.d.ts +7 -0
  55. package/output/core/modules/openapi/index.d.ts.map +1 -0
  56. package/output/{app/core/openapi.js → core/modules/openapi/index.js} +11 -12
  57. package/output/core/modules/permission-operations/index.d.ts +26 -0
  58. package/output/core/modules/permission-operations/index.d.ts.map +1 -0
  59. package/output/core/modules/permission-operations/index.js +36 -0
  60. package/output/core/modules/permission-operations/index.test.d.ts +2 -0
  61. package/output/core/modules/permission-operations/index.test.d.ts.map +1 -0
  62. package/output/core/modules/permission-operations/index.test.js +45 -0
  63. package/output/{app/core/request-id.d.ts → core/modules/request-id/index.d.ts} +1 -4
  64. package/output/core/modules/request-id/index.d.ts.map +1 -0
  65. package/output/{app/core/request-id.js → core/modules/request-id/index.js} +2 -2
  66. package/output/core/modules/response/index.d.ts +50 -0
  67. package/output/core/modules/response/index.d.ts.map +1 -0
  68. package/output/{app/core/response.js → core/modules/response/index.js} +31 -18
  69. package/output/{schema/index.d.ts → core/modules/schema/builders.d.ts} +8 -1
  70. package/output/core/modules/schema/builders.d.ts.map +1 -0
  71. package/output/{schema/index.js → core/modules/schema/builders.js} +7 -4
  72. package/output/core/modules/schema/index.d.ts +34 -0
  73. package/output/core/modules/schema/index.d.ts.map +1 -0
  74. package/output/core/modules/schema/index.js +62 -0
  75. package/output/core/modules/schema/index.test.d.ts +2 -0
  76. package/output/core/modules/schema/index.test.d.ts.map +1 -0
  77. package/output/core/modules/schema/index.test.js +55 -0
  78. package/output/core/modules/system-routes/index.d.ts +15 -0
  79. package/output/core/modules/system-routes/index.d.ts.map +1 -0
  80. package/output/core/modules/system-routes/index.js +205 -0
  81. package/output/core/resource.d.ts +56 -0
  82. package/output/core/resource.d.ts.map +1 -0
  83. package/output/core/resource.js +56 -0
  84. package/output/core/test-support/database.d.ts +89 -0
  85. package/output/core/test-support/database.d.ts.map +1 -0
  86. package/output/core/test-support/database.js +131 -0
  87. package/output/core/test-support/database.test.d.ts +2 -0
  88. package/output/core/test-support/database.test.d.ts.map +1 -0
  89. package/output/core/test-support/database.test.js +111 -0
  90. package/output/core/test-support/environment.d.ts +38 -0
  91. package/output/core/test-support/environment.d.ts.map +1 -0
  92. package/output/core/test-support/environment.js +45 -0
  93. package/output/core/test-support/service-context.d.ts +64 -0
  94. package/output/core/test-support/service-context.d.ts.map +1 -0
  95. package/output/core/test-support/service-context.js +63 -0
  96. package/output/core/test-support/service-context.test.d.ts +2 -0
  97. package/output/core/test-support/service-context.test.d.ts.map +1 -0
  98. package/output/core/test-support/service-context.test.js +55 -0
  99. package/output/core/test-support/test-server.d.ts +154 -0
  100. package/output/core/test-support/test-server.d.ts.map +1 -0
  101. package/output/core/test-support/test-server.js +114 -0
  102. package/output/core/test-support/test-server.test.d.ts +2 -0
  103. package/output/core/test-support/test-server.test.d.ts.map +1 -0
  104. package/output/core/test-support/test-server.test.js +296 -0
  105. package/output/core/test-support/testdata.d.ts +255 -0
  106. package/output/core/test-support/testdata.d.ts.map +1 -0
  107. package/output/core/test-support/testdata.js +271 -0
  108. package/output/core/test-support/testdata.test.d.ts +2 -0
  109. package/output/core/test-support/testdata.test.d.ts.map +1 -0
  110. package/output/core/test-support/testdata.test.js +334 -0
  111. package/output/{app → core}/type-augment.d.ts +2 -2
  112. package/output/core/type-augment.d.ts.map +1 -0
  113. package/output/core/types.d.ts +582 -0
  114. package/output/core/types.d.ts.map +1 -0
  115. package/output/cors/index.d.ts +3 -0
  116. package/output/cors/index.d.ts.map +1 -0
  117. package/output/cors/index.js +28 -0
  118. package/output/index.d.ts +10 -4
  119. package/output/index.d.ts.map +1 -1
  120. package/output/index.js +25 -5
  121. package/output/rate-limit/index.d.ts +23 -0
  122. package/output/rate-limit/index.d.ts.map +1 -0
  123. package/output/rate-limit/index.js +43 -0
  124. package/output/{rate-limiter/rate-limiter.d.ts → rate-limit/limiter.d.ts} +1 -1
  125. package/output/rate-limit/limiter.d.ts.map +1 -0
  126. package/output/{rate-limiter → rate-limit}/memory-store.d.ts +6 -0
  127. package/output/rate-limit/memory-store.d.ts.map +1 -0
  128. package/output/{rate-limiter → rate-limit}/memory-store.js +8 -0
  129. package/output/{rate-limiter → rate-limit}/redis-store.d.ts +18 -0
  130. package/output/rate-limit/redis-store.d.ts.map +1 -0
  131. package/output/{rate-limiter → rate-limit}/redis-store.js +35 -0
  132. package/output/rate-limit/redis-store.test.d.ts.map +1 -0
  133. package/output/{rate-limiter → rate-limit}/redis-store.test.js +2 -2
  134. package/output/rate-limit/types.d.ts +45 -0
  135. package/output/rate-limit/types.d.ts.map +1 -0
  136. package/output/rate-limit/types.js +1 -0
  137. package/output/{redis-cache/redis-cache.d.ts → redis/cache.d.ts} +1 -1
  138. package/output/redis/cache.d.ts.map +1 -0
  139. package/output/redis/cache.test.d.ts +2 -0
  140. package/output/redis/cache.test.d.ts.map +1 -0
  141. package/output/{redis-cache/redis-cache.test.js → redis/cache.test.js} +1 -1
  142. package/output/{app/redis.d.ts → redis/index.d.ts} +12 -7
  143. package/output/redis/index.d.ts.map +1 -0
  144. package/output/{app/redis.js → redis/index.js} +15 -5
  145. package/output/redis/index.test.d.ts +2 -0
  146. package/output/{context → redis}/index.test.d.ts.map +1 -1
  147. package/output/redis/index.test.js +55 -0
  148. package/output/redis-queue/index.js +1 -1
  149. package/output/redis-queue/test-helpers.d.ts +1 -1
  150. package/output/redis-queue/test-helpers.js +1 -1
  151. package/output/redis-queue/types.d.ts +3 -12
  152. package/output/redis-queue/types.d.ts.map +1 -1
  153. package/package.json +5 -5
  154. package/output/app/auth.test.d.ts +0 -2
  155. package/output/app/auth.test.d.ts.map +0 -1
  156. package/output/app/auth.test.js +0 -148
  157. package/output/app/boot.d.ts +0 -48
  158. package/output/app/boot.d.ts.map +0 -1
  159. package/output/app/boot.js +0 -123
  160. package/output/app/config.test.d.ts +0 -2
  161. package/output/app/config.test.d.ts.map +0 -1
  162. package/output/app/config.test.js +0 -132
  163. package/output/app/context.d.ts +0 -68
  164. package/output/app/context.d.ts.map +0 -1
  165. package/output/app/context.js +0 -73
  166. package/output/app/core/cookie.d.ts +0 -25
  167. package/output/app/core/cookie.d.ts.map +0 -1
  168. package/output/app/core/identity.d.ts +0 -12
  169. package/output/app/core/identity.d.ts.map +0 -1
  170. package/output/app/core/index.d.ts +0 -44
  171. package/output/app/core/index.d.ts.map +0 -1
  172. package/output/app/core/index.js +0 -57
  173. package/output/app/core/logging.d.ts +0 -13
  174. package/output/app/core/logging.d.ts.map +0 -1
  175. package/output/app/core/openapi.d.ts +0 -17
  176. package/output/app/core/openapi.d.ts.map +0 -1
  177. package/output/app/core/request-id.d.ts.map +0 -1
  178. package/output/app/core/response.d.ts +0 -41
  179. package/output/app/core/response.d.ts.map +0 -1
  180. package/output/app/define-server.negative-cases.d.ts +0 -32
  181. package/output/app/define-server.negative-cases.d.ts.map +0 -1
  182. package/output/app/define-server.negative-cases.js +0 -209
  183. package/output/app/define-server.test.d.ts +0 -2
  184. package/output/app/define-server.test.d.ts.map +0 -1
  185. package/output/app/define-server.test.js +0 -97
  186. package/output/app/entries.d.ts +0 -29
  187. package/output/app/entries.d.ts.map +0 -1
  188. package/output/app/entries.js +0 -64
  189. package/output/app/env.d.ts +0 -59
  190. package/output/app/env.d.ts.map +0 -1
  191. package/output/app/env.js +0 -66
  192. package/output/app/env.test.js +0 -124
  193. package/output/app/error-contract.test.d.ts +0 -2
  194. package/output/app/error-contract.test.d.ts.map +0 -1
  195. package/output/app/error-contract.test.js +0 -125
  196. package/output/app/errors.d.ts +0 -20
  197. package/output/app/errors.d.ts.map +0 -1
  198. package/output/app/errors.js +0 -51
  199. package/output/app/generate-openapi.test.d.ts +0 -2
  200. package/output/app/generate-openapi.test.d.ts.map +0 -1
  201. package/output/app/generate-openapi.test.js +0 -123
  202. package/output/app/lifecycle-logging.test.d.ts +0 -2
  203. package/output/app/lifecycle-logging.test.d.ts.map +0 -1
  204. package/output/app/lifecycle-logging.test.js +0 -155
  205. package/output/app/lifecycle.d.ts +0 -70
  206. package/output/app/lifecycle.d.ts.map +0 -1
  207. package/output/app/lifecycle.js +0 -99
  208. package/output/app/lifecycle.test.d.ts +0 -2
  209. package/output/app/lifecycle.test.d.ts.map +0 -1
  210. package/output/app/lifecycle.test.js +0 -152
  211. package/output/app/middleware/auth.d.ts +0 -71
  212. package/output/app/middleware/auth.d.ts.map +0 -1
  213. package/output/app/middleware/auth.js +0 -190
  214. package/output/app/middleware/context.d.ts +0 -13
  215. package/output/app/middleware/context.d.ts.map +0 -1
  216. package/output/app/middleware/context.js +0 -15
  217. package/output/app/middleware/cors.d.ts +0 -14
  218. package/output/app/middleware/cors.d.ts.map +0 -1
  219. package/output/app/middleware/cors.js +0 -30
  220. package/output/app/middleware/index.d.ts +0 -17
  221. package/output/app/middleware/index.d.ts.map +0 -1
  222. package/output/app/middleware/index.js +0 -16
  223. package/output/app/middleware/rate-limit.d.ts +0 -24
  224. package/output/app/middleware/rate-limit.d.ts.map +0 -1
  225. package/output/app/middleware/rate-limit.js +0 -46
  226. package/output/app/middleware-order.test.d.ts +0 -2
  227. package/output/app/middleware-order.test.d.ts.map +0 -1
  228. package/output/app/middleware-order.test.js +0 -148
  229. package/output/app/permission-operations.d.ts +0 -48
  230. package/output/app/permission-operations.d.ts.map +0 -1
  231. package/output/app/permission-operations.js +0 -77
  232. package/output/app/permission-operations.test.d.ts +0 -2
  233. package/output/app/permission-operations.test.d.ts.map +0 -1
  234. package/output/app/permission-operations.test.js +0 -72
  235. package/output/app/permission-registration.test.d.ts +0 -2
  236. package/output/app/permission-registration.test.d.ts.map +0 -1
  237. package/output/app/permission-registration.test.js +0 -138
  238. package/output/app/redis.d.ts.map +0 -1
  239. package/output/app/registered-operations.test.d.ts +0 -2
  240. package/output/app/registered-operations.test.d.ts.map +0 -1
  241. package/output/app/registered-operations.test.js +0 -57
  242. package/output/app/request-id.test.d.ts +0 -2
  243. package/output/app/request-id.test.d.ts.map +0 -1
  244. package/output/app/request-id.test.js +0 -130
  245. package/output/app/resources.test.d.ts +0 -2
  246. package/output/app/resources.test.d.ts.map +0 -1
  247. package/output/app/resources.test.js +0 -197
  248. package/output/app/shutdown.d.ts +0 -22
  249. package/output/app/shutdown.d.ts.map +0 -1
  250. package/output/app/shutdown.js +0 -30
  251. package/output/app/shutdown.test.d.ts +0 -2
  252. package/output/app/shutdown.test.d.ts.map +0 -1
  253. package/output/app/shutdown.test.js +0 -103
  254. package/output/app/signal.d.ts +0 -12
  255. package/output/app/signal.d.ts.map +0 -1
  256. package/output/app/smoke.test.d.ts +0 -2
  257. package/output/app/smoke.test.d.ts.map +0 -1
  258. package/output/app/smoke.test.js +0 -210
  259. package/output/app/test-support.d.ts +0 -21
  260. package/output/app/test-support.d.ts.map +0 -1
  261. package/output/app/test-support.js +0 -24
  262. package/output/app/type-augment.d.ts.map +0 -1
  263. package/output/app/types.d.ts +0 -211
  264. package/output/app/types.d.ts.map +0 -1
  265. package/output/context/index.d.ts +0 -6
  266. package/output/context/index.d.ts.map +0 -1
  267. package/output/context/index.js +0 -14
  268. package/output/rate-limiter/index.d.ts +0 -5
  269. package/output/rate-limiter/index.d.ts.map +0 -1
  270. package/output/rate-limiter/index.js +0 -4
  271. package/output/rate-limiter/memory-store.d.ts.map +0 -1
  272. package/output/rate-limiter/rate-limiter.d.ts.map +0 -1
  273. package/output/rate-limiter/redis-store.d.ts.map +0 -1
  274. package/output/rate-limiter/redis-store.test.d.ts.map +0 -1
  275. package/output/rate-limiter/types.d.ts +0 -27
  276. package/output/rate-limiter/types.d.ts.map +0 -1
  277. package/output/redis-cache/index.d.ts +0 -4
  278. package/output/redis-cache/index.d.ts.map +0 -1
  279. package/output/redis-cache/index.js +0 -1
  280. package/output/redis-cache/redis-cache.d.ts.map +0 -1
  281. package/output/redis-cache/redis-cache.test.d.ts +0 -2
  282. package/output/redis-cache/redis-cache.test.d.ts.map +0 -1
  283. package/output/schema/index.d.ts.map +0 -1
  284. /package/output/{app → core}/env.test.d.ts +0 -0
  285. /package/output/{rate-limiter → core/modules/auth-provider}/types.js +0 -0
  286. /package/output/{context → core/modules/context}/index.test.d.ts +0 -0
  287. /package/output/{context → core/modules/context}/index.test.js +0 -0
  288. /package/output/{app → core}/type-augment.js +0 -0
  289. /package/output/{app → core}/types.js +0 -0
  290. /package/output/{rate-limiter/rate-limiter.js → rate-limit/limiter.js} +0 -0
  291. /package/output/{rate-limiter → rate-limit}/redis-store.test.d.ts +0 -0
  292. /package/output/{redis-cache/redis-cache.js → redis/cache.js} +0 -0
@@ -1,16 +1,19 @@
1
- import './type-augment.js';
2
- export { defineServer, isServer, SERVER_BRAND, startServer, generateOpenApi } from './entries.js';
3
- export { loadEnvFiles, loadRuntimeEnvFiles, resolveConfigEnv, ENV_FILE_LAYERS, TEST_ENV_FILE_LAYER, DEFAULT_LOG_LEVEL } from './env.js';
4
- export type { ConfigEnv } from './env.js';
5
- export { UserError, SystemError } from './errors.js';
6
- export { createRedis, createCache, DEFAULT_REDIS_CACHE_TTL_SECONDS } from './redis.js';
7
- export { createCorsMiddleware, createContextMiddleware, createAuthMiddleware, createRateLimitMiddleware, defaultMiddlewares, defaultAuthSkip } from './middleware/index.js';
8
- export { REQUEST_ID_HEADER } from './core/request-id.js';
9
- export { DOC_ROUTE_PREFIX, DOC_SPEC_ROUTE } from './core/openapi.js';
10
- export { extractPermissionOperations } from './permission-operations.js';
11
- export type { AppIdentity, AppInstance, LoggerLike, MaybePromise, RedisHandle, Live, ResourcesView, AssertNoDependencies, ServicesShape, ResourcesContext, ResourcesFactory, ConfigContext, ConfigFactory, ServiceUnit, ServiceContext, ServicesFactory, MiddlewareUnit, MiddlewareContext, MiddlewaresFactory, RouterContext, RoutersFactory, ServerConfig, Server, StartServerOptions, ShutdownContext, OpenApiDocument, } from './types.js';
12
- export type { CacheSetupParams } from './redis.js';
13
- export type { PermissionOperation } from './permission-operations.js';
14
- export type { CorsMiddlewareOptions, ContextMiddlewareOptions, AuthMiddlewareOptions, AuthSkip, AuthVerifyInput, AuthVerifyMetadata, AuthVerifyResult, AccountIntegrationConfig, RateLimitMiddlewareOptions } from './middleware/index.js';
15
- export type { AccessControl, RateLimitRule } from './type-augment.js';
1
+ import '../core/type-augment.js';
2
+ export { defineServer, startServer, generateOpenApi } from '../core/entries.js';
3
+ export { createTestServer } from '../core/test-support/test-server.js';
4
+ export { createTestServiceContext } from '../core/test-support/service-context.js';
5
+ export type { TestServiceContextOptions } from '../core/test-support/service-context.js';
6
+ export { loadRuntimeEnvironment } from '../core/test-support/environment.js';
7
+ export { createCorsMiddleware } from '../cors/index.js';
8
+ export { createAuthMiddleware } from '../auth/index.js';
9
+ export { createRateLimitMiddleware } from '../rate-limit/index.js';
10
+ export { createRedis, createCache } from '../redis/index.js';
11
+ export { defineResource } from '../core/resource.js';
12
+ export { defineTestData } from '../core/test-support/testdata.js';
13
+ export type { TestDataContext, TestDataDefinition, TestDataDefinitionOf, TestDataRunParams, TestDataScript, TestDataScriptOf, TestDataUnit, } from '../core/test-support/testdata.js';
14
+ export type { AppEnv, AppInstance, ClientsContext, ClientsFactory, ClientsView, ConfigContext, LoggerLike, Middleware, MiddlewareContext, MiddlewareEntry, MiddlewareUnit, ResourceUnit, ResourcesContext, ResourcesView, RouterContext, ServiceContext, } from '../core/types.js';
15
+ export type { AuthVerifyInput, AuthVerifyMetadata, AuthVerifyResult } from '../auth/index.js';
16
+ export type { AuthMiddlewareOptions } from '../auth/index.js';
17
+ export type { AuthClient, AuthVerifyPayload, RegisterPermissionOperationsPayload, } from '../core/modules/auth-provider/index.js';
18
+ export type { RateLimitMiddlewareOptions } from '../rate-limit/index.js';
16
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/app/index.ts"],"names":[],"mappings":"AAEA,OAAO,mBAAmB,CAAA;AAG1B,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAGjG,OAAO,EAAE,YAAY,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,eAAe,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AACvI,YAAY,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAGzC,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAGpD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,+BAA+B,EAAE,MAAM,YAAY,CAAA;AAGtF,OAAO,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,yBAAyB,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAG3K,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAGpE,OAAO,EAAE,2BAA2B,EAAE,MAAM,4BAA4B,CAAA;AAGxE,YAAY,EAEV,WAAW,EACX,WAAW,EACX,UAAU,EACV,YAAY,EACZ,WAAW,EAEX,IAAI,EACJ,aAAa,EACb,oBAAoB,EACpB,aAAa,EAEb,gBAAgB,EAChB,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,WAAW,EACX,cAAc,EACd,eAAe,EACf,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,aAAa,EACb,cAAc,EAEd,YAAY,EACZ,MAAM,EACN,kBAAkB,EAClB,eAAe,EACf,eAAe,GAChB,MAAM,YAAY,CAAA;AACnB,YAAY,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAClD,YAAY,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AACrE,YAAY,EAAE,qBAAqB,EAAE,wBAAwB,EAAE,qBAAqB,EAAE,QAAQ,EAAE,eAAe,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,wBAAwB,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAA;AAC1O,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/app/index.ts"],"names":[],"mappings":"AA8HA,OAAO,yBAAyB,CAAA;AAGhC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAiE/E,OAAO,EAAE,gBAAgB,EAAE,MAAM,qCAAqC,CAAA;AAQtE,OAAO,EAAE,wBAAwB,EAAE,MAAM,yCAAyC,CAAA;AAClF,YAAY,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAA;AAyBxF,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAA;AAyC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AACvD,OAAO,EAAE,yBAAyB,EAAE,MAAM,wBAAwB,CAAA;AAiBlE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAuB5D,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AA0BpD,OAAO,EAAE,cAAc,EAAE,MAAM,kCAAkC,CAAA;AACjE,YAAY,EACV,eAAe,EACf,kBAAkB,EAClB,oBAAoB,EACpB,iBAAiB,EACjB,cAAc,EACd,gBAAgB,EAChB,YAAY,GACb,MAAM,kCAAkC,CAAA;AAGzC,YAAY,EACV,MAAM,EACN,WAAW,EACX,cAAc,EACd,cAAc,EACd,WAAW,EACX,aAAa,EACb,UAAU,EACV,UAAU,EACV,iBAAiB,EACjB,eAAe,EACf,cAAc,EACd,YAAY,EACZ,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,cAAc,GACf,MAAM,kBAAkB,CAAA;AAEzB,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAA;AAC7F,YAAY,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAA;AAC7D,YAAY,EACV,UAAU,EACV,iBAAiB,EACjB,mCAAmC,GACpC,MAAM,wCAAwC,CAAA;AAC/C,YAAY,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAA"}
@@ -1,18 +1,330 @@
1
- // 提供 `request.user` / `request.token` / `request.ctx` 与 `routeOptions.config.accessControls`
2
- // 的统一类型增强。
3
- import './type-augment.js';
4
- // ── 入口 API(N1 §1 / N2 §2.8 / §2.10 / §2.11)───────────────────
5
- export { defineServer, isServer, SERVER_BRAND, startServer, generateOpenApi } from './entries.js';
6
- // ── 配置层(env 固定加载)────────────────────────────────────────
7
- export { loadEnvFiles, loadRuntimeEnvFiles, resolveConfigEnv, ENV_FILE_LAYERS, TEST_ENV_FILE_LAYER, DEFAULT_LOG_LEVEL } from './env.js';
8
- // ── 错误类型(N2 §2.12)──────────────────────────────────────────
9
- export { UserError, SystemError } from './errors.js';
10
- // ── 引导期 helper(`resources(ctx)` 里按需调用)─────────────────
11
- export { createRedis, createCache, DEFAULT_REDIS_CACHE_TTL_SECONDS } from './redis.js';
12
- // ── 标准中间件单元 ───────────────────────────────────────────────
13
- export { createCorsMiddleware, createContextMiddleware, createAuthMiddleware, createRateLimitMiddleware, defaultMiddlewares, defaultAuthSkip } from './middleware/index.js';
14
- // ── 公共路由 / header 常量 ───────────────────────────────────────
15
- export { REQUEST_ID_HEADER } from './core/request-id.js';
16
- export { DOC_ROUTE_PREFIX, DOC_SPEC_ROUTE } from './core/openapi.js';
17
- // ── 权限操作目录(框架内置;各仓用它替代自写实现)───────────────
18
- export { extractPermissionOperations } from './permission-operations.js';
1
+ // 服务入口 barrel(`@taicode/common-server/app`):**业务仓唯一需要 import 的路径**。
2
+ //
3
+ // 这里只有两样东西:**入口 API**(定义 / 启动 / 出文档 / 测试)与**可替换的能力工厂**
4
+ // (中间件、Redis)。框架自己决定得了一律不进这里——core 装配、`request.ctx`、系统路由、
5
+ // 响应信封全部在内部固定完成。
6
+ //
7
+ // ─────────────────────────────────────────────────────────────
8
+ // ## 三十秒上手:一个服务写两个文件(出 spec 是构建期的第三个)
9
+ // ─────────────────────────────────────────────────────────────
10
+ //
11
+ // ```ts
12
+ // // source/server.ts —— **只定义,不启动**:不建连、不 listen、不读库
13
+ // import { defineServer } from '@taicode/common-server/app'
14
+ //
15
+ // export const server = defineServer({
16
+ // app: { service: 'release', title: 'Release Service', version: packageJson.version },
17
+ //
18
+ // // 我们拥有的基础设施:**只读 env**,构造时不建连。
19
+ // // 要扩展 env 类型?两种写法:① 手写首位泛型 `defineServer<{ FOO?: string }>({...})`(简单,但
20
+ // // 其余泛型会退化为 object);② 给工厂 ctx 标注 `ResourcesContext<{ FOO?: string }>`(其余泛型照常推导)。
21
+ // // 加载都走内置逻辑,框架不提供自定义加载。
22
+ // resources: ({ env, disposer }) => ({ database: createDatabase(env.DATABASE_URL) }),
23
+ //
24
+ // // 启动期的配置快照:可以读库,一次读完、进程内不再变
25
+ // config: ({ resources }) => loadConfig(resources),
26
+ //
27
+ // // 可选:我们调用的外部服务(地址 / 凭证来自 config,故排在 config 之后);没有就省略
28
+ // clients: ({ config }) => ({ webhook: createWebhookClient(config.webhook) }),
29
+ //
30
+ // // 业务单元:**服务之间禁止互依**,要共享的东西放 clients
31
+ // services: ({ resources, config, clients }) => ({ user: createUserService({ resources, config, clients }) }),
32
+ //
33
+ // // 中间件:**有序数组,数组位置即注册序**;标准工厂返回具名单元,直接按序摆进去
34
+ // middlewares: (ctx) => [createCorsMiddleware(ctx), createAuthMiddleware(ctx, { client })],
35
+ //
36
+ // // 业务路由:**只能拿 app / services / clients**(绕不过 service 直连数据层)
37
+ // routers: ({ app, services }) => {
38
+ // app.register(createUserRouter(services.user))
39
+ // },
40
+ // })
41
+ // ```
42
+ //
43
+ // ```ts
44
+ // // source/index.ts —— 启动(listen + 信号处理都在这里面)
45
+ // import { startServer } from '@taicode/common-server/app'
46
+ // import { server } from './server'
47
+ //
48
+ // await startServer(server)
49
+ // ```
50
+ //
51
+ // ```ts
52
+ // // build/openapi-gen.ts —— 出 spec(不连库、不建连、不要求配置有效)
53
+ // import { generateOpenApi } from '@taicode/common-server/app'
54
+ // import { server } from '../source/server'
55
+ //
56
+ // await writeFile('openapi.json', JSON.stringify(await generateOpenApi(server), null, 2))
57
+ // ```
58
+ //
59
+ // ─────────────────────────────────────────────────────────────
60
+ // ## 每个槽位能拿什么(拿不到的就是**故意**拿不到)
61
+ // ─────────────────────────────────────────────────────────────
62
+ //
63
+ // | 槽位 | ctx 里有 | 刻意没有 | 为什么 |
64
+ // |---|---|---|---|
65
+ // | `resources` | `env`(可标注扩展) / `log` / `disposer` / `registerClassifyError` | `config` / `services` / `app` | 资源只依赖 env,从结构上消灭「资源依赖配置」的循环 |
66
+ // | `config` | `env`(可标注扩展) / `log` / `disposer` / `resources` | `services` / `app` | 此时服务还没构造 |
67
+ // | `clients`(可选) | `log` / `disposer` / `resources` / `config` | `env` / `services` / `app` | 运行期不许回头读 env;客户端地址来自 config |
68
+ // | `services` | `log` / `disposer` / `resources` / `config` / `clients` | `env` / `services` / `app` | 服务层属运行期;禁 service 互依 |
69
+ // | `middlewares` | `app` / `log` / `disposer` / `services` / `config` / `clients` | `resources` / `env` | 中间件碰数据必须穿过 service |
70
+ // | `routers` | `app` / `log` / `disposer` / `services` / `clients` | `resources` / `env` / `config` | 路由只能穿过 service |
71
+ //
72
+ // 唯一的**可选**槽位是 `clients`(省略即空客户端包);其余槽位恒定,没有业务路由也要写
73
+ // `routers: () => {}`。应用扩展 env **不是槽位**,而是 `defineServer` 的**第一个泛型**:可手写
74
+ // `defineServer<{ FOO?: string }>({...})`(简单,但其余泛型退化为 object),或给 `resources` /
75
+ // `config` 的 `ctx` 标注 `ResourcesContext<Env>`(其余泛型照常推导)。加载仍走内置逻辑,框架不提供自定义加载。
76
+ //
77
+ // 资源释放**在创建处登记**:`ctx.disposer.add(() => closeXxx())`,关闭时框架按阶段逆序 flush,
78
+ // 调用方不需要在任何地方再写一遍 close 逻辑。
79
+ //
80
+ // ─────────────────────────────────────────────────────────────
81
+ // ## 路由怎么写(`operationId` 必须有)
82
+ // ─────────────────────────────────────────────────────────────
83
+ //
84
+ // 每个路由都要给 schema,`operationId` / `summary` / `description` / `tags` 缺一即报错。
85
+ // `operationId` 不是文档装饰:**权限目录就是从它采集的**(启动时 `auth` 上报给 account),
86
+ // 没有它这个接口在权限系统里就是「未知操作」。
87
+ //
88
+ // ```ts
89
+ // app.post('/user', {
90
+ // schema: routerSchema({
91
+ // operationId: 'createUser',
92
+ // summary: '创建用户',
93
+ // description: '管理员创建用户,返回创建后的实体',
94
+ // tags: ['user'],
95
+ // body: z.object({ username: z.string() }),
96
+ // response: { 200: userSchema },
97
+ // }),
98
+ // config: {
99
+ // accessControls: ['IgnorePermission'], // 免登录写 'NoLoginRequired'
100
+ // rateLimit: { limit: 100, window: 60 }, // 可选:需装 rateLimit 中间件
101
+ // },
102
+ // }, handler)
103
+ // ```
104
+ //
105
+ // 返回体会被框架包成 `{ status, message, data }`,你只管返回业务数据;错误就
106
+ // `throw new UserError(...)` / `throw new SystemError(...)`(从 `@taicode/common-base` 引,
107
+ // 不是从本包)——状态码映射与信封由框架统一处理,**不要在路由里手拼响应体**。
108
+ //
109
+ // ─────────────────────────────────────────────────────────────
110
+ // ## 目录布局(见 `common/product/designs/create-server/design.md` §7.1)
111
+ // ─────────────────────────────────────────────────────────────
112
+ // ```
113
+ // source/
114
+ // ├── core/ 核心设计(锁定:不可替换、不可修改,**不对外导出装配**)
115
+ // │ └── test-support/ 测试支持(`createTestServer` / `loadRuntimeEnvironment`)
116
+ // ├── app/ 服务入口 barrel(本文件,不含实现)
117
+ // ├── auth/ 可选:鉴权中间件
118
+ // ├── cors/ 可选:CORS 中间件
119
+ // ├── rate-limit/ 可选:限流中间件 + 限流实现
120
+ // ├── redis/ 请求期 Redis / 缓存 helper
121
+ // └── redis-queue/ 可选:Redis 队列(含自己的 `test` 辅助)
122
+ // ```
123
+ //
124
+ // 测试工具**与正式工具同一个导出面**(不多开 `test-support` subpath):入口的测试同构体
125
+ // 就是入口,分两个出口只会让调用方多记一个路径。归属按「谁提供功能,谁提供配套测试工具」:
126
+ // 队列的测试辅助在 `redis-queue` 里,服务装配的测试入口在 `core/test-support/` 里。
127
+ import '../core/type-augment.js';
128
+ // ── 入口 API(定义 / 启动 / 出文档 / 测试)─────────────────────────
129
+ export { defineServer, startServer, generateOpenApi } from '../core/entries.js';
130
+ // 测试对象的**唯一**构造方式:同一份 `defineServer` 定义、同一套装配,不给槽位覆盖入口。
131
+ //
132
+ // env 加载、测试库结算、闸门、`process.env.DATABASE_URL` 改写都是它的**内部步骤**,
133
+ // 不单独导出。导出那一步就等于把「什么时候该跑闸门」交给调用方——顺序本身就是安全前提,
134
+ // 历史上有仓把环境闸门排在 env 加载之前(判的是空的 `ENV`),守卫于是变成装饰。
135
+ //
136
+ // ── 测试怎么写 ──────────────────────────────────────────────
137
+ //
138
+ // ```ts
139
+ // import { createTestServer } from '@taicode/common-server/app'
140
+ // import { server } from '../server'
141
+ //
142
+ // describe('UserRouter', () => {
143
+ // let testServer: Awaited<ReturnType<typeof createTestServer>>
144
+ // let db: PrismaClient
145
+ //
146
+ // beforeAll(async () => {
147
+ // testServer = await createTestServer(server) // 唯一构造方式
148
+ // db = testServer.resources.database // 生产用的那个客户端,不是新建的
149
+ // })
150
+ //
151
+ // afterAll(async () => {
152
+ // await testServer.app.close() // 八步关闭,与生产同一条路径
153
+ // })
154
+ //
155
+ // beforeEach(async () => {
156
+ // await db.user.deleteMany() // 真实数据库,不 mock
157
+ // })
158
+ //
159
+ // it('创建用户', async () => {
160
+ // const res = await testServer.app.inject({
161
+ // method: 'POST',
162
+ // url: '/user',
163
+ // payload: { username: 'alice' },
164
+ // })
165
+ //
166
+ // expect(res.statusCode).toBe(200)
167
+ // expect(await db.user.count()).toBe(1) // 断言落库,不只断言响应
168
+ // })
169
+ //
170
+ // it('service 行为', async () => {
171
+ // // 也可以完全不走 HTTP:直接调装配好的业务单元
172
+ // await testServer.services.user.create({ username: 'bob' })
173
+ // })
174
+ // })
175
+ // ```
176
+ //
177
+ // **三条规矩**:
178
+ // 1. **不要 mock 数据库 / Redis / 外部依赖**。它们由 `resources` / `clients` 真实构造,
179
+ // 测试连的是 `*_test` schema 上的真库。要替换某个外部服务,改组合(见下),不改装配。
180
+ // 2. **不要另造构造器**(`createTestApp` / `createTestDb` / `new PrismaClient()`)。
181
+ // 另造一份就等于测试里有第二套接线,绿了也不能说明生产是对的。
182
+ // 3. **只测一个模块**时组合定义,不要覆盖槽位:
183
+ // ```ts
184
+ // const onlyUser = defineServer({
185
+ // ...server,
186
+ // routers: ({ app, services }) => app.register(createUserRouter(services.user)),
187
+ // })
188
+ // const testServer = await createTestServer(onlyUser)
189
+ // ```
190
+ //
191
+ // 返回值 `TestServer` 就是测试需要的一切:`app`(原生 Fastify,用 `inject()`)/
192
+ // `env`(`env.DATABASE_URL` 就是本次实际连的测试连接串)/ `resources` / `config` /
193
+ // `clients` / `services` / `log`。没有 `close()` 之类同义包装——关闭就是 `app.close()`。
194
+ export { createTestServer } from '../core/test-support/test-server.js';
195
+ // ── 单元级测试的 `services` 入参 ─────────────────────────────────
196
+ //
197
+ // `createTestServer()` 跑全量装配,适合集成用例;只想验一个 service 的判断逻辑时,
198
+ // 此前四个仓各写一份 `createServiceContext(fakeDb, {...})`,且都带同一句
199
+ // `as unknown as XxxServiceContext` —— 同一个绕过动作被抄多遍,说明缺的是框架能力。
200
+ // 于是把它收进框架:**不伪造能力,只允许缺省能力**(缺省的键被读到当场抛错并点名)。
201
+ export { createTestServiceContext } from '../core/test-support/service-context.js';
202
+ // ── 工具进程的 env 出口(**不在任何入口里跑**的进程)──────────────
203
+ //
204
+ // `prisma.config.ts` / `scripts/*.ts` / 被它们静态 import 的配置模块:ES 模块顶层求值早于
205
+ // 入口函数执行,所以「入口里加载 env」救不了这些文件。这个函数就是它们唯一的开口,只加载
206
+ // 运行期分层、不带任何测试闸门。
207
+ //
208
+ // **什么时候需要它**:你的文件**不是** `startServer` / `createTestServer` 启动的,
209
+ // 却要读 `process.env`(数据库连接串、Redis 地址、脚本参数)。
210
+ //
211
+ // ```ts
212
+ // // prisma.config.ts —— 必须在任何读取 env 的 import 之前调用
213
+ // import { loadRuntimeEnvironment } from '@taicode/common-server/app'
214
+ // loadRuntimeEnvironment()
215
+ //
216
+ // // scripts/seed.ts
217
+ // import { loadRuntimeEnvironment } from '@taicode/common-server/app'
218
+ // const env = loadRuntimeEnvironment()
219
+ // console.log(env.DATABASE_URL)
220
+ // ```
221
+ //
222
+ // **什么时候不要用它**:`source/server.ts` / `source/index.ts` / 测试文件。前者由
223
+ // `startServer` 负责,后者由 `createTestServer` 负责——在那些地方再调一次只是重复读 dotenv,
224
+ // 还会让人误以为 env 需要自己加载。
225
+ export { loadRuntimeEnvironment } from '../core/test-support/environment.js';
226
+ // ── 可替换的标准中间件工厂 ────────────────────────────────────────
227
+ //
228
+ // 只在**要改默认行为**时才出现;不写就是框架默认。三者都接收 `middlewares` 槽位的 `ctx`,
229
+ // 都返回**具名单元**(`MiddlewareUnit`),按顺序摆进 `middlewares` 数组即可:
230
+ //
231
+ // ```ts
232
+ // middlewares: (ctx) => [
233
+ // createCorsMiddleware(ctx), // 允许的源来自 config 的 `cors` 键(请求期现读)
234
+ // createAuthMiddleware(ctx, { client }), // client 必填:鉴权提供方实现
235
+ // createRateLimitMiddleware(ctx, { store }), // store 可选,缺省进程内 memory
236
+ // ]
237
+ // ```
238
+ //
239
+ // **数组位置即注册序**(cors → auth → rateLimit):不再用对象键序(键序由引擎决定、纯数字键
240
+ // 还会被重排)。要插入自定义中间件,返回具名单元 `{ name, register }` 或一个裸注册函数即可。
241
+ //
242
+ // `cors` / `rateLimit` 都**没有行为开关**,配置面只有输入参数:
243
+ // - `cors`:允许的源来自 `config.cors`(`{ enabled, origins }`);未配置 → 放行任意源并 warn 一次。
244
+ // - `rateLimit`:**限流规则写在路由上**,不在中间件里:
245
+ // ```ts
246
+ // app.get('/user', {
247
+ // schema: ...,
248
+ // config: { rateLimit: { limit: 100, window: 60, keys: ['ip'] } }, // 未声明的路由不空转
249
+ // }, handler)
250
+ // ```
251
+ // 需要 Redis 存储时,在 `services` 里构造 `RedisRateLimitStore` 再注入 `store`
252
+ // (中间件拿不到 `resources`,这是刻意的)。
253
+ //
254
+ // ⚠️ 限流计数属于**单元内存状态**:它不会被 `clean()` 清掉(那是库外的东西),
255
+ // 只有重新装配(`TestServer.reprovision()` 重跑 `init()`)才能得到干净基线。
256
+ // 因此 store 必须由 `services` 单元提供:在中间件里 `new MemoryRateLimitStore()` 会让
257
+ // 实例只活在那次 `register(app)` 的闭包里,测试拿不到句柄,只能靠伪造 `remoteAddress`
258
+ // 换桶——那是测试在迁就实现细节。见 `rate-limit/types.ts` 的 `RateLimitStore.clear()`。
259
+ //
260
+ // `auth` 的两件事由框架固定、**不可关闭**:① 跳过判定(`OPTIONS` / 无匹配路由;
261
+ // `NoLoginRequired` **不再跳过身份解析** —— 免登录路由改为「有凭证就解析并回填身份、
262
+ // 解析失败按匿名放行」,否则 `/permission/user/operation/list` 恒返回空清单、
263
+ // `/sign-out` 删不掉 SignIn Secret);② 启动时从 `app.swagger()` 采集权限目录并上报。
264
+ // 你只需要实现 `AuthClient` 的 `verify` / `registerPermissionOperations` 两个方法。
265
+ export { createCorsMiddleware } from '../cors/index.js';
266
+ export { createAuthMiddleware } from '../auth/index.js';
267
+ export { createRateLimitMiddleware } from '../rate-limit/index.js';
268
+ // ── 请求期 Redis / 缓存 helper(资源包里自行构造)─────────────────
269
+ //
270
+ // 框架**不替你建 Redis**;在 `resources` 里按需构造,并在创建处登记释放:
271
+ //
272
+ // ```ts
273
+ // resources: ({ env, log, disposer }) => {
274
+ // const redis = createRedis(env, log) // 无 REDIS_URL → undefined
275
+ // if (redis) disposer.add(() => redis.dispose())
276
+ // return { redis, cache: createCache({ env, prefix: 'release', redis }) }
277
+ // }
278
+ // ```
279
+ //
280
+ // 两者都是**惰性包装**:构造时不建连、不 ping,首次真正使用才连——「出 spec 不连库 / 不连
281
+ // Redis」因此是结构性的,而不是靠调用方自觉。`createCache` 不传 `redis` 也能用(自建客户端,
282
+ // 或在没有 `REDIS_URL` 时退化成纯进程内一级缓存)。
283
+ export { createRedis, createCache } from '../redis/index.js';
284
+ // ── 资源单元(槽位内的生命周期)────────────────────────────────
285
+ //
286
+ // 资源多是第三方对象(PrismaClient / Redis client),形状改不了,所以 `setup()` 挂在外壳上:
287
+ //
288
+ // ```ts
289
+ // resources: (ctx) => ({
290
+ // database: defineResource(createPrisma(ctx.env.DATABASE_URL), {
291
+ // async setup() { await pushSchema(ctx.env.DATABASE_URL) }, // 框架在 config 之前调
292
+ // }),
293
+ // })
294
+ // ```
295
+ //
296
+ // 三个生命周期各管一段:
297
+ // - `setup()` 框架在 `config` 之前调(两个入口都跑);
298
+ // - `clean()` 框架在重新装配前调(**仅测试入口**,`TestServer.reprovision()`);
299
+ // - `dispose()` 框架在关闭流程最后一步调(两个入口都跑)。
300
+ //
301
+ // 「用例之间重置数据」(清表、清缓存、重新插入样本数据)**不在资源上**:资源只知道有哪些表 /
302
+ // 哪些 key,不知道谁往里写了什么。它由一套独立机制承担:`packages/server/testdata/*.ts|js`,
303
+ // 每个脚本自带 `load(ctx)`(+ 可选 `cleanup(ctx)`),框架在 `development` / `test` 下自动装载并执行。
304
+ // 两者串起来的唯一入口是 `createTestServer` 返回的 `reprovision()`。
305
+ export { defineResource } from '../core/resource.js';
306
+ // ── 测试数据脚本(`testdata/`)声明 ──────
307
+ //
308
+ // `testdata/*.ts` 的**推荐写法**是默认导出一个 `defineTestData({ … })`:结构显式、缺 `load`
309
+ // 在定义期就报错(不是等到装载期才发现),`ctx` 的类型由泛型一次性带进来:
310
+ //
311
+ // ```ts
312
+ // import { defineTestData } from '@taicode/common-server/app'
313
+ // import type { server } from '../source/server' // 纯类型导入,无副作用
314
+ //
315
+ // export default defineTestData<typeof server>({
316
+ // dependsOn: ['01-database'],
317
+ // async load(ctx) { … },
318
+ // })
319
+ // ```
320
+ //
321
+ // `cleanup` 是**可选**的:库里的行由资源单元的 `clean()` 在重新装配前清掉,只有「资源清不掉
322
+ // 的东西」(下游 mock 预置状态 / 临时文件)才需要自己写。
323
+ //
324
+ // 命名导出 `load` / `cleanup`(+ 可选 `dependsOn`)继续支持,用于把脚本当对象传
325
+ // (如测试里手造 in-memory 脚本)。同一个文件两者都有时**默认导出优先**。
326
+ //
327
+ // `ctx` 在 `app.ready()` 之后构造,因此装配产物全都在:`env` / `resources` /
328
+ // `config` / `clients` / `services` / `log`。**唯一不给的是 `app`**——拿到它就能 `inject()`,
329
+ // 等于用业务链路(中间件 / 信封 / 错误映射)造数据,基线就不再确定。
330
+ export { defineTestData } from '../core/test-support/testdata.js';
@@ -0,0 +1,73 @@
1
+ import type { AuthClient, AuthVerifyPayload } from '../core/modules/auth-provider/index.js';
2
+ import type { MiddlewareContext, MiddlewareUnit } from '../core/types.js';
3
+ /**
4
+ * 标准中间件单元 `auth`(可替换)。
5
+ *
6
+ * 本单元只做「**框架语义**」,不碰任何鉴权提供方的 HTTP:
7
+ *
8
+ * 1. 跳过判定(**固定**:`OPTIONS` → 无匹配路由);`NoLoginRequired` 的免登录语义在
9
+ * 请求期展开为「有凭证就解析、解析失败按匿名放行」,**不会**跳过身份解析;
10
+ * 2. 取凭证(`Authorization: Bearer` / `Authorization: apikey-*` / `cookieNames` 声明的 cookie);
11
+ * 3. 调 `client.verify`,把结果写回 `request.user` / `request.token`;
12
+ * 4. `onReady` 时调 `client.registerPermissionOperations`(**强制**,失败不致命)。
13
+ *
14
+ * 权限目录**只从 OpenAPI 文档派生**(`app.swagger()`):core 保证「所有路由必须声明 schema」
15
+ * (缺失直接启动失败),所以文档里出现什么,权限系统里就有什么,不存在两套来源漂移的可能。
16
+ *
17
+ * **提供方是业务注入的,且可替换**(本文件的 `client`,类型见
18
+ * `core/modules/auth-provider`):account 只是当前的主要实现,不是框架的预设——
19
+ * 自建 IAM、OIDC 网关、进程内静态校验都可以填进来。客户端构造需要地址、凭证、
20
+ * 协议与 HTTP 栈,这些只有业务侧才知道怎么配,框架不提供实现(设计 §5.2.2)。
21
+ *
22
+ * ## 为什么配置面只有两个字段
23
+ *
24
+ * `skip` / `mapError` / `registerPermissions` 曾经也是选项,现全部删除:
25
+ *
26
+ * - **跳过逻辑不可自定义**:放行是安全决策,一旦可按服务改写,「这条路由到底要不要
27
+ * 登录」就不再能从路由声明上读出来。约定只剩一条——**需要公开就声明
28
+ * `config.accessControls: ['NoLoginRequired']`**。
29
+ * - **错误映射不可自定义**:错误分类是响应契约的一部分(401/403 还是 500),让调用方
30
+ * 替换就等于允许各仓自造状态码。标准做法是提供方**自己抛** `UserError` /
31
+ * `SystemError`,框架原样透出;其它一切(网络 / 超时 / 非 JSON)统一
32
+ * `SystemError('EXTERNAL_SERVICE_ERROR')`。
33
+ * - **权限自注册不可关闭**:权限目录是 account 侧判权的前提,关掉它等于让本服务的
34
+ * 全部接口在权限系统里变成「未知操作」,只有坏处。
35
+ *
36
+ * 剩下两个字段都是**输入参数**(不是行为开关):提供方客户端 + 凭证在哪个 cookie 里。
37
+ */
38
+ /** `verify` 的入参:框架校验入参 + 服务名 */
39
+ export type AuthVerifyInput = AuthVerifyPayload;
40
+ /** 请求元信息 / 校验结果(形状定义在 auth-provider 契约里) */
41
+ export type { AuthVerifyMetadata, AuthVerifyResult } from '../core/modules/auth-provider/index.js';
42
+ /**
43
+ * `auth` 中间件的配置面:**只接受输入参数**,不提供行为开关。
44
+ *
45
+ * 标准行为(跳过判定 / 错误归类 / 权限自注册)由框架固定,不开放覆盖——
46
+ * 见文件头的「为什么配置面只有两个字段」。
47
+ */
48
+ export interface AuthMiddlewareOptions {
49
+ /**
50
+ * 鉴权提供方客户端(**必填**,业务服务实现;框架只定义契约,**允许任意实现替换**)。
51
+ *
52
+ * 由业务在自己的 `middlewares` 槽位里构造(可读 `ctx.config` / `ctx.services`),
53
+ * 职责:校验凭证 + 上报权限目录。account 只是其中一种实现——自带校验逻辑的服务
54
+ * (如 account 自己)实现同一个接口走进程内调用即可,不必绕 HTTP 回自己。
55
+ * 契约见 `core/modules/auth-provider`。
56
+ */
57
+ client: AuthClient;
58
+ /**
59
+ * 读取 token 的 cookie 名(**显式声明优先**)。
60
+ *
61
+ * 未传或传空数组时,请求期现读配置的 `server.cookieNames`(各仓的 cookie 名由运维改配置,
62
+ * 不能在注册期就定下);仍为空则回落 `['token']`。
63
+ */
64
+ cookieNames?: readonly string[];
65
+ }
66
+ /**
67
+ * 构造标准 `auth` 中间件(具名单元 `name: 'auth'`)。
68
+ *
69
+ * `client` **必填**(类型上)且必须有运行期实现:装了鉴权却没有校验实现是配置错误,
70
+ * 构造期直接抛错——不留「请求期才 fail-closed」的窗口,也不允许静默放行。
71
+ */
72
+ export declare function createAuthMiddleware<C = unknown, S = unknown>(ctx: MiddlewareContext<C, S>, options: AuthMiddlewareOptions): MiddlewareUnit;
73
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../source/auth/index.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAG3F,OAAO,KAAK,EAAuC,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AAG9G;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,gCAAgC;AAChC,MAAM,MAAM,eAAe,GAAG,iBAAiB,CAAA;AAE/C,4CAA4C;AAC5C,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,wCAAwC,CAAA;AAElG;;;;;GAKG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;;;;OAOG;IACH,MAAM,EAAE,UAAU,CAAA;IAClB;;;;;OAKG;IACH,WAAW,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CAChC;AA+ID;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,OAAO,EAAE,qBAAqB,GAAG,cAAc,CA8E3I"}