@autorest/typescript 6.0.0-beta.14 → 6.0.0-beta.17

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 (235) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/README.md +41 -5
  3. package/dist/src/autorestSession.d.ts +12 -4
  4. package/dist/src/autorestSession.d.ts.map +1 -1
  5. package/dist/src/autorestSession.js +3 -9
  6. package/dist/src/autorestSession.js.map +1 -1
  7. package/dist/src/conflictResolver.d.ts.map +1 -1
  8. package/dist/src/conflictResolver.js.map +1 -1
  9. package/dist/src/generators/clientFileGenerator.d.ts.map +1 -1
  10. package/dist/src/generators/clientFileGenerator.js +181 -31
  11. package/dist/src/generators/clientFileGenerator.js.map +1 -1
  12. package/dist/src/generators/indexGenerator.d.ts.map +1 -1
  13. package/dist/src/generators/indexGenerator.js +70 -9
  14. package/dist/src/generators/indexGenerator.js.map +1 -1
  15. package/dist/src/generators/modelsGenerator.d.ts.map +1 -1
  16. package/dist/src/generators/modelsGenerator.js +20 -2
  17. package/dist/src/generators/modelsGenerator.js.map +1 -1
  18. package/dist/src/generators/operationGenerator.d.ts.map +1 -1
  19. package/dist/src/generators/operationGenerator.js +10 -8
  20. package/dist/src/generators/operationGenerator.js.map +1 -1
  21. package/dist/src/generators/samples/sampleEnv.hbs +4 -0
  22. package/dist/src/generators/samples/sampleEnvGenerator.d.ts +3 -0
  23. package/dist/src/generators/samples/sampleEnvGenerator.d.ts.map +1 -0
  24. package/dist/src/generators/samples/sampleEnvGenerator.js +17 -0
  25. package/dist/src/generators/samples/sampleEnvGenerator.js.map +1 -0
  26. package/dist/src/generators/samples/sampleGenerator.d.ts +13 -0
  27. package/dist/src/generators/samples/sampleGenerator.d.ts.map +1 -0
  28. package/dist/src/generators/samples/sampleGenerator.js +43 -0
  29. package/dist/src/generators/samples/sampleGenerator.js.map +1 -0
  30. package/dist/src/generators/static/apiExtractorConfig.js +2 -2
  31. package/dist/src/generators/static/apiExtractorConfig.js.map +1 -1
  32. package/dist/src/generators/static/esLintConfigGenerator.d.ts +3 -0
  33. package/dist/src/generators/static/esLintConfigGenerator.d.ts.map +1 -0
  34. package/dist/src/generators/static/esLintConfigGenerator.js +26 -0
  35. package/dist/src/generators/static/esLintConfigGenerator.js.map +1 -0
  36. package/dist/src/generators/static/{README.md.hbs → hlcREADME.md.hbs} +23 -0
  37. package/dist/src/generators/static/karma.conf.js.hbs +141 -0
  38. package/dist/src/generators/static/karmaConfigFileGenerator.d.ts +3 -0
  39. package/dist/src/generators/static/karmaConfigFileGenerator.d.ts.map +1 -0
  40. package/dist/src/generators/static/karmaConfigFileGenerator.js +23 -0
  41. package/dist/src/generators/static/karmaConfigFileGenerator.js.map +1 -0
  42. package/dist/src/generators/static/packageFileGenerator.d.ts.map +1 -1
  43. package/dist/src/generators/static/packageFileGenerator.js +174 -48
  44. package/dist/src/generators/static/packageFileGenerator.js.map +1 -1
  45. package/dist/src/generators/static/readmeFileGenerator.d.ts +2 -2
  46. package/dist/src/generators/static/readmeFileGenerator.d.ts.map +1 -1
  47. package/dist/src/generators/static/readmeFileGenerator.js +35 -17
  48. package/dist/src/generators/static/readmeFileGenerator.js.map +1 -1
  49. package/dist/src/generators/static/rlcREADME.md.hbs +71 -0
  50. package/dist/src/generators/static/rollupConfigFileGenerator.d.ts.map +1 -1
  51. package/dist/src/generators/static/rollupConfigFileGenerator.js +12 -71
  52. package/dist/src/generators/static/rollupConfigFileGenerator.js.map +1 -1
  53. package/dist/src/generators/static/samples.ts.hbs +49 -0
  54. package/dist/src/generators/static/tsConfigFileGenerator.d.ts.map +1 -1
  55. package/dist/src/generators/static/tsConfigFileGenerator.js +40 -18
  56. package/dist/src/generators/static/tsConfigFileGenerator.js.map +1 -1
  57. package/dist/src/generators/test/envBrowserFileGenerator.d.ts +3 -0
  58. package/dist/src/generators/test/envBrowserFileGenerator.d.ts.map +1 -0
  59. package/dist/src/generators/test/envBrowserFileGenerator.js +15 -0
  60. package/dist/src/generators/test/envBrowserFileGenerator.js.map +1 -0
  61. package/dist/src/generators/test/envFileGenerator.d.ts +3 -0
  62. package/dist/src/generators/test/envFileGenerator.d.ts.map +1 -0
  63. package/dist/src/generators/test/envFileGenerator.js +23 -0
  64. package/dist/src/generators/test/envFileGenerator.js.map +1 -0
  65. package/dist/src/generators/test/recordedClientFileGenerator.d.ts +3 -0
  66. package/dist/src/generators/test/recordedClientFileGenerator.d.ts.map +1 -0
  67. package/dist/src/generators/test/recordedClientFileGenerator.js +23 -0
  68. package/dist/src/generators/test/recordedClientFileGenerator.js.map +1 -0
  69. package/dist/src/generators/test/rlcEnv.ts.hbs +3 -0
  70. package/dist/src/generators/test/rlcRecordedClient.ts.hbs +35 -0
  71. package/dist/src/generators/test/rlcSampleTest.spec.ts.hbs +19 -0
  72. package/dist/src/generators/{static → test}/sampleTest.ts.hbs +0 -0
  73. package/dist/src/generators/test/sampleTestGenerator.d.ts.map +1 -1
  74. package/dist/src/generators/test/sampleTestGenerator.js +23 -7
  75. package/dist/src/generators/test/sampleTestGenerator.js.map +1 -1
  76. package/dist/src/generators/utils/pagingOperations.d.ts +5 -0
  77. package/dist/src/generators/utils/pagingOperations.d.ts.map +1 -1
  78. package/dist/src/generators/utils/pagingOperations.js +2 -1
  79. package/dist/src/generators/utils/pagingOperations.js.map +1 -1
  80. package/dist/src/main.d.ts +2 -2
  81. package/dist/src/main.d.ts.map +1 -1
  82. package/dist/src/main.js +3 -3
  83. package/dist/src/main.js.map +1 -1
  84. package/dist/src/models/clientDetails.d.ts +7 -0
  85. package/dist/src/models/clientDetails.d.ts.map +1 -1
  86. package/dist/src/models/modelDetails.d.ts +2 -2
  87. package/dist/src/models/modelDetails.d.ts.map +1 -1
  88. package/dist/src/models/operationDetails.d.ts +1 -0
  89. package/dist/src/models/operationDetails.d.ts.map +1 -1
  90. package/dist/src/models/sampleDetails.d.ts +22 -0
  91. package/dist/src/models/sampleDetails.d.ts.map +1 -0
  92. package/dist/src/models/sampleDetails.js +3 -0
  93. package/dist/src/models/sampleDetails.js.map +1 -0
  94. package/dist/src/restLevelClient/generateClient.d.ts +1 -1
  95. package/dist/src/restLevelClient/generateClient.d.ts.map +1 -1
  96. package/dist/src/restLevelClient/generateClient.js +81 -163
  97. package/dist/src/restLevelClient/generateClient.js.map +1 -1
  98. package/dist/src/restLevelClient/generateClientDefinition.d.ts +6 -0
  99. package/dist/src/restLevelClient/generateClientDefinition.d.ts.map +1 -0
  100. package/dist/src/restLevelClient/generateClientDefinition.js +231 -0
  101. package/dist/src/restLevelClient/generateClientDefinition.js.map +1 -0
  102. package/dist/src/restLevelClient/generateMethodShortcuts.d.ts +8 -0
  103. package/dist/src/restLevelClient/generateMethodShortcuts.d.ts.map +1 -0
  104. package/dist/src/restLevelClient/generateMethodShortcuts.js +70 -0
  105. package/dist/src/restLevelClient/generateMethodShortcuts.js.map +1 -0
  106. package/dist/src/restLevelClient/generateObjectTypes.d.ts.map +1 -1
  107. package/dist/src/restLevelClient/generateObjectTypes.js.map +1 -1
  108. package/dist/src/restLevelClient/generatePagingHelper.d.ts.map +1 -1
  109. package/dist/src/restLevelClient/generatePagingHelper.js +2 -1
  110. package/dist/src/restLevelClient/generatePagingHelper.js.map +1 -1
  111. package/dist/src/restLevelClient/generateParameterTypes.d.ts.map +1 -1
  112. package/dist/src/restLevelClient/generateParameterTypes.js +186 -36
  113. package/dist/src/restLevelClient/generateParameterTypes.js.map +1 -1
  114. package/dist/src/restLevelClient/generatePollingHelper.d.ts.map +1 -1
  115. package/dist/src/restLevelClient/generatePollingHelper.js +3 -1
  116. package/dist/src/restLevelClient/generatePollingHelper.js.map +1 -1
  117. package/dist/src/restLevelClient/generateResponseTypes.d.ts.map +1 -1
  118. package/dist/src/restLevelClient/generateResponseTypes.js +13 -7
  119. package/dist/src/restLevelClient/generateResponseTypes.js.map +1 -1
  120. package/dist/src/restLevelClient/generateRestLevel.d.ts.map +1 -1
  121. package/dist/src/restLevelClient/generateRestLevel.js +26 -4
  122. package/dist/src/restLevelClient/generateRestLevel.js.map +1 -1
  123. package/dist/src/restLevelClient/generateSchemaTypes.d.ts.map +1 -1
  124. package/dist/src/restLevelClient/generateSchemaTypes.js +5 -2
  125. package/dist/src/restLevelClient/generateSchemaTypes.js.map +1 -1
  126. package/dist/src/restLevelClient/generateTopLevelIndexFile.d.ts +4 -0
  127. package/dist/src/restLevelClient/generateTopLevelIndexFile.d.ts.map +1 -0
  128. package/dist/src/restLevelClient/generateTopLevelIndexFile.js +41 -0
  129. package/dist/src/restLevelClient/generateTopLevelIndexFile.js.map +1 -0
  130. package/dist/src/restLevelClient/helpers/operationHelpers.d.ts +10 -0
  131. package/dist/src/restLevelClient/helpers/operationHelpers.d.ts.map +1 -0
  132. package/dist/src/restLevelClient/helpers/operationHelpers.js +64 -0
  133. package/dist/src/restLevelClient/helpers/operationHelpers.js.map +1 -0
  134. package/dist/src/restLevelClient/interfaces.d.ts +24 -0
  135. package/dist/src/restLevelClient/interfaces.d.ts.map +1 -0
  136. package/dist/src/restLevelClient/interfaces.js +3 -0
  137. package/dist/src/restLevelClient/interfaces.js.map +1 -0
  138. package/dist/src/restLevelClient/operationHelpers.d.ts.map +1 -1
  139. package/dist/src/restLevelClient/operationHelpers.js +4 -1
  140. package/dist/src/restLevelClient/operationHelpers.js.map +1 -1
  141. package/dist/src/restLevelClient/schemaHelpers.d.ts.map +1 -1
  142. package/dist/src/restLevelClient/schemaHelpers.js +4 -1
  143. package/dist/src/restLevelClient/schemaHelpers.js.map +1 -1
  144. package/dist/src/transforms/operationTransforms.d.ts.map +1 -1
  145. package/dist/src/transforms/operationTransforms.js +1 -0
  146. package/dist/src/transforms/operationTransforms.js.map +1 -1
  147. package/dist/src/transforms/samplesTransforms.d.ts +7 -0
  148. package/dist/src/transforms/samplesTransforms.d.ts.map +1 -0
  149. package/dist/src/transforms/samplesTransforms.js +283 -0
  150. package/dist/src/transforms/samplesTransforms.js.map +1 -0
  151. package/dist/src/transforms/transforms.d.ts +1 -2
  152. package/dist/src/transforms/transforms.d.ts.map +1 -1
  153. package/dist/src/transforms/transforms.js +4 -3
  154. package/dist/src/transforms/transforms.js.map +1 -1
  155. package/dist/src/typescriptGenerator.d.ts +2 -2
  156. package/dist/src/typescriptGenerator.d.ts.map +1 -1
  157. package/dist/src/typescriptGenerator.js +19 -8
  158. package/dist/src/typescriptGenerator.js.map +1 -1
  159. package/dist/src/utils/autorestOptions.d.ts +2 -2
  160. package/dist/src/utils/autorestOptions.d.ts.map +1 -1
  161. package/dist/src/utils/autorestOptions.js +107 -30
  162. package/dist/src/utils/autorestOptions.js.map +1 -1
  163. package/dist/src/utils/nameUtils.d.ts +6 -2
  164. package/dist/src/utils/nameUtils.d.ts.map +1 -1
  165. package/dist/src/utils/nameUtils.js +9 -5
  166. package/dist/src/utils/nameUtils.js.map +1 -1
  167. package/dist/src/utils/schemaHelpers.js +8 -3
  168. package/dist/src/utils/schemaHelpers.js.map +1 -1
  169. package/package.json +16 -10
  170. package/src/autorestSession.ts +20 -16
  171. package/src/conflictResolver.ts +0 -1
  172. package/src/generators/clientFileGenerator.ts +264 -33
  173. package/src/generators/indexGenerator.ts +78 -9
  174. package/src/generators/modelsGenerator.ts +21 -1
  175. package/src/generators/operationGenerator.ts +11 -9
  176. package/src/generators/samples/sampleEnv.hbs +4 -0
  177. package/src/generators/samples/sampleEnvGenerator.ts +14 -0
  178. package/src/generators/samples/sampleGenerator.ts +50 -0
  179. package/src/generators/static/apiExtractorConfig.ts +2 -2
  180. package/src/generators/static/esLintConfigGenerator.ts +24 -0
  181. package/src/generators/static/{README.md.hbs → hlcREADME.md.hbs} +23 -0
  182. package/src/generators/static/karma.conf.js.hbs +141 -0
  183. package/src/generators/static/karmaConfigFileGenerator.ts +20 -0
  184. package/src/generators/static/packageFileGenerator.ts +187 -48
  185. package/src/generators/static/readmeFileGenerator.ts +64 -35
  186. package/src/generators/static/rlcREADME.md.hbs +71 -0
  187. package/src/generators/static/rollupConfigFileGenerator.ts +13 -71
  188. package/src/generators/static/samples.ts.hbs +49 -0
  189. package/src/generators/static/tsConfigFileGenerator.ts +47 -20
  190. package/src/generators/test/envBrowserFileGenerator.ts +14 -0
  191. package/src/generators/test/envFileGenerator.ts +22 -0
  192. package/src/generators/test/recordedClientFileGenerator.ts +22 -0
  193. package/src/generators/test/rlcEnv.ts.hbs +3 -0
  194. package/src/generators/test/rlcRecordedClient.ts.hbs +35 -0
  195. package/src/generators/test/rlcSampleTest.spec.ts.hbs +19 -0
  196. package/src/generators/{static → test}/sampleTest.ts.hbs +0 -0
  197. package/src/generators/test/sampleTestGenerator.ts +23 -8
  198. package/src/generators/utils/pagingOperations.ts +1 -1
  199. package/src/main.ts +8 -5
  200. package/src/models/clientDetails.ts +8 -0
  201. package/src/models/modelDetails.ts +2 -2
  202. package/src/models/operationDetails.ts +1 -0
  203. package/src/models/sampleDetails.ts +22 -0
  204. package/src/restLevelClient/generateClient.ts +99 -250
  205. package/src/restLevelClient/generateClientDefinition.ts +343 -0
  206. package/src/restLevelClient/generateMethodShortcuts.ts +121 -0
  207. package/src/restLevelClient/generateObjectTypes.ts +1 -3
  208. package/src/restLevelClient/generatePagingHelper.ts +3 -2
  209. package/src/restLevelClient/generateParameterTypes.ts +295 -70
  210. package/src/restLevelClient/generatePollingHelper.ts +3 -3
  211. package/src/restLevelClient/generateResponseTypes.ts +13 -7
  212. package/src/restLevelClient/generateRestLevel.ts +32 -7
  213. package/src/restLevelClient/generateSchemaTypes.ts +5 -3
  214. package/src/restLevelClient/generateTopLevelIndexFile.ts +41 -0
  215. package/src/restLevelClient/helpers/operationHelpers.ts +93 -0
  216. package/src/restLevelClient/interfaces.ts +26 -0
  217. package/src/restLevelClient/operationHelpers.ts +8 -2
  218. package/src/restLevelClient/schemaHelpers.ts +4 -2
  219. package/src/transforms/operationTransforms.ts +1 -0
  220. package/src/transforms/samplesTransforms.ts +300 -0
  221. package/src/transforms/transforms.ts +6 -6
  222. package/src/typescriptGenerator.ts +20 -11
  223. package/src/utils/autorestOptions.ts +170 -59
  224. package/src/utils/nameUtils.ts +16 -8
  225. package/src/utils/schemaHelpers.ts +2 -2
  226. package/dist/src/generators/clientContextFileGenerator.d.ts +0 -5
  227. package/dist/src/generators/clientContextFileGenerator.d.ts.map +0 -1
  228. package/dist/src/generators/clientContextFileGenerator.js +0 -263
  229. package/dist/src/generators/clientContextFileGenerator.js.map +0 -1
  230. package/dist/src/restLevelClient/helpers/getOperationParameters.d.ts +0 -6
  231. package/dist/src/restLevelClient/helpers/getOperationParameters.d.ts.map +0 -1
  232. package/dist/src/restLevelClient/helpers/getOperationParameters.js +0 -29
  233. package/dist/src/restLevelClient/helpers/getOperationParameters.js.map +0 -1
  234. package/src/generators/clientContextFileGenerator.ts +0 -405
  235. package/src/restLevelClient/helpers/getOperationParameters.ts +0 -34
@@ -2,16 +2,31 @@ import { Project } from "ts-morph";
2
2
  import * as path from 'path';
3
3
  import * as fs from 'fs';
4
4
  import * as hbs from "handlebars";
5
+ import { getAutorestOptions } from "../../autorestSession";
5
6
 
6
7
  export function generateSampleTestFile(
7
8
  project: Project
8
9
  ) {
10
+ const { generateTest, restLevelClient } = getAutorestOptions();
11
+ if (!generateTest) {
12
+ return;
13
+ }
9
14
  const metadata = {}
10
- const file = fs.readFileSync(path.join(__dirname, "../static/sampleTest.ts.hbs"), {
11
- encoding: "utf-8"
12
- });
13
- const readmeFileContents = hbs.compile(file, { noEscape: true });
14
- project.createSourceFile("test/sampleTest.ts", readmeFileContents(metadata), {
15
- overwrite: true
16
- });
17
- }
15
+ if (restLevelClient) {
16
+ const file = fs.readFileSync(path.join(__dirname, "rlcSampleTest.spec.ts.hbs"), {
17
+ encoding: "utf-8"
18
+ });
19
+ const readmeFileContents = hbs.compile(file, { noEscape: true });
20
+ project.createSourceFile("test/public/sampleTest.spec.ts", readmeFileContents(metadata), {
21
+ overwrite: true
22
+ });
23
+ } else {
24
+ const file = fs.readFileSync(path.join(__dirname, "sampleTest.ts.hbs"), {
25
+ encoding: "utf-8"
26
+ });
27
+ const readmeFileContents = hbs.compile(file, { noEscape: true });
28
+ project.createSourceFile("test/sampleTest.ts", readmeFileContents(metadata), {
29
+ overwrite: true
30
+ });
31
+ }
32
+ }
@@ -230,7 +230,7 @@ export function writeAsyncIterators(
230
230
  * This method enforces Azure SDK Typescript guideline that paging methods should be named list*
231
231
  * https://azure.github.io/azure-sdk/typescript_design.html#ts-pagination-provide-list
232
232
  */
233
- function getPublicMethodName(operation: OperationDetails) {
233
+ export function getPublicMethodName(operation: OperationDetails) {
234
234
  let initialOperationName = normalizeName(operation.name, NameType.Operation);
235
235
  if (initialOperationName.indexOf("list") === 0) {
236
236
  initialOperationName = initialOperationName.replace("list", "");
package/src/main.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  // Copyright (c) Microsoft Corporation.
2
2
  // Licensed under the MIT License.
3
3
 
4
- import { AutoRestExtension, Host } from "@autorest/extension-base";
4
+ import {
5
+ AutoRestExtension,
6
+ AutorestExtensionHost
7
+ } from "@autorest/extension-base";
5
8
  import { generateTypeScriptLibrary } from "./typescriptGenerator";
6
9
  import { generateRestLevelClient } from "./restLevelClient/generateRestLevel";
7
10
  import {
@@ -10,7 +13,7 @@ import {
10
13
  getAutorestOptions
11
14
  } from "./autorestSession";
12
15
 
13
- export async function processRequest(host: Host) {
16
+ export async function processRequest(host: AutorestExtensionHost) {
14
17
  await initializeSession(host);
15
18
  const session = getSession();
16
19
  const { restLevelClient } = getAutorestOptions();
@@ -19,7 +22,7 @@ export async function processRequest(host: Host) {
19
22
  restLevelClient
20
23
  ? await generateRestLevelClient()
21
24
  : await generateTypeScriptLibrary(session.model, host);
22
- session.log(`Autorest.Typescript took ${Date.now() - start}ms`, "");
25
+ session.info(`Autorest.Typescript took ${Date.now() - start}ms`);
23
26
  } catch (err) {
24
27
  session.error("An error was encountered while handling a request:", err);
25
28
  throw err;
@@ -28,8 +31,8 @@ export async function processRequest(host: Host) {
28
31
 
29
32
  async function main() {
30
33
  const pluginHost = new AutoRestExtension();
31
- pluginHost.Add("typescript", processRequest);
32
- await pluginHost.Run();
34
+ pluginHost.add("typescript", processRequest);
35
+ await pluginHost.run();
33
36
  }
34
37
 
35
38
  main();
@@ -9,6 +9,7 @@ import { ObjectDetails } from "./modelDetails";
9
9
  import { EndpointDetails } from "../transforms/urlTransforms";
10
10
  import { KnownMediaType } from "@azure-tools/codegen";
11
11
  import { Info } from "@autorest/codemodel";
12
+ import { SampleGroup } from "./sampleDetails";
12
13
 
13
14
  export interface ClientOptions {
14
15
  mediaTypes?: Set<KnownMediaType>;
@@ -20,6 +21,11 @@ export interface TracingInfo {
20
21
  packagePrefix: string;
21
22
  }
22
23
 
24
+ export interface DependencyInfo {
25
+ link: string;
26
+ description: string;
27
+ }
28
+
23
29
  export interface ClientDetails {
24
30
  name: string;
25
31
  className: string;
@@ -32,4 +38,6 @@ export interface ClientDetails {
32
38
  parameters: ParameterDetails[];
33
39
  options: ClientOptions;
34
40
  endpoint: EndpointDetails;
41
+ samples?: SampleGroup[];
42
+ allTypes: string[];
35
43
  }
@@ -65,7 +65,7 @@ export type PolymorphicObjectDetails = BasicObjectDetails & {
65
65
  export interface PropertyDetails {
66
66
  name: string;
67
67
  description?: string;
68
- defaultValue?: string;
68
+ defaultValue?: string | number | boolean;
69
69
  serializedName: string;
70
70
  type: string;
71
71
  required: boolean;
@@ -83,7 +83,7 @@ export interface TypeDetails {
83
83
  typeName: string;
84
84
  isConstant?: boolean;
85
85
  nullable?: boolean;
86
- defaultValue?: string;
86
+ defaultValue?: string | number | boolean;
87
87
  kind: PropertyKind;
88
88
  usedModels: string[];
89
89
  }
@@ -103,6 +103,7 @@ export interface OperationSpecDetails {
103
103
  export interface OperationGroupDetails {
104
104
  key: string;
105
105
  name: string;
106
+ originalKey: string;
106
107
  operations: OperationDetails[];
107
108
  mediaTypes: Set<KnownMediaType>;
108
109
  isTopLevel: boolean;
@@ -0,0 +1,22 @@
1
+ export interface SampleGroup {
2
+ sampleFileName: string,
3
+ clientClassName: string,
4
+ clientPackageName: string,
5
+ samples: SampleDetails[],
6
+ importedTypes?: string[],
7
+ }
8
+
9
+ export interface SampleDetails {
10
+ sampleFunctionName: string,
11
+ clientParameterNames: string,
12
+ methodParameterNames: string,
13
+ clientParamAssignments: string[],
14
+ methodParamAssignments: string[],
15
+ originalFileLocation?: string,
16
+ operationName: string,
17
+ isTopLevel: boolean,
18
+ isPaging: boolean,
19
+ operationGroupName: string,
20
+ operationDescription: string,
21
+ clientClassName: string,
22
+ }
@@ -5,125 +5,44 @@ import {
5
5
  ImplementationLocation
6
6
  } from "@autorest/codemodel";
7
7
 
8
- import { getResponseTypeName } from "./operationHelpers";
9
-
10
8
  import {
11
- CallSignatureDeclarationStructure,
12
- MethodSignatureStructure,
13
- OptionalKind,
14
9
  Project,
15
- SourceFile,
16
10
  StatementStructures,
17
11
  StructureKind,
18
12
  VariableDeclarationKind,
19
13
  VariableStatementStructure,
20
- WriterFunction,
21
- Writers
14
+ WriterFunction
22
15
  } from "ts-morph";
16
+ import * as path from "path";
23
17
 
24
18
  import { getAutorestOptions, getSession } from "../autorestSession";
25
19
  import { transformBaseUrl } from "../transforms/urlTransforms";
26
20
  import { NameType, normalizeName } from "../utils/nameUtils";
27
21
  import { isConstantSchema } from "./schemaHelpers";
28
22
  import { getLanguageMetadata } from "../utils/languageHelpers";
29
- import { getOperationParameters } from "./helpers/getOperationParameters";
30
-
31
- type PathParameter = { name: string; description?: string };
23
+ import { generateMethodShortcutImplementation } from "./generateMethodShortcuts";
24
+ import { Paths } from "./interfaces";
25
+ import { pathDictionary } from "./generateClientDefinition";
32
26
 
33
- type Methods = {
34
- [key: string]: [
35
- {
36
- optionsName: string;
37
- description: string;
38
- hasOptionalOptions: boolean;
39
- returnType: string;
40
- }
41
- ];
42
- };
43
- type Paths = {
44
- [key: string]: {
45
- name: string;
46
- pathParameters: PathParameter[];
47
- methods: Methods;
48
- };
49
- };
50
-
51
- export function generatePathFirstClient(model: CodeModel, project: Project) {
27
+ export function generateClient(model: CodeModel, project: Project) {
52
28
  const name = normalizeName(
53
29
  getLanguageMetadata(model.language).name,
54
30
  NameType.File
55
31
  );
56
- const clientFile = project.createSourceFile(`src/${name}.ts`, undefined, {
57
- overwrite: true
58
- });
59
-
60
- // Get all paths
61
- const importedParameters = new Set<string>();
62
- const importedResponses = new Set<string>();
63
- const pathDictionary: Paths = {};
64
- for (const operationGroup of model.operationGroups) {
65
- for (const operation of operationGroup.operations) {
66
- const operationName = getLanguageMetadata(operation.language).name;
67
- const operationDescription = getLanguageMetadata(operation.language)
68
- .description;
69
- const pathParameters: PathParameter[] =
70
- operation.parameters
71
- ?.filter(p => p.protocol.http?.in === ParameterLocation.Path)
72
- .map(p => {
73
- const languageMetadata = getLanguageMetadata(p.language);
74
- return {
75
- name: languageMetadata.serializedName || languageMetadata.name,
76
- description: languageMetadata.description
77
- };
78
- }) || [];
79
-
80
- for (const request of operation.requests || []) {
81
- const path: string = (request.protocol.http?.path as string) || "";
82
- const method = request.protocol.http?.method;
83
-
84
- if (path && method) {
85
- if (!pathDictionary[path]) {
86
- pathDictionary[path] = {
87
- pathParameters,
88
- methods: {},
89
- name: operationName
90
- };
91
- }
92
- const hasOptionalOptions = !hasRequiredOptions(operation);
93
-
94
- const newMethod = {
95
- description: operationDescription,
96
- optionsName: getOperationOptionsType(operation, importedParameters),
97
- hasOptionalOptions,
98
- returnType: `Promise<${getOperationReturnType(
99
- operation,
100
- importedResponses
101
- )}>`
102
- };
103
-
104
- if (pathDictionary[path].methods[`${method}`]) {
105
- pathDictionary[path].methods[`${method}`].push(newMethod);
106
- } else {
107
- pathDictionary[path].methods[`${method}`] = [newMethod];
108
- }
109
- }
110
- }
32
+ const { srcPath } = getAutorestOptions();
33
+ const clientFile = project.createSourceFile(
34
+ path.join(srcPath, `${name}.ts`),
35
+ undefined,
36
+ {
37
+ overwrite: true
111
38
  }
112
- }
113
-
114
- clientFile.addInterface({
115
- name: "Routes",
116
- isExported: true,
117
- callSignatures: getPathFirstRoutesInterfaceDefinition(
118
- pathDictionary,
119
- clientFile
120
- )
121
- });
39
+ );
122
40
 
41
+ // Get all paths
123
42
  const clientName = getLanguageMetadata(model.language).name;
124
43
  const uriParameter = getClientUriParameter();
125
44
 
126
- const { addCredentials, credentialKeyHeaderName } = getAutorestOptions();
45
+ const { addCredentials, credentialKeyHeaderName, multiClient, batch, credentialScopes } = getAutorestOptions();
127
46
  const credentialTypes = addCredentials ? ["TokenCredential"] : [];
128
47
 
129
48
  if (credentialKeyHeaderName) {
@@ -132,84 +51,59 @@ export function generatePathFirstClient(model: CodeModel, project: Project) {
132
51
 
133
52
  const commonClientParams = [
134
53
  ...(uriParameter ? [{ name: uriParameter, type: "string" }] : []),
135
- ...(addCredentials === false
54
+ ...(addCredentials === false || !credentialScopes || credentialScopes.length === 0
136
55
  ? []
137
56
  : [{ name: "credentials", type: credentialTypes.join(" | ") }])
138
57
  ];
139
- const clientIterfaceName = `${clientName}RestClient`;
140
- // const factoryTypeName = `${clientName}Factory`;
141
- clientFile.addTypeAlias({
142
- isExported: true,
143
- name: clientIterfaceName,
144
- type: Writers.intersectionType(
145
- "Client",
146
- Writers.objectType({ properties: [{ name: "path", type: "Routes" }] })
147
- )
148
- });
58
+ const clientInterfaceName = `${clientName}Like`;
149
59
 
150
- clientFile.addFunction({
60
+ const functionStatement = {
151
61
  isExported: true,
152
- name: clientName,
62
+ name: `${clientName}`,
153
63
  parameters: [
154
64
  ...commonClientParams,
155
65
  { name: "options", type: "ClientOptions = {}" }
156
66
  ],
157
- returnType: clientIterfaceName,
158
- isDefaultExport: true,
159
- statements: getClientFactoryBody(clientIterfaceName)
160
- });
161
-
162
- if (importedParameters.size) {
163
- clientFile.addImportDeclaration({
164
- namedImports: [...importedParameters],
165
- moduleSpecifier: "./parameters"
166
- });
167
- }
67
+ returnType: clientInterfaceName,
68
+ isDefaultExport: false,
69
+ statements: getClientFactoryBody(clientInterfaceName, pathDictionary)
70
+ };
168
71
 
169
- if (importedResponses.size) {
170
- clientFile.addImportDeclaration({
171
- namedImports: [...importedResponses],
172
- moduleSpecifier: "./responses"
173
- });
72
+ if (!multiClient || !batch || batch.length === 1) {
73
+ functionStatement.isDefaultExport = true;
174
74
  }
75
+ clientFile.addFunction(functionStatement);
175
76
 
176
77
  clientFile.addImportDeclarations([
177
78
  {
178
- namedImports: ["getClient", "ClientOptions", "Client"],
79
+ namedImports: ["getClient", "ClientOptions"],
179
80
  moduleSpecifier: "@azure-rest/core-client"
180
81
  }
181
82
  ]);
182
83
 
84
+ if (addCredentials && credentialScopes && credentialScopes.length > 0) {
85
+ clientFile.addImportDeclarations([
86
+ {
87
+ namedImports: credentialTypes,
88
+ moduleSpecifier: "@azure/core-auth"
89
+ }
90
+ ]);
91
+ }
183
92
  clientFile.addImportDeclarations([
184
93
  {
185
- namedImports: credentialTypes,
186
- moduleSpecifier: "@azure/core-auth"
94
+ namedImports: [`${clientInterfaceName}`],
95
+ moduleSpecifier: "./clientDefinitions"
187
96
  }
188
97
  ]);
189
98
  }
190
99
 
191
- function hasRequiredOptions(operation: Operation) {
192
- return getOperationParameters(operation)
193
- .filter(p => p.implementation === ImplementationLocation.Method)
194
- .filter(p => ["query", "body", "headers"].includes(p.protocol.http?.in))
195
- .some(p => p.required);
196
- }
197
-
198
- function getOperationOptionsType(
199
- operation: Operation,
200
- importedParameters = new Set<string>()
201
- ) {
202
- const paramsName = `${
203
- getLanguageMetadata(operation.language).name
204
- }Parameters`;
205
- importedParameters.add(paramsName);
206
-
207
- return paramsName;
208
- }
209
-
210
100
  function getClientFactoryBody(
211
- clientTypeName: string
101
+ clientTypeName: string,
102
+ paths: Paths
212
103
  ): string | WriterFunction | (string | WriterFunction | StatementStructures)[] {
104
+ const { rlcShortcut, packageDetails } = getAutorestOptions();
105
+ let clientPackageName = packageDetails.nameWithoutScope;
106
+ const packageVersion = packageDetails.version;
213
107
  const { model } = getSession();
214
108
  const { endpoint, parameterName } = transformBaseUrl(model);
215
109
  let baseUrl: string;
@@ -229,6 +123,30 @@ function getClientFactoryBody(
229
123
  apiVersionStatement = `options.apiVersion = options.apiVersion ?? "${apiVersion}"`;
230
124
  }
231
125
 
126
+ if (!clientPackageName.endsWith("-rest")) {
127
+ clientPackageName = clientPackageName + "-rest";
128
+ }
129
+ const userAgentInfoStatement =
130
+ "const userAgentInfo = `azsdk-js-" +
131
+ clientPackageName +
132
+ "/" +
133
+ packageVersion +
134
+ "`;";
135
+ const userAgentPrefix =
136
+ "options.userAgentOptions && options.userAgentOptions.userAgentPrefix ? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`: `${userAgentInfo}`;";
137
+ const userAgentStatement: VariableStatementStructure = {
138
+ kind: StructureKind.VariableStatement,
139
+ declarationKind: VariableDeclarationKind.Const,
140
+ declarations: [{ name: "userAgentPrefix", initializer: userAgentPrefix }]
141
+ };
142
+
143
+ const userAgentOptionsStatement = `options = {
144
+ ...options,
145
+ userAgentOptions: {
146
+ userAgentPrefix
147
+ }
148
+ }`;
149
+
232
150
  const baseUrlStatement: VariableStatementStructure = {
233
151
  kind: StructureKind.VariableStatement,
234
152
  declarationKind: VariableDeclarationKind.Const,
@@ -254,17 +172,43 @@ function getClientFactoryBody(
254
172
  credentials: {
255
173
  ${scopes}
256
174
  ${apiKeyHeaderName}
257
- }
175
+ },
258
176
  }`
259
177
  : "";
260
178
 
261
- const getClient = `return getClient(
262
- baseUrl,
263
- ${credentials ? "credentials," : ""}
264
- options
265
- ) as ${clientTypeName};`;
179
+ const getClient = `const client = getClient(
180
+ baseUrl, ${credentials ? "credentials," : ""} options
181
+ ) as ${clientTypeName};
182
+ `;
266
183
 
267
- return [baseUrlStatement, apiVersionStatement, credentials, getClient];
184
+ let returnStatement = `return client;`;
185
+
186
+ if (rlcShortcut) {
187
+ const shortcutImplementations = generateMethodShortcutImplementation(
188
+ model,
189
+ paths
190
+ );
191
+ const shortcutBody = Object.keys(shortcutImplementations).map(key => {
192
+ // If the operation group has an empty name, it means its operations are client
193
+ // level operations so we need to spread the definitions. Otherwise they are
194
+ // within an operation group so we add them as key: value
195
+ return `${key ? `"${key}":` : "..."} {${shortcutImplementations[
196
+ key
197
+ ].join()}}`;
198
+ });
199
+ returnStatement = `return { ...client, ${shortcutBody.join()} };`;
200
+ }
201
+
202
+ return [
203
+ baseUrlStatement,
204
+ apiVersionStatement,
205
+ credentials,
206
+ userAgentInfoStatement,
207
+ userAgentStatement,
208
+ userAgentOptionsStatement,
209
+ getClient,
210
+ returnStatement
211
+ ];
268
212
  }
269
213
 
270
214
  function getApiVersion(): string | undefined {
@@ -291,103 +235,8 @@ function getApiVersion(): string | undefined {
291
235
  return undefined;
292
236
  }
293
237
 
294
- function getOperationReturnType(
295
- operation: Operation,
296
- importedResponses = new Set<string>()
297
- ) {
298
- let returnType: string = "HttpResponse";
299
- if (operation.responses && operation.responses.length) {
300
- const responses = [...operation.responses, ...(operation.exceptions || [])];
301
-
302
- const responseTypes = responses
303
- .filter(
304
- r => r.protocol.http?.statusCodes && r.protocol.http?.statusCodes.length
305
- )
306
- .map(r => {
307
- const responseName = getResponseTypeName(operation, r);
308
- importedResponses.add(responseName);
309
- return responseName;
310
- });
311
-
312
- if (responseTypes.length) {
313
- returnType = responseTypes.join(" | ");
314
- }
315
- }
316
-
317
- return returnType;
318
- }
319
-
320
- function getPathFirstRoutesInterfaceDefinition(
321
- paths: Paths,
322
- sourcefile: SourceFile
323
- ): CallSignatureDeclarationStructure[] {
324
- const signatures: CallSignatureDeclarationStructure[] = [];
325
- for (const key of Object.keys(paths)) {
326
- generatePathFirstRouteMethodsDefinition(
327
- paths[key].name,
328
- paths[key].methods,
329
- sourcefile
330
- );
331
- const pathParams = paths[key].pathParameters;
332
- signatures.push({
333
- docs: [
334
- `Resource for '${key
335
- .replace(/}/g, "\\}")
336
- .replace(
337
- /{/g,
338
- "\\{"
339
- )}' has methods for the following verbs: ${Object.keys(
340
- paths[key].methods
341
- ).join(", ")}`
342
- ],
343
- parameters: [
344
- { name: "path", type: `"${key}"` },
345
- ...pathParams.map(p => {
346
- return { name: p.name, type: "string", description: p.description };
347
- })
348
- ],
349
- returnType: paths[key].name,
350
- kind: StructureKind.CallSignature
351
- });
352
- }
353
- return signatures;
354
- }
355
-
356
238
  function getClientUriParameter() {
357
239
  const { model } = getSession();
358
240
  const { parameterName } = transformBaseUrl(model);
359
241
  return parameterName;
360
242
  }
361
-
362
- function generatePathFirstRouteMethodsDefinition(
363
- operationName: string,
364
- methods: Methods,
365
- file: SourceFile
366
- ): void {
367
- const methodDefinitions: OptionalKind<MethodSignatureStructure>[] = [];
368
- for (const key of Object.keys(methods)) {
369
- const method = methods[key];
370
- const description = methods[key][0].description;
371
-
372
- let areAllOptional = !method.some(m => !m.hasOptionalOptions);
373
-
374
- methodDefinitions.push({
375
- name: key,
376
- ...(description && { docs: [{ description }] }),
377
- parameters: [
378
- {
379
- name: "options",
380
- hasQuestionToken: areAllOptional,
381
- type: method.map(m => m.optionsName).join(" | ")
382
- }
383
- ],
384
- returnType: method.map(m => m.returnType).join(" | ")
385
- });
386
- }
387
-
388
- file.addInterface({
389
- methods: methodDefinitions,
390
- name: operationName,
391
- isExported: true
392
- });
393
- }