@azure-tools/typespec-ts 0.55.0-dev.7 → 0.55.0-dev.9

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 (174) hide show
  1. package/README.md +1 -77
  2. package/dist/src/index.d.ts.map +1 -1
  3. package/dist/src/index.js +24 -80
  4. package/dist/src/index.js.map +1 -1
  5. package/dist/src/lib.d.ts +2 -12
  6. package/dist/src/lib.d.ts.map +1 -1
  7. package/dist/src/lib.js +0 -65
  8. package/dist/src/lib.js.map +1 -1
  9. package/dist/src/rlc-common/helpers/name-constructors.d.ts.map +1 -1
  10. package/dist/src/rlc-common/helpers/name-constructors.js +1 -7
  11. package/dist/src/rlc-common/helpers/name-constructors.js.map +1 -1
  12. package/dist/src/rlc-common/index.d.ts +0 -16
  13. package/dist/src/rlc-common/index.d.ts.map +1 -1
  14. package/dist/src/rlc-common/index.js +0 -16
  15. package/dist/src/rlc-common/index.js.map +1 -1
  16. package/dist/src/rlc-common/interfaces.d.ts +0 -10
  17. package/dist/src/rlc-common/interfaces.d.ts.map +1 -1
  18. package/dist/src/rlc-common/interfaces.js.map +1 -1
  19. package/dist/src/rlc-common/metadata/build-package-file.d.ts.map +1 -1
  20. package/dist/src/rlc-common/metadata/build-package-file.js +0 -12
  21. package/dist/src/rlc-common/metadata/build-package-file.js.map +1 -1
  22. package/dist/src/rlc-common/metadata/build-readme-file.d.ts.map +1 -1
  23. package/dist/src/rlc-common/metadata/build-readme-file.js +4 -89
  24. package/dist/src/rlc-common/metadata/build-readme-file.js.map +1 -1
  25. package/dist/src/rlc-common/metadata/package-json/build-azure-monorepo-package.d.ts +0 -1
  26. package/dist/src/rlc-common/metadata/package-json/build-azure-monorepo-package.d.ts.map +1 -1
  27. package/dist/src/rlc-common/metadata/package-json/build-azure-monorepo-package.js +2 -2
  28. package/dist/src/rlc-common/metadata/package-json/build-azure-monorepo-package.js.map +1 -1
  29. package/dist/src/rlc-common/metadata/package-json/package-common.d.ts +0 -1
  30. package/dist/src/rlc-common/metadata/package-json/package-common.d.ts.map +1 -1
  31. package/dist/src/rlc-common/metadata/package-json/package-common.js +1 -1
  32. package/dist/src/rlc-common/metadata/package-json/package-common.js.map +1 -1
  33. package/dist/src/rlc-common/test/build-snippets.d.ts.map +1 -1
  34. package/dist/src/rlc-common/test/build-snippets.js +0 -1
  35. package/dist/src/rlc-common/test/build-snippets.js.map +1 -1
  36. package/dist/src/rlc-common/test/template.d.ts +1 -1
  37. package/dist/src/rlc-common/test/template.d.ts.map +1 -1
  38. package/dist/src/rlc-common/test/template.js +0 -4
  39. package/dist/src/rlc-common/test/template.js.map +1 -1
  40. package/dist/src/transform/transform-schemas.js +1 -1
  41. package/dist/src/transform/transform-schemas.js.map +1 -1
  42. package/dist/src/transform/transform.d.ts.map +1 -1
  43. package/dist/src/transform/transform.js +5 -11
  44. package/dist/src/transform/transform.js.map +1 -1
  45. package/dist/src/transform/transfrom-rlc-options.d.ts +2 -2
  46. package/dist/src/transform/transfrom-rlc-options.d.ts.map +1 -1
  47. package/dist/src/transform/transfrom-rlc-options.js +19 -39
  48. package/dist/src/transform/transfrom-rlc-options.js.map +1 -1
  49. package/dist/src/utils/client-utils.d.ts +1 -1
  50. package/dist/src/utils/client-utils.d.ts.map +1 -1
  51. package/dist/src/utils/client-utils.js +4 -5
  52. package/dist/src/utils/client-utils.js.map +1 -1
  53. package/dist/src/utils/emit-util.d.ts +0 -1
  54. package/dist/src/utils/emit-util.d.ts.map +1 -1
  55. package/dist/src/utils/emit-util.js +0 -13
  56. package/dist/src/utils/emit-util.js.map +1 -1
  57. package/dist/src/utils/model-utils.d.ts +1 -1
  58. package/dist/src/utils/model-utils.d.ts.map +1 -1
  59. package/dist/src/utils/model-utils.js +4 -11
  60. package/dist/src/utils/model-utils.js.map +1 -1
  61. package/dist/tsconfig.tsbuildinfo +1 -1
  62. package/package.json +9 -15
  63. package/src/index.ts +27 -105
  64. package/src/lib.ts +1 -77
  65. package/src/rlc-common/helpers/name-constructors.ts +1 -8
  66. package/src/rlc-common/index.ts +0 -16
  67. package/src/rlc-common/interfaces.ts +0 -11
  68. package/src/rlc-common/metadata/build-package-file.ts +0 -13
  69. package/src/rlc-common/metadata/build-readme-file.ts +4 -99
  70. package/src/rlc-common/metadata/package-json/build-azure-monorepo-package.ts +2 -3
  71. package/src/rlc-common/metadata/package-json/package-common.ts +1 -2
  72. package/src/rlc-common/test/build-snippets.ts +0 -1
  73. package/src/rlc-common/test/template.ts +0 -4
  74. package/src/transform/transform-schemas.ts +1 -1
  75. package/src/transform/transform.ts +4 -14
  76. package/src/transform/transfrom-rlc-options.ts +19 -63
  77. package/src/utils/client-utils.ts +4 -5
  78. package/src/utils/emit-util.ts +1 -14
  79. package/src/utils/model-utils.ts +4 -17
  80. package/dist/src/rlc-common/build-client-definitions.d.ts +0 -6
  81. package/dist/src/rlc-common/build-client-definitions.d.ts.map +0 -1
  82. package/dist/src/rlc-common/build-client-definitions.js +0 -157
  83. package/dist/src/rlc-common/build-client-definitions.js.map +0 -1
  84. package/dist/src/rlc-common/build-client.d.ts +0 -9
  85. package/dist/src/rlc-common/build-client.d.ts.map +0 -1
  86. package/dist/src/rlc-common/build-client.js +0 -375
  87. package/dist/src/rlc-common/build-client.js.map +0 -1
  88. package/dist/src/rlc-common/build-index-file.d.ts +0 -6
  89. package/dist/src/rlc-common/build-index-file.d.ts.map +0 -1
  90. package/dist/src/rlc-common/build-index-file.js +0 -180
  91. package/dist/src/rlc-common/build-index-file.js.map +0 -1
  92. package/dist/src/rlc-common/build-is-unexpected-helper.d.ts +0 -6
  93. package/dist/src/rlc-common/build-is-unexpected-helper.d.ts.map +0 -1
  94. package/dist/src/rlc-common/build-is-unexpected-helper.js +0 -219
  95. package/dist/src/rlc-common/build-is-unexpected-helper.js.map +0 -1
  96. package/dist/src/rlc-common/build-logger.d.ts +0 -6
  97. package/dist/src/rlc-common/build-logger.d.ts.map +0 -1
  98. package/dist/src/rlc-common/build-logger.js +0 -23
  99. package/dist/src/rlc-common/build-logger.js.map +0 -1
  100. package/dist/src/rlc-common/build-method-shortcuts.d.ts +0 -5
  101. package/dist/src/rlc-common/build-method-shortcuts.d.ts.map +0 -1
  102. package/dist/src/rlc-common/build-method-shortcuts.js +0 -54
  103. package/dist/src/rlc-common/build-method-shortcuts.js.map +0 -1
  104. package/dist/src/rlc-common/build-object-types.d.ts +0 -29
  105. package/dist/src/rlc-common/build-object-types.d.ts.map +0 -1
  106. package/dist/src/rlc-common/build-object-types.js +0 -410
  107. package/dist/src/rlc-common/build-object-types.js.map +0 -1
  108. package/dist/src/rlc-common/build-paginate-helper.d.ts +0 -6
  109. package/dist/src/rlc-common/build-paginate-helper.d.ts.map +0 -1
  110. package/dist/src/rlc-common/build-paginate-helper.js +0 -27
  111. package/dist/src/rlc-common/build-paginate-helper.js.map +0 -1
  112. package/dist/src/rlc-common/build-parameter-types.d.ts +0 -14
  113. package/dist/src/rlc-common/build-parameter-types.d.ts.map +0 -1
  114. package/dist/src/rlc-common/build-parameter-types.js +0 -338
  115. package/dist/src/rlc-common/build-parameter-types.js.map +0 -1
  116. package/dist/src/rlc-common/build-polling-helper.d.ts +0 -6
  117. package/dist/src/rlc-common/build-polling-helper.d.ts.map +0 -1
  118. package/dist/src/rlc-common/build-polling-helper.js +0 -59
  119. package/dist/src/rlc-common/build-polling-helper.js.map +0 -1
  120. package/dist/src/rlc-common/build-response-types.d.ts +0 -6
  121. package/dist/src/rlc-common/build-response-types.d.ts.map +0 -1
  122. package/dist/src/rlc-common/build-response-types.js +0 -140
  123. package/dist/src/rlc-common/build-response-types.js.map +0 -1
  124. package/dist/src/rlc-common/build-samples.d.ts +0 -3
  125. package/dist/src/rlc-common/build-samples.d.ts.map +0 -1
  126. package/dist/src/rlc-common/build-samples.js +0 -30
  127. package/dist/src/rlc-common/build-samples.js.map +0 -1
  128. package/dist/src/rlc-common/build-schema-type.d.ts +0 -20
  129. package/dist/src/rlc-common/build-schema-type.d.ts.map +0 -1
  130. package/dist/src/rlc-common/build-schema-type.js +0 -64
  131. package/dist/src/rlc-common/build-schema-type.js.map +0 -1
  132. package/dist/src/rlc-common/build-serialize-helper.d.ts +0 -6
  133. package/dist/src/rlc-common/build-serialize-helper.d.ts.map +0 -1
  134. package/dist/src/rlc-common/build-serialize-helper.js +0 -36
  135. package/dist/src/rlc-common/build-serialize-helper.js.map +0 -1
  136. package/dist/src/rlc-common/build-top-level-index-file.d.ts +0 -6
  137. package/dist/src/rlc-common/build-top-level-index-file.d.ts.map +0 -1
  138. package/dist/src/rlc-common/build-top-level-index-file.js +0 -44
  139. package/dist/src/rlc-common/build-top-level-index-file.js.map +0 -1
  140. package/dist/src/rlc-common/helpers/path-utils.d.ts +0 -2
  141. package/dist/src/rlc-common/helpers/path-utils.d.ts.map +0 -1
  142. package/dist/src/rlc-common/helpers/path-utils.js +0 -9
  143. package/dist/src/rlc-common/helpers/path-utils.js.map +0 -1
  144. package/dist/src/rlc-common/helpers/shortcut-methods.d.ts +0 -4
  145. package/dist/src/rlc-common/helpers/shortcut-methods.d.ts.map +0 -1
  146. package/dist/src/rlc-common/helpers/shortcut-methods.js +0 -49
  147. package/dist/src/rlc-common/helpers/shortcut-methods.js.map +0 -1
  148. package/dist/src/rlc-common/helpers/value-generation-util.d.ts +0 -12
  149. package/dist/src/rlc-common/helpers/value-generation-util.d.ts.map +0 -1
  150. package/dist/src/rlc-common/helpers/value-generation-util.js +0 -161
  151. package/dist/src/rlc-common/helpers/value-generation-util.js.map +0 -1
  152. package/dist/src/rlc-common/transform-sample-groups.d.ts +0 -9
  153. package/dist/src/rlc-common/transform-sample-groups.d.ts.map +0 -1
  154. package/dist/src/rlc-common/transform-sample-groups.js +0 -298
  155. package/dist/src/rlc-common/transform-sample-groups.js.map +0 -1
  156. package/src/rlc-common/build-client-definitions.ts +0 -226
  157. package/src/rlc-common/build-client.ts +0 -463
  158. package/src/rlc-common/build-index-file.ts +0 -220
  159. package/src/rlc-common/build-is-unexpected-helper.ts +0 -241
  160. package/src/rlc-common/build-logger.ts +0 -29
  161. package/src/rlc-common/build-method-shortcuts.ts +0 -71
  162. package/src/rlc-common/build-object-types.ts +0 -591
  163. package/src/rlc-common/build-paginate-helper.ts +0 -31
  164. package/src/rlc-common/build-parameter-types.ts +0 -460
  165. package/src/rlc-common/build-polling-helper.ts +0 -80
  166. package/src/rlc-common/build-response-types.ts +0 -170
  167. package/src/rlc-common/build-samples.ts +0 -32
  168. package/src/rlc-common/build-schema-type.ts +0 -79
  169. package/src/rlc-common/build-serialize-helper.ts +0 -49
  170. package/src/rlc-common/build-top-level-index-file.ts +0 -51
  171. package/src/rlc-common/helpers/path-utils.ts +0 -8
  172. package/src/rlc-common/helpers/shortcut-methods.ts +0 -58
  173. package/src/rlc-common/helpers/value-generation-util.ts +0 -234
  174. package/src/rlc-common/transform-sample-groups.ts +0 -383
@@ -1,463 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { joinPaths } from "@typespec/compiler";
5
- import {
6
- InterfaceDeclarationStructure,
7
- OptionalKind,
8
- Project,
9
- StatementStructures,
10
- StructureKind,
11
- VariableDeclarationKind,
12
- VariableStatementStructure,
13
- WriterFunction,
14
- } from "ts-morph";
15
- import { buildMethodShortcutImplementation } from "./build-method-shortcuts.js";
16
- import { getImportSpecifier } from "./helpers/imports-util.js";
17
- import { getClientName } from "./helpers/name-constructors.js";
18
- import { NameType, normalizeName } from "./helpers/name-utils.js";
19
- import { File, PathParameter, RLCModel } from "./interfaces.js";
20
-
21
- function getClientOptionsInterface(
22
- model: RLCModel,
23
- clientName: string,
24
- optionalUrlParameters?: PathParameter[],
25
- ): OptionalKind<InterfaceDeclarationStructure> | undefined {
26
- if ((!optionalUrlParameters || optionalUrlParameters.length === 0) && !model.apiVersionInfo) {
27
- return undefined;
28
- }
29
-
30
- const properties =
31
- optionalUrlParameters?.map((param) => {
32
- return {
33
- name: param.name,
34
- type: param.type,
35
- hasQuestionToken: true,
36
- docs: [param.description ?? "client level optional parameter " + param.name],
37
- };
38
- }) ?? [];
39
-
40
- if (
41
- model.apiVersionInfo?.isCrossedVersion === false &&
42
- !model.urlInfo?.urlParameters?.find((p) => p.name === "apiVersion") &&
43
- (model.apiVersionInfo.defaultValue || !model.apiVersionInfo?.required)
44
- ) {
45
- properties.push({
46
- name: "apiVersion",
47
- type: "string",
48
- hasQuestionToken: true,
49
- docs: ["The api version option of the client"],
50
- });
51
- }
52
- return {
53
- name: `${clientName}Options`,
54
- extends: ["ClientOptions"],
55
- isExported: true,
56
- properties,
57
- docs: ["The optional parameters for the client"],
58
- };
59
- }
60
-
61
- export function buildClient(model: RLCModel): File | undefined {
62
- const name = normalizeName(model.libraryName, NameType.File);
63
- const { srcPath } = model;
64
- const project = new Project({ useInMemoryFileSystem: true });
65
- const filePath = joinPaths(srcPath, `${name}.ts`);
66
- const clientFile = project.createSourceFile(filePath, undefined, {
67
- overwrite: true,
68
- });
69
-
70
- // Get all paths
71
- const clientInterfaceName = getClientName(model);
72
-
73
- normalizeUrlInfo(model);
74
- const urlParameters = model?.urlInfo?.urlParameters?.filter(
75
- // Do not include parameters with constant values in the signature, these should go in the options bag
76
- (p) => p.value === undefined,
77
- );
78
-
79
- const optionalUrlParameters = model?.urlInfo?.urlParameters?.filter(
80
- // Do not include parameters with constant values in the signature, these should go in the options bag
81
- (p) => Boolean(p.value),
82
- );
83
-
84
- const clientOptionsInterface = getClientOptionsInterface(
85
- model,
86
- clientInterfaceName,
87
- optionalUrlParameters,
88
- );
89
-
90
- if (clientOptionsInterface) {
91
- clientFile.addInterface(clientOptionsInterface);
92
- }
93
-
94
- if (!model.options) {
95
- return undefined;
96
- }
97
- const { multiClient, batch } = model.options;
98
- const {
99
- addCredentials,
100
- credentialScopes,
101
- credentialKeyHeaderName,
102
- customHttpAuthHeaderName,
103
- customHttpAuthSharedKeyPrefix,
104
- } = model.options;
105
- const credentialTypes = credentialScopes ? ["TokenCredential"] : [];
106
-
107
- if (credentialKeyHeaderName || customHttpAuthHeaderName) {
108
- credentialTypes.push("KeyCredential");
109
- }
110
-
111
- const commonClientParams = [
112
- ...(urlParameters ?? []),
113
- ...(addCredentials === false ||
114
- !isSecurityInfoDefined(credentialScopes, credentialKeyHeaderName, customHttpAuthHeaderName)
115
- ? []
116
- : [
117
- {
118
- name: "credentials",
119
- type: credentialTypes.join(" | "),
120
- description: `uniquely identify client credential`,
121
- },
122
- ]),
123
- ];
124
-
125
- let apiVersionStatement: string = "";
126
- // Set the default api-version when we have a default AND its position is query
127
- if (model.apiVersionInfo?.isCrossedVersion === false && !!model.apiVersionInfo?.defaultValue) {
128
- apiVersionStatement = `
129
- apiVersion = "${model.apiVersionInfo?.defaultValue}"`;
130
- } else if (model.apiVersionInfo?.isCrossedVersion === false && !model.apiVersionInfo.required) {
131
- apiVersionStatement = `
132
- apiVersion`;
133
- }
134
-
135
- const allClientParams = [
136
- ...commonClientParams,
137
- {
138
- name: apiVersionStatement === "" ? "options" : `{${apiVersionStatement}, ...options}`,
139
- documentName: "options",
140
- type: `${clientOptionsInterface?.name ?? "ClientOptions"} = {}`,
141
- description: "the parameter for all optional parameters",
142
- },
143
- ];
144
- const functionStatement = {
145
- isExported: true,
146
- name: `createClient`,
147
- parameters: allClientParams,
148
- docs: [
149
- {
150
- description:
151
- `Initialize a new instance of \`${clientInterfaceName}\`\n` +
152
- allClientParams
153
- .map((param) => {
154
- return `@param ${param.documentName ?? param.name} - ${
155
- param.description ?? "The parameter " + param.name
156
- }`;
157
- })
158
- .join("\n"),
159
- },
160
- ],
161
- returnType: clientInterfaceName,
162
- isDefaultExport: false,
163
- statements: getClientFactoryBody(model, clientInterfaceName, {
164
- isMultipleCredential: credentialTypes.length > 1,
165
- }),
166
- };
167
-
168
- if (!multiClient || !batch || batch.length === 1) {
169
- functionStatement.isDefaultExport = true;
170
- }
171
- clientFile.addFunction(functionStatement);
172
-
173
- const paths = srcPath.split("/");
174
- while (paths.length > 0 && paths[paths.length - 1] === "") {
175
- paths.pop();
176
- }
177
- const parentPath =
178
- paths.lastIndexOf("src") > -1 ? paths.length - 1 - paths.lastIndexOf("src") : 0;
179
-
180
- const loggerPath = `${parentPath > 0 ? "../".repeat(parentPath) : "./"}logger`;
181
- clientFile.addImportDeclarations([
182
- {
183
- isTypeOnly: true,
184
- namedImports: ["ClientOptions"],
185
- moduleSpecifier: getImportSpecifier("restClient", model.importInfo.runtimeImports),
186
- },
187
- ]);
188
- clientFile.addImportDeclarations([
189
- {
190
- namedImports: ["getClient"],
191
- moduleSpecifier: getImportSpecifier("restClient", model.importInfo.runtimeImports),
192
- },
193
- ]);
194
- clientFile.addImportDeclarations([
195
- {
196
- namedImports: ["logger"],
197
- moduleSpecifier: `${loggerPath}.js`,
198
- },
199
- ]);
200
-
201
- const includeKeyCredentialHelper =
202
- customHttpAuthHeaderName &&
203
- customHttpAuthSharedKeyPrefix &&
204
- credentialTypes.length > 1 &&
205
- credentialTypes.includes("KeyCredential");
206
- if (
207
- addCredentials &&
208
- isSecurityInfoDefined(credentialScopes, credentialKeyHeaderName, customHttpAuthHeaderName)
209
- ) {
210
- clientFile.addImportDeclarations([
211
- {
212
- isTypeOnly: true,
213
- namedImports: credentialTypes,
214
- moduleSpecifier: getImportSpecifier("coreAuth", model.importInfo.runtimeImports),
215
- },
216
- ]);
217
- if (includeKeyCredentialHelper) {
218
- clientFile.addImportDeclarations([
219
- {
220
- namedImports: ["isKeyCredential"],
221
- moduleSpecifier: getImportSpecifier("coreAuth", model.importInfo.runtimeImports),
222
- },
223
- ]);
224
- }
225
- }
226
- clientFile.addImportDeclarations([
227
- {
228
- isTypeOnly: true,
229
- namedImports: [`${clientInterfaceName}`],
230
- moduleSpecifier: "./clientDefinitions.js",
231
- },
232
- ]);
233
- if ((model.importInfo.internalImports?.rlcClientFactory?.importsSet?.size ?? 0) > 0) {
234
- clientFile.addImportDeclarations([
235
- {
236
- isTypeOnly: true,
237
- namedImports: Array.from(model.importInfo.internalImports.rlcClientFactory.importsSet!),
238
- moduleSpecifier: `./models.js`,
239
- },
240
- ]);
241
- }
242
- return { path: filePath, content: clientFile.getFullText() };
243
- }
244
-
245
- function isSecurityInfoDefined(
246
- credentialScopes?: string[],
247
- credentialKeyHeaderName?: string,
248
- customHttpAuthHeaderName?: string,
249
- ) {
250
- return credentialScopes || credentialKeyHeaderName || customHttpAuthHeaderName;
251
- }
252
-
253
- interface GetClientFactoryOptions {
254
- isMultipleCredential: boolean;
255
- }
256
-
257
- export function getClientFactoryBody(
258
- model: RLCModel,
259
- clientTypeName: string,
260
- options: GetClientFactoryOptions = { isMultipleCredential: false },
261
- ): string | WriterFunction | (string | WriterFunction | StatementStructures)[] {
262
- if (!model.options || !model.options.packageDetails || !model.urlInfo) {
263
- return "";
264
- }
265
- const { includeShortcuts, packageDetails, addCredentials } = model.options;
266
- let clientPackageName = packageDetails!.nameWithoutScope ?? packageDetails?.name ?? "";
267
- const packageVersion = packageDetails.version;
268
- const { endpoint, urlParameters } = model.urlInfo;
269
-
270
- const optionalUrlParameters: string[] = [];
271
-
272
- for (const param of urlParameters ?? []) {
273
- if (param.name === "apiVersion") {
274
- continue;
275
- }
276
- if (param.value) {
277
- const value = typeof param.value === "string" ? `"${param.value}"` : param.value;
278
- optionalUrlParameters.push(`const ${param.name} = options.${param.name} ?? ${value};`);
279
- }
280
- }
281
-
282
- let endpointUrl: string;
283
- if (urlParameters && endpoint) {
284
- let parsedEndpoint = endpoint;
285
- urlParameters.forEach((urlParameter) => {
286
- parsedEndpoint = parsedEndpoint.replace(`{${urlParameter.name}}`, `\${${urlParameter.name}}`);
287
- });
288
- endpointUrl = `options.endpoint ?? \`${parsedEndpoint}\``;
289
- } else {
290
- endpointUrl = `options.endpoint ?? "${endpoint}"`;
291
- }
292
-
293
- if (!model.options.isModularLibrary && !clientPackageName.endsWith("-rest")) {
294
- clientPackageName += "-rest";
295
- }
296
-
297
- const userAgentInfoStatement =
298
- "const userAgentInfo = `azsdk-js-" + clientPackageName + "/" + packageVersion + "`;";
299
- const userAgentPrefix =
300
- "options.userAgentOptions && options.userAgentOptions.userAgentPrefix ? `${options.userAgentOptions.userAgentPrefix} ${userAgentInfo}`: `${userAgentInfo}`;";
301
- const userAgentStatement: VariableStatementStructure = {
302
- kind: StructureKind.VariableStatement,
303
- declarationKind: VariableDeclarationKind.Const,
304
- declarations: [{ name: "userAgentPrefix", initializer: userAgentPrefix }],
305
- };
306
-
307
- const customHeaderOptions = model.telemetryOptions?.customRequestIdHeaderName
308
- ? `,
309
- telemetryOptions: {
310
- clientRequestIdHeaderName:
311
- options.telemetryOptions?.clientRequestIdHeaderName ??
312
- "${model.telemetryOptions?.customRequestIdHeaderName}"
313
- }`
314
- : "";
315
-
316
- const endpointUrlStatement: VariableStatementStructure = {
317
- kind: StructureKind.VariableStatement,
318
- declarationKind: VariableDeclarationKind.Const,
319
- declarations: [{ name: "endpointUrl", initializer: endpointUrl }],
320
- };
321
-
322
- const { credentialScopes, credentialKeyHeaderName } = model.options;
323
- const scopesString = credentialScopes
324
- ? credentialScopes.map((cs) => `"${cs}"`).join(", ") || "`${endpointUrl}/.default`"
325
- : "";
326
- const scopes = scopesString ? `scopes: options.credentials?.scopes ?? [${scopesString}],` : "";
327
-
328
- const apiKeyHeaderName = credentialKeyHeaderName
329
- ? `apiKeyHeaderName: options.credentials?.apiKeyHeaderName ?? "${credentialKeyHeaderName}",`
330
- : "";
331
- const loggerOptions = `,
332
- loggingOptions: {
333
- logger: options.loggingOptions?.logger ?? logger.info
334
- }`;
335
-
336
- const credentialsOptions =
337
- (scopes || apiKeyHeaderName) && addCredentials
338
- ? `,
339
- credentials: {
340
- ${scopes}
341
- ${apiKeyHeaderName}
342
- }`
343
- : "";
344
- const overrideOptionsStatement = `options = {
345
- ...options,
346
- userAgentOptions: {
347
- userAgentPrefix
348
- }${loggerOptions}${customHeaderOptions}${credentialsOptions}
349
- };`;
350
- const getClient = `const client = getClient(
351
- endpointUrl, ${credentialsOptions ? "credentials," : ""} options
352
- ) as ${clientTypeName};
353
- `;
354
- const { customHttpAuthHeaderName, customHttpAuthSharedKeyPrefix } = model.options;
355
- let customHttpAuthStatement = "";
356
- if (customHttpAuthHeaderName && customHttpAuthSharedKeyPrefix) {
357
- if (options.isMultipleCredential) {
358
- customHttpAuthStatement = `if (isKeyCredential(credentials)) {
359
- client.pipeline.addPolicy({
360
- name: "customKeyCredentialPolicy",
361
- async sendRequest(request, next) {
362
- request.headers.set("Authorization", "Bearer " + credentials.key);
363
- return next(request);
364
- },
365
- });
366
- }`;
367
- } else {
368
- customHttpAuthStatement = `
369
- client.pipeline.addPolicy({
370
- name: "customKeyCredentialPolicy",
371
- async sendRequest(request, next) {
372
- request.headers.set("${customHttpAuthHeaderName}", "${customHttpAuthSharedKeyPrefix} " + credentials.key);
373
- return next(request);
374
- }
375
- });`;
376
- }
377
- }
378
-
379
- let apiVersionPolicyStatement = `client.pipeline.removePolicy({ name: "ApiVersionPolicy" });`;
380
- if (model.apiVersionInfo?.isCrossedVersion !== false) {
381
- apiVersionPolicyStatement += `
382
- if (options.apiVersion) {
383
- logger.warning("This client does not support client api-version, please change it at the operation level");
384
- }`;
385
- } else if (!model.apiVersionInfo?.defaultValue && model.apiVersionInfo?.required) {
386
- apiVersionPolicyStatement += `
387
- if (options.apiVersion) {
388
- logger.warning("This client does not support to set api-version in options, please change it at positional argument");
389
- }`;
390
- }
391
- if (
392
- model.apiVersionInfo?.isCrossedVersion === false &&
393
- model.apiVersionInfo?.definedPosition === "query"
394
- ) {
395
- apiVersionPolicyStatement += `
396
- client.pipeline.addPolicy({
397
- name: 'ClientApiVersionPolicy',
398
- sendRequest: (req, next) => {
399
- // Use the apiVersion defined in request url directly
400
- // Append one if there is no apiVersion and we have one at client options
401
- const url = new URL(req.url);
402
- if (!url.searchParams.get("api-version") && apiVersion) {
403
- req.url = \`\${req.url}\${
404
- Array.from(url.searchParams.keys()).length > 0 ? "&" : "?"
405
- }api-version=\${apiVersion}\`;
406
- }
407
-
408
- return next(req);
409
- },
410
- });`;
411
- }
412
- let returnStatement = `return client;`;
413
-
414
- if (includeShortcuts) {
415
- const shortcutImplementations = buildMethodShortcutImplementation(model.paths);
416
- const shortcutBody = Object.keys(shortcutImplementations).map((key) => {
417
- // If the operation group has an empty name, it means its operations are client
418
- // level operations so we need to spread the definitions. Otherwise they are
419
- // within an operation group so we add them as key: value
420
- const shortcuts = shortcutImplementations[key];
421
- return `${
422
- key && key !== "client" ? `"${key}":` : "..."
423
- } {${shortcuts ? shortcuts.join() : ""}}`;
424
- });
425
- returnStatement = `return { ...client, ${shortcutBody.join()} };`;
426
- }
427
-
428
- return [
429
- ...optionalUrlParameters,
430
- endpointUrlStatement,
431
- userAgentInfoStatement,
432
- userAgentStatement,
433
- overrideOptionsStatement,
434
- getClient,
435
- apiVersionPolicyStatement,
436
- customHttpAuthStatement,
437
- returnStatement,
438
- ];
439
- }
440
-
441
- function normalizeUrlInfo(model: RLCModel) {
442
- if (
443
- !model ||
444
- !model.urlInfo ||
445
- !model.urlInfo.endpoint ||
446
- !model.urlInfo.urlParameters ||
447
- model.urlInfo.urlParameters.length === 0
448
- ) {
449
- return;
450
- }
451
-
452
- let parsedEndpoint = model.urlInfo.endpoint;
453
- const urlParameters = model.urlInfo.urlParameters;
454
- urlParameters.forEach((urlParameter) => {
455
- const name = urlParameter.name;
456
- const normalizedName = normalizeName(name, NameType.Parameter);
457
- if (name !== normalizedName) {
458
- urlParameter.name = normalizedName;
459
- parsedEndpoint = parsedEndpoint.replace(`{${name}}`, `{${normalizedName}}`);
460
- }
461
- });
462
- model.urlInfo.endpoint = parsedEndpoint;
463
- }
@@ -1,220 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { joinPaths } from "@typespec/compiler";
5
- import { Project, SourceFile } from "ts-morph";
6
- import { NameType, normalizeName } from "./helpers/name-utils.js";
7
- import {
8
- hasCsvCollection,
9
- hasInputModels,
10
- hasMultiCollection,
11
- hasOutputModels,
12
- hasPagingOperations,
13
- hasPipeCollection,
14
- hasPollingOperations,
15
- hasSsvCollection,
16
- hasTsvCollection,
17
- hasUnexpectedHelper,
18
- } from "./helpers/operation-helpers.js";
19
- import { RLCModel } from "./interfaces.js";
20
-
21
- export function buildIndexFile(model: RLCModel) {
22
- const multiClient = Boolean(model.options?.multiClient),
23
- batch = model.options?.batch;
24
- const project = new Project({ useInMemoryFileSystem: true });
25
- const { srcPath } = model;
26
- const filePath = joinPaths(srcPath, `index.ts`);
27
- const indexFile = project.createSourceFile(filePath, undefined, {
28
- overwrite: true,
29
- });
30
-
31
- if (!multiClient || !batch || batch?.length === 1) {
32
- // if we are generate single client package for RLC
33
- generateRLCIndex(indexFile, model);
34
- } else {
35
- generateRLCIndexForMultiClient(indexFile, model);
36
- }
37
- return {
38
- path: filePath,
39
- content: indexFile.getFullText(),
40
- };
41
- }
42
-
43
- // to generate a index.ts for each single module inside the multi client RLC package
44
- function generateRLCIndexForMultiClient(file: SourceFile, model: RLCModel) {
45
- const clientName = model.libraryName;
46
- const createClientFuncName = `createClient`;
47
- const moduleName = normalizeName(clientName, NameType.File);
48
-
49
- file.addImportDeclaration({
50
- namespaceImport: "Parameters",
51
- moduleSpecifier: "./parameters.js",
52
- });
53
-
54
- file.addImportDeclaration({
55
- namespaceImport: "Responses",
56
- moduleSpecifier: "./responses.js",
57
- });
58
-
59
- file.addImportDeclaration({
60
- namespaceImport: "Client",
61
- moduleSpecifier: "./clientDefinitions.js",
62
- });
63
-
64
- const exports = ["Parameters", "Responses", "Client"];
65
- if (hasInputModels(model)) {
66
- file.addImportDeclaration({
67
- namespaceImport: "Models",
68
- moduleSpecifier: "./models.js",
69
- });
70
- exports.push("Models");
71
- }
72
-
73
- if (hasOutputModels(model)) {
74
- file.addImportDeclaration({
75
- namespaceImport: "OutputModels",
76
- moduleSpecifier: "./outputModels.js",
77
- });
78
- exports.push("OutputModels");
79
- }
80
-
81
- if (hasPagingOperations(model)) {
82
- file.addImportDeclaration({
83
- namespaceImport: "PaginateHelper",
84
- moduleSpecifier: "./paginateHelper.js",
85
- });
86
- exports.push("PaginateHelper");
87
- }
88
-
89
- if (hasUnexpectedHelper(model)) {
90
- file.addImportDeclaration({
91
- namespaceImport: "UnexpectedHelper",
92
- moduleSpecifier: "./isUnexpected.js",
93
- });
94
- exports.push("UnexpectedHelper");
95
- }
96
-
97
- if (hasPollingOperations(model)) {
98
- file.addImportDeclaration({
99
- namespaceImport: "PollingHelper",
100
- moduleSpecifier: "./pollingHelper.js",
101
- });
102
- exports.push("PollingHelper");
103
- }
104
-
105
- if (
106
- hasMultiCollection(model) ||
107
- hasSsvCollection(model) ||
108
- hasPipeCollection(model) ||
109
- hasTsvCollection(model) ||
110
- hasCsvCollection(model)
111
- ) {
112
- file.addImportDeclaration({
113
- namespaceImport: "SerializeHelper",
114
- moduleSpecifier: "./serializeHelper.js",
115
- });
116
- exports.push("SerializeHelper");
117
- }
118
-
119
- file.addExportDeclarations([
120
- {
121
- moduleSpecifier: `./${moduleName}.js`,
122
- namedExports: [`${createClientFuncName}`, `${clientName}ClientOptions`],
123
- },
124
- {
125
- namedExports: [...exports],
126
- },
127
- ]);
128
- }
129
-
130
- function generateRLCIndex(file: SourceFile, model: RLCModel) {
131
- const clientName = model.libraryName;
132
- const createClientFuncName = `${clientName}`;
133
- const moduleName = normalizeName(clientName, NameType.File);
134
-
135
- file.addImportDeclaration({
136
- moduleSpecifier: `./${moduleName}.js`,
137
- defaultImport: createClientFuncName,
138
- });
139
-
140
- file.addExportDeclarations([
141
- {
142
- moduleSpecifier: `./${moduleName}.js`,
143
- },
144
- {
145
- moduleSpecifier: `./parameters.js`,
146
- },
147
- {
148
- moduleSpecifier: `./responses.js`,
149
- },
150
- {
151
- moduleSpecifier: `./clientDefinitions.js`,
152
- },
153
- ]);
154
-
155
- if (hasUnexpectedHelper(model)) {
156
- file.addExportDeclarations([
157
- {
158
- moduleSpecifier: `./isUnexpected.js`,
159
- },
160
- ]);
161
- }
162
-
163
- if (hasInputModels(model)) {
164
- file.addExportDeclarations([
165
- {
166
- moduleSpecifier: `./models.js`,
167
- },
168
- ]);
169
- }
170
-
171
- if (hasOutputModels(model)) {
172
- file.addExportDeclarations([
173
- {
174
- moduleSpecifier: `./outputModels.js`,
175
- },
176
- ]);
177
- }
178
-
179
- if (hasPagingOperations(model)) {
180
- file.addExportDeclarations([
181
- {
182
- moduleSpecifier: `./paginateHelper.js`,
183
- },
184
- ]);
185
- }
186
-
187
- if (hasPollingOperations(model)) {
188
- file.addExportDeclarations([
189
- {
190
- moduleSpecifier: `./pollingHelper.js`,
191
- },
192
- ]);
193
- }
194
-
195
- if (
196
- hasMultiCollection(model) ||
197
- hasSsvCollection(model) ||
198
- hasPipeCollection(model) ||
199
- hasTsvCollection(model) ||
200
- hasCsvCollection(model)
201
- ) {
202
- file.addExportDeclarations([
203
- {
204
- moduleSpecifier: `./serializeHelper.js`,
205
- },
206
- ]);
207
- }
208
-
209
- file.addExportDeclarations([
210
- {
211
- moduleSpecifier: "@azure/core-rest-pipeline",
212
- namedExports: ["RestError", "isRestError"],
213
- },
214
- ]);
215
-
216
- file.addExportAssignment({
217
- expression: createClientFuncName,
218
- isExportEquals: false,
219
- });
220
- }