@autorest/typescript 6.0.0-beta.15 → 6.0.0-beta.16

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 (219) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/README.md +37 -3
  3. package/dist/src/autorestSession.d.ts +8 -3
  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.js +6 -6
  19. package/dist/src/generators/operationGenerator.js.map +1 -1
  20. package/dist/src/generators/samples/sampleEnv.hbs +4 -0
  21. package/dist/src/generators/samples/sampleEnvGenerator.d.ts +3 -0
  22. package/dist/src/generators/samples/sampleEnvGenerator.d.ts.map +1 -0
  23. package/dist/src/generators/samples/sampleEnvGenerator.js +17 -0
  24. package/dist/src/generators/samples/sampleEnvGenerator.js.map +1 -0
  25. package/dist/src/generators/samples/sampleGenerator.d.ts +13 -0
  26. package/dist/src/generators/samples/sampleGenerator.d.ts.map +1 -0
  27. package/dist/src/generators/samples/sampleGenerator.js +43 -0
  28. package/dist/src/generators/samples/sampleGenerator.js.map +1 -0
  29. package/dist/src/generators/static/README.md.hbs +19 -0
  30. package/dist/src/generators/static/karma.conf.js.hbs +141 -0
  31. package/dist/src/generators/static/karmaConfigFileGenerator.d.ts +3 -0
  32. package/dist/src/generators/static/karmaConfigFileGenerator.d.ts.map +1 -0
  33. package/dist/src/generators/static/karmaConfigFileGenerator.js +23 -0
  34. package/dist/src/generators/static/karmaConfigFileGenerator.js.map +1 -0
  35. package/dist/src/generators/static/packageFileGenerator.d.ts.map +1 -1
  36. package/dist/src/generators/static/packageFileGenerator.js +90 -14
  37. package/dist/src/generators/static/packageFileGenerator.js.map +1 -1
  38. package/dist/src/generators/static/rollupConfigFileGenerator.d.ts.map +1 -1
  39. package/dist/src/generators/static/rollupConfigFileGenerator.js +7 -70
  40. package/dist/src/generators/static/rollupConfigFileGenerator.js.map +1 -1
  41. package/dist/src/generators/static/samples.ts.hbs +49 -0
  42. package/dist/src/generators/static/tsConfigFileGenerator.d.ts.map +1 -1
  43. package/dist/src/generators/static/tsConfigFileGenerator.js +9 -10
  44. package/dist/src/generators/static/tsConfigFileGenerator.js.map +1 -1
  45. package/dist/src/generators/test/envBrowserFileGenerator.d.ts +3 -0
  46. package/dist/src/generators/test/envBrowserFileGenerator.d.ts.map +1 -0
  47. package/dist/src/generators/test/envBrowserFileGenerator.js +15 -0
  48. package/dist/src/generators/test/envBrowserFileGenerator.js.map +1 -0
  49. package/dist/src/generators/test/envFileGenerator.d.ts +3 -0
  50. package/dist/src/generators/test/envFileGenerator.d.ts.map +1 -0
  51. package/dist/src/generators/test/envFileGenerator.js +23 -0
  52. package/dist/src/generators/test/envFileGenerator.js.map +1 -0
  53. package/dist/src/generators/test/recordedClientFileGenerator.d.ts +3 -0
  54. package/dist/src/generators/test/recordedClientFileGenerator.d.ts.map +1 -0
  55. package/dist/src/generators/test/recordedClientFileGenerator.js +23 -0
  56. package/dist/src/generators/test/recordedClientFileGenerator.js.map +1 -0
  57. package/dist/src/generators/test/rlcEnv.ts.hbs +3 -0
  58. package/dist/src/generators/test/rlcRecordedClient.ts.hbs +35 -0
  59. package/dist/src/generators/test/rlcSampleTest.spec.ts.hbs +19 -0
  60. package/dist/src/generators/{static → test}/sampleTest.ts.hbs +0 -0
  61. package/dist/src/generators/test/sampleTestGenerator.d.ts.map +1 -1
  62. package/dist/src/generators/test/sampleTestGenerator.js +23 -7
  63. package/dist/src/generators/test/sampleTestGenerator.js.map +1 -1
  64. package/dist/src/generators/utils/pagingOperations.d.ts +5 -0
  65. package/dist/src/generators/utils/pagingOperations.d.ts.map +1 -1
  66. package/dist/src/generators/utils/pagingOperations.js +2 -1
  67. package/dist/src/generators/utils/pagingOperations.js.map +1 -1
  68. package/dist/src/main.d.ts +2 -2
  69. package/dist/src/main.d.ts.map +1 -1
  70. package/dist/src/main.js +3 -3
  71. package/dist/src/main.js.map +1 -1
  72. package/dist/src/models/clientDetails.d.ts +3 -0
  73. package/dist/src/models/clientDetails.d.ts.map +1 -1
  74. package/dist/src/models/modelDetails.d.ts +2 -2
  75. package/dist/src/models/modelDetails.d.ts.map +1 -1
  76. package/dist/src/models/operationDetails.d.ts +1 -0
  77. package/dist/src/models/operationDetails.d.ts.map +1 -1
  78. package/dist/src/models/sampleDetails.d.ts +22 -0
  79. package/dist/src/models/sampleDetails.d.ts.map +1 -0
  80. package/dist/src/models/sampleDetails.js +3 -0
  81. package/dist/src/models/sampleDetails.js.map +1 -0
  82. package/dist/src/restLevelClient/generateClient.d.ts +1 -1
  83. package/dist/src/restLevelClient/generateClient.d.ts.map +1 -1
  84. package/dist/src/restLevelClient/generateClient.js +48 -164
  85. package/dist/src/restLevelClient/generateClient.js.map +1 -1
  86. package/dist/src/restLevelClient/generateClientDefinition.d.ts +6 -0
  87. package/dist/src/restLevelClient/generateClientDefinition.d.ts.map +1 -0
  88. package/dist/src/restLevelClient/generateClientDefinition.js +231 -0
  89. package/dist/src/restLevelClient/generateClientDefinition.js.map +1 -0
  90. package/dist/src/restLevelClient/generateMethodShortcuts.d.ts +8 -0
  91. package/dist/src/restLevelClient/generateMethodShortcuts.d.ts.map +1 -0
  92. package/dist/src/restLevelClient/generateMethodShortcuts.js +70 -0
  93. package/dist/src/restLevelClient/generateMethodShortcuts.js.map +1 -0
  94. package/dist/src/restLevelClient/generateObjectTypes.d.ts.map +1 -1
  95. package/dist/src/restLevelClient/generateObjectTypes.js.map +1 -1
  96. package/dist/src/restLevelClient/generatePagingHelper.d.ts.map +1 -1
  97. package/dist/src/restLevelClient/generatePagingHelper.js +2 -1
  98. package/dist/src/restLevelClient/generatePagingHelper.js.map +1 -1
  99. package/dist/src/restLevelClient/generateParameterTypes.d.ts.map +1 -1
  100. package/dist/src/restLevelClient/generateParameterTypes.js +104 -18
  101. package/dist/src/restLevelClient/generateParameterTypes.js.map +1 -1
  102. package/dist/src/restLevelClient/generatePollingHelper.d.ts.map +1 -1
  103. package/dist/src/restLevelClient/generatePollingHelper.js +3 -1
  104. package/dist/src/restLevelClient/generatePollingHelper.js.map +1 -1
  105. package/dist/src/restLevelClient/generateResponseTypes.d.ts.map +1 -1
  106. package/dist/src/restLevelClient/generateResponseTypes.js +4 -1
  107. package/dist/src/restLevelClient/generateResponseTypes.js.map +1 -1
  108. package/dist/src/restLevelClient/generateRestLevel.d.ts.map +1 -1
  109. package/dist/src/restLevelClient/generateRestLevel.js +19 -3
  110. package/dist/src/restLevelClient/generateRestLevel.js.map +1 -1
  111. package/dist/src/restLevelClient/generateSchemaTypes.d.ts.map +1 -1
  112. package/dist/src/restLevelClient/generateSchemaTypes.js +5 -2
  113. package/dist/src/restLevelClient/generateSchemaTypes.js.map +1 -1
  114. package/dist/src/restLevelClient/generateTopLevelIndexFile.d.ts +4 -0
  115. package/dist/src/restLevelClient/generateTopLevelIndexFile.d.ts.map +1 -0
  116. package/dist/src/restLevelClient/generateTopLevelIndexFile.js +41 -0
  117. package/dist/src/restLevelClient/generateTopLevelIndexFile.js.map +1 -0
  118. package/dist/src/restLevelClient/helpers/operationHelpers.d.ts +10 -0
  119. package/dist/src/restLevelClient/helpers/operationHelpers.d.ts.map +1 -0
  120. package/dist/src/restLevelClient/helpers/operationHelpers.js +64 -0
  121. package/dist/src/restLevelClient/helpers/operationHelpers.js.map +1 -0
  122. package/dist/src/restLevelClient/interfaces.d.ts +24 -0
  123. package/dist/src/restLevelClient/interfaces.d.ts.map +1 -0
  124. package/dist/src/restLevelClient/interfaces.js +3 -0
  125. package/dist/src/restLevelClient/interfaces.js.map +1 -0
  126. package/dist/src/restLevelClient/operationHelpers.d.ts.map +1 -1
  127. package/dist/src/restLevelClient/operationHelpers.js +4 -1
  128. package/dist/src/restLevelClient/operationHelpers.js.map +1 -1
  129. package/dist/src/restLevelClient/schemaHelpers.d.ts.map +1 -1
  130. package/dist/src/restLevelClient/schemaHelpers.js +4 -1
  131. package/dist/src/restLevelClient/schemaHelpers.js.map +1 -1
  132. package/dist/src/transforms/operationTransforms.d.ts.map +1 -1
  133. package/dist/src/transforms/operationTransforms.js +1 -0
  134. package/dist/src/transforms/operationTransforms.js.map +1 -1
  135. package/dist/src/transforms/samplesTransforms.d.ts +7 -0
  136. package/dist/src/transforms/samplesTransforms.d.ts.map +1 -0
  137. package/dist/src/transforms/samplesTransforms.js +283 -0
  138. package/dist/src/transforms/samplesTransforms.js.map +1 -0
  139. package/dist/src/transforms/transforms.d.ts +1 -2
  140. package/dist/src/transforms/transforms.d.ts.map +1 -1
  141. package/dist/src/transforms/transforms.js +4 -3
  142. package/dist/src/transforms/transforms.js.map +1 -1
  143. package/dist/src/typescriptGenerator.d.ts +2 -2
  144. package/dist/src/typescriptGenerator.d.ts.map +1 -1
  145. package/dist/src/typescriptGenerator.js +18 -7
  146. package/dist/src/typescriptGenerator.js.map +1 -1
  147. package/dist/src/utils/autorestOptions.d.ts +2 -2
  148. package/dist/src/utils/autorestOptions.d.ts.map +1 -1
  149. package/dist/src/utils/autorestOptions.js +70 -30
  150. package/dist/src/utils/autorestOptions.js.map +1 -1
  151. package/dist/src/utils/nameUtils.d.ts +6 -2
  152. package/dist/src/utils/nameUtils.d.ts.map +1 -1
  153. package/dist/src/utils/nameUtils.js +9 -5
  154. package/dist/src/utils/nameUtils.js.map +1 -1
  155. package/dist/src/utils/schemaHelpers.js +8 -3
  156. package/dist/src/utils/schemaHelpers.js.map +1 -1
  157. package/package.json +11 -5
  158. package/src/autorestSession.ts +16 -15
  159. package/src/conflictResolver.ts +0 -1
  160. package/src/generators/clientFileGenerator.ts +262 -33
  161. package/src/generators/indexGenerator.ts +78 -9
  162. package/src/generators/modelsGenerator.ts +21 -1
  163. package/src/generators/operationGenerator.ts +7 -7
  164. package/src/generators/samples/sampleEnv.hbs +4 -0
  165. package/src/generators/samples/sampleEnvGenerator.ts +14 -0
  166. package/src/generators/samples/sampleGenerator.ts +50 -0
  167. package/src/generators/static/README.md.hbs +19 -0
  168. package/src/generators/static/karma.conf.js.hbs +141 -0
  169. package/src/generators/static/karmaConfigFileGenerator.ts +20 -0
  170. package/src/generators/static/packageFileGenerator.ts +99 -14
  171. package/src/generators/static/rollupConfigFileGenerator.ts +7 -70
  172. package/src/generators/static/samples.ts.hbs +49 -0
  173. package/src/generators/static/tsConfigFileGenerator.ts +12 -12
  174. package/src/generators/test/envBrowserFileGenerator.ts +14 -0
  175. package/src/generators/test/envFileGenerator.ts +22 -0
  176. package/src/generators/test/recordedClientFileGenerator.ts +22 -0
  177. package/src/generators/test/rlcEnv.ts.hbs +3 -0
  178. package/src/generators/test/rlcRecordedClient.ts.hbs +35 -0
  179. package/src/generators/test/rlcSampleTest.spec.ts.hbs +19 -0
  180. package/src/generators/{static → test}/sampleTest.ts.hbs +0 -0
  181. package/src/generators/test/sampleTestGenerator.ts +23 -8
  182. package/src/generators/utils/pagingOperations.ts +1 -1
  183. package/src/main.ts +9 -5
  184. package/src/models/clientDetails.ts +3 -0
  185. package/src/models/modelDetails.ts +2 -2
  186. package/src/models/operationDetails.ts +1 -0
  187. package/src/models/sampleDetails.ts +22 -0
  188. package/src/restLevelClient/generateClient.ts +57 -253
  189. package/src/restLevelClient/generateClientDefinition.ts +343 -0
  190. package/src/restLevelClient/generateMethodShortcuts.ts +121 -0
  191. package/src/restLevelClient/generateObjectTypes.ts +1 -3
  192. package/src/restLevelClient/generatePagingHelper.ts +3 -2
  193. package/src/restLevelClient/generateParameterTypes.ts +155 -25
  194. package/src/restLevelClient/generatePollingHelper.ts +3 -3
  195. package/src/restLevelClient/generateResponseTypes.ts +4 -1
  196. package/src/restLevelClient/generateRestLevel.ts +24 -6
  197. package/src/restLevelClient/generateSchemaTypes.ts +5 -3
  198. package/src/restLevelClient/generateTopLevelIndexFile.ts +41 -0
  199. package/src/restLevelClient/helpers/operationHelpers.ts +93 -0
  200. package/src/restLevelClient/interfaces.ts +26 -0
  201. package/src/restLevelClient/operationHelpers.ts +8 -2
  202. package/src/restLevelClient/schemaHelpers.ts +4 -2
  203. package/src/transforms/operationTransforms.ts +1 -0
  204. package/src/transforms/samplesTransforms.ts +300 -0
  205. package/src/transforms/transforms.ts +6 -6
  206. package/src/typescriptGenerator.ts +19 -10
  207. package/src/utils/autorestOptions.ts +121 -58
  208. package/src/utils/nameUtils.ts +16 -8
  209. package/src/utils/schemaHelpers.ts +2 -2
  210. package/dist/src/generators/clientContextFileGenerator.d.ts +0 -5
  211. package/dist/src/generators/clientContextFileGenerator.d.ts.map +0 -1
  212. package/dist/src/generators/clientContextFileGenerator.js +0 -263
  213. package/dist/src/generators/clientContextFileGenerator.js.map +0 -1
  214. package/dist/src/restLevelClient/helpers/getOperationParameters.d.ts +0 -6
  215. package/dist/src/restLevelClient/helpers/getOperationParameters.d.ts.map +0 -1
  216. package/dist/src/restLevelClient/helpers/getOperationParameters.js +0 -29
  217. package/dist/src/restLevelClient/helpers/getOperationParameters.js.map +0 -1
  218. package/src/generators/clientContextFileGenerator.ts +0 -405
  219. package/src/restLevelClient/helpers/getOperationParameters.ts +0 -37
@@ -0,0 +1,3 @@
1
+ import * as dotenv from "dotenv";
2
+
3
+ dotenv.config();
@@ -0,0 +1,35 @@
1
+ import { Context } from "mocha";
2
+ import { env, record, RecorderEnvironmentSetup, Recorder } from "@azure-tools/test-recorder";
3
+ import "./env";
4
+
5
+ const recorderEnvSetup: RecorderEnvironmentSetup = {
6
+ replaceableVariables: {
7
+ ENDPOINT: "https://endpoint",
8
+ AZURE_CLIENT_ID: "azure_client_id",
9
+ AZURE_CLIENT_SECRET: "azure_client_secret",
10
+ AZURE_TENANT_ID: "88888888-8888-8888-8888-888888888888",
11
+ SUBSCRIPTION_ID: "azure_subscription_id"
12
+ },
13
+ customizationsOnRecordings: [
14
+ (recording: any): any =>
15
+ recording.replace(/"access_token":"[^"]*"/g, `"access_token":"access_token"`),
16
+ // If we put ENDPOINT in replaceableVariables above, it will not capture
17
+ // the endpoint string used with nock, which will be expanded to
18
+ // https://<endpoint>:443/ and therefore will not match, so we have to do
19
+ // this instead.
20
+ (recording: string): string => {
21
+ const replaced = recording.replace("endpoint:443", "endpoint");
22
+ return replaced;
23
+ },
24
+ ],
25
+ queryParametersToSkip: []
26
+ };
27
+
28
+ /**
29
+ * creates the recorder and reads the environment variables from the `.env` file.
30
+ * Should be called first in the test suite to make sure environment variables are
31
+ * read before they are being used.
32
+ */
33
+ export function createRecorder(context: Context): Recorder {
34
+ return record(context, recorderEnvSetup);
35
+ }
@@ -0,0 +1,19 @@
1
+ import { Recorder } from "@azure-tools/test-recorder";
2
+ import * as assert from "assert";
3
+ import { createRecorder } from "./utils/recordedClient";
4
+
5
+ describe("My test", () => {
6
+ let recorder: Recorder;
7
+
8
+ beforeEach(async function() {
9
+ recorder = createRecorder(this);
10
+ });
11
+
12
+ afterEach(async function() {
13
+ await recorder.stop();
14
+ });
15
+
16
+ it("sample test", async function() {
17
+ console.log("Hi, I'm a test!");
18
+ });
19
+ });
@@ -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 {
@@ -9,8 +12,9 @@ import {
9
12
  initializeSession,
10
13
  getAutorestOptions
11
14
  } from "./autorestSession";
15
+ import { serialize } from "@azure-tools/codegen";
12
16
 
13
- export async function processRequest(host: Host) {
17
+ export async function processRequest(host: AutorestExtensionHost) {
14
18
  await initializeSession(host);
15
19
  const session = getSession();
16
20
  const { restLevelClient } = getAutorestOptions();
@@ -19,7 +23,7 @@ export async function processRequest(host: Host) {
19
23
  restLevelClient
20
24
  ? await generateRestLevelClient()
21
25
  : await generateTypeScriptLibrary(session.model, host);
22
- session.log(`Autorest.Typescript took ${Date.now() - start}ms`, "");
26
+ session.info(`Autorest.Typescript took ${Date.now() - start}ms`);
23
27
  } catch (err) {
24
28
  session.error("An error was encountered while handling a request:", err);
25
29
  throw err;
@@ -28,8 +32,8 @@ export async function processRequest(host: Host) {
28
32
 
29
33
  async function main() {
30
34
  const pluginHost = new AutoRestExtension();
31
- pluginHost.Add("typescript", processRequest);
32
- await pluginHost.Run();
35
+ pluginHost.add("typescript", processRequest);
36
+ await pluginHost.run();
33
37
  }
34
38
 
35
39
  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>;
@@ -32,4 +33,6 @@ export interface ClientDetails {
32
33
  parameters: ParameterDetails[];
33
34
  options: ClientOptions;
34
35
  endpoint: EndpointDetails;
36
+ samples?: SampleGroup[];
37
+ allTypes: string[];
35
38
  }
@@ -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
+ }
@@ -2,136 +2,43 @@ import {
2
2
  CodeModel,
3
3
  Operation,
4
4
  ParameterLocation,
5
- ImplementationLocation,
6
- Schema,
7
- SchemaContext
5
+ ImplementationLocation
8
6
  } from "@autorest/codemodel";
9
7
 
10
- import { getResponseTypeName } from "./operationHelpers";
11
-
12
8
  import {
13
- CallSignatureDeclarationStructure,
14
- MethodSignatureStructure,
15
- OptionalKind,
16
9
  Project,
17
- SourceFile,
18
10
  StatementStructures,
19
11
  StructureKind,
20
12
  VariableDeclarationKind,
21
13
  VariableStatementStructure,
22
- WriterFunction,
23
- Writers
14
+ WriterFunction
24
15
  } from "ts-morph";
16
+ import * as path from 'path';
25
17
 
26
18
  import { getAutorestOptions, getSession } from "../autorestSession";
27
19
  import { transformBaseUrl } from "../transforms/urlTransforms";
28
20
  import { NameType, normalizeName } from "../utils/nameUtils";
29
- import { isConstantSchema, getElementType } from "./schemaHelpers";
21
+ import { isConstantSchema } from "./schemaHelpers";
30
22
  import { getLanguageMetadata } from "../utils/languageHelpers";
31
- import { getOperationParameters } from "./helpers/getOperationParameters";
32
- import { ParameterPath } from "@azure/core-http";
33
-
34
- type PathParameter = { name: string; schema: Schema, description?: string };
35
-
36
- type Methods = {
37
- [key: string]: [
38
- {
39
- optionsName: string;
40
- description: string;
41
- hasOptionalOptions: boolean;
42
- returnType: string;
43
- }
44
- ];
45
- };
46
- type Paths = {
47
- [key: string]: {
48
- name: string;
49
- pathParameters: PathParameter[];
50
- methods: Methods;
51
- };
52
- };
23
+ import { generateMethodShortcutImplementation } from "./generateMethodShortcuts";
24
+ import { Paths } from "./interfaces";
25
+ import { pathDictionary } from './generateClientDefinition';
53
26
 
54
- export function generatePathFirstClient(model: CodeModel, project: Project) {
27
+ export function generateClient(model: CodeModel, project: Project) {
55
28
  const name = normalizeName(
56
29
  getLanguageMetadata(model.language).name,
57
30
  NameType.File
58
31
  );
59
- const clientFile = project.createSourceFile(`src/${name}.ts`, undefined, {
32
+ const { srcPath } = getAutorestOptions();
33
+ const clientFile = project.createSourceFile(path.join(srcPath, `${name}.ts`), undefined, {
60
34
  overwrite: true
61
35
  });
62
36
 
63
37
  // Get all paths
64
- const importedParameters = new Set<string>();
65
- const importedResponses = new Set<string>();
66
- const pathDictionary: Paths = {};
67
- for (const operationGroup of model.operationGroups) {
68
- for (const operation of operationGroup.operations) {
69
- const operationName = getLanguageMetadata(operation.language).name;
70
- const operationDescription = getLanguageMetadata(operation.language)
71
- .description;
72
- const pathParameters: PathParameter[] =
73
- operation.parameters
74
- ?.filter(p => p.protocol.http?.in === ParameterLocation.Path)
75
- .map(p => {
76
- const languageMetadata = getLanguageMetadata(p.language);
77
- return {
78
- name: languageMetadata.serializedName || languageMetadata.name,
79
- schema: p.schema,
80
- description: languageMetadata.description
81
- };
82
- }) || [];
83
- const path: string = operation.requests?.[0].protocol.http?.path;
84
- pathParameters.sort(function compare(a: PathParameter, b: PathParameter) {
85
- return path.indexOf(a.name) - path.indexOf(b.name);
86
- })
87
-
88
- for (const request of operation.requests || []) {
89
- const path: string = (request.protocol.http?.path as string) || "";
90
- const method = request.protocol.http?.method;
91
-
92
- if (path && method) {
93
- if (!pathDictionary[path]) {
94
- pathDictionary[path] = {
95
- pathParameters,
96
- methods: {},
97
- name: operationName
98
- };
99
- }
100
- const hasOptionalOptions = !hasRequiredOptions(operation);
101
-
102
- const newMethod = {
103
- description: operationDescription,
104
- optionsName: getOperationOptionsType(operation, importedParameters),
105
- hasOptionalOptions,
106
- returnType: `Promise<${getOperationReturnType(
107
- operation,
108
- importedResponses
109
- )}>`
110
- };
111
-
112
- if (pathDictionary[path].methods[`${method}`]) {
113
- pathDictionary[path].methods[`${method}`].push(newMethod);
114
- } else {
115
- pathDictionary[path].methods[`${method}`] = [newMethod];
116
- }
117
- }
118
- }
119
- }
120
- }
121
-
122
- clientFile.addInterface({
123
- name: "Routes",
124
- isExported: true,
125
- callSignatures: getPathFirstRoutesInterfaceDefinition(
126
- pathDictionary,
127
- clientFile
128
- )
129
- });
130
-
131
38
  const clientName = getLanguageMetadata(model.language).name;
132
39
  const uriParameter = getClientUriParameter();
133
40
 
134
- const { addCredentials, credentialKeyHeaderName } = getAutorestOptions();
41
+ const { addCredentials, credentialKeyHeaderName, multiClient, batch } = getAutorestOptions();
135
42
  const credentialTypes = addCredentials ? ["TokenCredential"] : [];
136
43
 
137
44
  if (credentialKeyHeaderName) {
@@ -144,46 +51,28 @@ export function generatePathFirstClient(model: CodeModel, project: Project) {
144
51
  ? []
145
52
  : [{ name: "credentials", type: credentialTypes.join(" | ") }])
146
53
  ];
147
- const clientIterfaceName = `${clientName}RestClient`;
148
- // const factoryTypeName = `${clientName}Factory`;
149
- clientFile.addTypeAlias({
150
- isExported: true,
151
- name: clientIterfaceName,
152
- type: Writers.intersectionType(
153
- "Client",
154
- Writers.objectType({ properties: [{ name: "path", type: "Routes" }] })
155
- )
156
- });
54
+ const clientInterfaceName = `${clientName}Like`;
157
55
 
158
- clientFile.addFunction({
56
+ const functionStatement = {
159
57
  isExported: true,
160
- name: clientName,
58
+ name: `${clientName}`,
161
59
  parameters: [
162
60
  ...commonClientParams,
163
61
  { name: "options", type: "ClientOptions = {}" }
164
62
  ],
165
- returnType: clientIterfaceName,
166
- isDefaultExport: true,
167
- statements: getClientFactoryBody(clientIterfaceName)
168
- });
169
-
170
- if (importedParameters.size) {
171
- clientFile.addImportDeclaration({
172
- namedImports: [...importedParameters],
173
- moduleSpecifier: "./parameters"
174
- });
63
+ returnType: clientInterfaceName,
64
+ isDefaultExport: false,
65
+ statements: getClientFactoryBody(clientInterfaceName, pathDictionary)
175
66
  }
176
67
 
177
- if (importedResponses.size) {
178
- clientFile.addImportDeclaration({
179
- namedImports: [...importedResponses],
180
- moduleSpecifier: "./responses"
181
- });
68
+ if (!multiClient || !batch || batch.length === 1) {
69
+ functionStatement.isDefaultExport = true;
182
70
  }
71
+ clientFile.addFunction(functionStatement);
183
72
 
184
73
  clientFile.addImportDeclarations([
185
74
  {
186
- namedImports: ["getClient", "ClientOptions", "Client"],
75
+ namedImports: ["getClient", "ClientOptions"],
187
76
  moduleSpecifier: "@azure-rest/core-client"
188
77
  }
189
78
  ]);
@@ -192,32 +81,19 @@ export function generatePathFirstClient(model: CodeModel, project: Project) {
192
81
  {
193
82
  namedImports: credentialTypes,
194
83
  moduleSpecifier: "@azure/core-auth"
84
+ },
85
+ {
86
+ namedImports: [`${clientInterfaceName}`],
87
+ moduleSpecifier: './clientDefinitions'
195
88
  }
196
89
  ]);
197
90
  }
198
91
 
199
- function hasRequiredOptions(operation: Operation) {
200
- return getOperationParameters(operation)
201
- .filter(p => p.implementation === ImplementationLocation.Method)
202
- .filter(p => ["query", "body", "headers"].includes(p.protocol.http?.in))
203
- .some(p => p.required);
204
- }
205
-
206
- function getOperationOptionsType(
207
- operation: Operation,
208
- importedParameters = new Set<string>()
209
- ) {
210
- const paramsName = `${
211
- getLanguageMetadata(operation.language).name
212
- }Parameters`;
213
- importedParameters.add(paramsName);
214
-
215
- return paramsName;
216
- }
217
-
218
92
  function getClientFactoryBody(
219
- clientTypeName: string
93
+ clientTypeName: string,
94
+ paths: Paths
220
95
  ): string | WriterFunction | (string | WriterFunction | StatementStructures)[] {
96
+ const { rlcShortcut } = getAutorestOptions();
221
97
  const { model } = getSession();
222
98
  const { endpoint, parameterName } = transformBaseUrl(model);
223
99
  let baseUrl: string;
@@ -266,13 +142,36 @@ function getClientFactoryBody(
266
142
  }`
267
143
  : "";
268
144
 
269
- const getClient = `return getClient(
270
- baseUrl,
271
- ${credentials ? "credentials," : ""}
272
- options
273
- ) as ${clientTypeName};`;
145
+ const getClient = `const client = getClient(
146
+ baseUrl, ${credentials ? "credentials," : ""} options
147
+ ) as ${clientTypeName};
148
+ `;
149
+
150
+ let returnStatement = `return client;`;
151
+
152
+ if (rlcShortcut) {
153
+ const shortcutImplementations = generateMethodShortcutImplementation(
154
+ model,
155
+ paths
156
+ );
157
+ const shortcutBody = Object.keys(shortcutImplementations).map(key => {
158
+ // If the operation group has an empty name, it means its operations are client
159
+ // level operations so we need to spread the definitions. Otherwise they are
160
+ // within an operation group so we add them as key: value
161
+ return `${key ? `"${key}":` : "..."} {${shortcutImplementations[
162
+ key
163
+ ].join()}}`;
164
+ });
165
+ returnStatement = `return { ...client, ${shortcutBody.join()} };`;
166
+ }
274
167
 
275
- return [baseUrlStatement, apiVersionStatement, credentials, getClient];
168
+ return [
169
+ baseUrlStatement,
170
+ apiVersionStatement,
171
+ credentials,
172
+ getClient,
173
+ returnStatement
174
+ ];
276
175
  }
277
176
 
278
177
  function getApiVersion(): string | undefined {
@@ -299,103 +198,8 @@ function getApiVersion(): string | undefined {
299
198
  return undefined;
300
199
  }
301
200
 
302
- function getOperationReturnType(
303
- operation: Operation,
304
- importedResponses = new Set<string>()
305
- ) {
306
- let returnType: string = "HttpResponse";
307
- if (operation.responses && operation.responses.length) {
308
- const responses = [...operation.responses, ...(operation.exceptions || [])];
309
-
310
- const responseTypes = responses
311
- .filter(
312
- r => r.protocol.http?.statusCodes && r.protocol.http?.statusCodes.length
313
- )
314
- .map(r => {
315
- const responseName = getResponseTypeName(operation, r);
316
- importedResponses.add(responseName);
317
- return responseName;
318
- });
319
-
320
- if (responseTypes.length) {
321
- returnType = responseTypes.join(" | ");
322
- }
323
- }
324
-
325
- return returnType;
326
- }
327
-
328
- function getPathFirstRoutesInterfaceDefinition(
329
- paths: Paths,
330
- sourcefile: SourceFile
331
- ): CallSignatureDeclarationStructure[] {
332
- const signatures: CallSignatureDeclarationStructure[] = [];
333
- for (const key of Object.keys(paths)) {
334
- generatePathFirstRouteMethodsDefinition(
335
- paths[key].name,
336
- paths[key].methods,
337
- sourcefile
338
- );
339
- const pathParams = paths[key].pathParameters;
340
- signatures.push({
341
- docs: [
342
- `Resource for '${key
343
- .replace(/}/g, "\\}")
344
- .replace(
345
- /{/g,
346
- "\\{"
347
- )}' has methods for the following verbs: ${Object.keys(
348
- paths[key].methods
349
- ).join(", ")}`
350
- ],
351
- parameters: [
352
- { name: "path", type: `"${key}"` },
353
- ...pathParams.map(p => {
354
- return { name: p.name, type: getElementType(p.schema, [SchemaContext.Input, SchemaContext.Exception]), description: p.description };
355
- })
356
- ],
357
- returnType: paths[key].name,
358
- kind: StructureKind.CallSignature
359
- });
360
- }
361
- return signatures;
362
- }
363
-
364
201
  function getClientUriParameter() {
365
202
  const { model } = getSession();
366
203
  const { parameterName } = transformBaseUrl(model);
367
204
  return parameterName;
368
205
  }
369
-
370
- function generatePathFirstRouteMethodsDefinition(
371
- operationName: string,
372
- methods: Methods,
373
- file: SourceFile
374
- ): void {
375
- const methodDefinitions: OptionalKind<MethodSignatureStructure>[] = [];
376
- for (const key of Object.keys(methods)) {
377
- const method = methods[key];
378
- const description = methods[key][0].description;
379
-
380
- let areAllOptional = !method.some(m => !m.hasOptionalOptions);
381
-
382
- methodDefinitions.push({
383
- name: key,
384
- ...(description && { docs: [{ description }] }),
385
- parameters: [
386
- {
387
- name: "options",
388
- hasQuestionToken: areAllOptional,
389
- type: method.map(m => m.optionsName).join(" | ")
390
- }
391
- ],
392
- returnType: method.map(m => m.returnType).join(" | ")
393
- });
394
- }
395
-
396
- file.addInterface({
397
- methods: methodDefinitions,
398
- name: operationName,
399
- isExported: true
400
- });
401
- }