ts-procedures 8.5.0 → 9.0.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.
Files changed (635) hide show
  1. package/README.md +166 -101
  2. package/agent_config/claude-code/.claude-plugin/plugin.json +1 -1
  3. package/agent_config/claude-code/agents/ts-procedures-architect.md +11 -10
  4. package/agent_config/claude-code/skills/ts-procedures/SKILL.md +25 -12
  5. package/agent_config/claude-code/skills/ts-procedures/anti-patterns.md +10 -12
  6. package/agent_config/claude-code/skills/ts-procedures/api-reference.md +141 -45
  7. package/agent_config/claude-code/skills/ts-procedures/checklist.md +7 -6
  8. package/agent_config/claude-code/skills/ts-procedures/patterns.md +45 -6
  9. package/agent_config/claude-code/skills/ts-procedures/templates/client.md +1 -1
  10. package/agent_config/claude-code/skills/ts-procedures/templates/hono.md +1 -1
  11. package/agent_config/copilot/copilot-instructions.md +50 -33
  12. package/agent_config/cursor/cursorrules +50 -33
  13. package/build/adapters/astro/astro-context.js.map +1 -0
  14. package/build/adapters/astro/create-handler.js.map +1 -0
  15. package/build/adapters/astro/index.js.map +1 -0
  16. package/build/{implementations/http → adapters}/astro/index.test.js +1 -1
  17. package/build/adapters/astro/index.test.js.map +1 -0
  18. package/build/adapters/astro/rewrite-request.js.map +1 -0
  19. package/build/adapters/hono/envelope-parity.test.js +98 -0
  20. package/build/adapters/hono/envelope-parity.test.js.map +1 -0
  21. package/build/{implementations/http → adapters}/hono/handlers/http-stream.d.ts +1 -1
  22. package/build/adapters/hono/handlers/http-stream.js +55 -0
  23. package/build/adapters/hono/handlers/http-stream.js.map +1 -0
  24. package/build/{implementations/http → adapters}/hono/handlers/http-stream.test.js +1 -1
  25. package/build/adapters/hono/handlers/http-stream.test.js.map +1 -0
  26. package/build/{implementations/http → adapters}/hono/handlers/http.d.ts +1 -1
  27. package/build/adapters/hono/handlers/http.js +50 -0
  28. package/build/adapters/hono/handlers/http.js.map +1 -0
  29. package/build/{implementations/http → adapters}/hono/handlers/http.test.js +1 -1
  30. package/build/adapters/hono/handlers/http.test.js.map +1 -0
  31. package/build/{implementations/http → adapters}/hono/handlers/rpc.d.ts +2 -2
  32. package/build/adapters/hono/handlers/rpc.js +23 -0
  33. package/build/adapters/hono/handlers/rpc.js.map +1 -0
  34. package/build/{implementations/http → adapters}/hono/handlers/rpc.test.js +1 -1
  35. package/build/adapters/hono/handlers/rpc.test.js.map +1 -0
  36. package/build/adapters/hono/handlers/stream.d.ts +12 -0
  37. package/build/adapters/hono/handlers/stream.js +89 -0
  38. package/build/adapters/hono/handlers/stream.js.map +1 -0
  39. package/build/{implementations/http → adapters}/hono/handlers/stream.test.js +3 -2
  40. package/build/adapters/hono/handlers/stream.test.js.map +1 -0
  41. package/build/{implementations/http → adapters}/hono/index.d.ts +24 -12
  42. package/build/{implementations/http → adapters}/hono/index.js +19 -8
  43. package/build/adapters/hono/index.js.map +1 -0
  44. package/build/{implementations/http → adapters}/hono/index.test.js +2 -4
  45. package/build/adapters/hono/index.test.js.map +1 -0
  46. package/build/{implementations/http → adapters/hono}/on-request-error.test.js +2 -2
  47. package/build/adapters/hono/on-request-error.test.js.map +1 -0
  48. package/build/adapters/hono/request.d.ts +7 -0
  49. package/build/adapters/hono/request.js +22 -0
  50. package/build/adapters/hono/request.js.map +1 -0
  51. package/build/{implementations/http → adapters/hono}/route-errors.test.js +4 -4
  52. package/build/adapters/hono/route-errors.test.js.map +1 -0
  53. package/build/adapters/hono/types.d.ts +55 -0
  54. package/build/adapters/hono/types.js +19 -0
  55. package/build/adapters/hono/types.js.map +1 -0
  56. package/build/client/freeze.test.js +39 -0
  57. package/build/client/freeze.test.js.map +1 -0
  58. package/build/client/typed-error-dispatch.test.js +2 -2
  59. package/build/client/typed-error-dispatch.test.js.map +1 -1
  60. package/build/codegen/__fixtures__/make-envelope.d.ts +1 -1
  61. package/build/codegen/bin/cli.d.ts +5 -0
  62. package/build/codegen/bin/cli.js +139 -182
  63. package/build/codegen/bin/cli.js.map +1 -1
  64. package/build/codegen/bin/cli.test.js +12 -2
  65. package/build/codegen/bin/cli.test.js.map +1 -1
  66. package/build/codegen/bin/flag-specs.d.ts +9 -0
  67. package/build/codegen/bin/flag-specs.js +33 -31
  68. package/build/codegen/bin/flag-specs.js.map +1 -1
  69. package/build/codegen/bin/flag-specs.test.js +14 -1
  70. package/build/codegen/bin/flag-specs.test.js.map +1 -1
  71. package/build/codegen/collect-models.d.ts +1 -1
  72. package/build/codegen/emit/api-route.d.ts +8 -0
  73. package/build/codegen/emit/api-route.js +156 -0
  74. package/build/codegen/emit/api-route.js.map +1 -0
  75. package/build/codegen/emit/context.d.ts +30 -0
  76. package/build/codegen/emit/context.js +2 -0
  77. package/build/codegen/emit/context.js.map +1 -0
  78. package/build/codegen/emit/declarations.d.ts +24 -0
  79. package/build/codegen/emit/declarations.js +48 -0
  80. package/build/codegen/emit/declarations.js.map +1 -0
  81. package/build/codegen/emit/format-types.d.ts +61 -0
  82. package/build/codegen/emit/format-types.js +188 -0
  83. package/build/codegen/emit/format-types.js.map +1 -0
  84. package/build/codegen/emit/http-stream-route.d.ts +7 -0
  85. package/build/codegen/emit/http-stream-route.js +138 -0
  86. package/build/codegen/emit/http-stream-route.js.map +1 -0
  87. package/build/codegen/emit/route-shared.d.ts +35 -0
  88. package/build/codegen/emit/route-shared.js +88 -0
  89. package/build/codegen/emit/route-shared.js.map +1 -0
  90. package/build/codegen/emit/rpc-route.d.ts +7 -0
  91. package/build/codegen/emit/rpc-route.js +37 -0
  92. package/build/codegen/emit/rpc-route.js.map +1 -0
  93. package/build/codegen/emit/scope-file.d.ts +39 -0
  94. package/build/codegen/emit/scope-file.js +166 -0
  95. package/build/codegen/emit/scope-file.js.map +1 -0
  96. package/build/codegen/emit/stream-route.d.ts +7 -0
  97. package/build/codegen/emit/stream-route.js +62 -0
  98. package/build/codegen/emit/stream-route.js.map +1 -0
  99. package/build/codegen/emit-errors.d.ts +1 -1
  100. package/build/codegen/emit-errors.integration.test.js +1 -1
  101. package/build/codegen/emit-errors.integration.test.js.map +1 -1
  102. package/build/codegen/emit-index.js +13 -0
  103. package/build/codegen/emit-index.js.map +1 -1
  104. package/build/codegen/emit-index.test.js +25 -0
  105. package/build/codegen/emit-index.test.js.map +1 -1
  106. package/build/codegen/emit-scope.d.ts +13 -30
  107. package/build/codegen/emit-scope.js +15 -807
  108. package/build/codegen/emit-scope.js.map +1 -1
  109. package/build/codegen/emit-scope.test.js +86 -4
  110. package/build/codegen/emit-scope.test.js.map +1 -1
  111. package/build/codegen/goldens.test.js +69 -0
  112. package/build/codegen/goldens.test.js.map +1 -0
  113. package/build/codegen/group-routes.d.ts +1 -1
  114. package/build/codegen/pipeline.d.ts +1 -1
  115. package/build/codegen/resolve-envelope.d.ts +1 -1
  116. package/build/codegen/targets/_shared/error-schemas.d.ts +1 -1
  117. package/build/codegen/targets/_shared/route-slots.d.ts +1 -1
  118. package/build/codegen/targets/_shared/target-run.d.ts +1 -1
  119. package/build/codegen/targets/kotlin/emit-route-kotlin.d.ts +1 -1
  120. package/build/codegen/targets/swift/emit-route-swift.d.ts +1 -1
  121. package/build/core/create-http-stream.d.ts +50 -0
  122. package/build/core/create-http-stream.js +108 -0
  123. package/build/core/create-http-stream.js.map +1 -0
  124. package/build/{create-http-stream.test.js → core/create-http-stream.test.js} +1 -1
  125. package/build/core/create-http-stream.test.js.map +1 -0
  126. package/build/core/create-http.d.ts +51 -0
  127. package/build/core/create-http.js +65 -0
  128. package/build/core/create-http.js.map +1 -0
  129. package/build/{create-http.test.js → core/create-http.test.js} +13 -4
  130. package/build/core/create-http.test.js.map +1 -0
  131. package/build/core/create-stream.d.ts +26 -0
  132. package/build/core/create-stream.js +80 -0
  133. package/build/core/create-stream.js.map +1 -0
  134. package/build/{create-stream.test.js → core/create-stream.test.js} +23 -28
  135. package/build/core/create-stream.test.js.map +1 -0
  136. package/build/core/create.d.ts +22 -0
  137. package/build/core/create.js +71 -0
  138. package/build/core/create.js.map +1 -0
  139. package/build/{create.test.js → core/create.test.js} +25 -46
  140. package/build/core/create.test.js.map +1 -0
  141. package/build/core/definition-site.d.ts +24 -0
  142. package/build/{stack-utils.js → core/definition-site.js} +20 -20
  143. package/build/core/definition-site.js.map +1 -0
  144. package/build/{stack-utils.test.js → core/definition-site.test.js} +12 -3
  145. package/build/core/definition-site.test.js.map +1 -0
  146. package/build/{errors.d.ts → core/errors.d.ts} +19 -8
  147. package/build/{errors.js → core/errors.js} +21 -26
  148. package/build/core/errors.js.map +1 -0
  149. package/build/core/errors.test.js.map +1 -0
  150. package/build/core/factory-options.test.js +82 -0
  151. package/build/core/factory-options.test.js.map +1 -0
  152. package/build/core/http-route.d.ts +13 -0
  153. package/build/core/http-route.js +54 -0
  154. package/build/core/http-route.js.map +1 -0
  155. package/build/core/internal.d.ts +72 -0
  156. package/build/core/internal.js +128 -0
  157. package/build/core/internal.js.map +1 -0
  158. package/build/{migration.test.js → core/migration.test.js} +17 -1
  159. package/build/core/migration.test.js.map +1 -0
  160. package/build/core/procedures.d.ts +143 -0
  161. package/build/core/procedures.js +64 -0
  162. package/build/core/procedures.js.map +1 -0
  163. package/build/{index.test.js → core/procedures.test.js} +14 -11
  164. package/build/core/procedures.test.js.map +1 -0
  165. package/build/core/types.d.ts +182 -0
  166. package/build/{schema → core}/types.js.map +1 -1
  167. package/build/exports.d.ts +31 -11
  168. package/build/exports.js +23 -8
  169. package/build/exports.js.map +1 -1
  170. package/build/schema/adapter.d.ts +35 -0
  171. package/build/schema/adapter.js +13 -0
  172. package/build/schema/adapter.js.map +1 -0
  173. package/build/schema/adapter.test.js +53 -0
  174. package/build/schema/adapter.test.js.map +1 -0
  175. package/build/schema/compile.d.ts +37 -0
  176. package/build/schema/compile.js +38 -0
  177. package/build/schema/compile.js.map +1 -0
  178. package/build/schema/compile.test.js +78 -0
  179. package/build/schema/compile.test.js.map +1 -0
  180. package/build/schema/compute-schema.d.ts +47 -37
  181. package/build/schema/compute-schema.js +86 -29
  182. package/build/schema/compute-schema.js.map +1 -1
  183. package/build/schema/compute-schema.test.js +158 -40
  184. package/build/schema/compute-schema.test.js.map +1 -1
  185. package/build/schema/json-schema.d.ts +17 -0
  186. package/build/schema/json-schema.js +2 -0
  187. package/build/schema/json-schema.js.map +1 -0
  188. package/build/schema/typebox.d.ts +11 -0
  189. package/build/schema/typebox.js +24 -0
  190. package/build/schema/typebox.js.map +1 -0
  191. package/build/schema/typebox.test.js +34 -0
  192. package/build/schema/typebox.test.js.map +1 -0
  193. package/build/server/context.d.ts +8 -0
  194. package/build/server/context.js +7 -0
  195. package/build/server/context.js.map +1 -0
  196. package/build/server/context.test.js +16 -0
  197. package/build/server/context.test.js.map +1 -0
  198. package/build/{doc-envelope.d.ts → server/doc-envelope.d.ts} +1 -1
  199. package/build/server/doc-envelope.js.map +1 -0
  200. package/build/server/doc-envelope.test.d.ts +1 -0
  201. package/build/server/doc-envelope.test.js.map +1 -0
  202. package/build/{implementations/http → server}/doc-registry.d.ts +7 -2
  203. package/build/{implementations/http → server}/doc-registry.js +9 -5
  204. package/build/server/doc-registry.js.map +1 -0
  205. package/build/server/doc-registry.test.d.ts +1 -0
  206. package/build/{implementations/http → server}/doc-registry.test.js +27 -24
  207. package/build/server/doc-registry.test.js.map +1 -0
  208. package/build/server/docs/docs.test.d.ts +1 -0
  209. package/build/server/docs/docs.test.js +237 -0
  210. package/build/server/docs/docs.test.js.map +1 -0
  211. package/build/{implementations/http/hono → server}/docs/http-doc.d.ts +2 -2
  212. package/build/{implementations/http/hono → server}/docs/http-doc.js +1 -1
  213. package/build/server/docs/http-doc.js.map +1 -0
  214. package/build/{implementations/http/hono → server}/docs/http-stream-doc.d.ts +2 -2
  215. package/build/{implementations/http/hono → server}/docs/http-stream-doc.js +1 -1
  216. package/build/server/docs/http-stream-doc.js.map +1 -0
  217. package/build/{implementations/http/hono → server}/docs/rpc-doc.d.ts +2 -2
  218. package/build/{implementations/http/hono → server}/docs/rpc-doc.js +1 -1
  219. package/build/server/docs/rpc-doc.js.map +1 -0
  220. package/build/{implementations/http/hono → server}/docs/stream-doc.d.ts +2 -2
  221. package/build/{implementations/http/hono → server}/docs/stream-doc.js +1 -1
  222. package/build/server/docs/stream-doc.js.map +1 -0
  223. package/build/server/errors/dispatch.d.ts +96 -0
  224. package/build/{implementations/http/error-dispatch.js → server/errors/dispatch.js} +20 -10
  225. package/build/server/errors/dispatch.js.map +1 -0
  226. package/build/server/errors/dispatch.test.d.ts +1 -0
  227. package/build/server/errors/dispatch.test.js +418 -0
  228. package/build/server/errors/dispatch.test.js.map +1 -0
  229. package/build/{implementations/http/error-taxonomy.d.ts → server/errors/taxonomy.d.ts} +8 -17
  230. package/build/{implementations/http/error-taxonomy.js → server/errors/taxonomy.js} +6 -15
  231. package/build/server/errors/taxonomy.js.map +1 -0
  232. package/build/server/errors/taxonomy.test.d.ts +1 -0
  233. package/build/{implementations/http/error-taxonomy.test.js → server/errors/taxonomy.test.js} +45 -39
  234. package/build/server/errors/taxonomy.test.js.map +1 -0
  235. package/build/server/index.d.ts +29 -0
  236. package/build/server/index.js +27 -0
  237. package/build/server/index.js.map +1 -0
  238. package/build/server/no-framework-imports.test.d.ts +1 -0
  239. package/build/server/no-framework-imports.test.js +40 -0
  240. package/build/server/no-framework-imports.test.js.map +1 -0
  241. package/build/{implementations/http/hono/path.d.ts → server/paths.d.ts} +2 -3
  242. package/build/{implementations/http/hono/path.js → server/paths.js} +1 -1
  243. package/build/server/paths.js.map +1 -0
  244. package/build/server/paths.test.d.ts +1 -0
  245. package/build/server/paths.test.js +111 -0
  246. package/build/server/paths.test.js.map +1 -0
  247. package/build/server/request/params.d.ts +29 -0
  248. package/build/server/request/params.js +43 -0
  249. package/build/server/request/params.js.map +1 -0
  250. package/build/server/request/params.test.d.ts +1 -0
  251. package/build/server/request/params.test.js +91 -0
  252. package/build/server/request/params.test.js.map +1 -0
  253. package/build/server/request/query.d.ts +9 -0
  254. package/build/server/request/query.js +22 -0
  255. package/build/server/request/query.js.map +1 -0
  256. package/build/server/request/query.test.d.ts +1 -0
  257. package/build/server/request/query.test.js +60 -0
  258. package/build/server/request/query.test.js.map +1 -0
  259. package/build/server/sse.d.ts +70 -0
  260. package/build/server/sse.js +94 -0
  261. package/build/server/sse.js.map +1 -0
  262. package/build/server/sse.test.d.ts +1 -0
  263. package/build/server/sse.test.js +98 -0
  264. package/build/server/sse.test.js.map +1 -0
  265. package/build/{implementations → server}/types.d.ts +17 -15
  266. package/build/{implementations → server}/types.js.map +1 -1
  267. package/docs/astro-adapter.md +8 -9
  268. package/docs/client-and-codegen.md +4 -4
  269. package/docs/client-error-handling.md +92 -5
  270. package/docs/codegen-kotlin.md +2 -3
  271. package/docs/codegen-swift.md +1 -2
  272. package/docs/core.md +135 -54
  273. package/docs/http-integrations.md +83 -6
  274. package/docs/migration-v8-to-v9.md +192 -0
  275. package/docs/plans/2026-06-09-v9-rewrite.md +130 -0
  276. package/docs/specs/2026-06-09-v9-rewrite-design.md +221 -0
  277. package/docs/streaming.md +12 -0
  278. package/package.json +23 -47
  279. package/src/{implementations/http → adapters}/astro/index.test.ts +2 -2
  280. package/src/adapters/hono/__fixtures__/parity-envelope.json +389 -0
  281. package/src/adapters/hono/envelope-parity.test.ts +126 -0
  282. package/src/{implementations/http → adapters}/hono/handlers/http-stream.test.ts +1 -1
  283. package/src/adapters/hono/handlers/http-stream.ts +73 -0
  284. package/src/{implementations/http → adapters}/hono/handlers/http.test.ts +1 -1
  285. package/src/adapters/hono/handlers/http.ts +70 -0
  286. package/src/{implementations/http → adapters}/hono/handlers/rpc.test.ts +2 -2
  287. package/src/adapters/hono/handlers/rpc.ts +39 -0
  288. package/src/{implementations/http → adapters}/hono/handlers/stream.test.ts +4 -3
  289. package/src/{implementations/http → adapters}/hono/handlers/stream.ts +19 -92
  290. package/src/{implementations/http → adapters}/hono/index.test.ts +14 -16
  291. package/src/{implementations/http → adapters}/hono/index.ts +35 -30
  292. package/src/{implementations/http → adapters/hono}/on-request-error.test.ts +3 -3
  293. package/src/adapters/hono/request.ts +28 -0
  294. package/src/{implementations/http → adapters/hono}/route-errors.test.ts +5 -5
  295. package/src/{implementations/http → adapters}/hono/types.ts +43 -20
  296. package/src/client/freeze.test.ts +41 -0
  297. package/src/client/typed-error-dispatch.test.ts +3 -3
  298. package/src/codegen/__fixtures__/make-envelope.ts +1 -1
  299. package/src/codegen/__fixtures__/models-envelope.json +310 -0
  300. package/src/codegen/__fixtures__/users-envelope.json +9 -0
  301. package/src/codegen/__goldens__/MANIFEST.json +85 -0
  302. package/src/codegen/__goldens__/kotlin-default--models/Billing.kt +112 -0
  303. package/src/codegen/__goldens__/kotlin-default--models/BillingReports.kt +26 -0
  304. package/src/codegen/__goldens__/kotlin-default--models/Orders.kt +88 -0
  305. package/src/codegen/__goldens__/kotlin-default--users/Users.kt +189 -0
  306. package/src/codegen/__goldens__/swift-default--models/Billing.swift +97 -0
  307. package/src/codegen/__goldens__/swift-default--models/BillingReports.swift +20 -0
  308. package/src/codegen/__goldens__/swift-default--models/Orders.swift +81 -0
  309. package/src/codegen/__goldens__/swift-default--users/Users.swift +204 -0
  310. package/src/codegen/__goldens__/ts-default--models/_client.ts +1319 -0
  311. package/src/codegen/__goldens__/ts-default--models/_errors.ts +90 -0
  312. package/src/codegen/__goldens__/ts-default--models/_models.ts +10 -0
  313. package/src/codegen/__goldens__/ts-default--models/_types.ts +502 -0
  314. package/src/codegen/__goldens__/ts-default--models/billing-reports.ts +29 -0
  315. package/src/codegen/__goldens__/ts-default--models/billing.ts +67 -0
  316. package/src/codegen/__goldens__/ts-default--models/index.ts +48 -0
  317. package/src/codegen/__goldens__/ts-default--models/orders.ts +80 -0
  318. package/src/codegen/__goldens__/ts-default--users/_client.ts +1319 -0
  319. package/src/codegen/__goldens__/ts-default--users/_errors.ts +90 -0
  320. package/src/codegen/__goldens__/ts-default--users/_types.ts +502 -0
  321. package/src/codegen/__goldens__/ts-default--users/index.ts +38 -0
  322. package/src/codegen/__goldens__/ts-default--users/users.ts +169 -0
  323. package/src/codegen/__goldens__/ts-external-runtime--models/_errors.ts +90 -0
  324. package/src/codegen/__goldens__/ts-external-runtime--models/_models.ts +10 -0
  325. package/src/codegen/__goldens__/ts-external-runtime--models/billing-reports.ts +29 -0
  326. package/src/codegen/__goldens__/ts-external-runtime--models/billing.ts +67 -0
  327. package/src/codegen/__goldens__/ts-external-runtime--models/index.ts +48 -0
  328. package/src/codegen/__goldens__/ts-external-runtime--models/orders.ts +80 -0
  329. package/src/codegen/__goldens__/ts-external-runtime--users/_errors.ts +90 -0
  330. package/src/codegen/__goldens__/ts-external-runtime--users/index.ts +38 -0
  331. package/src/codegen/__goldens__/ts-external-runtime--users/users.ts +169 -0
  332. package/src/codegen/__goldens__/ts-flat--models/_client.ts +1319 -0
  333. package/src/codegen/__goldens__/ts-flat--models/_errors.ts +87 -0
  334. package/src/codegen/__goldens__/ts-flat--models/_models.ts +10 -0
  335. package/src/codegen/__goldens__/ts-flat--models/_types.ts +502 -0
  336. package/src/codegen/__goldens__/ts-flat--models/billing-reports.ts +28 -0
  337. package/src/codegen/__goldens__/ts-flat--models/billing.ts +51 -0
  338. package/src/codegen/__goldens__/ts-flat--models/index.ts +42 -0
  339. package/src/codegen/__goldens__/ts-flat--models/orders.ts +73 -0
  340. package/src/codegen/__goldens__/ts-flat--users/_client.ts +1319 -0
  341. package/src/codegen/__goldens__/ts-flat--users/_errors.ts +87 -0
  342. package/src/codegen/__goldens__/ts-flat--users/_types.ts +502 -0
  343. package/src/codegen/__goldens__/ts-flat--users/index.ts +34 -0
  344. package/src/codegen/__goldens__/ts-flat--users/users.ts +126 -0
  345. package/src/codegen/__goldens__/ts-no-share-models--models/_client.ts +1319 -0
  346. package/src/codegen/__goldens__/ts-no-share-models--models/_errors.ts +90 -0
  347. package/src/codegen/__goldens__/ts-no-share-models--models/_types.ts +502 -0
  348. package/src/codegen/__goldens__/ts-no-share-models--models/billing-reports.ts +29 -0
  349. package/src/codegen/__goldens__/ts-no-share-models--models/billing.ts +111 -0
  350. package/src/codegen/__goldens__/ts-no-share-models--models/index.ts +48 -0
  351. package/src/codegen/__goldens__/ts-no-share-models--models/orders.ts +112 -0
  352. package/src/codegen/__goldens__/ts-no-share-models--users/_client.ts +1319 -0
  353. package/src/codegen/__goldens__/ts-no-share-models--users/_errors.ts +90 -0
  354. package/src/codegen/__goldens__/ts-no-share-models--users/_types.ts +502 -0
  355. package/src/codegen/__goldens__/ts-no-share-models--users/index.ts +38 -0
  356. package/src/codegen/__goldens__/ts-no-share-models--users/users.ts +169 -0
  357. package/src/codegen/__goldens__/ts-shared-models-module--models/_client.ts +1319 -0
  358. package/src/codegen/__goldens__/ts-shared-models-module--models/_errors.ts +90 -0
  359. package/src/codegen/__goldens__/ts-shared-models-module--models/_models.ts +7 -0
  360. package/src/codegen/__goldens__/ts-shared-models-module--models/_types.ts +502 -0
  361. package/src/codegen/__goldens__/ts-shared-models-module--models/billing-reports.ts +29 -0
  362. package/src/codegen/__goldens__/ts-shared-models-module--models/billing.ts +67 -0
  363. package/src/codegen/__goldens__/ts-shared-models-module--models/index.ts +48 -0
  364. package/src/codegen/__goldens__/ts-shared-models-module--models/orders.ts +80 -0
  365. package/src/codegen/bin/cli.test.ts +13 -2
  366. package/src/codegen/bin/cli.ts +181 -144
  367. package/src/codegen/bin/flag-specs.test.ts +16 -1
  368. package/src/codegen/bin/flag-specs.ts +43 -31
  369. package/src/codegen/bundle-size.test.ts +1 -1
  370. package/src/codegen/collect-models.ts +1 -1
  371. package/src/codegen/e2e.test.ts +1 -1
  372. package/src/codegen/emit/api-route.ts +184 -0
  373. package/src/codegen/emit/context.ts +32 -0
  374. package/src/codegen/emit/declarations.ts +49 -0
  375. package/src/codegen/emit/format-types.ts +232 -0
  376. package/src/codegen/emit/http-stream-route.ts +162 -0
  377. package/src/codegen/emit/route-shared.ts +102 -0
  378. package/src/codegen/emit/rpc-route.ts +49 -0
  379. package/src/codegen/emit/scope-file.ts +226 -0
  380. package/src/codegen/emit/stream-route.ts +81 -0
  381. package/src/codegen/emit-errors.integration.test.ts +2 -2
  382. package/src/codegen/emit-errors.test.ts +1 -1
  383. package/src/codegen/emit-errors.ts +1 -1
  384. package/src/codegen/emit-index.test.ts +34 -0
  385. package/src/codegen/emit-index.ts +19 -0
  386. package/src/codegen/emit-scope.test.ts +96 -6
  387. package/src/codegen/emit-scope.ts +15 -1003
  388. package/src/codegen/goldens.test.ts +89 -0
  389. package/src/codegen/group-routes.test.ts +1 -1
  390. package/src/codegen/group-routes.ts +1 -1
  391. package/src/codegen/pipeline.test.ts +1 -1
  392. package/src/codegen/pipeline.ts +1 -1
  393. package/src/codegen/resolve-envelope.test.ts +1 -1
  394. package/src/codegen/resolve-envelope.ts +1 -1
  395. package/src/codegen/targets/_shared/error-schemas.test.ts +1 -1
  396. package/src/codegen/targets/_shared/error-schemas.ts +1 -1
  397. package/src/codegen/targets/_shared/route-slots.test.ts +1 -1
  398. package/src/codegen/targets/_shared/route-slots.ts +1 -1
  399. package/src/codegen/targets/_shared/target-run.ts +1 -1
  400. package/src/codegen/targets/kotlin/__fixtures__/users-golden.kt +6 -0
  401. package/src/codegen/targets/kotlin/emit-route-kotlin.test.ts +1 -1
  402. package/src/codegen/targets/kotlin/emit-route-kotlin.ts +1 -1
  403. package/src/codegen/targets/kotlin/emit-scope-kotlin.test.ts +1 -1
  404. package/src/codegen/targets/swift/__fixtures__/users-golden.swift +6 -0
  405. package/src/codegen/targets/swift/access-level.test.ts +1 -1
  406. package/src/codegen/targets/swift/emit-route-swift.test.ts +1 -1
  407. package/src/codegen/targets/swift/emit-route-swift.ts +1 -1
  408. package/src/codegen/targets/swift/emit-scope-swift.test.ts +1 -1
  409. package/src/codegen/targets/ts/shared-models.test.ts +1 -1
  410. package/src/{create-http-stream.test.ts → core/create-http-stream.test.ts} +1 -1
  411. package/src/core/create-http-stream.ts +207 -0
  412. package/src/{create-http.test.ts → core/create-http.test.ts} +15 -4
  413. package/src/core/create-http.ts +126 -0
  414. package/src/{create-stream.test.ts → core/create-stream.test.ts} +28 -31
  415. package/src/core/create-stream.ts +142 -0
  416. package/src/{create.test.ts → core/create.test.ts} +25 -57
  417. package/src/core/create.ts +121 -0
  418. package/src/{stack-utils.test.ts → core/definition-site.test.ts} +14 -3
  419. package/src/{stack-utils.ts → core/definition-site.ts} +20 -23
  420. package/src/{errors.test.ts → core/errors.test.ts} +1 -1
  421. package/src/{errors.ts → core/errors.ts} +30 -28
  422. package/src/core/factory-options.test.ts +112 -0
  423. package/src/core/http-route.ts +73 -0
  424. package/src/core/internal.ts +203 -0
  425. package/src/{migration.test.ts → core/migration.test.ts} +23 -1
  426. package/src/{index.test.ts → core/procedures.test.ts} +13 -11
  427. package/src/core/procedures.ts +75 -0
  428. package/src/core/types.ts +195 -0
  429. package/src/exports.ts +60 -11
  430. package/src/schema/adapter.test.ts +58 -0
  431. package/src/schema/adapter.ts +45 -0
  432. package/src/schema/compile.test.ts +95 -0
  433. package/src/schema/compile.ts +64 -0
  434. package/src/schema/compute-schema.test.ts +222 -41
  435. package/src/schema/compute-schema.ts +145 -71
  436. package/src/schema/json-schema.ts +21 -0
  437. package/src/schema/typebox.test.ts +40 -0
  438. package/src/schema/typebox.ts +27 -0
  439. package/src/server/context.test.ts +22 -0
  440. package/src/server/context.ts +18 -0
  441. package/src/{doc-envelope.test.ts → server/doc-envelope.test.ts} +2 -2
  442. package/src/{doc-envelope.ts → server/doc-envelope.ts} +1 -1
  443. package/src/{implementations/http → server}/doc-registry.test.ts +32 -26
  444. package/src/{implementations/http → server}/doc-registry.ts +11 -7
  445. package/src/server/docs/docs.test.ts +287 -0
  446. package/src/{implementations/http/hono → server}/docs/http-doc.ts +3 -3
  447. package/src/{implementations/http/hono → server}/docs/http-stream-doc.ts +3 -3
  448. package/src/{implementations/http/hono → server}/docs/rpc-doc.ts +3 -3
  449. package/src/{implementations/http/hono → server}/docs/stream-doc.ts +3 -3
  450. package/src/server/errors/dispatch.test.ts +450 -0
  451. package/src/server/errors/dispatch.ts +189 -0
  452. package/src/{implementations/http/error-taxonomy.test.ts → server/errors/taxonomy.test.ts} +45 -39
  453. package/src/{implementations/http/error-taxonomy.ts → server/errors/taxonomy.ts} +8 -17
  454. package/src/server/index.ts +29 -0
  455. package/src/server/no-framework-imports.test.ts +43 -0
  456. package/src/server/paths.test.ts +141 -0
  457. package/src/{implementations/http/hono/path.ts → server/paths.ts} +2 -13
  458. package/src/server/request/params.test.ts +143 -0
  459. package/src/server/request/params.ts +68 -0
  460. package/src/server/request/query.test.ts +70 -0
  461. package/src/server/request/query.ts +24 -0
  462. package/src/server/sse.test.ts +113 -0
  463. package/src/server/sse.ts +117 -0
  464. package/src/{implementations → server}/types.ts +17 -16
  465. package/build/create-http-stream.d.ts +0 -58
  466. package/build/create-http-stream.js +0 -122
  467. package/build/create-http-stream.js.map +0 -1
  468. package/build/create-http-stream.test.js.map +0 -1
  469. package/build/create-http.d.ts +0 -49
  470. package/build/create-http.js +0 -108
  471. package/build/create-http.js.map +0 -1
  472. package/build/create-http.test.js.map +0 -1
  473. package/build/create-stream.d.ts +0 -35
  474. package/build/create-stream.js +0 -123
  475. package/build/create-stream.js.map +0 -1
  476. package/build/create-stream.test.js.map +0 -1
  477. package/build/create.d.ts +0 -28
  478. package/build/create.js +0 -82
  479. package/build/create.js.map +0 -1
  480. package/build/create.test.js.map +0 -1
  481. package/build/doc-envelope.js.map +0 -1
  482. package/build/doc-envelope.test.js.map +0 -1
  483. package/build/errors.js.map +0 -1
  484. package/build/errors.test.js.map +0 -1
  485. package/build/implementations/http/astro/astro-context.js.map +0 -1
  486. package/build/implementations/http/astro/create-handler.js.map +0 -1
  487. package/build/implementations/http/astro/index.js.map +0 -1
  488. package/build/implementations/http/astro/index.test.js.map +0 -1
  489. package/build/implementations/http/astro/rewrite-request.js.map +0 -1
  490. package/build/implementations/http/doc-registry.js.map +0 -1
  491. package/build/implementations/http/doc-registry.test.js.map +0 -1
  492. package/build/implementations/http/error-dispatch.d.ts +0 -76
  493. package/build/implementations/http/error-dispatch.js.map +0 -1
  494. package/build/implementations/http/error-dispatch.test.js +0 -254
  495. package/build/implementations/http/error-dispatch.test.js.map +0 -1
  496. package/build/implementations/http/error-taxonomy.js.map +0 -1
  497. package/build/implementations/http/error-taxonomy.test.js.map +0 -1
  498. package/build/implementations/http/hono/docs/http-doc.js.map +0 -1
  499. package/build/implementations/http/hono/docs/http-stream-doc.js.map +0 -1
  500. package/build/implementations/http/hono/docs/rpc-doc.js.map +0 -1
  501. package/build/implementations/http/hono/docs/stream-doc.js.map +0 -1
  502. package/build/implementations/http/hono/handlers/http-stream.js +0 -123
  503. package/build/implementations/http/hono/handlers/http-stream.js.map +0 -1
  504. package/build/implementations/http/hono/handlers/http-stream.test.js.map +0 -1
  505. package/build/implementations/http/hono/handlers/http.js +0 -110
  506. package/build/implementations/http/hono/handlers/http.js.map +0 -1
  507. package/build/implementations/http/hono/handlers/http.test.js.map +0 -1
  508. package/build/implementations/http/hono/handlers/rpc.js +0 -32
  509. package/build/implementations/http/hono/handlers/rpc.js.map +0 -1
  510. package/build/implementations/http/hono/handlers/rpc.test.js.map +0 -1
  511. package/build/implementations/http/hono/handlers/stream.d.ts +0 -23
  512. package/build/implementations/http/hono/handlers/stream.js +0 -147
  513. package/build/implementations/http/hono/handlers/stream.js.map +0 -1
  514. package/build/implementations/http/hono/handlers/stream.test.js.map +0 -1
  515. package/build/implementations/http/hono/index.js.map +0 -1
  516. package/build/implementations/http/hono/index.test.js.map +0 -1
  517. package/build/implementations/http/hono/path.js.map +0 -1
  518. package/build/implementations/http/hono/path.test.js +0 -83
  519. package/build/implementations/http/hono/path.test.js.map +0 -1
  520. package/build/implementations/http/hono/types.d.ts +0 -51
  521. package/build/implementations/http/hono/types.js.map +0 -1
  522. package/build/implementations/http/on-request-error.test.js.map +0 -1
  523. package/build/implementations/http/route-errors.test.js.map +0 -1
  524. package/build/index.d.ts +0 -175
  525. package/build/index.js +0 -47
  526. package/build/index.js.map +0 -1
  527. package/build/index.test.js.map +0 -1
  528. package/build/migration.test.js.map +0 -1
  529. package/build/schema/extract-json-schema.d.ts +0 -2
  530. package/build/schema/extract-json-schema.js +0 -12
  531. package/build/schema/extract-json-schema.js.map +0 -1
  532. package/build/schema/extract-json-schema.test.js +0 -23
  533. package/build/schema/extract-json-schema.test.js.map +0 -1
  534. package/build/schema/parser.d.ts +0 -36
  535. package/build/schema/parser.js +0 -210
  536. package/build/schema/parser.js.map +0 -1
  537. package/build/schema/parser.test.js +0 -120
  538. package/build/schema/parser.test.js.map +0 -1
  539. package/build/schema/resolve-schema-lib.d.ts +0 -12
  540. package/build/schema/resolve-schema-lib.js +0 -11
  541. package/build/schema/resolve-schema-lib.js.map +0 -1
  542. package/build/schema/resolve-schema-lib.test.js +0 -17
  543. package/build/schema/resolve-schema-lib.test.js.map +0 -1
  544. package/build/schema/types.d.ts +0 -8
  545. package/build/schema/types.js +0 -2
  546. package/build/stack-utils.d.ts +0 -25
  547. package/build/stack-utils.js.map +0 -1
  548. package/build/stack-utils.test.js.map +0 -1
  549. package/build/types.d.ts +0 -142
  550. package/build/types.js +0 -2
  551. package/build/types.js.map +0 -1
  552. package/docs/decisions/2026-06-02-monorepo-split-evaluation.md +0 -80
  553. package/docs/handoffs/ajsc-named-type-collision.md +0 -134
  554. package/docs/handoffs/ajsc-named-type-support.md +0 -181
  555. package/docs/handoffs/shared-models-auto-resolve-response.md +0 -181
  556. package/docs/npm-workspaces-migration-plan.md +0 -611
  557. package/docs/superpowers/plans/2026-04-24-doc-registry-simplification.md +0 -886
  558. package/docs/superpowers/plans/2026-04-24-kotlin-codegen-target.md +0 -1265
  559. package/docs/superpowers/plans/2026-04-25-ajsc-v7-kotlin-polish.md +0 -1993
  560. package/docs/superpowers/plans/2026-04-29-safe-result-api.md +0 -2293
  561. package/docs/superpowers/plans/2026-05-07-astro-adapter.md +0 -1391
  562. package/docs/superpowers/plans/2026-05-08-create-http.md +0 -3355
  563. package/docs/superpowers/plans/2026-05-08-hono-app-builder-convergence.md +0 -3365
  564. package/docs/superpowers/plans/2026-06-05-dx-feedback-round.md +0 -1292
  565. package/docs/superpowers/plans/2026-06-06-shared-models-convention-and-diagnostics.md +0 -659
  566. package/docs/superpowers/specs/2026-04-24-kotlin-swift-codegen-design.md +0 -401
  567. package/docs/superpowers/specs/2026-04-25-ajsc-v7-kotlin-polish-design.md +0 -314
  568. package/docs/superpowers/specs/2026-04-25-swift-codegen-design.md +0 -264
  569. package/docs/superpowers/specs/2026-04-29-safe-result-api-design.md +0 -324
  570. package/docs/superpowers/specs/2026-05-07-astro-adapter-design.md +0 -252
  571. package/docs/superpowers/specs/2026-05-08-create-http-design.md +0 -409
  572. package/docs/superpowers/specs/2026-05-08-hono-app-builder-convergence-design.md +0 -411
  573. package/docs/superpowers/specs/2026-06-05-dx-feedback-round-design.md +0 -285
  574. package/src/create-http-stream.ts +0 -191
  575. package/src/create-http.ts +0 -210
  576. package/src/create-stream.ts +0 -228
  577. package/src/create.ts +0 -172
  578. package/src/implementations/http/README.md +0 -390
  579. package/src/implementations/http/error-dispatch.test.ts +0 -283
  580. package/src/implementations/http/error-dispatch.ts +0 -176
  581. package/src/implementations/http/hono/handlers/http-stream.ts +0 -152
  582. package/src/implementations/http/hono/handlers/http.ts +0 -145
  583. package/src/implementations/http/hono/handlers/rpc.ts +0 -54
  584. package/src/implementations/http/hono/path.test.ts +0 -96
  585. package/src/index.ts +0 -101
  586. package/src/schema/extract-json-schema.test.ts +0 -25
  587. package/src/schema/extract-json-schema.ts +0 -15
  588. package/src/schema/parser.test.ts +0 -182
  589. package/src/schema/parser.ts +0 -265
  590. package/src/schema/resolve-schema-lib.test.ts +0 -19
  591. package/src/schema/resolve-schema-lib.ts +0 -29
  592. package/src/schema/types.ts +0 -20
  593. package/src/types.ts +0 -133
  594. /package/build/{implementations/http → adapters}/astro/astro-context.d.ts +0 -0
  595. /package/build/{implementations/http → adapters}/astro/astro-context.js +0 -0
  596. /package/build/{implementations/http → adapters}/astro/create-handler.d.ts +0 -0
  597. /package/build/{implementations/http → adapters}/astro/create-handler.js +0 -0
  598. /package/build/{implementations/http → adapters}/astro/index.d.ts +0 -0
  599. /package/build/{implementations/http → adapters}/astro/index.js +0 -0
  600. /package/build/{implementations/http → adapters}/astro/index.test.d.ts +0 -0
  601. /package/build/{implementations/http → adapters}/astro/rewrite-request.d.ts +0 -0
  602. /package/build/{implementations/http → adapters}/astro/rewrite-request.js +0 -0
  603. /package/build/{create-http-stream.test.d.ts → adapters/hono/envelope-parity.test.d.ts} +0 -0
  604. /package/build/{implementations/http → adapters}/hono/handlers/http-stream.test.d.ts +0 -0
  605. /package/build/{implementations/http → adapters}/hono/handlers/http.test.d.ts +0 -0
  606. /package/build/{implementations/http → adapters}/hono/handlers/rpc.test.d.ts +0 -0
  607. /package/build/{implementations/http → adapters}/hono/handlers/stream.test.d.ts +0 -0
  608. /package/build/{implementations/http → adapters}/hono/index.test.d.ts +0 -0
  609. /package/build/{implementations/http → adapters/hono}/on-request-error.test.d.ts +0 -0
  610. /package/build/{implementations/http → adapters/hono}/route-errors.test.d.ts +0 -0
  611. /package/build/{create-http.test.d.ts → client/freeze.test.d.ts} +0 -0
  612. /package/build/{create-stream.test.d.ts → codegen/goldens.test.d.ts} +0 -0
  613. /package/build/{create.test.d.ts → core/create-http-stream.test.d.ts} +0 -0
  614. /package/build/{doc-envelope.test.d.ts → core/create-http.test.d.ts} +0 -0
  615. /package/build/{errors.test.d.ts → core/create-stream.test.d.ts} +0 -0
  616. /package/build/{implementations/http/doc-registry.test.d.ts → core/create.test.d.ts} +0 -0
  617. /package/build/{implementations/http/error-dispatch.test.d.ts → core/definition-site.test.d.ts} +0 -0
  618. /package/build/{implementations/http/error-taxonomy.test.d.ts → core/errors.test.d.ts} +0 -0
  619. /package/build/{errors.test.js → core/errors.test.js} +0 -0
  620. /package/build/{implementations/http/hono/path.test.d.ts → core/factory-options.test.d.ts} +0 -0
  621. /package/build/{migration.test.d.ts → core/migration.test.d.ts} +0 -0
  622. /package/build/{index.test.d.ts → core/procedures.test.d.ts} +0 -0
  623. /package/build/{implementations/http/hono → core}/types.js +0 -0
  624. /package/build/schema/{extract-json-schema.test.d.ts → adapter.test.d.ts} +0 -0
  625. /package/build/schema/{parser.test.d.ts → compile.test.d.ts} +0 -0
  626. /package/build/schema/{resolve-schema-lib.test.d.ts → typebox.test.d.ts} +0 -0
  627. /package/build/{stack-utils.test.d.ts → server/context.test.d.ts} +0 -0
  628. /package/build/{doc-envelope.js → server/doc-envelope.js} +0 -0
  629. /package/build/{doc-envelope.test.js → server/doc-envelope.test.js} +0 -0
  630. /package/build/{implementations → server}/types.js +0 -0
  631. /package/src/{implementations/http → adapters}/astro/README.md +0 -0
  632. /package/src/{implementations/http → adapters}/astro/astro-context.ts +0 -0
  633. /package/src/{implementations/http → adapters}/astro/create-handler.ts +0 -0
  634. /package/src/{implementations/http → adapters}/astro/index.ts +0 -0
  635. /package/src/{implementations/http → adapters}/astro/rewrite-request.ts +0 -0
@@ -1,809 +1,17 @@
1
- import { jsonSchemaToTypeString, jsonSchemaToTypeBody, jsonSchemaToTypeBodyWithRefs, jsonSchemaToExtractedTypes, renameExtractedTypes, extractedDeclName, } from './emit-types.js';
2
- import { substituteModelRefs } from './model-refs.js';
3
- import { CODEGEN_HEADER } from './constants.js';
4
- import { toPascalCase } from './naming.js';
5
- // ---------------------------------------------------------------------------
6
- // Helpers
7
- // ---------------------------------------------------------------------------
8
1
  /**
9
- * Infers the route kind when the `kind` discriminant is missing.
10
- * This provides backward compatibility with servers running older ts-procedures
11
- * versions that don't set `kind` on route docs.
12
- */
13
- function inferRouteKind(route) {
14
- if ('streamMode' in route && 'fullPath' in route)
15
- return 'http-stream';
16
- if ('streamMode' in route)
17
- return 'stream';
18
- if ('fullPath' in route)
19
- return 'api';
20
- return 'rpc';
21
- }
22
- /**
23
- * Checks whether a JSON schema looks like an SSE envelope.
24
- * SSE envelopes have properties: data, event, id (and optionally retry).
25
- */
26
- function isSseEnvelope(schema) {
27
- const props = schema.properties;
28
- if (props == null || typeof props !== 'object')
29
- return false;
30
- const keys = Object.keys(props);
31
- return keys.includes('data') && keys.includes('event') && keys.includes('id');
32
- }
33
- /**
34
- * Unwraps an SSE envelope schema to return the inner `data` property schema.
35
- * If not an SSE envelope, returns the schema unchanged.
36
- */
37
- function unwrapSseEnvelope(schema) {
38
- if (!isSseEnvelope(schema))
39
- return schema;
40
- const props = schema.properties;
41
- const dataSchema = props['data'];
42
- return dataSchema ?? schema;
43
- }
44
- /**
45
- * Returns the PascalCase display name for a route, appending `V{version}`
46
- * when version > 1. Version 1 produces no suffix for backward compatibility.
47
- */
48
- function versionedPascal(name, version) {
49
- const pascal = toPascalCase(name);
50
- if (version != null && version > 1)
51
- return `${pascal}V${version}`;
52
- return pascal;
53
- }
54
- // ---------------------------------------------------------------------------
55
- // Shared type formatting helpers
56
- // ---------------------------------------------------------------------------
57
- /** Indent each line of a multi-line string by a given prefix. */
58
- function indent(text, prefix) {
59
- return text.split('\n').map((line) => (line ? prefix + line : line)).join('\n');
60
- }
61
- // ---------------------------------------------------------------------------
62
- // Shared-model conversion wrappers (ajsc x-named-type)
63
- // ---------------------------------------------------------------------------
64
- /**
65
- * Drop-in replacement for `jsonSchemaToExtractedTypes` that, when shared models
66
- * are in play, rewrites every `$id` subschema into an `x-named-type` node before
67
- * ajsc. ajsc emits each as a bare verbatim reference and reports it via
68
- * `referencedNamedTypes`; this wrapper folds those names into
69
- * `ctx.referencedModels` so `emitScopeFile` can build the `_models` import.
70
- *
71
- * Short-circuits to the exact original call when no models are configured, so
72
- * output is byte-identical for envelopes without `$id` models.
73
- */
74
- async function convertExtracted(schema, ctx) {
75
- if (ctx.idToModelName == null || ctx.idToModelName.size === 0) {
76
- return jsonSchemaToExtractedTypes(schema, ctx.ajsc);
77
- }
78
- const { schema: sub } = substituteModelRefs(schema, ctx.idToModelName);
79
- const result = await jsonSchemaToExtractedTypes(sub, ctx.ajsc);
80
- if (result != null) {
81
- assertNoModelNameCollision(result);
82
- for (const name of result.referencedNamedTypes)
83
- ctx.referencedModels.add(name);
84
- }
85
- return result;
86
- }
87
- /**
88
- * Guards ajsc's documented `x-named-type` collision caveat. If a shared-model
89
- * name (referenced via `x-named-type`) also matches the name ajsc derives for a
90
- * sibling *structural* sub-type, ajsc silently MERGES them — the model reference
91
- * resolves to the unrelated structural type. The merge happens inside ajsc's own
92
- * pass, so it can't be disentangled afterward (a post-hoc rename would rewrite
93
- * the model reference too, producing a silently-wrong type). Instead we detect
94
- * the collision — a name present in BOTH `referencedNamedTypes` AND
95
- * `extractedTypeNames` (ajsc's own documented collision signal) — and fail fast
96
- * with an actionable message. The throw is wrapped with the route's name/scope
97
- * by `emitScopeFile`.
98
- */
99
- function assertNoModelNameCollision(result) {
100
- if (result.referencedNamedTypes.length === 0 || result.extractedTypeNames.length === 0)
101
- return;
102
- const extracted = new Set(result.extractedTypeNames);
103
- const collisions = result.referencedNamedTypes.filter((name) => extracted.has(name));
104
- if (collisions.length === 0)
105
- return;
106
- const names = collisions.map((n) => `'${n}'`).join(', ');
107
- throw new Error(`shared model ${names} collides with a generated type of the same name derived ` +
108
- `from a property in this route's schema — ajsc would silently merge them. Rename ` +
109
- `the colliding property, or change the model's $id/title so the generated names differ`);
110
- }
111
- /**
112
- * Drop-in replacement for `jsonSchemaToTypeBody` mirroring {@link convertExtracted}
113
- * for the bare-body (flat / merged-alias) conversion path.
114
- */
115
- async function convertBody(schema, ctx) {
116
- if (ctx.idToModelName == null || ctx.idToModelName.size === 0) {
117
- return jsonSchemaToTypeBody(schema, ctx.ajsc);
118
- }
119
- const { schema: sub } = substituteModelRefs(schema, ctx.idToModelName);
120
- const result = await jsonSchemaToTypeBodyWithRefs(sub, ctx.ajsc);
121
- if (result == null)
122
- return undefined;
123
- for (const name of result.referencedNamedTypes)
124
- ctx.referencedModels.add(name);
125
- return result.body;
126
- }
127
- /**
128
- * Tracks extracted declarations emitted into a single namespace, guarding
129
- * against duplicate identifiers (defense-in-depth on top of the rename pass).
130
- *
131
- * - Exact-string duplicates (the same sub-type extracted from two schemas) are
132
- * silently skipped.
133
- * - A same-name-but-different-body declaration is a genuine collision the
134
- * rename pass failed to resolve; emitting it would produce an opaque
135
- * `TS2300: Duplicate identifier` in the consumer's build. We fail fast at
136
- * codegen with a message that names the offending identifier instead.
137
- *
138
- * Returns the indented declaration line to push, or `null` when it should be
139
- * skipped (exact duplicate).
140
- */
141
- class DeclarationCollector {
142
- context;
143
- seenStrings = new Set();
144
- seenNames = new Map();
145
- constructor(context) {
146
- this.context = context;
147
- }
148
- /** Returns the indented line to emit, or `null` for an exact duplicate. */
149
- accept(decl, indentPrefix) {
150
- if (this.seenStrings.has(decl))
151
- return null;
152
- const name = extractedDeclName(decl);
153
- if (name != null) {
154
- if (this.seenNames.has(name)) {
155
- throw new Error(`[ts-procedures-codegen] duplicate identifier '${name}' while emitting ${this.context}. ` +
156
- `An extracted sub-type collided with another of the same name and could not be renamed. ` +
157
- `This is a codegen bug — please report it with the offending schema.`);
158
- }
159
- this.seenNames.set(name, decl);
160
- }
161
- this.seenStrings.add(decl);
162
- return indent(decl, indentPrefix);
163
- }
164
- }
165
- /**
166
- * Converts multiple schemas into type declarations and type references.
167
- * In flat mode: `export type ${routePascal}${shortName} = <body>`
168
- * In namespace mode: extracted sub-types + named types inside `export namespace ${routePascal} { ... }`
169
- *
170
- * `extraReserved` lets the caller pre-reserve identifier names that the route
171
- * will inject AFTER formatTypes returns (e.g. emitApiRoute's structured
172
- * `Params`). Without this, an ajsc-extracted sub-type could shadow the
173
- * injected one and produce duplicate `export type Params` declarations.
174
- */
175
- async function formatTypes(routePascal, types, ctx, extraReserved) {
176
- const declarations = [];
177
- const refs = {};
178
- if (ctx.namespaceTypes) {
179
- const nsLines = [];
180
- const collector = new DeclarationCollector(`namespace ${routePascal}`);
181
- // Pre-reserve every name the route will declare itself (each shortName +
182
- // any caller-supplied extras). Extracted sub-types whose names land in
183
- // this set get renamed (e.g. `Params` → `Params_`) by renameExtractedTypes,
184
- // and the body string is patched in lockstep so the reference still
185
- // resolves. The set is mutated as we go, so a sub-type renamed in schema A
186
- // is also reserved against schema B.
187
- const taken = new Set(extraReserved ?? []);
188
- for (const t of types) {
189
- if (t.schema != null)
190
- taken.add(t.shortName);
191
- }
192
- for (const { shortName, schema } of types) {
193
- if (schema == null)
194
- continue;
195
- const rawResult = await convertExtracted(schema, ctx);
196
- if (rawResult == null)
197
- continue;
198
- const result = renameExtractedTypes(rawResult, taken);
199
- // Collect extracted sub-types (dedupe exact dups; throw on real collisions)
200
- for (const decl of result.declarations) {
201
- const line = collector.accept(decl, ' ');
202
- if (line != null)
203
- nsLines.push(line);
204
- }
205
- nsLines.push(` export type ${shortName} = ${result.body}`);
206
- refs[shortName] = `${ctx.scopePascal}.${routePascal}.${shortName}`;
207
- }
208
- if (nsLines.length > 0) {
209
- declarations.push(` export namespace ${routePascal} {\n${nsLines.join('\n')}\n }`);
210
- }
211
- }
212
- else {
213
- for (const { shortName, schema } of types) {
214
- if (schema == null)
215
- continue;
216
- const flatName = `${routePascal}${shortName}`;
217
- const body = await convertBody(schema, ctx);
218
- if (body == null)
219
- continue;
220
- declarations.push(`export type ${flatName} = ${body}`);
221
- refs[shortName] = flatName;
222
- }
223
- }
224
- return { declarations, refs };
225
- }
226
- // ---------------------------------------------------------------------------
227
- // Route-level Errors union injection
228
- // ---------------------------------------------------------------------------
229
- /**
230
- * Builds the body of an `Errors` type union from the route's declared error
231
- * keys. Filters to keys actually emitted in `_errors.ts` so generated code
232
- * never references undefined types.
233
- *
234
- * In namespace mode the union uses qualified names (`ApiErrors.UseCaseError`);
235
- * in flat mode it uses the bundled wildcard import alias (`_errors.UseCaseError`).
236
- * Returns `null` when no keys remain.
237
- */
238
- function buildErrorUnion(routeErrors, ctx) {
239
- if (!routeErrors || routeErrors.length === 0)
240
- return null;
241
- const available = ctx.errorKeys;
242
- const filtered = available ? routeErrors.filter((k) => available.has(k)) : routeErrors;
243
- if (filtered.length === 0)
244
- return null;
245
- const qualify = ctx.namespaceTypes
246
- ? (k) => `${toPascalCase(ctx.serviceName)}Errors.${k}`
247
- : (k) => `_errors.${k}`;
248
- return filtered.map(qualify).join(' | ');
249
- }
250
- /**
251
- * Injects `export type Errors = ...` into an existing route namespace block
252
- * (namespace mode) or appends a flat `export type ${pascal}Errors = ...` in
253
- * flat mode. Mutates the `declarations` array in place and returns whether an
254
- * injection happened.
255
- */
256
- function injectRouteErrors(declarations, routePascal, errorUnion, namespaceTypes) {
257
- if (!errorUnion)
258
- return false;
259
- if (namespaceTypes) {
260
- const lastIdx = declarations.length - 1;
261
- if (lastIdx < 0)
262
- return false;
263
- const lastDecl = declarations[lastIdx];
264
- const closingIdx = lastDecl.lastIndexOf(' }');
265
- if (closingIdx === -1)
266
- return false;
267
- declarations[lastIdx] =
268
- lastDecl.slice(0, closingIdx) +
269
- ` export type Errors = ${errorUnion}\n` +
270
- lastDecl.slice(closingIdx);
271
- return true;
272
- }
273
- declarations.push(`export type ${routePascal}Errors = ${errorUnion}`);
274
- return true;
275
- }
276
- // ---------------------------------------------------------------------------
277
- // Route emitters
278
- // ---------------------------------------------------------------------------
279
- async function emitRpcRoute(route, ctx) {
280
- const pascal = versionedPascal(route.name, route.version);
281
- const { declarations, refs } = await formatTypes(pascal, [
282
- { shortName: 'Params', schema: route.jsonSchema.body },
283
- { shortName: 'Response', schema: route.jsonSchema.response },
284
- ], ctx);
285
- const paramsTypeName = refs['Params'] ?? 'unknown';
286
- const responseTypeName = refs['Response'] ?? 'unknown';
287
- const scopeStr = Array.isArray(route.scope) ? route.scope.join('-') : route.scope;
288
- const errorUnion = buildErrorUnion(route.errors, ctx);
289
- const hasErrors = errorUnion !== null;
290
- const errorsRef = ctx.namespaceTypes
291
- ? `${ctx.scopePascal}.${pascal}.Errors`
292
- : `${pascal}Errors`;
293
- const helperCall = hasErrors
294
- ? `client.bindCallableTyped<${paramsTypeName}, ${responseTypeName}, ${errorsRef}>`
295
- : `client.bindCallable<${paramsTypeName}, ${responseTypeName}>`;
296
- const callable = [
297
- ` /** ${route.method.toUpperCase()} ${route.path} */`,
298
- ` ${pascal}: ${helperCall}({`,
299
- ` name: '${pascal}',`,
300
- ` scope: '${scopeStr}',`,
301
- ` path: '${route.path}',`,
302
- ` method: '${route.method}',`,
303
- ` kind: 'rpc',`,
304
- ` }),`,
305
- ].join('\n');
306
- const hasErrorsInjected = injectRouteErrors(declarations, pascal, errorUnion, ctx.namespaceTypes);
307
- return { typeDeclarations: declarations, callable, hasStream: false, hasErrors: hasErrorsInjected };
308
- }
309
- /**
310
- * Formats a group of named types into a nested sub-namespace block (for namespace mode).
311
- * Returns an array of lines to be inserted into the parent namespace, and a map of
312
- * shortName → qualified type reference for use in callables.
313
- *
314
- * In flat mode, returns declarations like `export type ${prefix}${shortName} = ...`
315
- * and refs like `${prefix}${shortName}`.
316
- */
317
- async function formatSubNamespace(routePascal, nsName, // e.g. 'Req' or 'Response'
318
- types, ctx, taken) {
319
- const refs = {};
320
- const nsLines = [];
321
- const collector = new DeclarationCollector(`namespace ${routePascal}.${nsName}`);
322
- // Pre-reserve short names to prevent sub-type extraction collision
323
- for (const t of types) {
324
- if (t.schema != null)
325
- taken.add(t.shortName);
326
- }
327
- for (const { shortName, schema } of types) {
328
- if (schema == null)
329
- continue;
330
- if (ctx.namespaceTypes) {
331
- const rawResult = await convertExtracted(schema, ctx);
332
- if (rawResult == null)
333
- continue;
334
- const result = renameExtractedTypes(rawResult, taken);
335
- for (const decl of result.declarations) {
336
- const line = collector.accept(decl, ' ');
337
- if (line != null)
338
- nsLines.push(line);
339
- }
340
- nsLines.push(` export type ${shortName} = ${result.body}`);
341
- refs[shortName] = `${ctx.scopePascal}.${routePascal}.${nsName}.${shortName}`;
342
- }
343
- else {
344
- const flatName = `${routePascal}${nsName}${shortName}`;
345
- const body = await convertBody(schema, ctx);
346
- if (body == null)
347
- continue;
348
- refs[shortName] = flatName;
349
- }
350
- }
351
- if (ctx.namespaceTypes) {
352
- if (nsLines.length === 0)
353
- return { nsBlock: null, refs };
354
- const nsBlock = ` export namespace ${nsName} {\n${nsLines.join('\n')}\n }`;
355
- return { nsBlock, refs };
356
- }
357
- return { nsBlock: null, refs };
358
- }
359
- /**
360
- * Builds the conditional return type string for an API or http-stream callable.
361
- *
362
- * - Both body + headers → `{ body: <Body>; headers: <Headers> }`
363
- * - Only body → `<Body>`
364
- * - Only headers → `{ headers: <Headers> }`
365
- * - Neither → `void`
366
- */
367
- function buildApiReturnType(bodyRef, headersRef) {
368
- if (bodyRef && headersRef) {
369
- return `{ body: ${bodyRef}; headers: ${headersRef} }`;
370
- }
371
- if (bodyRef)
372
- return bodyRef;
373
- if (headersRef)
374
- return `{ headers: ${headersRef} }`;
375
- return 'void';
376
- }
377
- async function emitApiRoute(route, ctx) {
378
- const pascal = toPascalCase(route.name);
379
- const req = route.jsonSchema.req ?? {};
380
- const res = route.jsonSchema.res ?? {};
381
- // Request channels
382
- const reqChannelKeys = ['pathParams', 'query', 'body', 'headers'];
383
- const reqTypes = [];
384
- const presentChannels = [];
385
- for (const channel of reqChannelKeys) {
386
- const schema = req[channel];
387
- if (schema != null) {
388
- reqTypes.push({ shortName: toPascalCase(channel), schema });
389
- presentChannels.push(channel);
390
- }
391
- }
392
- // Response slots
393
- const resTypes = [
394
- { shortName: 'Body', schema: res.body },
395
- { shortName: 'Headers', schema: res.headers },
396
- ];
397
- const scopeStr = route.scope ?? 'default';
398
- const errorUnion = buildErrorUnion(route.errors, ctx);
399
- const hasErrors = errorUnion !== null;
400
- const errorsRef = ctx.namespaceTypes
401
- ? `${ctx.scopePascal}.${pascal}.Errors`
402
- : `${pascal}Errors`;
403
- const declarations = [];
404
- let paramsTypeName = 'unknown';
405
- let returnTypeName = 'void';
406
- // Track reserved names across all sub-namespaces. Model names are reserved
407
- // so an ajsc-extracted sub-type can't silently merge with a referenced model.
408
- const taken = new Set(['Req', 'Response']);
409
- if (ctx.namespaceTypes) {
410
- // Namespace mode: emit nested Req {} and Response {} namespaces inside route namespace.
411
- // Also emit merged type aliases `export type Req = { ... }` and `export type Response = ...`
412
- // so they can be used as type arguments to bindCallable (TS requires a TYPE, not a namespace).
413
- const nsLines = [];
414
- const { nsBlock: reqBlock, refs: reqRefs } = await formatSubNamespace(pascal, 'Req', reqTypes, ctx, taken);
415
- if (reqBlock) {
416
- nsLines.push(reqBlock);
417
- // Merged type alias for Req so it can be used as a generic type arg
418
- const reqFields = presentChannels
419
- .map((ch) => `${ch}: Req.${toPascalCase(ch)}`)
420
- .join('; ');
421
- nsLines.push(` export type Req = { ${reqFields} }`);
422
- }
423
- const { nsBlock: resBlock, refs: resRefs } = await formatSubNamespace(pascal, 'Response', resTypes, ctx, taken);
424
- if (resBlock) {
425
- nsLines.push(resBlock);
426
- // No merged Response type alias needed: we reference Response.Body / Response.Headers
427
- // directly in the return type string, which are namespace-qualified paths (valid).
428
- }
429
- // Emit Errors type last (injected by injectRouteErrors below)
430
- // Build the route namespace block
431
- if (nsLines.length > 0) {
432
- declarations.push(` export namespace ${pascal} {\n${nsLines.join('\n\n')}\n }`);
433
- }
434
- // Params type: use the merged Req type alias
435
- if (presentChannels.length > 0) {
436
- paramsTypeName = `${ctx.scopePascal}.${pascal}.Req`;
437
- }
438
- // Return type
439
- const bodyRef = resRefs['Body'];
440
- const headersRef = resRefs['Headers'];
441
- returnTypeName = buildApiReturnType(bodyRef, headersRef);
442
- }
443
- else {
444
- // Flat mode: emit individual types prefixed with route + sub-namespace name
445
- for (const { shortName, schema } of reqTypes) {
446
- if (schema == null)
447
- continue;
448
- const flatName = `${pascal}Req${shortName}`;
449
- const body = await convertBody(schema, ctx);
450
- if (body == null)
451
- continue;
452
- declarations.push(`export type ${flatName} = ${body}`);
453
- }
454
- // Flat mode: compose structured Req type
455
- if (presentChannels.length > 0) {
456
- const structureFields = presentChannels
457
- .map((ch) => `${ch}: ${pascal}Req${toPascalCase(ch)}`)
458
- .join('; ');
459
- declarations.push(`export type ${pascal}Req = { ${structureFields} }`);
460
- paramsTypeName = `${pascal}Req`;
461
- }
462
- // Flat mode: emit response types
463
- let bodyRef;
464
- let headersRef;
465
- for (const { shortName, schema } of resTypes) {
466
- if (schema == null)
467
- continue;
468
- const flatName = `${pascal}Response${shortName}`;
469
- const body = await convertBody(schema, ctx);
470
- if (body == null)
471
- continue;
472
- declarations.push(`export type ${flatName} = ${body}`);
473
- if (shortName === 'Body')
474
- bodyRef = flatName;
475
- if (shortName === 'Headers')
476
- headersRef = flatName;
477
- }
478
- returnTypeName = buildApiReturnType(bodyRef, headersRef);
479
- // Flat mode errors
480
- if (errorUnion) {
481
- declarations.push(`export type ${pascal}Errors = ${errorUnion}`);
482
- }
483
- }
484
- const responseHeadersDeclared = res.headers != null;
485
- const helperCall = hasErrors
486
- ? `client.bindCallableTyped<${paramsTypeName}, ${returnTypeName}, ${errorsRef}>`
487
- : `client.bindCallable<${paramsTypeName}, ${returnTypeName}>`;
488
- const descriptorLines = [
489
- ` name: '${route.name}',`,
490
- ` scope: '${scopeStr}',`,
491
- ` path: '${route.fullPath}',`,
492
- ` method: '${route.method}',`,
493
- ` kind: 'api',`,
494
- ...(responseHeadersDeclared ? [` responseHeadersDeclared: true,`] : []),
495
- ];
496
- const callable = [
497
- ` /** ${route.method.toUpperCase()} ${route.fullPath} */`,
498
- ` ${route.name}: ${helperCall}({`,
499
- ...descriptorLines,
500
- ` }),`,
501
- ].join('\n');
502
- const hasErrorsInjected = ctx.namespaceTypes
503
- ? injectRouteErrors(declarations, pascal, errorUnion, ctx.namespaceTypes)
504
- : errorUnion !== null; // flat mode already emitted errors above
505
- return { typeDeclarations: declarations, callable, hasStream: false, hasErrors: hasErrorsInjected };
506
- }
507
- async function emitHttpStreamRoute(route, ctx) {
508
- const pascal = toPascalCase(route.name);
509
- const req = route.jsonSchema.req ?? {};
510
- const res = route.jsonSchema.res ?? {};
511
- // Request channels
512
- const reqChannelKeys = ['pathParams', 'query', 'body', 'headers'];
513
- const reqTypes = [];
514
- const presentChannels = [];
515
- for (const channel of reqChannelKeys) {
516
- const schema = req[channel];
517
- if (schema != null) {
518
- reqTypes.push({ shortName: toPascalCase(channel), schema });
519
- presentChannels.push(channel);
520
- }
521
- }
522
- // Yield + ReturnType
523
- const yieldSchema = route.jsonSchema.yield;
524
- const returnSchema = route.jsonSchema.returnType;
525
- const resHeadersSchema = res.headers;
526
- const scopeStr = route.scope ?? 'default';
527
- const declarations = [];
528
- let paramsTypeName = 'unknown';
529
- let yieldTypeName = 'unknown';
530
- let returnTypeName = 'void';
531
- const taken = new Set(['Req', 'Response', 'Yield', 'ReturnType']);
532
- if (ctx.namespaceTypes) {
533
- const nsLines = [];
534
- // Req sub-namespace
535
- const { nsBlock: reqBlock, refs: reqRefs } = await formatSubNamespace(pascal, 'Req', reqTypes, ctx, taken);
536
- if (reqBlock) {
537
- nsLines.push(reqBlock);
538
- // Merged type alias so Req can be used as a generic type arg (same pattern as emitApiRoute)
539
- const reqFields = presentChannels
540
- .map((ch) => `${ch}: Req.${toPascalCase(ch)}`)
541
- .join('; ');
542
- nsLines.push(` export type Req = { ${reqFields} }`);
543
- }
544
- // Response sub-namespace (headers only for http-stream)
545
- const resTypes = [{ shortName: 'Headers', schema: resHeadersSchema }];
546
- const { nsBlock: resBlock } = await formatSubNamespace(pascal, 'Response', resTypes, ctx, taken);
547
- if (resBlock)
548
- nsLines.push(resBlock);
549
- // Yield and ReturnType directly in the route namespace
550
- const directTypes = [
551
- { shortName: 'Yield', schema: yieldSchema },
552
- { shortName: 'ReturnType', schema: returnSchema },
553
- ];
554
- const collector = new DeclarationCollector(`namespace ${pascal}`);
555
- for (const { shortName, schema } of directTypes) {
556
- if (schema == null)
557
- continue;
558
- const rawResult = await convertExtracted(schema, ctx);
559
- if (rawResult == null)
560
- continue;
561
- const result = renameExtractedTypes(rawResult, taken);
562
- for (const decl of result.declarations) {
563
- const line = collector.accept(decl, ' ');
564
- if (line != null)
565
- nsLines.push(line);
566
- }
567
- nsLines.push(` export type ${shortName} = ${result.body}`);
568
- }
569
- if (nsLines.length > 0) {
570
- declarations.push(` export namespace ${pascal} {\n${nsLines.join('\n')}\n }`);
571
- }
572
- if (presentChannels.length > 0) {
573
- paramsTypeName = `${ctx.scopePascal}.${pascal}.Req`;
574
- }
575
- if (yieldSchema != null)
576
- yieldTypeName = `${ctx.scopePascal}.${pascal}.Yield`;
577
- if (returnSchema != null)
578
- returnTypeName = `${ctx.scopePascal}.${pascal}.ReturnType`;
579
- }
580
- else {
581
- // Flat mode
582
- for (const { shortName, schema } of reqTypes) {
583
- if (schema == null)
584
- continue;
585
- const flatName = `${pascal}Req${shortName}`;
586
- const body = await convertBody(schema, ctx);
587
- if (body == null)
588
- continue;
589
- declarations.push(`export type ${flatName} = ${body}`);
590
- }
591
- if (presentChannels.length > 0) {
592
- const structureFields = presentChannels
593
- .map((ch) => `${ch}: ${pascal}Req${toPascalCase(ch)}`)
594
- .join('; ');
595
- declarations.push(`export type ${pascal}Req = { ${structureFields} }`);
596
- paramsTypeName = `${pascal}Req`;
597
- }
598
- if (resHeadersSchema != null) {
599
- const body = await convertBody(resHeadersSchema, ctx);
600
- if (body != null)
601
- declarations.push(`export type ${pascal}ResponseHeaders = ${body}`);
602
- }
603
- if (yieldSchema != null) {
604
- const body = await convertBody(yieldSchema, ctx);
605
- if (body != null) {
606
- declarations.push(`export type ${pascal}Yield = ${body}`);
607
- yieldTypeName = `${pascal}Yield`;
608
- }
609
- }
610
- if (returnSchema != null) {
611
- const body = await convertBody(returnSchema, ctx);
612
- if (body != null) {
613
- declarations.push(`export type ${pascal}ReturnType = ${body}`);
614
- returnTypeName = `${pascal}ReturnType`;
615
- }
616
- }
617
- }
618
- const callable = [
619
- ` /** ${route.method.toUpperCase()} ${route.fullPath} */`,
620
- ` ${route.name}(req: ${paramsTypeName}, options?: ProcedureCallOptions): TypedStream<${yieldTypeName}, ${returnTypeName}> {`,
621
- ` return client.stream<${yieldTypeName}, ${returnTypeName}>({`,
622
- ` name: '${route.name}',`,
623
- ` scope: '${scopeStr}',`,
624
- ` path: '${route.fullPath}',`,
625
- ` method: '${route.method}',`,
626
- ` kind: 'http-stream',`,
627
- ` streamMode: '${route.streamMode}',`,
628
- ` params: req,`,
629
- ` }, options)`,
630
- ` },`,
631
- ].join('\n');
632
- const hasErrors = injectRouteErrors(declarations, pascal, buildErrorUnion(route.errors, ctx), ctx.namespaceTypes);
633
- return { typeDeclarations: declarations, callable, hasStream: true, hasErrors };
634
- }
635
- async function emitStreamRoute(route, ctx) {
636
- const pascal = versionedPascal(route.name, route.version);
637
- // Unwrap SSE envelope from yieldType
638
- let yieldSchema = route.jsonSchema.yieldType;
639
- if (yieldSchema != null && route.streamMode === 'sse' && isSseEnvelope(yieldSchema)) {
640
- yieldSchema = unwrapSseEnvelope(yieldSchema);
641
- }
642
- const { declarations, refs } = await formatTypes(pascal, [
643
- { shortName: 'Params', schema: route.jsonSchema.params },
644
- { shortName: 'Yield', schema: yieldSchema },
645
- { shortName: 'Return', schema: route.jsonSchema.returnType },
646
- ], ctx);
647
- const paramsTypeName = refs['Params'] ?? 'unknown';
648
- const yieldTypeName = refs['Yield'] ?? 'unknown';
649
- const returnTypeName = refs['Return'] ?? 'void';
650
- const scopeStr = Array.isArray(route.scope) ? route.scope.join('-') : route.scope;
651
- const callable = [
652
- ` /** ${route.methods.map((m) => m.toUpperCase()).join('|')} ${route.path} */`,
653
- ` ${pascal}(params: ${paramsTypeName}, options?: ProcedureCallOptions): TypedStream<${yieldTypeName}, ${returnTypeName}> {`,
654
- ` return client.stream<${yieldTypeName}, ${returnTypeName}>({`,
655
- ` name: '${pascal}',`,
656
- ` scope: '${scopeStr}',`,
657
- ` path: '${route.path}',`,
658
- ` method: '${route.methods[0] ?? 'get'}',`,
659
- ` kind: 'stream',`,
660
- ` streamMode: '${route.streamMode}',`,
661
- ` params,`,
662
- ` }, options)`,
663
- ` },`,
664
- ].join('\n');
665
- const hasErrors = injectRouteErrors(declarations, pascal, buildErrorUnion(route.errors, ctx), ctx.namespaceTypes);
666
- return { typeDeclarations: declarations, callable, hasStream: true, hasErrors };
667
- }
668
- // ---------------------------------------------------------------------------
669
- // emitScopeFile
670
- // ---------------------------------------------------------------------------
671
- /**
672
- * Generates a complete TypeScript scope file for a ScopeGroup.
673
- *
674
- * When `namespaceTypes` is true, types are wrapped in nested TypeScript namespaces
675
- * and ajsc runs with `inlineTypes: false` so formatting options (enumStyle, depluralize,
676
- * jsdoc, etc.) produce extracted sub-types inside each namespace.
677
- */
678
- export async function emitScopeFile(group, options) {
679
- const { ajsc: ajscOpts, clientImportPath = 'ts-procedures/client', namespaceTypes = false, serviceName = 'Api', errorKeys, idToModelName, } = options ?? {};
680
- const pascal = toPascalCase(group.camelCase);
681
- const referencedModels = new Set();
682
- const ctx = {
683
- ajsc: ajscOpts,
684
- namespaceTypes,
685
- scopePascal: pascal,
686
- serviceName,
687
- errorKeys,
688
- idToModelName,
689
- referencedModels,
690
- };
691
- const allTypeDeclarations = [];
692
- const callables = [];
693
- let hasStream = false;
694
- let scopeHasErrors = false;
695
- for (const route of group.routes) {
696
- let chunks;
697
- const kind = route.kind ?? inferRouteKind(route);
698
- try {
699
- if (kind === 'rpc') {
700
- chunks = await emitRpcRoute(route, ctx);
701
- }
702
- else if (kind === 'api') {
703
- chunks = await emitApiRoute(route, ctx);
704
- }
705
- else if (kind === 'stream') {
706
- chunks = await emitStreamRoute(route, ctx);
707
- }
708
- else if (kind === 'http-stream') {
709
- chunks = await emitHttpStreamRoute(route, ctx);
710
- }
711
- else {
712
- throw new Error(`Unknown route kind "${kind}"`);
713
- }
714
- }
715
- catch (err) {
716
- const msg = err instanceof Error ? err.message : String(err);
717
- throw new Error(`[ts-procedures-codegen] Failed to emit route "${route.name}" (kind: ${kind}, scope: ${group.scopeKey}): ${msg}`);
718
- }
719
- allTypeDeclarations.push(...chunks.typeDeclarations);
720
- callables.push(chunks.callable);
721
- if (chunks.hasStream)
722
- hasStream = true;
723
- if (chunks.hasErrors)
724
- scopeHasErrors = true;
725
- }
726
- // Build client import line — Result/ResultNoTyped are no longer referenced
727
- // directly in scope files; the bindCallable helpers infer them from ClientInstance.
728
- const clientImports = hasStream
729
- ? `import type { ClientInstance, ProcedureCallOptions, TypedStream } from '${clientImportPath}'`
730
- : `import type { ClientInstance, ProcedureCallOptions } from '${clientImportPath}'`;
731
- // Build _errors import line when at least one route emits an Errors union.
732
- // Namespace mode uses the qualified `${Service}Errors` namespace; flat mode
733
- // pulls classes in via a wildcard alias (`_errors.UseCaseError`).
734
- let errorsImport = '';
735
- if (scopeHasErrors) {
736
- if (namespaceTypes) {
737
- errorsImport = `import type { ${toPascalCase(serviceName)}Errors } from './_errors'`;
738
- }
739
- else {
740
- errorsImport = `import type * as _errors from './_errors'`;
741
- }
742
- }
743
- const callablesBlock = callables.join('\n\n');
744
- // Assemble the type + callable section for this scope. ajsc already emitted
745
- // bare model references (via `x-named-type`); `convertExtracted` / `convertBody`
746
- // folded the reported names into `ctx.referencedModels`, which drives the
747
- // `_models` import below. No post-pass over the body is needed.
748
- let body;
749
- if (namespaceTypes) {
750
- // Namespace mode: types AND `bindScope` live inside `export namespace ${pascal}`.
751
- // Putting the function inside the namespace makes the merged symbol
752
- // value+type, which lets `index.ts` use `export import ${pascal} = …`
753
- // under `verbatimModuleSyntax: true`. (A type-only namespace would trip
754
- // TS1269/TS1288.) Consumers call `${Pascal}.bindScope(client)`; the
755
- // generated `index.ts` factory wires this internally.
756
- const callableLines = indent(callablesBlock, ' ');
757
- const namespaceMembers = [
758
- ...(allTypeDeclarations.length > 0 ? [allTypeDeclarations.join('\n\n')] : []),
759
- [
760
- ' /** Binds every callable in this scope to a configured client. */',
761
- ' export function bindScope(client: ClientInstance) {',
762
- ' return {',
763
- callableLines,
764
- ' }',
765
- ' }',
766
- ].join('\n'),
767
- ].join('\n\n');
768
- body = [
769
- `export namespace ${pascal} {`,
770
- namespaceMembers,
771
- '}',
772
- '',
773
- ].join('\n');
774
- }
775
- else {
776
- // Flat mode: types at module level, `bind${pascal}Scope` standalone.
777
- const typesBlock = allTypeDeclarations.length > 0
778
- ? allTypeDeclarations.join('\n') + '\n'
779
- : '';
780
- body = [
781
- '// ── Types ────────────────────────────────────────',
782
- '',
783
- typesBlock,
784
- '// ── Callables ────────────────────────────────────',
785
- '',
786
- `export function bind${pascal}Scope(client: ClientInstance) {`,
787
- ' return {',
788
- callablesBlock,
789
- ' }',
790
- '}',
791
- '',
792
- ].join('\n');
793
- }
794
- // Build the shared-models import line when any route referenced a `$id` model.
795
- // Matches the `_errors` import convention exactly: `'./_models'` with no `.js`.
796
- let modelsImport = '';
797
- if (referencedModels.size > 0) {
798
- const names = [...referencedModels].sort().join(', ');
799
- modelsImport = `import type { ${names} } from './_models'`;
800
- }
801
- const importsBlock = [clientImports, errorsImport, modelsImport].filter(Boolean).join('\n');
802
- return [
803
- CODEGEN_HEADER,
804
- importsBlock,
805
- '',
806
- body,
807
- ].join('\n');
808
- }
2
+ * Thin re-export barrel for the TS scope emitter, which now lives in
3
+ * `./emit/`. Kept so existing imports of `./emit-scope.js` keep working.
4
+ *
5
+ * Module layout:
6
+ * - `emit/scope-file.ts` — `emitScopeFile` orchestration + assembly
7
+ * - `emit/rpc-route.ts` — `kind: 'rpc'` emitter
8
+ * - `emit/api-route.ts` — `kind: 'api'` emitter
9
+ * - `emit/stream-route.ts` — `kind: 'stream'` emitter
10
+ * - `emit/http-stream-route.ts` — `kind: 'http-stream'` emitter
11
+ * - `emit/format-types.ts` — schema→type conversion + formatTypes helpers
12
+ * - `emit/declarations.ts` — `indent` + `DeclarationCollector`
13
+ * - `emit/route-shared.ts` — errors-union / JSDoc / naming helpers
14
+ * - `emit/context.ts` — shared `EmitRouteContext` / `RouteChunks`
15
+ */
16
+ export { emitScopeFile } from './emit/scope-file.js';
809
17
  //# sourceMappingURL=emit-scope.js.map