sdkloom 0.0.0-stage → 0.1.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 (383) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +239 -2
  3. package/kits/allowed-sources.json +24 -0
  4. package/kits/csharp/kit.json +6 -0
  5. package/kits/csharp/src/Runtime/Client.cs +295 -0
  6. package/kits/csharp/src/Runtime/Context.cs +216 -0
  7. package/kits/csharp/src/Runtime/Errors.cs +121 -0
  8. package/kits/csharp/src/Runtime/Http.cs +218 -0
  9. package/kits/csharp/src/Runtime/Json.cs +650 -0
  10. package/kits/csharp/src/Runtime/Lang.cs +212 -0
  11. package/kits/csharp/src/Runtime/Models.cs +206 -0
  12. package/kits/csharp/src/Runtime/Multipart.cs +108 -0
  13. package/kits/csharp/src/Runtime/Pagination.cs +122 -0
  14. package/kits/csharp/src/Runtime/Sse.cs +197 -0
  15. package/kits/csharp/src/Runtime/Testing.cs +375 -0
  16. package/kits/csharp/src/Runtime/Transport.cs +212 -0
  17. package/kits/csharp/src/Runtime/Wait.cs +57 -0
  18. package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
  19. package/kits/csharp/tests/ClientTests.cs +128 -0
  20. package/kits/csharp/tests/HttpTests.cs +44 -0
  21. package/kits/csharp/tests/JsonTests.cs +132 -0
  22. package/kits/csharp/tests/LangTests.cs +57 -0
  23. package/kits/csharp/tests/MultipartTests.cs +31 -0
  24. package/kits/csharp/tests/PaginationTests.cs +93 -0
  25. package/kits/csharp/tests/SseTests.cs +122 -0
  26. package/kits/csharp/tests/TestRunner.cs +93 -0
  27. package/kits/csharp/tests/TestingTests.cs +80 -0
  28. package/kits/csharp/tests/TransportTests.cs +145 -0
  29. package/kits/csharp/tests/WaitTests.cs +39 -0
  30. package/kits/csharp/tests/WebhooksTests.cs +46 -0
  31. package/kits/csharp/versions.json +12 -0
  32. package/kits/go/kit.json +6 -0
  33. package/kits/go/src/sdkruntime/client.go +322 -0
  34. package/kits/go/src/sdkruntime/context.go +207 -0
  35. package/kits/go/src/sdkruntime/errors.go +74 -0
  36. package/kits/go/src/sdkruntime/http.go +261 -0
  37. package/kits/go/src/sdkruntime/json.go +527 -0
  38. package/kits/go/src/sdkruntime/lang.go +462 -0
  39. package/kits/go/src/sdkruntime/multipart.go +75 -0
  40. package/kits/go/src/sdkruntime/pagination.go +150 -0
  41. package/kits/go/src/sdkruntime/sse.go +267 -0
  42. package/kits/go/src/sdkruntime/testing.go +343 -0
  43. package/kits/go/src/sdkruntime/transport.go +168 -0
  44. package/kits/go/src/sdkruntime/wait.go +49 -0
  45. package/kits/go/src/sdkruntime/webhooks.go +129 -0
  46. package/kits/go/tests/client_test.go +185 -0
  47. package/kits/go/tests/http_test.go +78 -0
  48. package/kits/go/tests/json_test.go +127 -0
  49. package/kits/go/tests/lang_test.go +98 -0
  50. package/kits/go/tests/multipart_test.go +29 -0
  51. package/kits/go/tests/pagination_test.go +69 -0
  52. package/kits/go/tests/sse_test.go +129 -0
  53. package/kits/go/tests/testing_test.go +121 -0
  54. package/kits/go/tests/transport_test.go +133 -0
  55. package/kits/go/tests/wait_test.go +46 -0
  56. package/kits/go/tests/webhooks_test.go +62 -0
  57. package/kits/go/versions.json +12 -0
  58. package/kits/java/kit.json +6 -0
  59. package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
  60. package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
  61. package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
  62. package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
  63. package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
  64. package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
  65. package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
  66. package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
  67. package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
  68. package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
  69. package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
  70. package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
  71. package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
  72. package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
  73. package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
  74. package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
  75. package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
  76. package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
  77. package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
  78. package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
  79. package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
  80. package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
  81. package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
  82. package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
  83. package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
  84. package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
  85. package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
  86. package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
  87. package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
  88. package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
  89. package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
  90. package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
  91. package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
  92. package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
  93. package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
  94. package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
  95. package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
  96. package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
  97. package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
  98. package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
  99. package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
  100. package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
  101. package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
  102. package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
  103. package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
  104. package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
  105. package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
  106. package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
  107. package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
  108. package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
  109. package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
  110. package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
  111. package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
  112. package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
  113. package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
  114. package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
  115. package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
  116. package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
  117. package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
  118. package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
  119. package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
  120. package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
  121. package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
  122. package/kits/java/versions.json +41 -0
  123. package/kits/python/kit.json +6 -0
  124. package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
  125. package/kits/python/src/sdkloom_runtime/client.py +232 -0
  126. package/kits/python/src/sdkloom_runtime/compat.py +195 -0
  127. package/kits/python/src/sdkloom_runtime/context.py +134 -0
  128. package/kits/python/src/sdkloom_runtime/errors.py +65 -0
  129. package/kits/python/src/sdkloom_runtime/http.py +192 -0
  130. package/kits/python/src/sdkloom_runtime/lang.py +207 -0
  131. package/kits/python/src/sdkloom_runtime/models.py +221 -0
  132. package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
  133. package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
  134. package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
  135. package/kits/python/src/sdkloom_runtime/sse.py +121 -0
  136. package/kits/python/src/sdkloom_runtime/testing.py +224 -0
  137. package/kits/python/src/sdkloom_runtime/transport.py +120 -0
  138. package/kits/python/src/sdkloom_runtime/wait.py +44 -0
  139. package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
  140. package/kits/python/tests/support.py +28 -0
  141. package/kits/python/tests/test_client.py +275 -0
  142. package/kits/python/tests/test_compat.py +87 -0
  143. package/kits/python/tests/test_http.py +94 -0
  144. package/kits/python/tests/test_lang.py +104 -0
  145. package/kits/python/tests/test_models.py +110 -0
  146. package/kits/python/tests/test_multipart.py +37 -0
  147. package/kits/python/tests/test_pagination.py +70 -0
  148. package/kits/python/tests/test_sse.py +61 -0
  149. package/kits/python/tests/test_testing.py +130 -0
  150. package/kits/python/tests/test_transport.py +124 -0
  151. package/kits/python/tests/test_wait.py +46 -0
  152. package/kits/python/tests/test_webhooks.py +57 -0
  153. package/kits/python/versions.json +33 -0
  154. package/kits/rust/kit.json +6 -0
  155. package/kits/rust/src/runtime/client.rs +212 -0
  156. package/kits/rust/src/runtime/context.rs +182 -0
  157. package/kits/rust/src/runtime/crypto.rs +84 -0
  158. package/kits/rust/src/runtime/errors.rs +147 -0
  159. package/kits/rust/src/runtime/http.rs +248 -0
  160. package/kits/rust/src/runtime/json.rs +424 -0
  161. package/kits/rust/src/runtime/lang.rs +296 -0
  162. package/kits/rust/src/runtime/mod.rs +30 -0
  163. package/kits/rust/src/runtime/models.rs +266 -0
  164. package/kits/rust/src/runtime/multipart.rs +81 -0
  165. package/kits/rust/src/runtime/pagination.rs +117 -0
  166. package/kits/rust/src/runtime/sse.rs +195 -0
  167. package/kits/rust/src/runtime/testing.rs +308 -0
  168. package/kits/rust/src/runtime/transport.rs +324 -0
  169. package/kits/rust/src/runtime/wait.rs +29 -0
  170. package/kits/rust/src/runtime/webhooks.rs +142 -0
  171. package/kits/rust/tests/client.rs +152 -0
  172. package/kits/rust/tests/http.rs +44 -0
  173. package/kits/rust/tests/json.rs +82 -0
  174. package/kits/rust/tests/lang.rs +74 -0
  175. package/kits/rust/tests/multipart.rs +21 -0
  176. package/kits/rust/tests/pagination.rs +61 -0
  177. package/kits/rust/tests/sse.rs +77 -0
  178. package/kits/rust/tests/testing.rs +90 -0
  179. package/kits/rust/tests/transport.rs +218 -0
  180. package/kits/rust/tests/wait.rs +32 -0
  181. package/kits/rust/tests/webhooks.rs +73 -0
  182. package/kits/rust/versions.json +6 -0
  183. package/kits/typescript/kit.json +6 -0
  184. package/kits/typescript/src/client.ts +223 -0
  185. package/kits/typescript/src/context.ts +108 -0
  186. package/kits/typescript/src/errors.ts +63 -0
  187. package/kits/typescript/src/helpers.ts +49 -0
  188. package/kits/typescript/src/http.ts +192 -0
  189. package/kits/typescript/src/models.ts +139 -0
  190. package/kits/typescript/src/multipart.ts +66 -0
  191. package/kits/typescript/src/pagination.ts +54 -0
  192. package/kits/typescript/src/pipeline.ts +25 -0
  193. package/kits/typescript/src/sse.ts +102 -0
  194. package/kits/typescript/src/testing.ts +217 -0
  195. package/kits/typescript/src/transport.ts +101 -0
  196. package/kits/typescript/src/wait.ts +36 -0
  197. package/kits/typescript/src/webhooks.ts +168 -0
  198. package/kits/typescript/tests/client.test.ts +269 -0
  199. package/kits/typescript/tests/helpers.test.ts +27 -0
  200. package/kits/typescript/tests/http.test.ts +80 -0
  201. package/kits/typescript/tests/models.test.ts +71 -0
  202. package/kits/typescript/tests/multipart.test.ts +28 -0
  203. package/kits/typescript/tests/pagination.test.ts +76 -0
  204. package/kits/typescript/tests/sse.test.ts +82 -0
  205. package/kits/typescript/tests/testing.test.ts +103 -0
  206. package/kits/typescript/tests/transport.test.ts +127 -0
  207. package/kits/typescript/tests/wait.test.ts +31 -0
  208. package/kits/typescript/tests/webhooks.test.ts +53 -0
  209. package/kits/typescript/versions.json +15 -0
  210. package/package.json +37 -4
  211. package/packages/cli/recipes/apiKeyHeader.ts +16 -0
  212. package/packages/cli/recipes/apiVersionHeader.ts +16 -0
  213. package/packages/cli/recipes/basic.ts +13 -0
  214. package/packages/cli/recipes/bearer.ts +10 -0
  215. package/packages/cli/recipes/hmacSign.ts +23 -0
  216. package/packages/cli/recipes/idempotencyKey.ts +19 -0
  217. package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
  218. package/packages/cli/recipes/retry.ts +42 -0
  219. package/packages/cli/recipes/timing.ts +12 -0
  220. package/packages/cli/recipes/userAgent.ts +8 -0
  221. package/packages/cli/src/build.js +93 -0
  222. package/packages/cli/src/commands/build.js +31 -0
  223. package/packages/cli/src/commands/conformance-check.js +50 -0
  224. package/packages/cli/src/commands/contract.js +181 -0
  225. package/packages/cli/src/commands/diff.js +113 -0
  226. package/packages/cli/src/commands/doctor.js +43 -0
  227. package/packages/cli/src/commands/import-openapi.js +42 -0
  228. package/packages/cli/src/commands/init.js +27 -0
  229. package/packages/cli/src/commands/kit-check.js +145 -0
  230. package/packages/cli/src/commands/publish.js +81 -0
  231. package/packages/cli/src/commands/recipe.js +49 -0
  232. package/packages/cli/src/commands/record.js +181 -0
  233. package/packages/cli/src/commands/serve.js +156 -0
  234. package/packages/cli/src/commands/spec-check.js +57 -0
  235. package/packages/cli/src/commands/test.js +177 -0
  236. package/packages/cli/src/commands/variants.js +32 -0
  237. package/packages/cli/src/commands/verify.js +57 -0
  238. package/packages/cli/src/commands/version.js +63 -0
  239. package/packages/cli/src/containers.js +82 -0
  240. package/packages/cli/src/coverage.js +72 -0
  241. package/packages/cli/src/git.js +25 -0
  242. package/packages/cli/src/io.js +50 -0
  243. package/packages/cli/src/java/ScenarioHarness.java +297 -0
  244. package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
  245. package/packages/cli/src/java.js +70 -0
  246. package/packages/cli/src/js/harness.mjs +120 -0
  247. package/packages/cli/src/kits.js +103 -0
  248. package/packages/cli/src/languages/csharp.js +274 -0
  249. package/packages/cli/src/languages/go.js +185 -0
  250. package/packages/cli/src/languages/index.js +18 -0
  251. package/packages/cli/src/languages/java.js +187 -0
  252. package/packages/cli/src/languages/python.js +120 -0
  253. package/packages/cli/src/languages/rust.js +189 -0
  254. package/packages/cli/src/languages/types.js +93 -0
  255. package/packages/cli/src/languages/typescript.js +112 -0
  256. package/packages/cli/src/main.js +13 -0
  257. package/packages/cli/src/packaging.js +39 -0
  258. package/packages/cli/src/portability.js +45 -0
  259. package/packages/cli/src/profiles.js +55 -0
  260. package/packages/cli/src/project.js +56 -0
  261. package/packages/cli/src/python/scenario_harness.py +162 -0
  262. package/packages/cli/src/python/schema_roundtrip.py +42 -0
  263. package/packages/cli/src/registries.js +214 -0
  264. package/packages/cli/src/resolve-core.js +39 -0
  265. package/packages/cli/src/run.js +133 -0
  266. package/packages/cli/src/runtimes.js +61 -0
  267. package/packages/cli/src/scenarios-java.js +82 -0
  268. package/packages/cli/src/scenarios-javascript.js +54 -0
  269. package/packages/cli/src/scenarios-python.js +90 -0
  270. package/packages/cli/src/scenarios.js +183 -0
  271. package/packages/cli/src/schema.js +84 -0
  272. package/packages/cli/src/templates.js +85 -0
  273. package/packages/cli/src/toolchains.js +68 -0
  274. package/packages/cli/src/transpiled-tests.js +92 -0
  275. package/packages/codegen/src/canonical-json.js +31 -0
  276. package/packages/codegen/src/capabilities.js +50 -0
  277. package/packages/codegen/src/code-writer.js +54 -0
  278. package/packages/codegen/src/file-tree.js +25 -0
  279. package/packages/codegen/src/hash.js +6 -0
  280. package/packages/codegen/src/index.js +16 -0
  281. package/packages/codegen/src/lock.js +106 -0
  282. package/packages/conformance/src/case.js +143 -0
  283. package/packages/conformance/src/checks.js +40 -0
  284. package/packages/conformance/src/index.js +3 -0
  285. package/packages/conformance/src/matrix.js +48 -0
  286. package/packages/conformance/src/suite.js +48 -0
  287. package/packages/differ/src/diff.js +500 -0
  288. package/packages/differ/src/index.js +3 -0
  289. package/packages/differ/src/notes.js +112 -0
  290. package/packages/differ/src/support.js +34 -0
  291. package/packages/gen-csharp/src/emit-client.js +397 -0
  292. package/packages/gen-csharp/src/emit-harness.js +333 -0
  293. package/packages/gen-csharp/src/emit-mock.js +111 -0
  294. package/packages/gen-csharp/src/emit-models.js +119 -0
  295. package/packages/gen-csharp/src/emit-webhooks.js +57 -0
  296. package/packages/gen-csharp/src/generate.js +245 -0
  297. package/packages/gen-csharp/src/index.js +5 -0
  298. package/packages/gen-csharp/src/names.js +284 -0
  299. package/packages/gen-go/src/emit-client.js +405 -0
  300. package/packages/gen-go/src/emit-harness.js +294 -0
  301. package/packages/gen-go/src/emit-mock.js +112 -0
  302. package/packages/gen-go/src/emit-models.js +146 -0
  303. package/packages/gen-go/src/emit-webhooks.js +58 -0
  304. package/packages/gen-go/src/generate.js +205 -0
  305. package/packages/gen-go/src/index.js +5 -0
  306. package/packages/gen-go/src/names.js +197 -0
  307. package/packages/gen-java/src/emit-client.js +441 -0
  308. package/packages/gen-java/src/emit-models.js +342 -0
  309. package/packages/gen-java/src/emit-testing.js +123 -0
  310. package/packages/gen-java/src/emit-webhooks.js +70 -0
  311. package/packages/gen-java/src/generate.js +244 -0
  312. package/packages/gen-java/src/index.js +4 -0
  313. package/packages/gen-java/src/names.js +197 -0
  314. package/packages/gen-python/src/emit-client.js +272 -0
  315. package/packages/gen-python/src/emit-models.js +139 -0
  316. package/packages/gen-python/src/emit-testing.js +93 -0
  317. package/packages/gen-python/src/emit-webhooks.js +48 -0
  318. package/packages/gen-python/src/generate.js +237 -0
  319. package/packages/gen-python/src/index.js +2 -0
  320. package/packages/gen-python/src/names.js +161 -0
  321. package/packages/gen-python/src/style.js +67 -0
  322. package/packages/gen-rust/src/emit-client.js +379 -0
  323. package/packages/gen-rust/src/emit-harness.js +278 -0
  324. package/packages/gen-rust/src/emit-mock.js +121 -0
  325. package/packages/gen-rust/src/emit-models.js +117 -0
  326. package/packages/gen-rust/src/emit-webhooks.js +58 -0
  327. package/packages/gen-rust/src/generate.js +243 -0
  328. package/packages/gen-rust/src/index.js +5 -0
  329. package/packages/gen-rust/src/names.js +223 -0
  330. package/packages/gen-typescript/src/emit-client.js +434 -0
  331. package/packages/gen-typescript/src/emit-models.js +93 -0
  332. package/packages/gen-typescript/src/emit-package.js +90 -0
  333. package/packages/gen-typescript/src/emit-testing.js +138 -0
  334. package/packages/gen-typescript/src/emit-webhooks.js +52 -0
  335. package/packages/gen-typescript/src/generate.js +70 -0
  336. package/packages/gen-typescript/src/index.js +3 -0
  337. package/packages/gen-typescript/src/naming.js +64 -0
  338. package/packages/gen-typescript/src/runtime.js +81 -0
  339. package/packages/gen-typescript/src/types.js +136 -0
  340. package/packages/ir/src/api-surface.js +22 -0
  341. package/packages/ir/src/checks.js +52 -0
  342. package/packages/ir/src/index.js +32 -0
  343. package/packages/ir/src/ir.js +214 -0
  344. package/packages/ir/src/pages.js +18 -0
  345. package/packages/ir/src/validate-ir.js +462 -0
  346. package/packages/ir/src/validate-types.js +140 -0
  347. package/packages/kit/src/block-parser.js +196 -0
  348. package/packages/kit/src/index.js +15 -0
  349. package/packages/kit/src/kit-check.js +203 -0
  350. package/packages/kit/src/sources.js +53 -0
  351. package/packages/kit/src/version-policy.js +133 -0
  352. package/packages/kit/src/version-range.js +132 -0
  353. package/packages/openapi/src/import.js +292 -0
  354. package/packages/openapi/src/index.js +1 -0
  355. package/packages/openapi/src/names.js +50 -0
  356. package/packages/openapi/src/schemas.js +264 -0
  357. package/packages/packager/src/archive.js +106 -0
  358. package/packages/packager/src/crate.js +39 -0
  359. package/packages/packager/src/gomodule.js +11 -0
  360. package/packages/packager/src/index.js +9 -0
  361. package/packages/packager/src/maven.js +85 -0
  362. package/packages/packager/src/metadata.js +34 -0
  363. package/packages/packager/src/npm.js +71 -0
  364. package/packages/packager/src/nuget.js +78 -0
  365. package/packages/packager/src/sbom.js +56 -0
  366. package/packages/packager/src/wheel.js +62 -0
  367. package/packages/spec/src/index.js +11 -0
  368. package/packages/spec/src/requirements.js +139 -0
  369. package/packages/transpile/src/ast.js +51 -0
  370. package/packages/transpile/src/check.js +669 -0
  371. package/packages/transpile/src/emit-csharp.js +469 -0
  372. package/packages/transpile/src/emit-go.js +529 -0
  373. package/packages/transpile/src/emit-java.js +360 -0
  374. package/packages/transpile/src/emit-python.js +320 -0
  375. package/packages/transpile/src/emit-rust.js +499 -0
  376. package/packages/transpile/src/errors.js +15 -0
  377. package/packages/transpile/src/index.js +13 -0
  378. package/packages/transpile/src/lexer.js +161 -0
  379. package/packages/transpile/src/parser.js +543 -0
  380. package/packages/transpile/src/tests/ast.js +46 -0
  381. package/packages/transpile/src/tests/parse.js +376 -0
  382. package/packages/transpile/src/tests/translate.js +849 -0
  383. package/packages/transpile/src/transpile.js +57 -0
@@ -0,0 +1,112 @@
1
+ // Human-readable release notes: a CHANGELOG entry for every release and, for a major release, a migration guide with
2
+ // before/after code in each language.
3
+
4
+
5
+ import { LANGUAGES, } from "./diff.ts";
6
+
7
+ const snake = (name ) => name.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^A-Za-z0-9]+/g, "_").toLowerCase();
8
+ const camel = (name ) =>
9
+ name
10
+ .split(/[^A-Za-z0-9]+/)
11
+ .filter((part) => part !== "")
12
+ .map((part, index) => (index === 0 ? part.charAt(0).toLowerCase() + part.slice(1) : part.charAt(0).toUpperCase() + part.slice(1)))
13
+ .join("");
14
+ const pascal = (name ) => camel(name).charAt(0).toUpperCase() + camel(name).slice(1);
15
+
16
+ /** "all languages", "java" or "python, java": where, among the SDK's languages, a change has the given bump. */
17
+ function where(change , bump , languages ) {
18
+ const affected = languages.filter((language) => change.bump[language] === bump);
19
+ return affected.length === languages.length ? "all languages" : affected.join(", ");
20
+ }
21
+
22
+ function strongest(change , languages ) {
23
+ return languages.map((language) => change.bump[language]).reduce ((best, bump) => (bump === "major" || (bump === "minor" && best === "patch") ? bump : best), "patch");
24
+ }
25
+
26
+ /** languages: the SDK's target languages; a change that only breaks a language the SDK does not have is not breaking. */
27
+ export function changelogEntry(version , date , changes , languages = LANGUAGES) {
28
+ const lines = [`## ${version} — ${date}`, ""];
29
+ const breaking = changes.filter((change) => strongest(change, languages) === "major");
30
+ const minor = changes.filter((change) => strongest(change, languages) === "minor");
31
+ if (changes.length === 0) lines.push("No API changes.", "");
32
+ if (breaking.length > 0) {
33
+ lines.push("### Breaking", "");
34
+ breaking.forEach((change) => lines.push(`- \`${change.path}\`: ${change.message} (${where(change, "major", languages)})`));
35
+ lines.push("");
36
+ }
37
+ if (minor.length > 0) {
38
+ lines.push("### Added and changed", "");
39
+ minor.forEach((change) => lines.push(`- \`${change.path}\`: ${change.message}`));
40
+ lines.push("");
41
+ }
42
+ return lines.join("\n");
43
+ }
44
+
45
+ /** How a caller writes one operation call in a language. */
46
+ export function callSignature(operation , language ) {
47
+ const path = operation.params.filter((param) => param.in === "path");
48
+ const named = operation.params.filter((param) => param.in !== "path");
49
+ const body = operation.body === undefined ? [] : ["body"];
50
+ if (language === "typescript") {
51
+ const args = [...path.map((param) => (/^[A-Za-z_$][\w$]*$/.test(param.name) ? param.name : JSON.stringify(param.name))), ...named.map((param) => `${JSON.stringify(param.name).replace(/^"([A-Za-z_$][\w$]*)"$/, "$1")}${param.optional ? "?" : ""}`), ...body];
52
+ return `await client.${operation.name}(${args.length === 0 ? "" : `{ ${args.join(", ")} }`})`;
53
+ }
54
+ if (language === "python") {
55
+ return `client.${snake(operation.name)}(${[...path.map((param) => snake(param.name)), ...body, ...named.map((param) => `${snake(param.name)}=…`)].join(", ")})`;
56
+ }
57
+ if (language === "csharp") {
58
+ const params = named.length === 0 ? [] : [`new ${pascal(operation.name)}Params { … }`];
59
+ return `await client.${pascal(operation.name)}Async(${[...path.map((param) => camel(param.name)), ...body, ...params].join(", ")})`;
60
+ }
61
+ if (language === "rust") {
62
+ const params = named.length === 0 ? [] : [`&${pascal(operation.name)}Params { … }`];
63
+ return `client.${snake(operation.name)}(${[...path.map((param) => `&${snake(param.name)}`), ...body.map((item) => `&${item}`), ...params].join(", ")})?`;
64
+ }
65
+ if (language === "go") {
66
+ const params = named.length === 0 ? [] : [`${pascal(operation.name)}Params{…}`];
67
+ return `client.${pascal(operation.name)}(${["ctx", ...path.map((param) => camel(param.name)), ...body, ...params].join(", ")})`;
68
+ }
69
+ const params = named.length === 0 ? [] : [`${pascal(operation.name)}Params.builder()…build()`];
70
+ return `client.${camel(operation.name)}(${[...path.map((param) => camel(param.name)), ...body, ...params].join(", ")})`;
71
+ }
72
+
73
+ /** How a caller reads a field of a model in a language. */
74
+ function fieldRead(model , field , language ) {
75
+ const value = camel(model);
76
+ if (language === "python") return `${snake(model)}.${snake(field)}`;
77
+ if (language === "java") return `${value}.${camel(field)}()`;
78
+ if (language === "go") return `${value}.${pascal(field)}`;
79
+ if (language === "rust") return `${snake(model)}.${snake(field)}`;
80
+ if (language === "csharp") return `${value}.${pascal(field)}`;
81
+ return /^[A-Za-z_$][\w$]*$/.test(field) ? `${value}.${field}` : `${value}[${JSON.stringify(field)}]`;
82
+ }
83
+
84
+ const FENCE = { typescript: "ts", python: "python", java: "java", go: "go", rust: "rust", csharp: "csharp" };
85
+
86
+ function code(language , before , after ) {
87
+ return [`**${language}**`, "", `\`\`\`${FENCE[language]}`, `// before`.replace("//", language === "python" ? "#" : "//"), before, `// after`.replace("//", language === "python" ? "#" : "//"), after, "```", ""];
88
+ }
89
+
90
+ /** A migration guide for a major release: every breaking change, with before/after code where the call site changes. */
91
+ export function migrationGuide(version , before , after , changes , languages = LANGUAGES) {
92
+ const breaking = changes.filter((change) => strongest(change, languages) === "major");
93
+ const lines = [`# Migrating to ${version}`, "", `${breaking.length} breaking change${breaking.length === 1 ? "" : "s"}.`, ""];
94
+ for (const change of breaking) {
95
+ lines.push(`## \`${change.path}\``, "", `${change.message}. Breaking in: ${where(change, "major", languages)}.`, "");
96
+ const parts = change.path.split(".");
97
+ const affected = languages.filter((language) => change.bump[language] === "major");
98
+ if (parts[0] === "operations") {
99
+ const old = before.operations.find((operation) => operation.name === parts[1]);
100
+ const now = after.operations.find((operation) => operation.name === parts[1]);
101
+ for (const language of affected) {
102
+ lines.push(...code(language, old === undefined ? "(not available)" : callSignature(old, language), now === undefined ? "(removed)" : callSignature(now, language)));
103
+ }
104
+ } else if (parts[0] === "types" && parts.length === 3) {
105
+ const [, model = "", field = ""] = parts;
106
+ for (const language of affected) {
107
+ lines.push(...code(language, fieldRead(model, field, language), change.rule === "FIELD-REMOVED" ? "(removed)" : `${fieldRead(model, field, language)} // ${change.message}`));
108
+ }
109
+ }
110
+ }
111
+ return lines.join("\n");
112
+ }
@@ -0,0 +1,34 @@
1
+ // Supported language versions are part of the contract: raising the minimum (Python >=3.8 → >=3.9) breaks users on the
2
+ // old version, so it needs a major release in that language; lowering it only adds users.
3
+
4
+
5
+ import { LANGUAGES } from "./diff.ts";
6
+
7
+ function minimum(range ) {
8
+ const match = />=\s*(\d+(?:\.\d+)*)/.exec(range);
9
+ return match?.[1]?.split(".").map(Number);
10
+ }
11
+
12
+ function compare(a , b ) {
13
+ for (let index = 0; index < Math.max(a.length, b.length); index += 1) {
14
+ const difference = (a[index] ?? 0) - (b[index] ?? 0);
15
+ if (difference !== 0) return difference;
16
+ }
17
+ return 0;
18
+ }
19
+
20
+ /** Changes in supported ranges, given as language → range (e.g. { python: ">=3.8" }). */
21
+ export function diffSupport(before , after ) {
22
+ return LANGUAGES.flatMap((language) => {
23
+ const old = before[language];
24
+ const now = after[language];
25
+ if (old === undefined || now === undefined || old === now) return [];
26
+ const oldMin = minimum(old);
27
+ const newMin = minimum(now);
28
+ if (oldMin === undefined || newMin === undefined) return [];
29
+ const order = compare(newMin, oldMin);
30
+ if (order === 0) return [];
31
+ const bump = Object.fromEntries(LANGUAGES.map((candidate) => [candidate, candidate === language ? (order > 0 ? "major" : "minor") : "patch"])) ;
32
+ return [{ rule: order > 0 ? "SUPPORT-NARROWED" : "SUPPORT-WIDENED", path: `support.${language}`, message: `${language} ${now} instead of ${old}`, bump }];
33
+ });
34
+ }
@@ -0,0 +1,397 @@
1
+ // Client.cs: Options, Client (one async method per operation), RawClient, the params classes, the operation specs and an
2
+ // exception class per declared error.
3
+
4
+
5
+ import { pagedItems, } from "@sdkloom/ir";
6
+ import { csharpName } from "@sdkloom/transpile";
7
+ import { propertyName } from "./emit-models.ts";
8
+ import { csharp, csType, decode, decoder, doc, encoder, fieldType, initializer, paramEncoder, pascal, pathParamName, str } from "./names.ts";
9
+
10
+ /** Options every client has besides middleware configuration. */
11
+ export const CLIENT_OPTIONS = ["Server", "BaseUrl", "Transport", "TimeoutMs", "Middleware", "OnEvent", "Clock"];
12
+
13
+ export function configFields(ir ) {
14
+ const fields = new Map ();
15
+ for (const middleware of ir.middlewares) {
16
+ for (const item of middleware.config) {
17
+ if (CLIENT_OPTIONS.includes(pascal(item.name))) throw new Error(`config field ${item.name} of middleware ${middleware.name} collides with a client option`);
18
+ if (!fields.has(item.name)) fields.set(item.name, item);
19
+ }
20
+ }
21
+ return [...fields.values()];
22
+ }
23
+
24
+
25
+
26
+ export function kindOf(node ) {
27
+ if (node === undefined) return undefined;
28
+ return node.kind === "primitive" && (node.name === "bytes" || node.name === "stream" || node.name === "multipart") ? node.name : "json";
29
+ }
30
+
31
+ /** What an operation's data is: byte[] for bytes and streams, the decoded type, or nothing. */
32
+ export function resultType(ir , operation ) {
33
+ if (operation.events !== undefined) return `IAsyncEnumerable<ServerSentEvent<${csType(ir, operation.events)}>>`;
34
+ const kind = kindOf(operation.returns);
35
+ if (kind === undefined) return undefined;
36
+ return kind === "json" ? csType(ir, operation.returns ) : "byte[]";
37
+ }
38
+
39
+ /** An event stream whose data stays text: no decoder, the data is a string. */
40
+ export function textEvents(node ) {
41
+ return node.kind === "primitive" && node.name === "string";
42
+ }
43
+
44
+ export function paramsType(operation ) {
45
+ return `${pascal(operation.name)}Params`;
46
+ }
47
+
48
+ export function methodName(operation ) {
49
+ return `${pascal(operation.name)}Async`;
50
+ }
51
+
52
+ export function exceptionName(error ) {
53
+ return `${pascal(error)}Exception`;
54
+ }
55
+
56
+ function named(operation ) {
57
+ return operation.params.filter((param) => param.in !== "path");
58
+ }
59
+
60
+ /** Path parameters, the body, then the params object (optional when every named parameter is). */
61
+ export function signature(ir , operation ) {
62
+ const path = operation.params.filter((param) => param.in === "path");
63
+ const parameters = path.map((param) => `${csType(ir, param.type)} ${pathParamName(param.name)}`);
64
+ const call = path.map((param) => pathParamName(param.name));
65
+ if (operation.body !== undefined) {
66
+ parameters.push(`${kindOf(operation.body) === "json" || kindOf(operation.body) === "multipart" ? csType(ir, operation.body) : "byte[]"} body`);
67
+ call.push("body");
68
+ }
69
+ if (named(operation).length > 0) {
70
+ const optional = named(operation).every((param) => param.optional);
71
+ parameters.push(`${paramsType(operation)}${optional ? "? parameters = null" : " parameters"}`);
72
+ call.push("parameters");
73
+ }
74
+ return { parameters, call };
75
+ }
76
+
77
+ function emitParams(writer , ir , operation ) {
78
+ if (named(operation).length === 0) return;
79
+ doc(writer, [`The query and header parameters of ${methodName(operation)}.`]);
80
+ writer.line(`public sealed class ${paramsType(operation)}`).line("{").indent((body) => {
81
+ named(operation).forEach((param, index) => {
82
+ if (index > 0) body.line();
83
+ doc(body, param.doc === undefined ? [] : [param.doc]);
84
+ const presence = { type: param.type, optional: param.optional, nullable: false };
85
+ body.line(`public ${fieldType(ir, presence)} ${pascal(param.name)} { get; set; }${initializer(ir, presence)}`);
86
+ });
87
+ body.line();
88
+ doc(body, ["A copy, e.g. to change one parameter for the next call."]);
89
+ body.line(`public ${paramsType(operation)} Copy() => (${paramsType(operation)})MemberwiseClone();`);
90
+ });
91
+ writer.line("}").line();
92
+ }
93
+
94
+ const KIND = { json: "BodyKind.Json", bytes: "BodyKind.Bytes", stream: "BodyKind.Stream", multipart: "BodyKind.Multipart" };
95
+
96
+ function specName(operation ) {
97
+ return `${pascal(operation.name)}Spec`;
98
+ }
99
+
100
+ function emitSpec(writer , operation ) {
101
+ const body = kindOf(operation.body);
102
+ const result = kindOf(operation.returns);
103
+ const errors = operation.errors.map((error) => `[${error.status}] = (status, body, response) => new ${exceptionName(error.name)}(status, body, response)`);
104
+ writer.line(`private static readonly OperationSpec ${specName(operation)} = new OperationSpec(`).indent((spec) => {
105
+ spec.line(`${str(operation.name)},`);
106
+ spec.line(`${str(operation.method)},`);
107
+ spec.line(`${str(operation.path)},`);
108
+ spec.line(`${body === undefined ? "null" : KIND[body]},`);
109
+ spec.line(`${operation.events !== undefined ? "BodyKind.Events" : result === undefined ? "null" : KIND[result]},`);
110
+ spec.line(`new Dictionary<int, ErrorFactory> {${errors.length === 0 ? "" : ` ${errors.join(", ")} `}},`);
111
+ spec.line(`new string[] {${operation.use.length === 0 ? "" : ` ${operation.use.map(str).join(", ")} `}}${operation.since === undefined ? "" : `, ${str(operation.since)}`});`);
112
+ });
113
+ writer.line();
114
+ }
115
+
116
+ function operationDoc(operation ) {
117
+ return [
118
+ operation.doc ?? pascal(operation.name),
119
+ "",
120
+ `HTTP: ${operation.method} ${operation.path}`,
121
+ ...(operation.since === undefined ? [] : [`Since API version ${operation.since}.`]),
122
+ ...(operation.events === undefined ? [] : [`Server-sent events, read as they arrive: await foreach (var item in client.${methodName(operation)}(...)). The request is sent when iteration starts.`]),
123
+ ...(operation.stability === "stable" ? [] : [`Stability: ${operation.stability}`]),
124
+ ...operation.errors.map((error) => `Throws ${exceptionName(error.name)} for HTTP ${error.status}.`),
125
+ "Throws ApiException for any other non-2xx response, TransportException when there is no response.",
126
+ ];
127
+ }
128
+
129
+ function pairs(entries ) {
130
+ return entries.length === 0 ? "None" : `new[] { ${entries.join(", ")} }`;
131
+ }
132
+
133
+ function emitRaw(writer , ir , operation ) {
134
+ const type = resultType(ir, operation);
135
+ const { parameters } = signature(ir, operation);
136
+ const returns = type === undefined ? "Task<HttpResponse>" : `Task<RawResult<${type}>>`;
137
+ doc(writer, [`${methodName(operation)} with the response, so the HTTP status and headers are available; options change this call.`]);
138
+ if (operation.deprecated !== undefined) writer.line(`[Obsolete(${str(operation.deprecated)})]`);
139
+ const all = [...parameters, "CallOptions? options = null", "CancellationToken cancellationToken = default"];
140
+ writer.line(`public async ${returns} ${methodName(operation)}(${all.join(", ")})`).line("{").indent((fn) => {
141
+ if (named(operation).length > 0) fn.line(`var named = parameters ?? new ${paramsType(operation)}();`);
142
+ const value = (param ) => `P(${str(param.name)}, ${paramEncoder(ir, param, `named.${pascal(param.name)}`)})`;
143
+ const path = operation.params.filter((param) => param.in === "path").map((param) => `P(${str(param.name)}, ${encoder(ir, param.type, pathParamName(param.name))})`);
144
+ const query = operation.params.filter((param) => param.in === "query").map(value);
145
+ const headers = operation.params.filter((param) => param.in === "header").map(value);
146
+ const kind = kindOf(operation.body);
147
+ const body = kind === undefined ? "null" : kind === "json" ? `Js.Utf8(${encoder(ir, operation.body , "body")}.ToJsonString())` : kind === "multipart" ? "body.ToBody()" : "body";
148
+ fn.line(`var args = new CallArgs(${pairs(path)}, ${pairs(query)}, ${pairs(headers)}, ${body});`);
149
+ fn.line(`var response = await Call.Send(_runtime, ${specName(operation)}, args, options, cancellationToken).ConfigureAwait(false);`);
150
+ const result = kindOf(operation.returns);
151
+ if (operation.events !== undefined) {
152
+ const data = csType(ir, operation.events);
153
+ const decode = textEvents(operation.events) ? "null" : decoder(ir, operation.events, `operations.${operation.name}.events`);
154
+ fn.line(`return new RawResult<${type ?? ""}>(EventStreams.Read<${data}>(response, ${decode}, cancellationToken), response);`);
155
+ } else if (result === undefined) fn.line("return response;");
156
+ else if (result !== "json") fn.line(`return new RawResult<byte[]>(response.Body, response);`);
157
+ else fn.line(`return new RawResult<${type ?? ""}>(${decode(ir, operation.returns , `operations.${operation.name}.returns`, "response.Json()", '"$"')}, response);`);
158
+ });
159
+ writer.line("}").line();
160
+ }
161
+
162
+ function emitMethod(writer , ir , operation ) {
163
+ const type = resultType(ir, operation);
164
+ const { parameters, call } = signature(ir, operation);
165
+ doc(writer, operationDoc(operation));
166
+ if (operation.deprecated !== undefined) writer.line(`[Obsolete(${str(operation.deprecated)})]`);
167
+ const all = [...parameters, "CancellationToken cancellationToken = default"];
168
+ const raw = `Raw.${methodName(operation)}(${[...call, "null", "cancellationToken"].join(", ")})`;
169
+ if (operation.events !== undefined) {
170
+ // An async iterator: the request is sent when iteration starts.
171
+ const events = all.map((parameter) => (parameter.startsWith("CancellationToken ") ? `[EnumeratorCancellation] ${parameter}` : parameter));
172
+ writer.line(`public async ${type ?? ""} ${methodName(operation)}(${events.join(", ")})`).line("{").indent((fn) => {
173
+ fn.line(`var result = await ${raw}.ConfigureAwait(false);`);
174
+ fn.line("await foreach (var item in result.Data.ConfigureAwait(false))").line("{").indent((loop) => loop.line("yield return item;")).line("}");
175
+ });
176
+ writer.line("}").line();
177
+ return;
178
+ }
179
+ if (type === undefined) {
180
+ writer.line(`public Task ${methodName(operation)}(${all.join(", ")}) => ${raw};`).line();
181
+ return;
182
+ }
183
+ writer.line(`public async Task<${type}> ${methodName(operation)}(${all.join(", ")}) => (await ${raw}.ConfigureAwait(false)).Data;`).line();
184
+ }
185
+
186
+ /** <Name>AndWaitAsync: starts the work, then polls until it is done (kit block lro.wait). */
187
+ function emitWait(writer , ir , operation , wait ) {
188
+ const type = resultType(ir, operation) ?? "";
189
+ const poll = ir.operations.find((candidate) => candidate.name === wait.poll);
190
+ if (poll === undefined) throw new Error(`operations.${operation.name}.wait: no operation ${wait.poll}`);
191
+ const { parameters, call } = signature(ir, operation);
192
+ const returned = operation.returns?.kind === "ref" ? operation.returns.name : "";
193
+ const read = (field ) => `current.${propertyName(returned, field)}`;
194
+ doc(writer, [
195
+ `${methodName(operation)}, then ${methodName(poll)} every ${wait.intervalMs} ms until ${wait.field} is ${wait.done.join(" or ")}; the finished result.`,
196
+ `Throws WaitTimeoutException when the work is not done within ${wait.timeoutMs} ms.`,
197
+ ]);
198
+ writer.line(`public async Task<${type}> ${pascal(operation.name)}AndWaitAsync(${[...parameters, "CancellationToken cancellationToken = default"].join(", ")})`).line("{").indent((fn) => {
199
+ fn.line(`var first = await ${methodName(operation)}(${[...call, "cancellationToken"].join(", ")}).ConfigureAwait(false);`);
200
+ fn.line(`var done = new[] { ${wait.done.map(str).join(", ")} };`);
201
+ fn.line(`return await Waits.Until(first, (current, token) => ${methodName(poll)}(${read(wait.from)}, cancellationToken: token), current => Array.IndexOf(done, ${read(wait.field)}) >= 0, Raw.Clock, ${wait.intervalMs}, ${wait.timeoutMs}, cancellationToken).ConfigureAwait(false);`);
202
+ });
203
+ writer.line("}").line();
204
+ }
205
+
206
+ /** <Name>AllAsync: every item of every page, fetched as the caller iterates (kit block pagination.iterate). */
207
+ function emitIterator(writer , ir , operation , paginate ) {
208
+ const returns = operation.returns;
209
+ const page = returns?.kind === "ref" ? ir.types.find((named) => named.name === returns.name) : undefined;
210
+ const fields = page?.type.kind === "object" ? page.type.fields : [];
211
+ const { item: itemNode, field: itemsField } = pagedItems(ir, operation);
212
+ const item = csType(ir, itemNode);
213
+ const params = paramsType(operation);
214
+ const { parameters, call } = signature(ir, operation);
215
+ const fetchArgs = [...call.map((arg) => (arg === "parameters" ? "current" : arg)), "token"];
216
+ const property = (name ) => propertyName(page?.name ?? "", name);
217
+ const name = `${pascal(operation.name)}AllAsync`;
218
+ doc(writer, [`Every item of every ${pascal(operation.name)} page, fetched as you iterate: await foreach (var item in client.${name}(...)).`]);
219
+ if (operation.deprecated !== undefined) writer.line(`[Obsolete(${str(operation.deprecated)})]`);
220
+ if (paginate.kind === "link") {
221
+ // The Link header names the next page: the same operation is called at that URL ("" is the first page).
222
+ const raw = `RawResult<${resultType(ir, operation) ?? ""}>`;
223
+ writer.line(`public IAsyncEnumerable<${item}> ${name}(${[...parameters, "CancellationToken cancellationToken = default"].join(", ")}) =>`).indent((body) => {
224
+ body.line("Pages.Iterate(").indent((args) => {
225
+ args.line('"",');
226
+ args.line(`(url, token) => Raw.${methodName(operation)}(${[...call, 'url.Length == 0 ? null : new CallOptions { Url = url }', "token"].join(", ")}),`);
227
+ args.line(`(${raw} page) => page.Data${itemsField === undefined ? "" : `.${property(itemsField)}`},`);
228
+ args.line(`(url, page, items) => Links.Next(page.Response.Header("link")) is string next ? new Maybe<string>(next) : default,`);
229
+ args.line("cancellationToken);");
230
+ });
231
+ });
232
+ writer.line();
233
+ return;
234
+ }
235
+ writer.line(`public IAsyncEnumerable<${item}> ${name}(${[...parameters, "CancellationToken cancellationToken = default"].join(", ")}) =>`).indent((body) => {
236
+ body.line("Pages.Iterate(").indent((args) => {
237
+ args.line(`parameters ?? new ${params}(),`);
238
+ args.line(`(current, token) => ${methodName(operation)}(${fetchArgs.join(", ")}),`);
239
+ args.line(`page => page.${property(itemsField ?? "")},`);
240
+ args.line("(current, page, items) =>").line("{").indent((fn) => {
241
+ const param = `following.${pascal(paginate.param)}`;
242
+ if (paginate.kind === "cursor") {
243
+ const next = fields.find((entry) => entry.name === paginate.next);
244
+ if (next === undefined) throw new Error(`operations.${operation.name}.paginate: no field ${paginate.next}`);
245
+ const read = `page.${property(paginate.next)}`;
246
+ fn.line(`var cursor = ${next.optional && next.nullable ? `${read}.IsSet ? ${read}.Value : null` : read};`);
247
+ fn.line("if (string.IsNullOrEmpty(cursor))").line("{").indent((branch) => branch.line(`return default(Maybe<${params}>);`)).line("}").line();
248
+ fn.line("var following = current.Copy();");
249
+ fn.line(`${param} = cursor;`);
250
+ } else {
251
+ const limit = paginate.limit === undefined ? "" : ` || (current.${pascal(paginate.limit)}.HasValue && items.Count < current.${pascal(paginate.limit)}.Value)`;
252
+ fn.line(`if (items.Count == 0${limit})`).line("{").indent((branch) => branch.line(`return default(Maybe<${params}>);`)).line("}").line();
253
+ fn.line("var following = current.Copy();");
254
+ fn.line(`${param} = (current.${pascal(paginate.param)} ?? 0) + items.Count;`);
255
+ }
256
+ fn.line(`return new Maybe<${params}>(following);`);
257
+ });
258
+ args.line("},");
259
+ args.line("cancellationToken);");
260
+ });
261
+ });
262
+ writer.line();
263
+ }
264
+
265
+ function configEntry(ir , name ) {
266
+ const middleware = ir.middlewares.find((candidate) => candidate.name === name);
267
+ if (middleware === undefined) throw new Error(`unknown middleware ${name}`);
268
+ const config = middleware.config.map((item) => `P(${str(item.name)}, ${paramEncoder(ir, item, `settings.${pascal(item.name)}`)})`);
269
+ return `MiddlewareEntry.Of(${str(name)}, Middlewares.${csharpName(name)}, ${pairs(config)})`;
270
+ }
271
+
272
+ function emitOptions(writer , ir ) {
273
+ doc(writer, ["How a Client is configured; set what you need."]);
274
+ writer.line("public sealed class Options").line("{").indent((body) => {
275
+ configFields(ir).forEach((item) => {
276
+ doc(body, item.doc === undefined ? [] : [item.doc]);
277
+ const presence = { type: item.type, optional: item.optional, nullable: false };
278
+ body.line(`public ${fieldType(ir, presence)} ${pascal(item.name)} { get; set; }${initializer(ir, presence)}`).line();
279
+ });
280
+ const option = (lines , declaration ) => {
281
+ doc(body, lines);
282
+ body.line(declaration).line();
283
+ };
284
+ option(["A key of Client.Servers; Client.DefaultServer when null."], "public string? Server { get; set; }");
285
+ option(["Replaces the server's URL, e.g. for a proxy or a test server."], "public string? BaseUrl { get; set; }");
286
+ option(["Sends requests; one shared HttpClient (no redirects) when null. Wrap your own HttpClient in HttpClientTransport to change proxies or TLS."], "public ITransport? Transport { get; set; }");
287
+ option(["Limits each call; Client.DefaultTimeoutMs when null."], "public long? TimeoutMs { get; set; }");
288
+ option(["Runs after the SDK's own middleware, in order."], "public IReadOnlyList<MiddlewareEntry> Middleware { get; set; } = Array.Empty<MiddlewareEntry>();");
289
+ option(["Receives every event middleware emits."], "public EventListener? OnEvent { get; set; }");
290
+ doc(body, ["Measures time and waits; the system clock when null."]);
291
+ body.line("public IClock? Clock { get; set; }");
292
+ const versions = ir.apiVersions;
293
+ if (versions !== undefined) {
294
+ if (configFields(ir).some((item) => pascal(item.name) === "ApiVersion")) throw new Error("a middleware config field apiVersion collides with the ApiVersion client option of apiVersions");
295
+ body.line();
296
+ doc(body, [`The API version to use, sent as ${versions.header}: ${versions.values.join(", ")}. Defaults to ${versions.values.at(-1) ?? ""}, the newest.`]);
297
+ body.line("public string? ApiVersion { get; set; }");
298
+ }
299
+ });
300
+ writer.line("}").line();
301
+ }
302
+
303
+ function emitClient(writer , ir , sdk ) {
304
+ const opLevel = [...new Set(ir.operations.flatMap((operation) => operation.use))].sort();
305
+ doc(writer, [ir.doc ?? ir.name, "", "Thread-safe: build one and share it."]);
306
+ writer.line("public sealed class Client").line("{").indent((body) => {
307
+ body.line("private static readonly KeyValuePair<string, Json>[] None = Array.Empty<KeyValuePair<string, Json>>();").line();
308
+ doc(body, ["The API's servers by name."]);
309
+ body.line(`public static readonly IReadOnlyDictionary<string, string> Servers = new Dictionary<string, string> { ${ir.servers.map((server) => `[${str(server.name)}] = ${str(server.url)}`).join(", ")} };`).line();
310
+ doc(body, ["Used when Options.Server is null."]);
311
+ body.line(`public const string DefaultServer = ${str(ir.defaultServer)};`).line();
312
+ doc(body, ["Limits a call when Options.TimeoutMs is null."]);
313
+ body.line("public const long DefaultTimeoutMs = 60000;").line();
314
+ doc(body, ["Builds a client; throws SdkException for an unknown server."]);
315
+ body.line("public Client(Options? options = null)").line("{").indent((fn) => {
316
+ fn.line("var settings = options ?? new Options();");
317
+ fn.line("var server = settings.Server ?? DefaultServer;");
318
+ fn.line('var baseUrl = settings.BaseUrl ?? (Servers.TryGetValue(server, out var url) ? url : throw new SdkException("unknown server " + server));');
319
+ fn.line("var clock = settings.Clock ?? new SystemClock();");
320
+ fn.line("var runtime = new ClientRuntime(").indent((args) => {
321
+ args.line("baseUrl,");
322
+ args.line("settings.Transport ?? new HttpClientTransport(),");
323
+ args.line("clock,");
324
+ args.line(`new MiddlewareEntry[] {${ir.use.length === 0 ? "" : ` ${ir.use.map((name) => configEntry(ir, name)).join(", ")} `}},`);
325
+ args.line(`new Dictionary<string, MiddlewareEntry> {${opLevel.length === 0 ? "" : ` ${opLevel.map((name) => `[${str(name)}] = ${configEntry(ir, name)}`).join(", ")} `}},`);
326
+ args.line("settings.Middleware.ToArray(),");
327
+ args.line("settings.OnEvent == null ? Array.Empty<EventListener>() : new[] { settings.OnEvent },");
328
+ args.line("settings.TimeoutMs ?? DefaultTimeoutMs,");
329
+ const versions = ir.apiVersions;
330
+ if (versions === undefined) args.line(`new SdkInfo(${str(sdk.name)}, ${str(sdk.version)}, "csharp"));`);
331
+ else {
332
+ args.line(`new SdkInfo(${str(sdk.name)}, ${str(sdk.version)}, "csharp"),`);
333
+ args.line(`new ApiVersion(${str(versions.header)}, settings.ApiVersion ?? ${str(versions.values.at(-1) ?? "")}, new[] { ${versions.values.map(str).join(", ")} }));`);
334
+ }
335
+ });
336
+ fn.line("Raw = new RawClient(runtime);");
337
+ });
338
+ body.line("}").line();
339
+ doc(body, ["The operations that also return the response and take per-call options."]);
340
+ body.line("public RawClient Raw { get; }").line();
341
+ ir.operations.forEach((operation) => {
342
+ emitMethod(body, ir, operation);
343
+ if (operation.paginate !== undefined) emitIterator(body, ir, operation, operation.paginate);
344
+ if (operation.wait !== undefined) emitWait(body, ir, operation, operation.wait);
345
+ });
346
+ body.line("private static KeyValuePair<string, Json> P(string name, Json value) => new KeyValuePair<string, Json>(name, value);");
347
+ });
348
+ writer.line("}").line();
349
+
350
+ doc(writer, ["The same operations, returning the response too and taking per-call options."]);
351
+ writer.line("public sealed class RawClient").line("{").indent((body) => {
352
+ body.line("private static readonly KeyValuePair<string, Json>[] None = Array.Empty<KeyValuePair<string, Json>>();");
353
+ body.line("private readonly ClientRuntime _runtime;").line();
354
+ body.line("internal RawClient(ClientRuntime runtime)").line("{").indent((fn) => fn.line("_runtime = runtime;")).line("}").line();
355
+ body.line("internal IClock Clock => _runtime.Clock;").line();
356
+ ir.operations.forEach((operation) => emitSpec(body, operation));
357
+ ir.operations.forEach((operation) => emitRaw(body, ir, operation));
358
+ body.line("private static KeyValuePair<string, Json> P(string name, Json value) => new KeyValuePair<string, Json>(name, value);");
359
+ });
360
+ writer.line("}");
361
+ }
362
+
363
+ export function emitErrors(ir , header , namespace ) {
364
+ const declared = [...new Map(ir.operations.flatMap((operation) => operation.errors).map((error) => [error.name, error])).values()].sort((a, b) => (a.name < b.name ? -1 : 1));
365
+ const writer = csharp().line(header).line().line(`using ${namespace}.Runtime;`).line();
366
+ writer.line(`namespace ${namespace}`).line("{").indent((body) => {
367
+ declared.forEach((error, index) => {
368
+ if (index > 0) body.line();
369
+ doc(body, [`The declared ${error.name} error.`]);
370
+ body.line(`public sealed class ${exceptionName(error.name)} : ApiException`).line("{").indent((cls) => {
371
+ cls.line(`public ${exceptionName(error.name)}(int status, Json body, HttpResponse response)`).indent((base) => base.line(`: base(${str(error.name)}, status, body, response)`));
372
+ cls.line("{").line("}");
373
+ });
374
+ body.line("}");
375
+ });
376
+ });
377
+ writer.line("}");
378
+ return writer.toString();
379
+ }
380
+
381
+ /** Method names a Client already has. */
382
+ const TAKEN = ["Raw", "Servers", "DefaultServer", "DefaultTimeoutMs"];
383
+
384
+ export function emitClientFile(ir , header , namespace , sdk ) {
385
+ ir.operations.forEach((operation) => {
386
+ if (TAKEN.includes(pascal(operation.name))) throw new Error(`operation ${operation.name} would clash with the client's ${pascal(operation.name)} member; rename the operation`);
387
+ });
388
+ const writer = csharp().line(header).line("#pragma warning disable CS0618 // the client calls its own deprecated operations").line();
389
+ ["System", "System.Collections.Generic", "System.Linq", "System.Runtime.CompilerServices", "System.Threading", "System.Threading.Tasks", `${namespace}.Runtime`].forEach((name) => writer.line(`using ${name};`));
390
+ writer.line().line(`namespace ${namespace}`).line("{").indent((body) => {
391
+ emitOptions(body, ir);
392
+ ir.operations.forEach((operation) => emitParams(body, ir, operation));
393
+ emitClient(body, ir, sdk);
394
+ });
395
+ writer.line("}");
396
+ return writer.toString().replace(/\n\n(\s*)}\n/g, "\n$1}\n");
397
+ }