@azure-tools/typespec-ts 0.11.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 (353) hide show
  1. package/.eslintrc.json +23 -0
  2. package/.prettierignore +1 -0
  3. package/.prettierrc +7 -0
  4. package/.rush/temp/package-deps_build.json +303 -0
  5. package/.rush/temp/shrinkwrap-deps.json +469 -0
  6. package/CHANGELOG.md +65 -0
  7. package/CONTRIBUTING.md +134 -0
  8. package/README.md +150 -0
  9. package/dist/src/emitUtil.d.ts +6 -0
  10. package/dist/src/emitUtil.d.ts.map +1 -0
  11. package/dist/src/emitUtil.js +45 -0
  12. package/dist/src/emitUtil.js.map +1 -0
  13. package/dist/src/index.d.ts +3 -0
  14. package/dist/src/index.d.ts.map +1 -0
  15. package/dist/src/index.js +59 -0
  16. package/dist/src/index.js.map +1 -0
  17. package/dist/src/lib.d.ts +159 -0
  18. package/dist/src/lib.d.ts.map +1 -0
  19. package/dist/src/lib.js +189 -0
  20. package/dist/src/lib.js.map +1 -0
  21. package/dist/src/modelUtils.d.ts +12 -0
  22. package/dist/src/modelUtils.d.ts.map +1 -0
  23. package/dist/src/modelUtils.js +812 -0
  24. package/dist/src/modelUtils.js.map +1 -0
  25. package/dist/src/operationUtil.d.ts +16 -0
  26. package/dist/src/operationUtil.d.ts.map +1 -0
  27. package/dist/src/operationUtil.js +142 -0
  28. package/dist/src/operationUtil.js.map +1 -0
  29. package/dist/src/transform/transform.d.ts +8 -0
  30. package/dist/src/transform/transform.d.ts.map +1 -0
  31. package/dist/src/transform/transform.js +131 -0
  32. package/dist/src/transform/transform.js.map +1 -0
  33. package/dist/src/transform/transformAnnotationDetails.d.ts +6 -0
  34. package/dist/src/transform/transformAnnotationDetails.d.ts.map +1 -0
  35. package/dist/src/transform/transformAnnotationDetails.js +188 -0
  36. package/dist/src/transform/transformAnnotationDetails.js.map +1 -0
  37. package/dist/src/transform/transformApiVersionParam.d.ts +5 -0
  38. package/dist/src/transform/transformApiVersionParam.d.ts.map +1 -0
  39. package/dist/src/transform/transformApiVersionParam.js +76 -0
  40. package/dist/src/transform/transformApiVersionParam.js.map +1 -0
  41. package/dist/src/transform/transformParameters.d.ts +14 -0
  42. package/dist/src/transform/transformParameters.d.ts.map +1 -0
  43. package/dist/src/transform/transformParameters.js +315 -0
  44. package/dist/src/transform/transformParameters.js.map +1 -0
  45. package/dist/src/transform/transformPaths.d.ts +10 -0
  46. package/dist/src/transform/transformPaths.d.ts.map +1 -0
  47. package/dist/src/transform/transformPaths.js +124 -0
  48. package/dist/src/transform/transformPaths.js.map +1 -0
  49. package/dist/src/transform/transformResponses.d.ts +5 -0
  50. package/dist/src/transform/transformResponses.d.ts.map +1 -0
  51. package/dist/src/transform/transformResponses.js +136 -0
  52. package/dist/src/transform/transformResponses.js.map +1 -0
  53. package/dist/src/transform/transformSchemas.d.ts +4 -0
  54. package/dist/src/transform/transformSchemas.d.ts.map +1 -0
  55. package/dist/src/transform/transformSchemas.js +175 -0
  56. package/dist/src/transform/transformSchemas.js.map +1 -0
  57. package/dist/src/transform/transfromRLCOptions.d.ts +10 -0
  58. package/dist/src/transform/transfromRLCOptions.d.ts.map +1 -0
  59. package/dist/src/transform/transfromRLCOptions.js +156 -0
  60. package/dist/src/transform/transfromRLCOptions.js.map +1 -0
  61. package/dist/tsconfig.tsbuildinfo +1 -0
  62. package/package.json +73 -0
  63. package/src/emitUtil.ts +63 -0
  64. package/src/index.ts +106 -0
  65. package/src/lib.ts +207 -0
  66. package/src/modelUtils.ts +961 -0
  67. package/src/operationUtil.ts +203 -0
  68. package/src/transform/transform.ts +224 -0
  69. package/src/transform/transformAnnotationDetails.ts +231 -0
  70. package/src/transform/transformApiVersionParam.ts +104 -0
  71. package/src/transform/transformParameters.ts +480 -0
  72. package/src/transform/transformPaths.ts +187 -0
  73. package/src/transform/transformResponses.ts +190 -0
  74. package/src/transform/transformSchemas.ts +209 -0
  75. package/src/transform/transfromRLCOptions.ts +202 -0
  76. package/test/commands/cadl-ranch-list.ts +83 -0
  77. package/test/commands/check-clean-tree.ts +55 -0
  78. package/test/commands/childProcessOnExit.ts +16 -0
  79. package/test/commands/gen-cadl-ranch.ts +21 -0
  80. package/test/commands/run.ts +41 -0
  81. package/test/integration/arrayItemTypes.spec.ts +93 -0
  82. package/test/integration/authApiKey.spec.ts +38 -0
  83. package/test/integration/authOauth2.spec.ts +65 -0
  84. package/test/integration/authUnion.spec.ts +72 -0
  85. package/test/integration/dictionary.spec.ts +102 -0
  86. package/test/integration/extensibleEnums.spec.ts +61 -0
  87. package/test/integration/fixedEnums.spec.ts +52 -0
  88. package/test/integration/generated/arrays/itemTypes/src/arrayItemTypesClient.ts +31 -0
  89. package/test/integration/generated/arrays/itemTypes/src/clientDefinitions.ts +150 -0
  90. package/test/integration/generated/arrays/itemTypes/src/index.ts +13 -0
  91. package/test/integration/generated/arrays/itemTypes/src/models.ts +9 -0
  92. package/test/integration/generated/arrays/itemTypes/src/outputModels.ts +9 -0
  93. package/test/integration/generated/arrays/itemTypes/src/parameters.ts +78 -0
  94. package/test/integration/generated/arrays/itemTypes/src/responses.ts +104 -0
  95. package/test/integration/generated/arrays/itemTypes/tspconfig.yaml +12 -0
  96. package/test/integration/generated/authentication/apiKey/.eslintrc.json +11 -0
  97. package/test/integration/generated/authentication/apiKey/README.md +56 -0
  98. package/test/integration/generated/authentication/apiKey/api-extractor.json +18 -0
  99. package/test/integration/generated/authentication/apiKey/karma.conf.js +133 -0
  100. package/test/integration/generated/authentication/apiKey/package.json +103 -0
  101. package/test/integration/generated/authentication/apiKey/rollup.config.js +118 -0
  102. package/test/integration/generated/authentication/apiKey/src/authApiKeyClient.ts +41 -0
  103. package/test/integration/generated/authentication/apiKey/src/clientDefinitions.ts +33 -0
  104. package/test/integration/generated/authentication/apiKey/src/index.ts +12 -0
  105. package/test/integration/generated/authentication/apiKey/src/outputModels.ts +6 -0
  106. package/test/integration/generated/authentication/apiKey/src/parameters.ts +7 -0
  107. package/test/integration/generated/authentication/apiKey/src/responses.ts +21 -0
  108. package/test/integration/generated/authentication/apiKey/test/public/sampleTest.spec.ts +23 -0
  109. package/test/integration/generated/authentication/apiKey/test/public/utils/env.browser.ts +2 -0
  110. package/test/integration/generated/authentication/apiKey/test/public/utils/env.ts +6 -0
  111. package/test/integration/generated/authentication/apiKey/test/public/utils/recordedClient.ts +29 -0
  112. package/test/integration/generated/authentication/apiKey/tsconfig.json +25 -0
  113. package/test/integration/generated/authentication/apiKey/tspconfig.yaml +10 -0
  114. package/test/integration/generated/authentication/oauth2/karma.conf.js +133 -0
  115. package/test/integration/generated/authentication/oauth2/src/authOauth2Client.ts +41 -0
  116. package/test/integration/generated/authentication/oauth2/src/clientDefinitions.ts +33 -0
  117. package/test/integration/generated/authentication/oauth2/src/index.ts +12 -0
  118. package/test/integration/generated/authentication/oauth2/src/outputModels.ts +6 -0
  119. package/test/integration/generated/authentication/oauth2/src/parameters.ts +7 -0
  120. package/test/integration/generated/authentication/oauth2/src/responses.ts +21 -0
  121. package/test/integration/generated/authentication/oauth2/test/public/sampleTest.spec.ts +23 -0
  122. package/test/integration/generated/authentication/oauth2/test/public/utils/env.browser.ts +2 -0
  123. package/test/integration/generated/authentication/oauth2/test/public/utils/env.ts +6 -0
  124. package/test/integration/generated/authentication/oauth2/test/public/utils/recordedClient.ts +29 -0
  125. package/test/integration/generated/authentication/oauth2/tspconfig.yaml +12 -0
  126. package/test/integration/generated/authentication/union/karma.conf.js +133 -0
  127. package/test/integration/generated/authentication/union/src/authUnionClient.ts +42 -0
  128. package/test/integration/generated/authentication/union/src/clientDefinitions.ts +27 -0
  129. package/test/integration/generated/authentication/union/src/index.ts +11 -0
  130. package/test/integration/generated/authentication/union/src/parameters.ts +7 -0
  131. package/test/integration/generated/authentication/union/src/responses.ts +14 -0
  132. package/test/integration/generated/authentication/union/test/public/sampleTest.spec.ts +23 -0
  133. package/test/integration/generated/authentication/union/test/public/utils/env.browser.ts +2 -0
  134. package/test/integration/generated/authentication/union/test/public/utils/env.ts +6 -0
  135. package/test/integration/generated/authentication/union/test/public/utils/recordedClient.ts +29 -0
  136. package/test/integration/generated/authentication/union/tspconfig.yaml +12 -0
  137. package/test/integration/generated/dictionary/.eslintrc.json +11 -0
  138. package/test/integration/generated/dictionary/README.md +56 -0
  139. package/test/integration/generated/dictionary/api-extractor.json +18 -0
  140. package/test/integration/generated/dictionary/package.json +80 -0
  141. package/test/integration/generated/dictionary/rollup.config.js +118 -0
  142. package/test/integration/generated/dictionary/src/clientDefinitions.ts +165 -0
  143. package/test/integration/generated/dictionary/src/dictClient.ts +29 -0
  144. package/test/integration/generated/dictionary/src/index.ts +13 -0
  145. package/test/integration/generated/dictionary/src/models.ts +9 -0
  146. package/test/integration/generated/dictionary/src/outputModels.ts +9 -0
  147. package/test/integration/generated/dictionary/src/parameters.ts +86 -0
  148. package/test/integration/generated/dictionary/src/responses.ts +115 -0
  149. package/test/integration/generated/dictionary/tsconfig.json +25 -0
  150. package/test/integration/generated/dictionary/tspconfig.yaml +12 -0
  151. package/test/integration/generated/enums/extensible/.eslintrc.json +11 -0
  152. package/test/integration/generated/enums/extensible/README.md +56 -0
  153. package/test/integration/generated/enums/extensible/api-extractor.json +18 -0
  154. package/test/integration/generated/enums/extensible/package.json +80 -0
  155. package/test/integration/generated/enums/extensible/rollup.config.js +118 -0
  156. package/test/integration/generated/enums/extensible/src/clientDefinitions.ts +45 -0
  157. package/test/integration/generated/enums/extensible/src/enumsExtensible.ts +31 -0
  158. package/test/integration/generated/enums/extensible/src/index.ts +11 -0
  159. package/test/integration/generated/enums/extensible/src/parameters.ts +23 -0
  160. package/test/integration/generated/enums/extensible/src/responses.ts +26 -0
  161. package/test/integration/generated/enums/extensible/tsconfig.json +25 -0
  162. package/test/integration/generated/enums/extensible/tspconfig.yaml +11 -0
  163. package/test/integration/generated/enums/fixed/.eslintrc.json +11 -0
  164. package/test/integration/generated/enums/fixed/README.md +56 -0
  165. package/test/integration/generated/enums/fixed/api-extractor.json +18 -0
  166. package/test/integration/generated/enums/fixed/package.json +80 -0
  167. package/test/integration/generated/enums/fixed/rollup.config.js +118 -0
  168. package/test/integration/generated/enums/fixed/src/clientDefinitions.ts +43 -0
  169. package/test/integration/generated/enums/fixed/src/enumsFixed.ts +31 -0
  170. package/test/integration/generated/enums/fixed/src/index.ts +11 -0
  171. package/test/integration/generated/enums/fixed/src/parameters.ts +36 -0
  172. package/test/integration/generated/enums/fixed/src/responses.ts +27 -0
  173. package/test/integration/generated/enums/fixed/tsconfig.json +25 -0
  174. package/test/integration/generated/enums/fixed/tspconfig.yaml +11 -0
  175. package/test/integration/generated/hello/.eslintrc.json +11 -0
  176. package/test/integration/generated/hello/README.md +56 -0
  177. package/test/integration/generated/hello/api-extractor.json +18 -0
  178. package/test/integration/generated/hello/package.json +80 -0
  179. package/test/integration/generated/hello/rollup.config.js +118 -0
  180. package/test/integration/generated/hello/src/clientDefinitions.ts +19 -0
  181. package/test/integration/generated/hello/src/hello.ts +29 -0
  182. package/test/integration/generated/hello/src/index.ts +11 -0
  183. package/test/integration/generated/hello/src/parameters.ts +6 -0
  184. package/test/integration/generated/hello/src/responses.ts +10 -0
  185. package/test/integration/generated/hello/tsconfig.json +25 -0
  186. package/test/integration/generated/hello/tspconfig.yaml +11 -0
  187. package/test/integration/generated/lro/lroBasic/.eslintrc.json +11 -0
  188. package/test/integration/generated/lro/lroBasic/README.md +56 -0
  189. package/test/integration/generated/lro/lroBasic/api-extractor.json +18 -0
  190. package/test/integration/generated/lro/lroBasic/package.json +82 -0
  191. package/test/integration/generated/lro/lroBasic/rollup.config.js +118 -0
  192. package/test/integration/generated/lro/lroBasic/src/azureLro.ts +31 -0
  193. package/test/integration/generated/lro/lroBasic/src/clientDefinitions.ts +37 -0
  194. package/test/integration/generated/lro/lroBasic/src/index.ts +12 -0
  195. package/test/integration/generated/lro/lroBasic/src/parameters.ts +8 -0
  196. package/test/integration/generated/lro/lroBasic/src/pollingHelper.ts +75 -0
  197. package/test/integration/generated/lro/lroBasic/src/responses.ts +22 -0
  198. package/test/integration/generated/lro/lroBasic/tsconfig.json +25 -0
  199. package/test/integration/generated/lro/lroBasic/tspconfig.yaml +11 -0
  200. package/test/integration/generated/models/inheritance/.eslintrc.json +11 -0
  201. package/test/integration/generated/models/inheritance/README.md +56 -0
  202. package/test/integration/generated/models/inheritance/api-extractor.json +18 -0
  203. package/test/integration/generated/models/inheritance/package.json +80 -0
  204. package/test/integration/generated/models/inheritance/rollup.config.js +118 -0
  205. package/test/integration/generated/models/inheritance/src/clientDefinitions.ts +79 -0
  206. package/test/integration/generated/models/inheritance/src/index.ts +13 -0
  207. package/test/integration/generated/models/inheritance/src/models.ts +52 -0
  208. package/test/integration/generated/models/inheritance/src/modelsInheritance.ts +31 -0
  209. package/test/integration/generated/models/inheritance/src/outputModels.ts +52 -0
  210. package/test/integration/generated/models/inheritance/src/parameters.ts +35 -0
  211. package/test/integration/generated/models/inheritance/src/responses.ts +56 -0
  212. package/test/integration/generated/models/inheritance/tsconfig.json +25 -0
  213. package/test/integration/generated/models/inheritance/tspconfig.yaml +11 -0
  214. package/test/integration/generated/models/propertyOptional/.eslintrc.json +11 -0
  215. package/test/integration/generated/models/propertyOptional/README.md +56 -0
  216. package/test/integration/generated/models/propertyOptional/api-extractor.json +18 -0
  217. package/test/integration/generated/models/propertyOptional/package.json +80 -0
  218. package/test/integration/generated/models/propertyOptional/rollup.config.js +118 -0
  219. package/test/integration/generated/models/propertyOptional/src/clientDefinitions.ts +263 -0
  220. package/test/integration/generated/models/propertyOptional/src/index.ts +13 -0
  221. package/test/integration/generated/models/propertyOptional/src/models.ts +46 -0
  222. package/test/integration/generated/models/propertyOptional/src/modelsPropertyOptional.ts +31 -0
  223. package/test/integration/generated/models/propertyOptional/src/outputModels.ts +46 -0
  224. package/test/integration/generated/models/propertyOptional/src/parameters.ts +124 -0
  225. package/test/integration/generated/models/propertyOptional/src/responses.ts +169 -0
  226. package/test/integration/generated/models/propertyOptional/tsconfig.json +25 -0
  227. package/test/integration/generated/models/propertyOptional/tspconfig.yaml +11 -0
  228. package/test/integration/generated/models/propertyTypes/.eslintrc.json +11 -0
  229. package/test/integration/generated/models/propertyTypes/README.md +56 -0
  230. package/test/integration/generated/models/propertyTypes/api-extractor.json +18 -0
  231. package/test/integration/generated/models/propertyTypes/package.json +80 -0
  232. package/test/integration/generated/models/propertyTypes/rollup.config.js +118 -0
  233. package/test/integration/generated/models/propertyTypes/src/clientDefinitions.ts +234 -0
  234. package/test/integration/generated/models/propertyTypes/src/index.ts +13 -0
  235. package/test/integration/generated/models/propertyTypes/src/models.ts +99 -0
  236. package/test/integration/generated/models/propertyTypes/src/modelsPropertyTypes.ts +31 -0
  237. package/test/integration/generated/models/propertyTypes/src/outputModels.ts +99 -0
  238. package/test/integration/generated/models/propertyTypes/src/parameters.ts +147 -0
  239. package/test/integration/generated/models/propertyTypes/src/responses.ts +186 -0
  240. package/test/integration/generated/models/propertyTypes/tsconfig.json +25 -0
  241. package/test/integration/generated/models/propertyTypes/tspconfig.yaml +11 -0
  242. package/test/integration/generated/models/usage/.eslintrc.json +11 -0
  243. package/test/integration/generated/models/usage/README.md +56 -0
  244. package/test/integration/generated/models/usage/api-extractor.json +18 -0
  245. package/test/integration/generated/models/usage/package.json +80 -0
  246. package/test/integration/generated/models/usage/rollup.config.js +118 -0
  247. package/test/integration/generated/models/usage/src/clientDefinitions.ts +41 -0
  248. package/test/integration/generated/models/usage/src/index.ts +13 -0
  249. package/test/integration/generated/models/usage/src/models.ts +12 -0
  250. package/test/integration/generated/models/usage/src/modelsUsage.ts +31 -0
  251. package/test/integration/generated/models/usage/src/outputModels.ts +12 -0
  252. package/test/integration/generated/models/usage/src/parameters.ts +19 -0
  253. package/test/integration/generated/models/usage/src/responses.ts +22 -0
  254. package/test/integration/generated/models/usage/tsconfig.json +25 -0
  255. package/test/integration/generated/models/usage/tspconfig.yaml +11 -0
  256. package/test/integration/generated/models/visibility/.eslintrc.json +11 -0
  257. package/test/integration/generated/models/visibility/README.md +56 -0
  258. package/test/integration/generated/models/visibility/api-extractor.json +18 -0
  259. package/test/integration/generated/models/visibility/package.json +80 -0
  260. package/test/integration/generated/models/visibility/rollup.config.js +118 -0
  261. package/test/integration/generated/models/visibility/src/clientDefinitions.ts +40 -0
  262. package/test/integration/generated/models/visibility/src/index.ts +13 -0
  263. package/test/integration/generated/models/visibility/src/models.ts +14 -0
  264. package/test/integration/generated/models/visibility/src/modelsVisibilityAutomatic.ts +31 -0
  265. package/test/integration/generated/models/visibility/src/outputModels.ts +16 -0
  266. package/test/integration/generated/models/visibility/src/parameters.ts +41 -0
  267. package/test/integration/generated/models/visibility/src/responses.ts +36 -0
  268. package/test/integration/generated/models/visibility/tsconfig.json +25 -0
  269. package/test/integration/generated/models/visibility/tspconfig.yaml +11 -0
  270. package/test/integration/generated/resiliency/devDriven/.eslintrc.json +11 -0
  271. package/test/integration/generated/resiliency/devDriven/README.md +56 -0
  272. package/test/integration/generated/resiliency/devDriven/api-extractor.json +18 -0
  273. package/test/integration/generated/resiliency/devDriven/package.json +81 -0
  274. package/test/integration/generated/resiliency/devDriven/rollup.config.js +118 -0
  275. package/test/integration/generated/resiliency/devDriven/src/clientDefinitions.ts +75 -0
  276. package/test/integration/generated/resiliency/devDriven/src/index.ts +15 -0
  277. package/test/integration/generated/resiliency/devDriven/src/isUnexpected.ts +117 -0
  278. package/test/integration/generated/resiliency/devDriven/src/models.ts +8 -0
  279. package/test/integration/generated/resiliency/devDriven/src/outputModels.ts +25 -0
  280. package/test/integration/generated/resiliency/devDriven/src/paginateHelper.ts +154 -0
  281. package/test/integration/generated/resiliency/devDriven/src/parameters.ts +17 -0
  282. package/test/integration/generated/resiliency/devDriven/src/resiliencyDevDriven.ts +31 -0
  283. package/test/integration/generated/resiliency/devDriven/src/responses.ts +62 -0
  284. package/test/integration/generated/resiliency/devDriven/tsconfig.json +25 -0
  285. package/test/integration/generated/resiliency/devDriven/tspconfig.yaml +11 -0
  286. package/test/integration/generated/resiliency/srvDriven1/.eslintrc.json +11 -0
  287. package/test/integration/generated/resiliency/srvDriven1/README.md +56 -0
  288. package/test/integration/generated/resiliency/srvDriven1/api-extractor.json +18 -0
  289. package/test/integration/generated/resiliency/srvDriven1/package.json +80 -0
  290. package/test/integration/generated/resiliency/srvDriven1/rollup.config.js +118 -0
  291. package/test/integration/generated/resiliency/srvDriven1/src/clientDefinitions.ts +70 -0
  292. package/test/integration/generated/resiliency/srvDriven1/src/index.ts +13 -0
  293. package/test/integration/generated/resiliency/srvDriven1/src/models.ts +6 -0
  294. package/test/integration/generated/resiliency/srvDriven1/src/outputModels.ts +6 -0
  295. package/test/integration/generated/resiliency/srvDriven1/src/parameters.ts +51 -0
  296. package/test/integration/generated/resiliency/srvDriven1/src/resiliencyServiceDriven1.ts +31 -0
  297. package/test/integration/generated/resiliency/srvDriven1/src/responses.ts +34 -0
  298. package/test/integration/generated/resiliency/srvDriven1/tsconfig.json +25 -0
  299. package/test/integration/generated/resiliency/srvDriven1/tspconfig.yaml +11 -0
  300. package/test/integration/generated/resiliency/srvDriven2/.eslintrc.json +11 -0
  301. package/test/integration/generated/resiliency/srvDriven2/README.md +56 -0
  302. package/test/integration/generated/resiliency/srvDriven2/api-extractor.json +18 -0
  303. package/test/integration/generated/resiliency/srvDriven2/package.json +80 -0
  304. package/test/integration/generated/resiliency/srvDriven2/rollup.config.js +118 -0
  305. package/test/integration/generated/resiliency/srvDriven2/src/clientDefinitions.ts +93 -0
  306. package/test/integration/generated/resiliency/srvDriven2/src/index.ts +13 -0
  307. package/test/integration/generated/resiliency/srvDriven2/src/models.ts +6 -0
  308. package/test/integration/generated/resiliency/srvDriven2/src/outputModels.ts +6 -0
  309. package/test/integration/generated/resiliency/srvDriven2/src/parameters.ts +68 -0
  310. package/test/integration/generated/resiliency/srvDriven2/src/resiliencyServiceDriven2.ts +31 -0
  311. package/test/integration/generated/resiliency/srvDriven2/src/responses.ts +45 -0
  312. package/test/integration/generated/resiliency/srvDriven2/tsconfig.json +25 -0
  313. package/test/integration/generated/resiliency/srvDriven2/tspconfig.yaml +11 -0
  314. package/test/integration/generated/specialWords/src/clientDefinitions.ts +61 -0
  315. package/test/integration/generated/specialWords/src/index.ts +13 -0
  316. package/test/integration/generated/specialWords/src/models.ts +17 -0
  317. package/test/integration/generated/specialWords/src/outputModels.ts +17 -0
  318. package/test/integration/generated/specialWords/src/parameters.ts +37 -0
  319. package/test/integration/generated/specialWords/src/responses.ts +31 -0
  320. package/test/integration/generated/specialWords/src/specialWordsClient.ts +31 -0
  321. package/test/integration/generated/specialWords/tspconfig.yaml +11 -0
  322. package/test/integration/generated/unions/src/clientDefinitions.ts +53 -0
  323. package/test/integration/generated/unions/src/index.ts +12 -0
  324. package/test/integration/generated/unions/src/models.ts +27 -0
  325. package/test/integration/generated/unions/src/parameters.ts +34 -0
  326. package/test/integration/generated/unions/src/responses.ts +24 -0
  327. package/test/integration/generated/unions/src/unionsClient.ts +31 -0
  328. package/test/integration/generated/unions/tspconfig.yaml +11 -0
  329. package/test/integration/helloWorld.spec.ts +20 -0
  330. package/test/integration/lroBasic.spec.ts +25 -0
  331. package/test/integration/modelInheritance.spec.ts +192 -0
  332. package/test/integration/modelPropertyOptional.spec.ts +362 -0
  333. package/test/integration/modelPropertyType.spec.ts +119 -0
  334. package/test/integration/modelUsage.spec.ts +47 -0
  335. package/test/integration/resiliencyDpgCustomization.spec.ts +52 -0
  336. package/test/integration/specialWords.spec.ts +73 -0
  337. package/test/integration/unions.spec.ts +69 -0
  338. package/test/tsconfig.json +4 -0
  339. package/test/unit/azureCoreOperationsGenerator.spec.ts +71 -0
  340. package/test/unit/clientDefinitionGenerator.spec.ts +73 -0
  341. package/test/unit/clientFactoryGenerator.spec.ts +507 -0
  342. package/test/unit/docsGenerator.spec.ts +201 -0
  343. package/test/unit/modelsGenerator.spec.ts +1319 -0
  344. package/test/unit/parametersGenerator.spec.ts +561 -0
  345. package/test/unit/responsesGenerator.spec.ts +462 -0
  346. package/test/unit/spreadAndAlias.spec.ts +200 -0
  347. package/test/unit/util/emitUtil.ts +156 -0
  348. package/test/unit/util/testUtil.ts +83 -0
  349. package/test/util/customBearerTokenTestingPolicy.ts +78 -0
  350. package/test/util/matrix.ts +48 -0
  351. package/tsconfig.json +36 -0
  352. package/tsconfig.test.json +7 -0
  353. package/typespec-ts.build.log +2 -0
@@ -0,0 +1,812 @@
1
+ // Copyright (c) Microsoft Corporation.
2
+ // Licensed under the MIT License.
3
+ import { getDiscriminator, getDoc, getEffectiveModelType, getFormat, getFriendlyName, getMaxLength, getMaxValue, getMinLength, getMinValue, getPattern, getProperty, getPropertyType, getSummary, getVisibility, isNeverType, isNumericType, isSecret, isStringType, isTemplateDeclaration, isUnknownType, isNullType, getProjectedName } from "@typespec/compiler";
4
+ import { reportDiagnostic } from "./lib.js";
5
+ import { NameType, normalizeName, SchemaContext } from "@azure-tools/rlc-common";
6
+ import { getResourceOperation } from "@typespec/rest";
7
+ import { getHeaderFieldName, getPathParamName, getQueryParamName, isStatusCode } from "@typespec/http";
8
+ import { getPagedResult, isFixed } from "@azure-tools/typespec-azure-core";
9
+ import { extractPagedMetadataNested } from "./operationUtil.js";
10
+ export function getBinaryType(usage) {
11
+ return usage.includes(SchemaContext.Output)
12
+ ? "Uint8Array"
13
+ : "string | Uint8Array | ReadableStream<Uint8Array> | NodeJS.ReadableStream";
14
+ }
15
+ export function getSchemaForType(program, typeInput, usage, needRef) {
16
+ const type = getEffectiveModelFromType(program, typeInput);
17
+ const builtinType = mapCadlTypeToTypeScript(program, type, usage);
18
+ if (builtinType !== undefined) {
19
+ // add in description elements for types derived from primitive types (SecureString, etc.)
20
+ const doc = getDoc(program, type);
21
+ if (doc) {
22
+ builtinType.description = doc;
23
+ }
24
+ return builtinType;
25
+ }
26
+ if (type.kind === "Model") {
27
+ const schema = getSchemaForModel(program, type, usage, needRef);
28
+ if (usage && usage.includes(SchemaContext.Output)) {
29
+ schema.outputTypeName = `${schema.name}Output`;
30
+ schema.typeName = `${schema.name}`;
31
+ }
32
+ schema.usage = usage;
33
+ return schema;
34
+ }
35
+ else if (type.kind === "Union") {
36
+ return getSchemaForUnion(program, type, usage);
37
+ }
38
+ else if (type.kind === "UnionVariant") {
39
+ return getSchemaForUnionVariant(program, type, usage);
40
+ }
41
+ else if (type.kind === "Enum") {
42
+ return getSchemaForEnum(program, type);
43
+ }
44
+ else if (type.kind === "Scalar") {
45
+ return getSchemaForScalar(program, type);
46
+ }
47
+ if (isUnknownType(type)) {
48
+ const returnType = { type: "unknown" };
49
+ if (usage && usage.includes(SchemaContext.Output)) {
50
+ returnType.outputTypeName = "any";
51
+ returnType.typeName = "unknown";
52
+ }
53
+ return returnType;
54
+ }
55
+ if (isNeverType(type)) {
56
+ return { type: "never" };
57
+ }
58
+ if (isNullType(type)) {
59
+ return { type: "null" };
60
+ }
61
+ reportDiagnostic(program, {
62
+ code: "invalid-schema",
63
+ format: { type: type.kind },
64
+ target: type
65
+ });
66
+ return undefined;
67
+ }
68
+ export function getEffectiveModelFromType(program, type) {
69
+ if (type.kind === "Model") {
70
+ const effective = getEffectiveModelType(program, type, isSchemaProperty);
71
+ if (effective.name) {
72
+ return effective;
73
+ }
74
+ }
75
+ function isSchemaProperty(property) {
76
+ const headerInfo = getHeaderFieldName(program, property);
77
+ const queryInfo = getQueryParamName(program, property);
78
+ const pathInfo = getPathParamName(program, property);
79
+ const statusCodeInfo = isStatusCode(program, property);
80
+ return !(headerInfo || queryInfo || pathInfo || statusCodeInfo);
81
+ }
82
+ return type;
83
+ }
84
+ export function includeDerivedModel(model) {
85
+ var _a;
86
+ return (!isTemplateDeclaration(model) &&
87
+ (model.templateArguments === undefined ||
88
+ ((_a = model.templateArguments) === null || _a === void 0 ? void 0 : _a.length) === 0 ||
89
+ model.derivedModels.length > 0));
90
+ }
91
+ function getSchemaForScalar(program, scalar) {
92
+ let result = getSchemaForStdScalar(program, scalar);
93
+ if (!result && scalar.baseScalar) {
94
+ result = getSchemaForScalar(program, scalar.baseScalar);
95
+ }
96
+ return applyIntrinsicDecorators(program, scalar, result);
97
+ }
98
+ function getSchemaForUnion(program, union, usage) {
99
+ var _a;
100
+ const variants = Array.from(union.variants.values());
101
+ const values = [];
102
+ for (const variant of variants) {
103
+ // We already know it's not a model type
104
+ values.push(getSchemaForType(program, variant.type, usage));
105
+ }
106
+ const schema = {};
107
+ if (values.length > 0) {
108
+ schema.enum = values;
109
+ const unionAlias = values
110
+ .map((item) => { var _a; return `${(_a = getTypeName(item, [SchemaContext.Input])) !== null && _a !== void 0 ? _a : item}`; })
111
+ .join(" | ");
112
+ const outputUnionAlias = values
113
+ .map((item) => { var _a; return `${(_a = getTypeName(item, [SchemaContext.Output])) !== null && _a !== void 0 ? _a : item}`; })
114
+ .join(" | ");
115
+ if (!union.expression) {
116
+ schema.name = union.name;
117
+ schema.type = "object";
118
+ schema.typeName = union.name;
119
+ schema.outputTypeName = union.name + "Output";
120
+ schema.alias = unionAlias;
121
+ schema.outputAlias = outputUnionAlias;
122
+ }
123
+ else if (union.expression && !union.name) {
124
+ schema.type = unionAlias;
125
+ schema.outputTypeName = outputUnionAlias;
126
+ }
127
+ else {
128
+ schema.type = (_a = union.name) !== null && _a !== void 0 ? _a : unionAlias;
129
+ }
130
+ }
131
+ return schema;
132
+ }
133
+ function getSchemaForUnionVariant(program, variant, usage) {
134
+ return getSchemaForType(program, variant, usage);
135
+ }
136
+ // An openapi "string" can be defined in several different ways in Cadl
137
+ function isOasString(type) {
138
+ if (type.kind === "String") {
139
+ // A string literal
140
+ return true;
141
+ }
142
+ else if (type.kind === "Model" && type.name === "string") {
143
+ // string type
144
+ return true;
145
+ }
146
+ else if (type.kind === "Union") {
147
+ // A union where all variants are an OasString
148
+ return type.options.every((o) => isOasString(o));
149
+ }
150
+ return false;
151
+ }
152
+ function isStringLiteral(type) {
153
+ return (type.kind === "String" ||
154
+ (type.kind === "Union" && type.options.every((o) => o.kind === "String")));
155
+ }
156
+ // Return any string literal values for type
157
+ function getStringValues(type) {
158
+ if (type.kind === "String") {
159
+ return [type.value];
160
+ }
161
+ else if (type.kind === "Union") {
162
+ return type.options.flatMap(getStringValues).filter((v) => v);
163
+ }
164
+ return [];
165
+ }
166
+ function validateDiscriminator(program, discriminator, derivedModels) {
167
+ var _a;
168
+ const { propertyName } = discriminator;
169
+ const retVals = derivedModels.map((t) => {
170
+ const prop = getProperty(t, propertyName);
171
+ if (!prop) {
172
+ reportDiagnostic(program, {
173
+ code: "discriminator",
174
+ messageId: "missing",
175
+ target: t
176
+ });
177
+ return false;
178
+ }
179
+ let retval = true;
180
+ if (!isOasString(prop.type)) {
181
+ reportDiagnostic(program, {
182
+ code: "discriminator",
183
+ messageId: "type",
184
+ target: prop
185
+ });
186
+ retval = false;
187
+ }
188
+ if (prop.optional) {
189
+ reportDiagnostic(program, {
190
+ code: "discriminator",
191
+ messageId: "required",
192
+ target: prop
193
+ });
194
+ retval = false;
195
+ }
196
+ return retval;
197
+ });
198
+ // Map of discriminator value to the model in which it is declared
199
+ const discriminatorValues = new Map();
200
+ for (const t of derivedModels) {
201
+ // Get the discriminator property directly in the child model
202
+ const prop = (_a = t.properties) === null || _a === void 0 ? void 0 : _a.get(propertyName);
203
+ // Issue warning diagnostic if discriminator property missing or is not a string literal
204
+ if (!prop || !isStringLiteral(prop.type)) {
205
+ reportDiagnostic(program, {
206
+ code: "discriminator-value",
207
+ messageId: "literal",
208
+ target: prop || t
209
+ });
210
+ }
211
+ if (prop) {
212
+ const vals = getStringValues(prop.type);
213
+ vals.forEach((val) => {
214
+ if (discriminatorValues.has(val)) {
215
+ reportDiagnostic(program, {
216
+ code: "discriminator",
217
+ messageId: "duplicate",
218
+ format: {
219
+ val: val,
220
+ model1: discriminatorValues.get(val),
221
+ model2: t.name
222
+ },
223
+ target: prop
224
+ });
225
+ retVals.push(false);
226
+ }
227
+ else {
228
+ discriminatorValues.set(val, t.name);
229
+ }
230
+ });
231
+ }
232
+ }
233
+ return retVals.every((v) => v);
234
+ }
235
+ /**
236
+ * A "schema property" here is a property that is emitted to OpenAPI schema.
237
+ *
238
+ * Headers, parameters, status codes are not schema properties even they are
239
+ * represented as properties in Cadl.
240
+ */
241
+ function isSchemaProperty(program, property) {
242
+ const headerInfo = getHeaderFieldName(program, property);
243
+ const queryInfo = getQueryParamName(program, property);
244
+ const pathInfo = getPathParamName(program, property);
245
+ const statusCodeinfo = isStatusCode(program, property);
246
+ return !(headerInfo || queryInfo || pathInfo || statusCodeinfo);
247
+ }
248
+ // function getDefaultValue(program: Program, type: Type): any {
249
+ // switch (type.kind) {
250
+ // case "String":
251
+ // return type.value;
252
+ // case "Number":
253
+ // return type.value;
254
+ // case "Boolean":
255
+ // return type.value;
256
+ // case "Tuple":
257
+ // return type.values.map(item => getDefaultValue(program, item));
258
+ // default:
259
+ // reportDiagnostic(program, {
260
+ // code: "invalid-default",
261
+ // format: { type: type.kind },
262
+ // target: type,
263
+ // });
264
+ // }
265
+ // }
266
+ function getSchemaForModel(program, model, usage, needRef) {
267
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
268
+ const overridedModelName = (_b = (_a = getProjectedName(program, model, "javascript")) !== null && _a !== void 0 ? _a : getProjectedName(program, model, "client")) !== null && _b !== void 0 ? _b : getFriendlyName(program, model);
269
+ let name = model.name;
270
+ if (!overridedModelName &&
271
+ model.templateArguments &&
272
+ model.templateArguments.length > 0 &&
273
+ getPagedResult(program, model)) {
274
+ name =
275
+ model.templateArguments
276
+ .map((it) => {
277
+ switch (it.kind) {
278
+ case "Model":
279
+ return it.name;
280
+ case "String":
281
+ return it.value;
282
+ default:
283
+ return "";
284
+ }
285
+ })
286
+ .join("") + "List";
287
+ }
288
+ let modelSchema = {
289
+ name: overridedModelName !== null && overridedModelName !== void 0 ? overridedModelName : name,
290
+ type: "object",
291
+ description: (_c = getDoc(program, model)) !== null && _c !== void 0 ? _c : ""
292
+ };
293
+ // normalized the output name
294
+ modelSchema.name = normalizeName(modelSchema.name, NameType.Interface, true /** shouldGuard */);
295
+ if ((model.name === "ErrorResponse" ||
296
+ model.name === "ErrorModel" ||
297
+ model.name === "Error") &&
298
+ model.kind === "Model" &&
299
+ ((_d = model.namespace) === null || _d === void 0 ? void 0 : _d.name) === "Foundations" &&
300
+ ((_e = model.namespace.namespace) === null || _e === void 0 ? void 0 : _e.name) === "Core") {
301
+ modelSchema.fromCore = true;
302
+ }
303
+ if (getPagedResult(program, model)) {
304
+ const paged = extractPagedMetadataNested(program, model);
305
+ if (paged && paged.itemsProperty) {
306
+ const items = paged.itemsProperty;
307
+ if (items && items.templateArguments) {
308
+ const templateName = (_f = items.templateArguments) === null || _f === void 0 ? void 0 : _f.map((it) => {
309
+ switch (it.kind) {
310
+ case "Model":
311
+ return it.name;
312
+ case "String":
313
+ return it.value;
314
+ default:
315
+ return "";
316
+ }
317
+ }).join("");
318
+ modelSchema.alias = `Paged<${templateName}>`;
319
+ modelSchema.outputAlias = `Paged<${templateName}Output>`;
320
+ }
321
+ }
322
+ }
323
+ modelSchema.properties = {};
324
+ const derivedModels = model.derivedModels.filter(includeDerivedModel);
325
+ // getSchemaOrRef on all children to push them into components.schemas
326
+ const discriminator = getDiscriminator(program, model);
327
+ if (derivedModels.length > 0) {
328
+ modelSchema.children = {
329
+ all: [],
330
+ immediate: []
331
+ };
332
+ }
333
+ for (const child of derivedModels) {
334
+ const childSchema = getSchemaForType(program, child, usage, true);
335
+ for (const [name, prop] of child.properties) {
336
+ if (name === (discriminator === null || discriminator === void 0 ? void 0 : discriminator.propertyName)) {
337
+ const propSchema = getSchemaForType(program, prop.type, usage, true);
338
+ childSchema.discriminatorValue = propSchema.type.replace(/"/g, "");
339
+ break;
340
+ }
341
+ }
342
+ (_h = (_g = modelSchema.children) === null || _g === void 0 ? void 0 : _g.all) === null || _h === void 0 ? void 0 : _h.push(childSchema);
343
+ (_k = (_j = modelSchema.children) === null || _j === void 0 ? void 0 : _j.immediate) === null || _k === void 0 ? void 0 : _k.push(childSchema);
344
+ }
345
+ // Enable option `isPolyParent` and discriminator only when it has valid children
346
+ if (discriminator &&
347
+ ((_m = (_l = modelSchema === null || modelSchema === void 0 ? void 0 : modelSchema.children) === null || _l === void 0 ? void 0 : _l.all) === null || _m === void 0 ? void 0 : _m.length) &&
348
+ ((_p = (_o = modelSchema === null || modelSchema === void 0 ? void 0 : modelSchema.children) === null || _o === void 0 ? void 0 : _o.all) === null || _p === void 0 ? void 0 : _p.length) > 0) {
349
+ if (!validateDiscriminator(program, discriminator, derivedModels)) {
350
+ // appropriate diagnostic is generated in the validate function
351
+ return {};
352
+ }
353
+ const { propertyName } = discriminator;
354
+ modelSchema.discriminator = {
355
+ name: propertyName,
356
+ type: "string",
357
+ required: true,
358
+ description: `Discriminator property for ${model.name}.`
359
+ };
360
+ modelSchema.isPolyParent = true;
361
+ }
362
+ // applyExternalDocs(model, modelSchema);
363
+ if (needRef) {
364
+ return modelSchema;
365
+ }
366
+ for (const [propName, prop] of model.properties) {
367
+ const restApiName = getProjectedName(program, prop, "json");
368
+ const name = `"${restApiName !== null && restApiName !== void 0 ? restApiName : propName}"`;
369
+ if (!isSchemaProperty(program, prop)) {
370
+ continue;
371
+ }
372
+ const propSchema = getSchemaForType(program, prop.type, usage, true);
373
+ if (propSchema === undefined) {
374
+ continue;
375
+ }
376
+ if (!prop.optional) {
377
+ propSchema.required = true;
378
+ }
379
+ const propertyDescription = getFormattedPropertyDoc(program, prop, propSchema);
380
+ propSchema.usage = usage;
381
+ // Use the description from ModelProperty not devired from Model Type
382
+ propSchema.description = propertyDescription;
383
+ modelSchema.properties[name] = propSchema;
384
+ // if this property is a discriminator property, remove it to keep autorest validation happy
385
+ if (model.baseModel) {
386
+ const { propertyName } = getDiscriminator(program, model.baseModel) || {};
387
+ if (propertyName && name === `"${propertyName}"`) {
388
+ continue;
389
+ }
390
+ }
391
+ // Apply decorators on the property to the type's schema
392
+ const newPropSchema = applyIntrinsicDecorators(program, prop, propSchema);
393
+ if (newPropSchema === undefined) {
394
+ continue;
395
+ }
396
+ // Use the description from ModelProperty not devired from Model Type
397
+ newPropSchema.description = propertyDescription;
398
+ if (prop.default) {
399
+ // modelSchema.properties[name]['default'] = getDefaultValue(program, prop.default);
400
+ }
401
+ // Should the property be marked as readOnly?
402
+ const vis = getVisibility(program, prop);
403
+ if (vis && vis.includes("read")) {
404
+ const mutability = [];
405
+ if (vis.includes("read")) {
406
+ if (vis.length > 1) {
407
+ mutability.push(SchemaContext.Output);
408
+ }
409
+ else {
410
+ newPropSchema["readOnly"] = true;
411
+ }
412
+ }
413
+ if (vis.includes("write") || vis.includes("create")) {
414
+ mutability.push(SchemaContext.Input);
415
+ }
416
+ if (mutability.length > 0) {
417
+ newPropSchema["usage"] = mutability;
418
+ }
419
+ }
420
+ modelSchema.properties[name] = newPropSchema;
421
+ }
422
+ // Special case: if a model type extends a single *templated* base type and
423
+ // has no properties of its own, absorb the definition of the base model
424
+ // into this schema definition. The assumption here is that any model type
425
+ // defined like this is just meant to rename the underlying instance of a
426
+ // templated type.
427
+ if (model.baseModel &&
428
+ model.baseModel.templateArguments &&
429
+ model.baseModel.templateArguments.length > 0 &&
430
+ modelSchema.properties &&
431
+ Object.keys(modelSchema.properties).length === 0) {
432
+ // Take the base model schema but carry across the documentation property
433
+ // that we set before
434
+ const baseSchema = getSchemaForType(program, model.baseModel, usage);
435
+ modelSchema = {
436
+ ...baseSchema,
437
+ description: modelSchema.description
438
+ };
439
+ }
440
+ else if (model.baseModel) {
441
+ modelSchema.parents = {
442
+ all: [getSchemaForType(program, model.baseModel, usage, true)],
443
+ immediate: [getSchemaForType(program, model.baseModel, usage, true)]
444
+ };
445
+ }
446
+ return modelSchema;
447
+ }
448
+ // Map an Cadl type to an OA schema. Returns undefined when the resulting
449
+ // OA schema is just a regular object schema.
450
+ function mapCadlTypeToTypeScript(program, cadlType, usage) {
451
+ switch (cadlType.kind) {
452
+ case "Number":
453
+ return { type: `${cadlType.value}` };
454
+ case "String":
455
+ return { type: `"${cadlType.value}"` };
456
+ case "Boolean":
457
+ return { type: `${cadlType.value}` };
458
+ case "Model":
459
+ return mapCadlStdTypeToTypeScript(program, cadlType, usage);
460
+ }
461
+ if (cadlType.kind === undefined) {
462
+ if (typeof cadlType === "string") {
463
+ return { type: `"${cadlType}"` };
464
+ }
465
+ else if (typeof cadlType === "number" || typeof cadlType === "boolean") {
466
+ return { type: `${cadlType}` };
467
+ }
468
+ }
469
+ }
470
+ function applyIntrinsicDecorators(program, cadlType, target) {
471
+ const newTarget = { ...target };
472
+ const docStr = getDoc(program, cadlType);
473
+ const isString = isStringType(program, getPropertyType(cadlType));
474
+ const isNumeric = isNumericType(program, getPropertyType(cadlType));
475
+ if (isString && !target.documentation && docStr) {
476
+ newTarget.description = docStr;
477
+ }
478
+ const restApiName = getProjectedName(program, cadlType, "json");
479
+ if (restApiName) {
480
+ newTarget.name = restApiName;
481
+ }
482
+ const summaryStr = getSummary(program, cadlType);
483
+ if (isString && !target.summary && summaryStr) {
484
+ newTarget.summary = summaryStr;
485
+ }
486
+ const formatStr = getFormat(program, cadlType);
487
+ if (isString && !target.format && formatStr) {
488
+ newTarget.format = formatStr;
489
+ }
490
+ const pattern = getPattern(program, cadlType);
491
+ if (isString && !target.pattern && pattern) {
492
+ newTarget.pattern = pattern;
493
+ }
494
+ const minLength = getMinLength(program, cadlType);
495
+ if (isString && !target.minLength && minLength !== undefined) {
496
+ newTarget.minLength = minLength;
497
+ }
498
+ const maxLength = getMaxLength(program, cadlType);
499
+ if (isString && !target.maxLength && maxLength !== undefined) {
500
+ newTarget.maxLength = maxLength;
501
+ }
502
+ const minValue = getMinValue(program, cadlType);
503
+ if (isNumeric && !target.minimum && minValue !== undefined) {
504
+ newTarget.minimum = minValue;
505
+ }
506
+ const maxValue = getMaxValue(program, cadlType);
507
+ if (isNumeric && !target.maximum && maxValue !== undefined) {
508
+ newTarget.maximum = maxValue;
509
+ }
510
+ if (isSecret(program, cadlType)) {
511
+ newTarget.format = "password";
512
+ newTarget["x-ms-secret"] = true;
513
+ }
514
+ return newTarget;
515
+ }
516
+ function getSchemaForEnum(program, e) {
517
+ var _a;
518
+ const values = [];
519
+ const type = enumMemberType(e.members.values().next().value);
520
+ for (const option of e.members.values()) {
521
+ if (type !== enumMemberType(option)) {
522
+ reportDiagnostic(program, { code: "union-unsupported", target: e });
523
+ continue;
524
+ }
525
+ values.push((_a = option.value) !== null && _a !== void 0 ? _a : option.name);
526
+ }
527
+ const schema = { type, description: getDoc(program, e) };
528
+ if (values.length > 0) {
529
+ schema.enum = values;
530
+ schema.type =
531
+ type === "string"
532
+ ? values.map((item) => `"${item}"`).join("|")
533
+ : values.map((item) => `${item}`).join("|");
534
+ if (!isFixed(program, e)) {
535
+ schema.name = "string";
536
+ schema.typeName = "string";
537
+ }
538
+ }
539
+ return schema;
540
+ function enumMemberType(member) {
541
+ if (typeof member.value === "number") {
542
+ return "number";
543
+ }
544
+ return "string";
545
+ }
546
+ }
547
+ /**
548
+ * Map Cadl intrinsic models to open api definitions
549
+ */
550
+ function mapCadlStdTypeToTypeScript(program, cadlType, usage) {
551
+ var _a;
552
+ const indexer = cadlType.indexer;
553
+ if (indexer !== undefined) {
554
+ if (!isNeverType(indexer.key)) {
555
+ const name = indexer.key.name;
556
+ let schema = {};
557
+ if (name === "string") {
558
+ const valueType = getSchemaForType(program, indexer.value, usage, true);
559
+ schema = {
560
+ type: "dictionary",
561
+ additionalProperties: valueType,
562
+ description: getDoc(program, cadlType)
563
+ };
564
+ if (!program.checker.isStdType(indexer.value) &&
565
+ !isUnknownType(indexer.value) &&
566
+ !isUnionType(indexer.value)) {
567
+ schema.typeName = `Record<string, ${valueType.name}>`;
568
+ schema.valueTypeName = valueType.name;
569
+ if (usage && usage.includes(SchemaContext.Output)) {
570
+ schema.outputTypeName = `Record<string, ${valueType.name}Output>`;
571
+ schema.outputValueTypeName = `${valueType.name}Output`;
572
+ }
573
+ }
574
+ else if (isUnknownType(indexer.value)) {
575
+ schema.typeName = `Record<string, ${valueType.type}>`;
576
+ if (usage && usage.includes(SchemaContext.Output)) {
577
+ schema.outputTypeName = `Record<string, ${valueType.outputTypeName}>`;
578
+ }
579
+ }
580
+ else {
581
+ schema.typeName = `Record<string, ${valueType.type}>`;
582
+ }
583
+ }
584
+ else if (name === "integer") {
585
+ schema = {
586
+ type: "array",
587
+ items: getSchemaForType(program, indexer.value, usage, true),
588
+ description: getDoc(program, cadlType)
589
+ };
590
+ if (!program.checker.isStdType(indexer.value) &&
591
+ !isUnknownType(indexer.value) &&
592
+ ((_a = indexer.value) === null || _a === void 0 ? void 0 : _a.kind) &&
593
+ schema.items.name &&
594
+ !schema.items.enum) {
595
+ schema.typeName = `Array<${schema.items.name}>`;
596
+ if (usage && usage.includes(SchemaContext.Output)) {
597
+ schema.outputTypeName = `Array<${schema.items.name}Output>`;
598
+ }
599
+ }
600
+ else {
601
+ if (schema.items.typeName) {
602
+ schema.typeName = schema.items.typeName
603
+ .split("|")
604
+ .map((typeName) => {
605
+ return `${typeName}[]`;
606
+ })
607
+ .join(" | ");
608
+ if (schema.items.outputTypeName &&
609
+ usage &&
610
+ usage.includes(SchemaContext.Output)) {
611
+ schema.outputTypeName = schema.items.outputTypeName
612
+ .split("|")
613
+ .map((typeName) => {
614
+ return `${typeName}[]`;
615
+ })
616
+ .join(" | ");
617
+ }
618
+ }
619
+ else if (schema.items.type.includes("|")) {
620
+ schema.typeName = `(${schema.items.type})[]`;
621
+ }
622
+ else {
623
+ schema.typeName = `${schema.items.type}[]`;
624
+ }
625
+ }
626
+ }
627
+ schema.usage = usage;
628
+ return schema;
629
+ }
630
+ }
631
+ }
632
+ function isUnionType(type) {
633
+ return type.kind === "Union";
634
+ }
635
+ function getSchemaForStdScalar(program, cadlType) {
636
+ if (!program.checker.isStdType(cadlType)) {
637
+ return undefined;
638
+ }
639
+ const name = cadlType.name;
640
+ const description = getSummary(program, cadlType);
641
+ switch (name) {
642
+ case "bytes":
643
+ return { type: "string", format: "byte", description };
644
+ case "integer":
645
+ return applyIntrinsicDecorators(program, cadlType, {
646
+ type: "number"
647
+ });
648
+ case "int8":
649
+ return applyIntrinsicDecorators(program, cadlType, {
650
+ type: "number",
651
+ format: "int8"
652
+ });
653
+ case "int16":
654
+ return applyIntrinsicDecorators(program, cadlType, {
655
+ type: "number",
656
+ format: "int16"
657
+ });
658
+ case "int32":
659
+ return applyIntrinsicDecorators(program, cadlType, {
660
+ type: "number",
661
+ format: "int32"
662
+ });
663
+ case "int64":
664
+ return applyIntrinsicDecorators(program, cadlType, {
665
+ type: "number",
666
+ format: "int64"
667
+ });
668
+ case "safeint":
669
+ return applyIntrinsicDecorators(program, cadlType, {
670
+ type: "number",
671
+ format: "int64"
672
+ });
673
+ case "uint8":
674
+ return applyIntrinsicDecorators(program, cadlType, {
675
+ type: "number",
676
+ format: "uint8"
677
+ });
678
+ case "uint16":
679
+ return applyIntrinsicDecorators(program, cadlType, {
680
+ type: "number",
681
+ format: "uint16"
682
+ });
683
+ case "uint32":
684
+ return applyIntrinsicDecorators(program, cadlType, {
685
+ type: "number",
686
+ format: "uint32"
687
+ });
688
+ case "uint64":
689
+ return applyIntrinsicDecorators(program, cadlType, {
690
+ type: "number",
691
+ format: "uint64"
692
+ });
693
+ case "float64":
694
+ return applyIntrinsicDecorators(program, cadlType, {
695
+ type: "number",
696
+ format: "double"
697
+ });
698
+ case "float32":
699
+ return applyIntrinsicDecorators(program, cadlType, {
700
+ type: "number",
701
+ format: "float"
702
+ });
703
+ case "string":
704
+ return applyIntrinsicDecorators(program, cadlType, { type: "string" });
705
+ case "boolean":
706
+ return { type: "boolean", description };
707
+ case "plainDate":
708
+ return {
709
+ type: "string",
710
+ format: "date",
711
+ description,
712
+ typeName: "Date | string",
713
+ outputTypeName: "string"
714
+ };
715
+ case "zonedDateTime":
716
+ return {
717
+ type: "string",
718
+ format: "date-time",
719
+ description,
720
+ typeName: "Date | string",
721
+ outputTypeName: "string"
722
+ };
723
+ case "plainTime":
724
+ return {
725
+ type: "string",
726
+ format: "time",
727
+ description,
728
+ typeName: "Date | string",
729
+ outputTypeName: "string"
730
+ };
731
+ case "duration":
732
+ return { type: "string", format: "duration", description };
733
+ }
734
+ }
735
+ export function getTypeName(schema, usage) {
736
+ var _a, _b;
737
+ // TODO: Handle more cases
738
+ return (_b = (_a = getPriorityName(schema, usage)) !== null && _a !== void 0 ? _a : schema.type) !== null && _b !== void 0 ? _b : "any";
739
+ }
740
+ export function getImportedModelName(schema) {
741
+ switch (schema.type) {
742
+ case "array":
743
+ return [schema.items]
744
+ .filter((i) => i.type === "object")
745
+ .map((i) => { var _a; return (_a = getPriorityName(i)) !== null && _a !== void 0 ? _a : ""; });
746
+ case "object":
747
+ return getPriorityName(schema) ? [getPriorityName(schema)] : undefined;
748
+ case "dictionary": {
749
+ const importName = getDictionaryValueName(schema);
750
+ return importName ? [importName] : undefined;
751
+ }
752
+ default:
753
+ return;
754
+ }
755
+ }
756
+ function getPriorityName(schema, usage) {
757
+ var _a, _b, _c;
758
+ return usage &&
759
+ usage.includes(SchemaContext.Input) &&
760
+ !usage.includes(SchemaContext.Output)
761
+ ? (_a = schema.typeName) !== null && _a !== void 0 ? _a : schema.name
762
+ : (_c = (_b = schema.outputTypeName) !== null && _b !== void 0 ? _b : schema.typeName) !== null && _c !== void 0 ? _c : schema.name;
763
+ }
764
+ function getDictionaryValueName(schema) {
765
+ var _a, _b;
766
+ return (_b = (_a = schema.outputValueTypeName) !== null && _a !== void 0 ? _a : schema.valueTypeName) !== null && _b !== void 0 ? _b : undefined;
767
+ }
768
+ function getEnumStringDescription(type) {
769
+ if (type.name === "string" && type.enum && type.enum.length > 0) {
770
+ return `Possible values: ${type.enum.join(", ")}`;
771
+ }
772
+ return undefined;
773
+ }
774
+ export function getFormattedPropertyDoc(program, cadlType, schemaType, sperator = "\n\n") {
775
+ const propertyDoc = getDoc(program, cadlType);
776
+ const enhancedDocFromType = getEnumStringDescription(schemaType);
777
+ if (propertyDoc && enhancedDocFromType) {
778
+ return `${propertyDoc}${sperator}${enhancedDocFromType}`;
779
+ }
780
+ return propertyDoc !== null && propertyDoc !== void 0 ? propertyDoc : enhancedDocFromType;
781
+ }
782
+ export function getBodyType(program, route) {
783
+ var _a, _b;
784
+ let bodyModel = route.parameters.bodyType;
785
+ if (bodyModel && bodyModel.kind === "Model" && route.operation) {
786
+ const resourceType = (_a = getResourceOperation(program, route.operation)) === null || _a === void 0 ? void 0 : _a.resourceType;
787
+ if (resourceType && route.responses && route.responses.length > 0) {
788
+ const resp = route.responses[0];
789
+ if (resp && resp.responses && resp.responses.length > 0) {
790
+ const responseBody = (_b = resp.responses[0]) === null || _b === void 0 ? void 0 : _b.body;
791
+ if (responseBody) {
792
+ const bodyTypeInResponse = getEffectiveModelFromType(program, responseBody.type);
793
+ // response body type is reosurce type, and request body type (if templated) contains resource type
794
+ if (bodyTypeInResponse === resourceType &&
795
+ bodyModel.templateArguments &&
796
+ bodyModel.templateArguments.some((it) => {
797
+ return it.kind === "Model" || it.kind === "Union"
798
+ ? it === bodyTypeInResponse
799
+ : false;
800
+ })) {
801
+ bodyModel = resourceType;
802
+ }
803
+ }
804
+ }
805
+ }
806
+ if (resourceType && bodyModel.name === "") {
807
+ bodyModel = resourceType;
808
+ }
809
+ }
810
+ return bodyModel;
811
+ }
812
+ //# sourceMappingURL=modelUtils.js.map