@azure-tools/typespec-ts 0.33.0 → 0.34.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 (199) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/src/framework/declaration.js.map +1 -1
  3. package/dist/src/framework/dependency.d.ts +25 -0
  4. package/dist/src/framework/dependency.d.ts.map +1 -1
  5. package/dist/src/framework/hooks/binder.d.ts +1 -1
  6. package/dist/src/framework/hooks/binder.d.ts.map +1 -1
  7. package/dist/src/framework/hooks/binder.js +16 -12
  8. package/dist/src/framework/hooks/binder.js.map +1 -1
  9. package/dist/src/framework/hooks/sdkTypes.d.ts +4 -2
  10. package/dist/src/framework/hooks/sdkTypes.d.ts.map +1 -1
  11. package/dist/src/framework/hooks/sdkTypes.js +43 -8
  12. package/dist/src/framework/hooks/sdkTypes.js.map +1 -1
  13. package/dist/src/framework/refkey.js +1 -1
  14. package/dist/src/framework/refkey.js.map +1 -1
  15. package/dist/src/framework/sample.js +1 -1
  16. package/dist/src/framework/sample.js.map +1 -1
  17. package/dist/src/index.d.ts +2 -0
  18. package/dist/src/index.d.ts.map +1 -1
  19. package/dist/src/index.js +81 -24
  20. package/dist/src/index.js.map +1 -1
  21. package/dist/src/lib.d.ts +37 -3
  22. package/dist/src/lib.d.ts.map +1 -1
  23. package/dist/src/lib.js +26 -1
  24. package/dist/src/lib.js.map +1 -1
  25. package/dist/src/modular/buildClassicalClient.d.ts +2 -2
  26. package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
  27. package/dist/src/modular/buildClassicalClient.js +25 -61
  28. package/dist/src/modular/buildClassicalClient.js.map +1 -1
  29. package/dist/src/modular/buildClassicalOperationGroups.d.ts.map +1 -1
  30. package/dist/src/modular/buildClassicalOperationGroups.js +0 -7
  31. package/dist/src/modular/buildClassicalOperationGroups.js.map +1 -1
  32. package/dist/src/modular/buildClientContext.d.ts +7 -3
  33. package/dist/src/modular/buildClientContext.d.ts.map +1 -1
  34. package/dist/src/modular/buildClientContext.js +58 -38
  35. package/dist/src/modular/buildClientContext.js.map +1 -1
  36. package/dist/src/modular/buildCodeModel.d.ts +18 -0
  37. package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
  38. package/dist/src/modular/buildCodeModel.js +95 -42
  39. package/dist/src/modular/buildCodeModel.js.map +1 -1
  40. package/dist/src/modular/buildOperations.d.ts +3 -4
  41. package/dist/src/modular/buildOperations.d.ts.map +1 -1
  42. package/dist/src/modular/buildOperations.js +24 -45
  43. package/dist/src/modular/buildOperations.js.map +1 -1
  44. package/dist/src/modular/buildProjectFiles.d.ts.map +1 -1
  45. package/dist/src/modular/buildProjectFiles.js +3 -1
  46. package/dist/src/modular/buildProjectFiles.js.map +1 -1
  47. package/dist/src/modular/buildRestorePoller.js +1 -1
  48. package/dist/src/modular/buildRestorePoller.js.map +1 -1
  49. package/dist/src/modular/buildRootIndex.d.ts +5 -0
  50. package/dist/src/modular/buildRootIndex.d.ts.map +1 -1
  51. package/dist/src/modular/buildRootIndex.js +62 -17
  52. package/dist/src/modular/buildRootIndex.js.map +1 -1
  53. package/dist/src/modular/buildSubpathIndex.d.ts +1 -1
  54. package/dist/src/modular/buildSubpathIndex.d.ts.map +1 -1
  55. package/dist/src/modular/buildSubpathIndex.js +11 -10
  56. package/dist/src/modular/buildSubpathIndex.js.map +1 -1
  57. package/dist/src/modular/emitModels.d.ts +13 -28
  58. package/dist/src/modular/emitModels.d.ts.map +1 -1
  59. package/dist/src/modular/emitModels.js +435 -240
  60. package/dist/src/modular/emitModels.js.map +1 -1
  61. package/dist/src/modular/emitModelsOptions.d.ts +26 -0
  62. package/dist/src/modular/emitModelsOptions.d.ts.map +1 -0
  63. package/dist/src/modular/emitModelsOptions.js +106 -0
  64. package/dist/src/modular/emitModelsOptions.js.map +1 -0
  65. package/dist/src/modular/emitSamples.d.ts +7 -0
  66. package/dist/src/modular/emitSamples.d.ts.map +1 -0
  67. package/dist/src/modular/emitSamples.js +354 -0
  68. package/dist/src/modular/emitSamples.js.map +1 -0
  69. package/dist/src/modular/external-dependencies.d.ts +7 -0
  70. package/dist/src/modular/external-dependencies.d.ts.map +1 -1
  71. package/dist/src/modular/external-dependencies.js +58 -1
  72. package/dist/src/modular/external-dependencies.js.map +1 -1
  73. package/dist/src/modular/helpers/classicalOperationHelpers.d.ts.map +1 -1
  74. package/dist/src/modular/helpers/classicalOperationHelpers.js +2 -2
  75. package/dist/src/modular/helpers/classicalOperationHelpers.js.map +1 -1
  76. package/dist/src/modular/helpers/clientHelpers.d.ts +13 -6
  77. package/dist/src/modular/helpers/clientHelpers.d.ts.map +1 -1
  78. package/dist/src/modular/helpers/clientHelpers.js +117 -48
  79. package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
  80. package/dist/src/modular/helpers/namingHelpers.d.ts +3 -2
  81. package/dist/src/modular/helpers/namingHelpers.d.ts.map +1 -1
  82. package/dist/src/modular/helpers/namingHelpers.js +3 -0
  83. package/dist/src/modular/helpers/namingHelpers.js.map +1 -1
  84. package/dist/src/modular/helpers/operationHelpers.d.ts +9 -11
  85. package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
  86. package/dist/src/modular/helpers/operationHelpers.js +255 -408
  87. package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
  88. package/dist/src/modular/helpers/typeHelpers.d.ts +12 -0
  89. package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
  90. package/dist/src/modular/helpers/typeHelpers.js +30 -6
  91. package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
  92. package/dist/src/modular/modularCodeModel.d.ts +2 -1
  93. package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
  94. package/dist/src/modular/serialization/buildDeserializerFunction.d.ts +5 -0
  95. package/dist/src/modular/serialization/buildDeserializerFunction.d.ts.map +1 -0
  96. package/dist/src/modular/serialization/buildDeserializerFunction.js +327 -0
  97. package/dist/src/modular/serialization/buildDeserializerFunction.js.map +1 -0
  98. package/dist/src/modular/serialization/buildSerializerFunction.d.ts +4 -3
  99. package/dist/src/modular/serialization/buildSerializerFunction.d.ts.map +1 -1
  100. package/dist/src/modular/serialization/buildSerializerFunction.js +285 -102
  101. package/dist/src/modular/serialization/buildSerializerFunction.js.map +1 -1
  102. package/dist/src/modular/serialization/serializeUtils.d.ts +15 -0
  103. package/dist/src/modular/serialization/serializeUtils.d.ts.map +1 -0
  104. package/dist/src/modular/serialization/serializeUtils.js +102 -0
  105. package/dist/src/modular/serialization/serializeUtils.js.map +1 -0
  106. package/dist/src/modular/type-expressions/get-credential-expression.d.ts +3 -0
  107. package/dist/src/modular/type-expressions/get-credential-expression.d.ts.map +1 -0
  108. package/dist/src/modular/type-expressions/get-credential-expression.js +17 -0
  109. package/dist/src/modular/type-expressions/get-credential-expression.js.map +1 -0
  110. package/dist/src/modular/type-expressions/get-enum-expression.d.ts +6 -0
  111. package/dist/src/modular/type-expressions/get-enum-expression.d.ts.map +1 -0
  112. package/dist/src/modular/type-expressions/get-enum-expression.js +18 -0
  113. package/dist/src/modular/type-expressions/get-enum-expression.js.map +1 -0
  114. package/dist/src/modular/type-expressions/get-model-expression.d.ts +9 -0
  115. package/dist/src/modular/type-expressions/get-model-expression.d.ts.map +1 -0
  116. package/dist/src/modular/type-expressions/get-model-expression.js +42 -0
  117. package/dist/src/modular/type-expressions/get-model-expression.js.map +1 -0
  118. package/dist/src/modular/type-expressions/get-type-expression.d.ts +8 -0
  119. package/dist/src/modular/type-expressions/get-type-expression.d.ts.map +1 -0
  120. package/dist/src/modular/type-expressions/get-type-expression.js +94 -0
  121. package/dist/src/modular/type-expressions/get-type-expression.js.map +1 -0
  122. package/dist/src/modular/type-expressions/get-union-expression.d.ts +5 -0
  123. package/dist/src/modular/type-expressions/get-union-expression.d.ts.map +1 -0
  124. package/dist/src/modular/type-expressions/get-union-expression.js +13 -0
  125. package/dist/src/modular/type-expressions/get-union-expression.js.map +1 -0
  126. package/dist/src/modular/type-expressions/utils.d.ts +5 -0
  127. package/dist/src/modular/type-expressions/utils.d.ts.map +1 -0
  128. package/dist/src/modular/type-expressions/utils.js +9 -0
  129. package/dist/src/modular/type-expressions/utils.js.map +1 -0
  130. package/dist/src/transform/transform.js +1 -1
  131. package/dist/src/transform/transform.js.map +1 -1
  132. package/dist/src/transform/transformApiVersionInfo.js +1 -1
  133. package/dist/src/transform/transformApiVersionInfo.js.map +1 -1
  134. package/dist/src/transform/transformParameters.js +1 -1
  135. package/dist/src/transform/transformParameters.js.map +1 -1
  136. package/dist/src/transform/transformSchemas.d.ts.map +1 -1
  137. package/dist/src/transform/transformSchemas.js +3 -3
  138. package/dist/src/transform/transformSchemas.js.map +1 -1
  139. package/dist/src/utils/clientUtils.d.ts.map +1 -1
  140. package/dist/src/utils/clientUtils.js +1 -4
  141. package/dist/src/utils/clientUtils.js.map +1 -1
  142. package/dist/src/utils/credentialUtils.d.ts +5 -0
  143. package/dist/src/utils/credentialUtils.d.ts.map +1 -1
  144. package/dist/src/utils/credentialUtils.js +36 -4
  145. package/dist/src/utils/credentialUtils.js.map +1 -1
  146. package/dist/src/utils/modelUtils.d.ts.map +1 -1
  147. package/dist/src/utils/modelUtils.js +28 -43
  148. package/dist/src/utils/modelUtils.js.map +1 -1
  149. package/dist/tsconfig.tsbuildinfo +1 -1
  150. package/package.json +26 -26
  151. package/src/framework/declaration.ts +0 -1
  152. package/src/framework/dependency.ts +25 -0
  153. package/src/framework/hooks/binder.ts +19 -14
  154. package/src/framework/hooks/sdkTypes.ts +53 -13
  155. package/src/framework/refkey.ts +1 -1
  156. package/src/framework/sample.ts +1 -1
  157. package/src/index.ts +96 -25
  158. package/src/lib.ts +26 -3
  159. package/src/modular/buildClassicalClient.ts +30 -97
  160. package/src/modular/buildClassicalOperationGroups.ts +0 -14
  161. package/src/modular/buildClientContext.ts +90 -50
  162. package/src/modular/buildCodeModel.ts +118 -47
  163. package/src/modular/buildOperations.ts +41 -76
  164. package/src/modular/buildProjectFiles.ts +4 -2
  165. package/src/modular/buildRestorePoller.ts +1 -1
  166. package/src/modular/buildRootIndex.ts +75 -35
  167. package/src/modular/buildSubpathIndex.ts +13 -11
  168. package/src/modular/emitModels.ts +588 -326
  169. package/src/modular/emitModelsOptions.ts +161 -0
  170. package/src/modular/emitSamples.ts +481 -0
  171. package/src/modular/external-dependencies.ts +59 -1
  172. package/src/modular/helpers/classicalOperationHelpers.ts +6 -2
  173. package/src/modular/helpers/clientHelpers.ts +182 -76
  174. package/src/modular/helpers/namingHelpers.ts +14 -2
  175. package/src/modular/helpers/operationHelpers.ts +356 -638
  176. package/src/modular/helpers/typeHelpers.ts +38 -6
  177. package/src/modular/modularCodeModel.ts +3 -0
  178. package/src/modular/serialization/buildDeserializerFunction.ts +492 -0
  179. package/src/modular/serialization/buildSerializerFunction.ts +434 -141
  180. package/src/modular/serialization/serializeUtils.ts +143 -0
  181. package/src/modular/type-expressions/get-credential-expression.ts +18 -0
  182. package/src/modular/type-expressions/get-enum-expression.ts +28 -0
  183. package/src/modular/type-expressions/get-model-expression.ts +63 -0
  184. package/src/modular/type-expressions/get-type-expression.ts +111 -0
  185. package/src/modular/type-expressions/get-union-expression.ts +20 -0
  186. package/src/modular/type-expressions/utils.ts +15 -0
  187. package/src/transform/transform.ts +2 -2
  188. package/src/transform/transformApiVersionInfo.ts +2 -2
  189. package/src/transform/transformParameters.ts +1 -1
  190. package/src/transform/transformSchemas.ts +9 -8
  191. package/src/utils/clientUtils.ts +2 -12
  192. package/src/utils/credentialUtils.ts +43 -3
  193. package/src/utils/modelUtils.ts +32 -41
  194. package/static/static-helpers/serialization/serializers.ts +60 -0
  195. package/dist/src/modular/buildSerializeUtils.d.ts +0 -14
  196. package/dist/src/modular/buildSerializeUtils.d.ts.map +0 -1
  197. package/dist/src/modular/buildSerializeUtils.js +0 -534
  198. package/dist/src/modular/buildSerializeUtils.js.map +0 -1
  199. package/src/modular/buildSerializeUtils.ts +0 -749
@@ -1,9 +1,3 @@
1
- import {
2
- getImportSpecifier,
3
- Imports as RuntimeImports,
4
- NameType,
5
- normalizeName
6
- } from "@azure-tools/rlc-common";
7
1
  import {
8
2
  ClassDeclaration,
9
3
  MethodDeclarationStructure,
@@ -11,34 +5,44 @@ import {
11
5
  SourceFile,
12
6
  StructureKind
13
7
  } from "ts-morph";
14
- import { isRLCMultiEndpoint } from "../utils/clientUtils.js";
15
- import { SdkContext } from "../utils/interfaces.js";
8
+ import { Client, ModularCodeModel } from "./modularCodeModel.js";
9
+ import { NameType, normalizeName } from "@azure-tools/rlc-common";
16
10
  import {
17
11
  buildUserAgentOptions,
18
- getClientParameters,
19
- importCredential
12
+ getClientParametersDeclaration
20
13
  } from "./helpers/clientHelpers.js";
21
- import { getDocsFromDescription } from "./helpers/docsHelpers.js";
22
14
  import {
15
+ getClassicalClientName,
23
16
  getClassicalLayerPrefix,
24
17
  getClientName
25
18
  } from "./helpers/namingHelpers.js";
19
+
20
+ import { SdkContext } from "../utils/interfaces.js";
21
+ import { getDocsFromDescription } from "./helpers/docsHelpers.js";
26
22
  import { getOperationFunction } from "./helpers/operationHelpers.js";
27
- import { Client, ModularCodeModel } from "./modularCodeModel.js";
23
+ import { isRLCMultiEndpoint } from "../utils/clientUtils.js";
24
+ import { resolveReference } from "../framework/reference.js";
28
25
  import { shouldPromoteSubscriptionId } from "./helpers/classicalOperationHelpers.js";
26
+ import { useDependencies } from "../framework/hooks/useDependencies.js";
29
27
 
30
28
  export function buildClassicalClient(
31
- client: Client,
29
+ _client: Client,
32
30
  dpgContext: SdkContext,
33
31
  codeModel: ModularCodeModel
34
32
  ) {
35
- const { description } = client;
33
+ const { description, tcgcClient: client } = _client;
34
+ const dependencies = useDependencies();
36
35
  const modularClientName = getClientName(client);
37
- const classicalClientName = `${getClientName(client)}Client`;
38
- const classicalParams = getClientParameters(client, dpgContext, true);
39
- const contextParams = getClientParameters(client, dpgContext, false);
36
+ const classicalClientName = `${getClassicalClientName(client)}`;
37
+ const classicalParams = getClientParametersDeclaration(_client, dpgContext, {
38
+ requiredOnly: true
39
+ });
40
+ const contextParams = getClientParametersDeclaration(_client, dpgContext, {
41
+ onClientOnly: true,
42
+ requiredOnly: true
43
+ });
40
44
  const srcPath = codeModel.modularOptions.sourceRoot;
41
- const subfolder = client.subfolder ?? "";
45
+ const subfolder = _client.subfolder ?? "";
42
46
 
43
47
  const clientFile = codeModel.project.createSourceFile(
44
48
  `${srcPath}/${subfolder !== "" ? subfolder + "/" : ""}${normalizeName(
@@ -64,21 +68,20 @@ export function buildClassicalClient(
64
68
  if (isRLCMultiEndpoint(dpgContext)) {
65
69
  clientClass.addProperty({
66
70
  name: "_client",
67
- type: `Client.${client.rlcClientName}`,
71
+ type: `Client.${_client.rlcClientName}`,
68
72
  scope: Scope.Private
69
73
  });
70
74
  } else {
71
75
  clientClass.addProperty({
72
76
  name: "_client",
73
- type: `${client.rlcClientName}`,
77
+ type: `${_client.rlcClientName}`,
74
78
  scope: Scope.Private
75
79
  });
76
80
  }
77
-
78
81
  // Add the pipeline member. This will be the pipeline from /api
79
82
  clientClass.addProperty({
80
83
  name: "pipeline",
81
- type: "Pipeline",
84
+ type: resolveReference(dependencies.Pipeline),
82
85
  scope: Scope.Public,
83
86
  isReadonly: true,
84
87
  docs: ["The pipeline used by this client to make requests"]
@@ -96,6 +99,7 @@ export function buildClassicalClient(
96
99
  if (x === "options") {
97
100
  return `{...options, userAgentOptions: ${buildUserAgentOptions(
98
101
  constructor,
102
+ codeModel,
99
103
  "azsdk-js-client"
100
104
  )}}`;
101
105
  } else {
@@ -107,11 +111,10 @@ export function buildClassicalClient(
107
111
  `this._client = create${modularClientName}(${paramNames.join(",")})`
108
112
  ]);
109
113
  constructor.addStatements(`this.pipeline = this._client.pipeline`);
110
- importCredential(codeModel.runtimeImports, clientFile);
111
- importPipeline(codeModel.runtimeImports, clientFile);
112
- importAllModels(clientFile, srcPath, subfolder);
113
- buildClientOperationGroups(clientFile, client, dpgContext, clientClass);
114
+
115
+ buildClientOperationGroups(clientFile, _client, dpgContext, clientClass);
114
116
  importAllApis(clientFile, srcPath, subfolder);
117
+ clientFile.fixUnusedIdentifiers();
115
118
  return clientFile;
116
119
  }
117
120
 
@@ -137,76 +140,6 @@ function importAllApis(
137
140
  });
138
141
  }
139
142
 
140
- function importAllModels(
141
- clientFile: SourceFile,
142
- srcPath: string,
143
- subfolder: string
144
- ) {
145
- const project = clientFile.getProject();
146
- const apiModels = project.getSourceFile(
147
- `${srcPath}/${subfolder !== "" ? subfolder + "/" : ""}models/models.ts`
148
- );
149
-
150
- if (!apiModels) {
151
- return;
152
- }
153
-
154
- const exported = [...apiModels.getExportedDeclarations().keys()].filter(
155
- (e) => {
156
- return !e.startsWith("_");
157
- }
158
- );
159
-
160
- if (exported.length > 0) {
161
- clientFile.addImportDeclaration({
162
- moduleSpecifier: `./models/models.js`,
163
- namedImports: exported
164
- });
165
- }
166
-
167
- const apiModelsOptions = project.getSourceFile(
168
- `${srcPath}/${subfolder !== "" ? subfolder + "/" : ""}models/options.ts`
169
- );
170
-
171
- if (!apiModelsOptions) {
172
- return;
173
- }
174
-
175
- const exportedOptions = [
176
- ...apiModelsOptions.getExportedDeclarations().keys()
177
- ];
178
-
179
- clientFile.addImportDeclaration({
180
- moduleSpecifier: `./models/options.js`,
181
- namedImports: exportedOptions
182
- });
183
-
184
- const pagingTypes = project.getSourceFile(
185
- `${srcPath}/${subfolder !== "" ? subfolder + "/" : ""}models/pagingTypes.ts`
186
- );
187
-
188
- if (!pagingTypes) {
189
- return;
190
- }
191
-
192
- const exportedPaingTypes = [...pagingTypes.getExportedDeclarations().keys()];
193
-
194
- clientFile.addImportDeclaration({
195
- moduleSpecifier: `./models/pagingTypes.js`,
196
- namedImports: exportedPaingTypes
197
- });
198
- }
199
-
200
- function importPipeline(
201
- runtimeImports: RuntimeImports,
202
- clientSourceFile: SourceFile
203
- ): void {
204
- clientSourceFile.addImportDeclaration({
205
- moduleSpecifier: getImportSpecifier("restPipeline", runtimeImports),
206
- namedImports: ["Pipeline"]
207
- });
208
- }
209
-
210
143
  function buildClientOperationGroups(
211
144
  clientFile: SourceFile,
212
145
  client: Client,
@@ -231,7 +164,7 @@ function buildClientOperationGroups(
231
164
  );
232
165
  if (groupName === "") {
233
166
  operationGroup.operations.forEach((op) => {
234
- const declarations = getOperationFunction(op, clientType);
167
+ const declarations = getOperationFunction(dpgContext, op, clientType);
235
168
  const method: MethodDeclarationStructure = {
236
169
  docs: declarations.docs,
237
170
  name: declarations.propertyName ?? declarations.name ?? "FIXME",
@@ -1,6 +1,5 @@
1
1
  import { NameType } from "@azure-tools/rlc-common";
2
2
  import { SourceFile } from "ts-morph";
3
- import { importModels } from "./buildOperations.js";
4
3
  import { getClassicalOperation } from "./helpers/classicalOperationHelpers.js";
5
4
  import { getClassicalLayerPrefix } from "./helpers/namingHelpers.js";
6
5
  import {
@@ -44,15 +43,6 @@ export function buildClassicOperationFiles(
44
43
  );
45
44
  getClassicalOperation(dpgContext, client, classicFile, operationGroup);
46
45
 
47
- // Import models used from ./models.ts
48
- // We SHOULD keep this because otherwise ts-morph will "helpfully" try to import models from the rest layer when we call fixMissingImports().
49
- importModels(
50
- srcPath,
51
- classicFile,
52
- codeModel.project,
53
- subfolder,
54
- operationGroup.namespaceHierarchies.length
55
- );
56
46
  importApis(classicFile, client, codeModel, operationGroup);
57
47
  // We need to import the paging helpers and types explicitly because ts-morph may not be able to find them.
58
48
  classicOperationFiles.set(classicOperationFileName, classicFile);
@@ -93,10 +83,6 @@ export function buildClassicOperationFiles(
93
83
  operationGroup,
94
84
  layer
95
85
  );
96
-
97
- // Import models used from ./models.ts
98
- // We SHOULD keep this because otherwise ts-morph will "helpfully" try to import models from the rest layer when we call fixMissingImports().
99
- importModels(srcPath, classicFile, codeModel.project, subfolder, layer);
100
86
  importApis(classicFile, client, codeModel, operationGroup, layer);
101
87
  classicOperationFiles.set(classicOperationFileName, classicFile);
102
88
  }
@@ -1,71 +1,90 @@
1
+ import { Client, ModularCodeModel } from "./modularCodeModel.js";
1
2
  import {
2
- getImportSpecifier,
3
- isAzurePackage,
4
3
  NameType,
4
+ isAzurePackage,
5
5
  normalizeName
6
6
  } from "@azure-tools/rlc-common";
7
- import { SourceFile } from "ts-morph";
8
- import { SdkContext } from "../utils/interfaces.js";
9
- import { importModels } from "./buildOperations.js";
10
7
  import {
11
8
  buildGetClientCredentialParam,
12
9
  buildGetClientEndpointParam,
13
10
  buildGetClientOptionsParam,
11
+ getClientParameterName,
14
12
  getClientParameters,
15
- importCredential
13
+ getClientParametersDeclaration
16
14
  } from "./helpers/clientHelpers.js";
17
- import { getDocsFromDescription } from "./helpers/docsHelpers.js";
15
+
16
+ import { SdkContext } from "../utils/interfaces.js";
17
+ import { SourceFile } from "ts-morph";
18
18
  import { getClientName } from "./helpers/namingHelpers.js";
19
- import { getType } from "./helpers/typeHelpers.js";
20
- import { Client, ModularCodeModel } from "./modularCodeModel.js";
19
+ import { getDocsFromDescription } from "./helpers/docsHelpers.js";
20
+ import { getTypeExpression } from "./type-expressions/get-type-expression.js";
21
+ import { resolveReference } from "../framework/reference.js";
22
+ import { useDependencies } from "../framework/hooks/useDependencies.js";
23
+ import { buildEnumTypes, getApiVersionEnum } from "./emitModels.js";
24
+ import {
25
+ SdkHttpParameter,
26
+ SdkParameter
27
+ } from "@azure-tools/typespec-client-generator-core";
28
+
29
+ /**
30
+ * This function gets the path of the file containing the modular client context
31
+ */
32
+ export function getClientContextPath(
33
+ _client: Client,
34
+ codeModel: ModularCodeModel
35
+ ): string {
36
+ const { subfolder, tcgcClient: client } = _client;
37
+ const name = getClientName(client);
38
+ const srcPath = codeModel.modularOptions.sourceRoot;
39
+ const contentPath = `${srcPath}/${
40
+ subfolder && subfolder !== "" ? subfolder + "/" : ""
41
+ }api/${normalizeName(name, NameType.File)}Context.ts`;
42
+ return contentPath;
43
+ }
21
44
 
22
45
  /**
23
46
  * This function creates the file containing the modular client context
24
47
  */
25
48
  export function buildClientContext(
26
- client: Client,
49
+ _client: Client,
27
50
  dpgContext: SdkContext,
28
51
  codeModel: ModularCodeModel
29
52
  ): SourceFile {
30
- const { description, subfolder } = client;
53
+ const { description, tcgcClient: client } = _client;
54
+ const dependencies = useDependencies();
31
55
  const name = getClientName(client);
32
- const params = getClientParameters(client, dpgContext);
33
- const srcPath = codeModel.modularOptions.sourceRoot;
56
+ const requiredParams = getClientParametersDeclaration(_client, dpgContext, {
57
+ onClientOnly: false,
58
+ requiredOnly: true
59
+ });
34
60
  const clientContextFile = codeModel.project.createSourceFile(
35
- `${srcPath}/${
36
- subfolder && subfolder !== "" ? subfolder + "/" : ""
37
- }/api/${normalizeName(name, NameType.File)}Context.ts`
61
+ getClientContextPath(_client, codeModel)
38
62
  );
39
63
 
40
- importCredential(codeModel.runtimeImports, clientContextFile);
41
- importModels(srcPath, clientContextFile, codeModel.project, subfolder);
42
- clientContextFile.addImportDeclaration({
43
- moduleSpecifier: getImportSpecifier("restClient", codeModel.runtimeImports),
44
- namedImports: ["ClientOptions", "Client", "getClient"]
45
- });
46
-
47
64
  clientContextFile.addInterface({
48
65
  isExported: true,
49
- name: `${client.rlcClientName}`,
50
- extends: ["Client"]
66
+ name: `${_client.rlcClientName}`,
67
+ extends: [resolveReference(dependencies.Client)],
68
+ docs: getDocsFromDescription(description)
51
69
  });
52
70
 
53
71
  clientContextFile.addInterface({
54
72
  name: `${name}ClientOptionalParams`,
55
73
  isExported: true,
56
- extends: ["ClientOptions"],
57
- properties: client.parameters
58
- .filter((p) => {
59
- return (
60
- p.optional || (p.type.type !== "constant" && p.clientDefaultValue)
61
- );
62
- })
74
+ extends: [resolveReference(dependencies.ClientOptions)],
75
+ properties: getClientParameters(_client, dpgContext, {
76
+ optionalOnly: true
77
+ })
78
+ .filter((p) => p.name !== "endpoint")
63
79
  .map((p) => {
64
80
  return {
65
- name: p.clientName,
66
- type: getType(p.type).name,
81
+ name: normalizeName(p.name, NameType.Parameter),
82
+ type:
83
+ p.name.toLowerCase() === "apiversion"
84
+ ? "string"
85
+ : getTypeExpression(dpgContext, p.type),
67
86
  hasQuestionToken: true,
68
- docs: getDocsFromDescription(p.description)
87
+ docs: getDocsWithKnownVersion(dpgContext, p)
69
88
  };
70
89
  }),
71
90
  docs: ["Optional parameters for the client."]
@@ -84,13 +103,17 @@ export function buildClientContext(
84
103
  const factoryFunction = clientContextFile.addFunction({
85
104
  docs: getDocsFromDescription(description),
86
105
  name: `create${name}`,
87
- returnType: `${client.rlcClientName}`,
88
- parameters: params,
106
+ returnType: `${_client.rlcClientName}`,
107
+ parameters: requiredParams,
89
108
  isExported: true
90
109
  });
91
110
 
92
- const endpointParam = buildGetClientEndpointParam(factoryFunction, client);
93
- const credentialParam = buildGetClientCredentialParam(client, codeModel);
111
+ const endpointParam = buildGetClientEndpointParam(
112
+ factoryFunction,
113
+ dpgContext,
114
+ _client
115
+ );
116
+ const credentialParam = buildGetClientCredentialParam(_client, codeModel);
94
117
  const optionsParam = buildGetClientOptionsParam(
95
118
  factoryFunction,
96
119
  codeModel,
@@ -98,20 +121,17 @@ export function buildClientContext(
98
121
  );
99
122
 
100
123
  factoryFunction.addStatements(
101
- `const clientContext = getClient(${endpointParam}, ${credentialParam}, ${optionsParam});`
124
+ `const clientContext = ${resolveReference(
125
+ dependencies.getClient
126
+ )}(${endpointParam}, ${credentialParam}, ${optionsParam});`
102
127
  );
103
128
 
104
129
  const { customHttpAuthHeaderName, customHttpAuthSharedKeyPrefix } =
105
130
  codeModel.options;
106
131
 
107
132
  if (customHttpAuthHeaderName && customHttpAuthSharedKeyPrefix) {
108
- clientContextFile.addImportDeclaration({
109
- moduleSpecifier: getImportSpecifier("coreAuth", codeModel.runtimeImports),
110
- namedImports: ["isKeyCredential"]
111
- });
112
-
113
133
  factoryFunction.addStatements(`
114
- if(isKeyCredential(credential)) {
134
+ if(${resolveReference(dependencies.isKeyCredential)}(credential)) {
115
135
  clientContext.pipeline.addPolicy({
116
136
  name: "customKeyCredentialPolicy",
117
137
  sendRequest(request, next) {
@@ -126,11 +146,13 @@ export function buildClientContext(
126
146
  let apiVersionPolicyStatement = `clientContext.pipeline.removePolicy({ name: "ApiVersionPolicy" });`;
127
147
 
128
148
  if (dpgContext.hasApiVersionInClient) {
129
- const apiVersionParam = client.parameters.find((x) => x.isApiVersion);
149
+ const apiVersionParam = getClientParameters(_client, dpgContext).find(
150
+ (x) => x.isApiVersionParam && x.kind === "method"
151
+ );
130
152
 
131
- if (apiVersionParam?.location === "query") {
153
+ if (apiVersionParam) {
132
154
  if (apiVersionParam.clientDefaultValue) {
133
- apiVersionPolicyStatement += `const ${apiVersionParam.clientName} = options.${apiVersionParam.clientName} ?? "${apiVersionParam.clientDefaultValue}";`;
155
+ apiVersionPolicyStatement += `const apiVersion = options.apiVersion ?? "${apiVersionParam.clientDefaultValue}";`;
134
156
  }
135
157
 
136
158
  apiVersionPolicyStatement += `
@@ -143,7 +165,7 @@ export function buildClientContext(
143
165
  if (!url.searchParams.get("api-version")) {
144
166
  req.url = \`\${req.url}\${
145
167
  Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"
146
- }api-version=\${${apiVersionParam.clientName}}\`;
168
+ }api-version=\${${getClientParameterName(apiVersionParam)}}\`;
147
169
  }
148
170
 
149
171
  return next(req);
@@ -173,3 +195,21 @@ export function buildClientContext(
173
195
  clientContextFile.fixUnusedIdentifiers();
174
196
  return clientContextFile;
175
197
  }
198
+
199
+ function getDocsWithKnownVersion(
200
+ dpgContext: SdkContext,
201
+ param: SdkParameter | SdkHttpParameter
202
+ ) {
203
+ const docs = getDocsFromDescription(param.doc);
204
+ if (param.name.toLowerCase() !== "apiversion") {
205
+ return docs;
206
+ }
207
+ const apiVersionEnum = getApiVersionEnum(dpgContext);
208
+ if (apiVersionEnum) {
209
+ const [_, knownValuesEnum] = buildEnumTypes(dpgContext, apiVersionEnum);
210
+ docs.push(
211
+ `Known values of {@link ${knownValuesEnum.name}} that the service accepts.`
212
+ );
213
+ }
214
+ return docs;
215
+ }