@autorest/typescript 6.0.31 → 6.0.32

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 (52) hide show
  1. package/README.md +1 -0
  2. package/dist/hlcREADME.md.hbs +2 -2
  3. package/dist/src/autorestSession.d.ts +1 -0
  4. package/dist/src/autorestSession.d.ts.map +1 -1
  5. package/dist/src/autorestSession.js.map +1 -1
  6. package/dist/src/generators/clientFileGenerator.d.ts.map +1 -1
  7. package/dist/src/generators/clientFileGenerator.js +8 -7
  8. package/dist/src/generators/clientFileGenerator.js.map +1 -1
  9. package/dist/src/generators/indexGenerator.d.ts.map +1 -1
  10. package/dist/src/generators/indexGenerator.js +6 -5
  11. package/dist/src/generators/indexGenerator.js.map +1 -1
  12. package/dist/src/generators/operationGenerator.d.ts.map +1 -1
  13. package/dist/src/generators/operationGenerator.js +11 -9
  14. package/dist/src/generators/operationGenerator.js.map +1 -1
  15. package/dist/src/generators/operationInterfaceGenerator.d.ts.map +1 -1
  16. package/dist/src/generators/operationInterfaceGenerator.js +5 -3
  17. package/dist/src/generators/operationInterfaceGenerator.js.map +1 -1
  18. package/dist/src/generators/parametersGenerator.d.ts.map +1 -1
  19. package/dist/src/generators/parametersGenerator.js +3 -2
  20. package/dist/src/generators/parametersGenerator.js.map +1 -1
  21. package/dist/src/generators/static/hlcREADME.md.hbs +2 -2
  22. package/dist/src/generators/static/packageFileGenerator.js +1 -1
  23. package/dist/src/generators/static/packageFileGenerator.js.map +1 -1
  24. package/dist/src/generators/static/readmeFileGenerator.js +1 -1
  25. package/dist/src/generators/static/readmeFileGenerator.js.map +1 -1
  26. package/dist/src/generators/utils/pagingOperations.d.ts.map +1 -1
  27. package/dist/src/generators/utils/pagingOperations.js +4 -2
  28. package/dist/src/generators/utils/pagingOperations.js.map +1 -1
  29. package/dist/src/generators/utils/tracingUtils.d.ts.map +1 -1
  30. package/dist/src/generators/utils/tracingUtils.js +3 -2
  31. package/dist/src/generators/utils/tracingUtils.js.map +1 -1
  32. package/dist/src/utils/autorestOptions.d.ts.map +1 -1
  33. package/dist/src/utils/autorestOptions.js +8 -1
  34. package/dist/src/utils/autorestOptions.js.map +1 -1
  35. package/dist/src/utils/nameConstructors.d.ts +21 -0
  36. package/dist/src/utils/nameConstructors.d.ts.map +1 -0
  37. package/dist/src/utils/nameConstructors.js +28 -0
  38. package/dist/src/utils/nameConstructors.js.map +1 -0
  39. package/package.json +2 -2
  40. package/src/autorestSession.ts +1 -0
  41. package/src/generators/clientFileGenerator.ts +28 -30
  42. package/src/generators/indexGenerator.ts +6 -5
  43. package/src/generators/operationGenerator.ts +32 -34
  44. package/src/generators/operationInterfaceGenerator.ts +6 -3
  45. package/src/generators/parametersGenerator.ts +3 -2
  46. package/src/generators/static/hlcREADME.md.hbs +2 -2
  47. package/src/generators/static/packageFileGenerator.ts +1 -1
  48. package/src/generators/static/readmeFileGenerator.ts +1 -1
  49. package/src/generators/utils/pagingOperations.ts +7 -5
  50. package/src/generators/utils/tracingUtils.ts +3 -2
  51. package/src/utils/autorestOptions.ts +8 -1
  52. package/src/utils/nameConstructors.ts +35 -0
@@ -51,6 +51,7 @@ import {
51
51
  import { calculateMethodName } from "./utils/operationsUtils";
52
52
  import { getAutorestOptions } from "../autorestSession";
53
53
  import { createLroImports, createLroType } from "../utils/lroHelpers";
54
+ import { getImportModuleName } from "../utils/nameConstructors";
54
55
 
55
56
  /**
56
57
  * Function that writes the code for all the operations.
@@ -65,7 +66,7 @@ export function generateOperations(
65
66
  clientDetails: ClientDetails,
66
67
  project: Project
67
68
  ): void {
68
- const { srcPath } = getAutorestOptions();
69
+ const { srcPath, moduleKind } = getAutorestOptions();
69
70
  let fileNames: string[] = [];
70
71
 
71
72
  // Toplevel operations are inlined in the client
@@ -88,7 +89,7 @@ export function generateOperations(
88
89
  operationIndexFile.addExportDeclarations(
89
90
  fileNames.map(fileName => {
90
91
  return {
91
- moduleSpecifier: `./${fileName}`
92
+ moduleSpecifier: getImportModuleName(`./${fileName}`, moduleKind)
92
93
  } as ExportDeclarationStructure;
93
94
  })
94
95
  );
@@ -328,8 +329,8 @@ function buildRequestBody({
328
329
  required = rb.required
329
330
  ? rb.required
330
331
  : rb.parameter.groupedBy && rb.parameter.groupedBy.required
331
- ? rb.parameter.groupedBy.required
332
- : required;
332
+ ? rb.parameter.groupedBy.required
333
+ : required;
333
334
 
334
335
  if (required) break;
335
336
  }
@@ -465,6 +466,7 @@ function addClass(
465
466
  clientDetails: ClientDetails
466
467
  ) {
467
468
  let importedModels = new Set<string>();
469
+ const { moduleKind } = getAutorestOptions();
468
470
 
469
471
  let allModelsNames = getAllModelsNames(clientDetails);
470
472
 
@@ -526,7 +528,7 @@ function addClass(
526
528
 
527
529
  operationGroupFile.addImportDeclaration({
528
530
  namedImports,
529
- moduleSpecifier: "../models"
531
+ moduleSpecifier: getImportModuleName({ cjsName: "../models", esModulesName: "../models/index.js" }, moduleKind)
530
532
  });
531
533
  }
532
534
  }
@@ -750,14 +752,13 @@ function compileOperationOptionsToRequestOptionsBase(
750
752
  // In Lro we have a couple extra properties to add that's why we use
751
753
  // the private getOperationOptions function instead of the one in core-http
752
754
  return isLro
753
- ? `this.getOperationOptions(${options}${
754
- lroResourceLocationConfig === undefined
755
- ? ""
756
- : `, "${lroResourceLocationConfig}"`
757
- })`
755
+ ? `this.getOperationOptions(${options}${lroResourceLocationConfig === undefined
756
+ ? ""
757
+ : `, "${lroResourceLocationConfig}"`
758
+ })`
758
759
  : !useCoreV2
759
- ? `coreHttp.operationOptionsToRequestOptionsBase(options || {})`
760
- : `options || {}`;
760
+ ? `coreHttp.operationOptionsToRequestOptionsBase(options || {})`
761
+ : `options || {}`;
761
762
  }
762
763
 
763
764
  function writeNoOverloadsOperationBody(
@@ -910,11 +911,11 @@ function writeLroOperationBody(
910
911
  const sendOperationStatement = !useCoreV2
911
912
  ? `const directSendOperation = async (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec): Promise<${responseName}> => {
912
913
  ${getTracingClientWithSpanStatement(
913
- sendRequestStatement,
914
- responseName,
915
- isTracingEnabled,
916
- spanName
917
- )}
914
+ sendRequestStatement,
915
+ responseName,
916
+ isTracingEnabled,
917
+ spanName
918
+ )}
918
919
  };
919
920
  const sendOperation = async (args: coreHttp.OperationArguments, spec: coreHttp.OperationSpec) => {
920
921
  const response = await directSendOperation(args, spec);
@@ -956,21 +957,18 @@ function writeLroOperationBody(
956
957
  }};
957
958
  }`;
958
959
 
959
- const commonOptions = `intervalInMs: options?.updateIntervalInMs${
960
- lroResourceLocationConfig
961
- ? `, ${
962
- useLegacyLro ? "lroResourceLocationConfig" : "resourceLocationConfig"
963
- }: "${lroResourceLocationConfig.toLowerCase()}"`
964
- : ""
965
- }`;
960
+ const commonOptions = `intervalInMs: options?.updateIntervalInMs${lroResourceLocationConfig
961
+ ? `, ${useLegacyLro ? "lroResourceLocationConfig" : "resourceLocationConfig"
962
+ }: "${lroResourceLocationConfig.toLowerCase()}"`
963
+ : ""
964
+ }`;
966
965
  methodDeclaration.addStatements([
967
966
  sendOperationStatement,
968
967
  `const lro = createLroSpec({sendOperationFn, args: ${operationParamsName},
969
968
  spec: ${operationSpecName}})`,
970
- `const poller = ${
971
- useLegacyLro
972
- ? `new LroEngine(lro, { resumeFrom: options?.resumeFrom, ${commonOptions} })`
973
- : `await createHttpPoller<${responseName}, OperationState<${responseName}>>(lro, { restoreFrom: options?.resumeFrom, ${commonOptions} })`
969
+ `const poller = ${useLegacyLro
970
+ ? `new LroEngine(lro, { resumeFrom: options?.resumeFrom, ${commonOptions} })`
971
+ : `await createHttpPoller<${responseName}, OperationState<${responseName}>>(lro, { restoreFrom: options?.resumeFrom, ${commonOptions} })`
974
972
  };`,
975
973
  "await poller.poll();",
976
974
  "return poller;"
@@ -1239,7 +1237,7 @@ function addImports(
1239
1237
  operationGroupFile: SourceFile,
1240
1238
  clientDetails: ClientDetails
1241
1239
  ) {
1242
- const { useCoreV2, useLegacyLro } = getAutorestOptions();
1240
+ const { useCoreV2, useLegacyLro, moduleKind } = getAutorestOptions();
1243
1241
 
1244
1242
  const { className, mappers } = clientDetails;
1245
1243
  addPagingEsNextRef(operationGroupDetails.operations, operationGroupFile);
@@ -1254,7 +1252,7 @@ function addImports(
1254
1252
 
1255
1253
  operationGroupFile.addImportDeclaration({
1256
1254
  namedImports: [`${operationGroupInterfaceName}`],
1257
- moduleSpecifier: "../operationsInterfaces"
1255
+ moduleSpecifier: getImportModuleName({ cjsName: "../operationsInterfaces", esModulesName: "../operationsInterfaces/index.js" }, moduleKind)
1258
1256
  });
1259
1257
 
1260
1258
  if (!useCoreV2) {
@@ -1284,14 +1282,14 @@ function addImports(
1284
1282
  if (mappers.length) {
1285
1283
  operationGroupFile.addImportDeclaration({
1286
1284
  namespaceImport: "Mappers",
1287
- moduleSpecifier: "../models/mappers"
1285
+ moduleSpecifier: getImportModuleName("../models/mappers", moduleKind)
1288
1286
  });
1289
1287
  }
1290
1288
 
1291
1289
  if (shouldImportParameters(clientDetails)) {
1292
1290
  operationGroupFile.addImportDeclaration({
1293
1291
  namespaceImport: "Parameters",
1294
- moduleSpecifier: "../models/parameters"
1292
+ moduleSpecifier: getImportModuleName("../models/parameters", moduleKind)
1295
1293
  });
1296
1294
  }
1297
1295
 
@@ -1301,7 +1299,7 @@ function addImports(
1301
1299
 
1302
1300
  operationGroupFile.addImportDeclaration({
1303
1301
  namedImports: [`${clientClassName}`],
1304
- moduleSpecifier: `../${clientFileName}`
1302
+ moduleSpecifier: getImportModuleName(`../${clientFileName}`, moduleKind)
1305
1303
  });
1306
1304
 
1307
1305
  if (hasLroOperation(operationGroupDetails)) {
@@ -1311,7 +1309,7 @@ function addImports(
1311
1309
  });
1312
1310
  operationGroupFile.addImportDeclaration({
1313
1311
  namedImports: ["createLroSpec"],
1314
- moduleSpecifier: `../lroImpl`
1312
+ moduleSpecifier: getImportModuleName(`../lroImpl`, moduleKind)
1315
1313
  });
1316
1314
  }
1317
1315
  }
@@ -30,6 +30,7 @@ import {
30
30
  import { calculateMethodName } from "./utils/operationsUtils";
31
31
  import { getAutorestOptions } from "../autorestSession";
32
32
  import { createLroType } from "../utils/lroHelpers";
33
+ import { getImportModuleName } from "../utils/nameConstructors";
33
34
 
34
35
  /**
35
36
  * Function that writes the code for all the operations.
@@ -44,7 +45,7 @@ export function generateOperationsInterfaces(
44
45
  clientDetails: ClientDetails,
45
46
  project: Project
46
47
  ): void {
47
- const { srcPath } = getAutorestOptions();
48
+ const { srcPath, moduleKind } = getAutorestOptions();
48
49
  let fileNames: string[] = [];
49
50
 
50
51
  // Toplevel operations are inlined in the client
@@ -67,7 +68,7 @@ export function generateOperationsInterfaces(
67
68
  operationIndexFile.addExportDeclarations(
68
69
  fileNames.map(fileName => {
69
70
  return {
70
- moduleSpecifier: `./${fileName}`
71
+ moduleSpecifier: getImportModuleName(`./${fileName}`, moduleKind)
71
72
  } as ExportDeclarationStructure;
72
73
  })
73
74
  );
@@ -128,6 +129,8 @@ function addInterface(
128
129
  operationGroupDetails: OperationGroupDetails,
129
130
  clientDetails: ClientDetails
130
131
  ) {
132
+ const { moduleKind } = getAutorestOptions();
133
+
131
134
  let importedModels = new Set<string>();
132
135
 
133
136
  let allModelsNames = getAllModelsNames(clientDetails);
@@ -164,7 +167,7 @@ function addInterface(
164
167
 
165
168
  operationGroupFile.addImportDeclaration({
166
169
  namedImports,
167
- moduleSpecifier: "../models"
170
+ moduleSpecifier: getImportModuleName({ cjsName: "../models", esModulesName: "../models/index.js" }, moduleKind)
168
171
  });
169
172
  }
170
173
  }
@@ -15,12 +15,13 @@ import { writeMapper } from "./mappersGenerator";
15
15
  import { shouldImportParameters } from "./utils/importUtils";
16
16
  import { logger } from "../utils/logger";
17
17
  import { getAutorestOptions } from "../autorestSession";
18
+ import { getImportModuleName } from "../utils/nameConstructors";
18
19
 
19
20
  export function generateParameters(
20
21
  clientDetails: ClientDetails,
21
22
  project: Project
22
23
  ): void {
23
- const { useCoreV2, srcPath } = getAutorestOptions();
24
+ const { useCoreV2, srcPath, moduleKind } = getAutorestOptions();
24
25
  if (!shouldImportParameters(clientDetails)) {
25
26
  logger.verbose(
26
27
  "There are no parameters to generate, skipping parameters file generation"
@@ -46,7 +47,7 @@ export function generateParameters(
46
47
  if (importedMappers.length) {
47
48
  parametersFile.addImportDeclaration({
48
49
  namedImports: importedMappers,
49
- moduleSpecifier: "../models/mappers"
50
+ moduleSpecifier: getImportModuleName("../models/mappers", moduleKind)
50
51
  });
51
52
  }
52
53
 
@@ -59,7 +59,7 @@ npm install @azure/identity
59
59
 
60
60
  You will also need to **register a new AAD application and grant access to {{ serviceName}}** by assigning the suitable role to your service principal (note: roles such as `"Owner"` will not grant the necessary permissions).
61
61
 
62
- For more information about how to create an Azure AD Application check out [this guide](https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).
62
+ For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).
63
63
 
64
64
  {{#if azureArm}}
65
65
  ```javascript
@@ -146,7 +146,7 @@ If you'd like to contribute to this library, please read the [contributing guide
146
146
  {{#if impressionURL}}![Impressions]({{ impressionURL }})
147
147
  {{/if}}
148
148
 
149
- [azure_cli]: https://docs.microsoft.com/cli/azure
149
+ [azure_cli]: https://learn.microsoft.com/cli/azure
150
150
  [azure_sub]: https://azure.microsoft.com/free/
151
151
  [azure_sub]: https://azure.microsoft.com/free/
152
152
  [azure_portal]: https://portal.azure.com
@@ -238,7 +238,7 @@ function regularAutorestPackage(
238
238
  productName: description,
239
239
  productSlugs: ["azure"],
240
240
  disableDocsMs: true,
241
- apiRefLink: `https://docs.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
241
+ apiRefLink: `https://learn.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
242
242
  };
243
243
  }
244
244
  return packageInfo;
@@ -155,7 +155,7 @@ function createMetadata(
155
155
  clientDescriptiveName: `${serviceName} client`,
156
156
  description: codeModel.info?.description,
157
157
  apiRefURL: azureHuh
158
- ? `https://docs.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
158
+ ? `https://learn.microsoft.com/javascript/api/${clientPackageName}${apiRefUrlQueryParameter}`
159
159
  : undefined,
160
160
  packageNPMURL: `https://www.npmjs.com/package/${clientPackageName}`,
161
161
  contributingGuideURL: repoURL && `${repoURL}/blob/main/CONTRIBUTING.md`,
@@ -20,6 +20,7 @@ import {
20
20
  getPagingResponseBodyType
21
21
  } from "./responseTypeUtils";
22
22
  import { getAutorestOptions } from "../../autorestSession";
23
+ import { getImportModuleName } from "../../utils/nameConstructors";
23
24
 
24
25
  type MethodParameter = OptionalKind<
25
26
  ParameterDeclarationStructure & {
@@ -54,8 +55,9 @@ export function addPagingImports(
54
55
  sourceFile: SourceFile,
55
56
  isClient = false
56
57
  ) {
57
- const { disablePagingAsyncIterators } = getAutorestOptions();
58
+ const { disablePagingAsyncIterators, moduleKind } = getAutorestOptions();
58
59
  if (!disablePagingAsyncIterators && hasAsyncIteratorOperations(operations)) {
60
+ const pagingHelperPath = isClient ? "./pagingHelper" : "../pagingHelper";
59
61
  sourceFile.addImportDeclarations([
60
62
  {
61
63
  namedImports: ["PagedAsyncIterableIterator", "PageSettings"],
@@ -63,7 +65,7 @@ export function addPagingImports(
63
65
  },
64
66
  {
65
67
  namedImports: ["setContinuationToken"],
66
- moduleSpecifier: isClient ? "./pagingHelper" : "../pagingHelper"
68
+ moduleSpecifier: getImportModuleName(pagingHelperPath, moduleKind)
67
69
  }
68
70
  ]);
69
71
  }
@@ -227,9 +229,9 @@ export function writeAsyncIterators(
227
229
  },
228
230
  nextMethod: nextMethodParameters
229
231
  ? {
230
- name: `${operation.namePrefix}${nextOperationName}`,
231
- parameters: nextMethodParameters
232
- }
232
+ name: `${operation.namePrefix}${nextOperationName}`,
233
+ parameters: nextMethodParameters
234
+ }
233
235
  : undefined,
234
236
  publicMethod: {
235
237
  name: getPublicMethodName(operation),
@@ -1,5 +1,6 @@
1
1
  import { SourceFile } from "ts-morph";
2
2
  import { getAutorestOptions } from "../../autorestSession";
3
+ import { getImportModuleName } from "../../utils/nameConstructors";
3
4
 
4
5
  /**
5
6
  * Adds the required imports to have operations tracing
@@ -10,12 +11,12 @@ export function addTracingOperationImports(
10
11
  sourceFile: SourceFile,
11
12
  traverseToRoot = ".."
12
13
  ) {
13
- const { tracingInfo } = getAutorestOptions();
14
+ const { tracingInfo, moduleKind } = getAutorestOptions();
14
15
  if (tracingInfo) {
15
16
  sourceFile.addImportDeclarations([
16
17
  {
17
18
  namedImports: ["tracingClient"],
18
- moduleSpecifier: `${traverseToRoot}/tracing`
19
+ moduleSpecifier: getImportModuleName(`${traverseToRoot}/tracing`, moduleKind)
19
20
  }
20
21
  ]);
21
22
  }
@@ -46,6 +46,8 @@ export async function extractAutorestOptions(): Promise<AutorestOptions> {
46
46
  const dependencyInfo = await getDependencyInfo(host);
47
47
  const flavor = await getFlavor(host);
48
48
 
49
+ const moduleKind = await getModuleKind(host);
50
+
49
51
  return {
50
52
  azureArm,
51
53
  addCredentials,
@@ -79,7 +81,8 @@ export async function extractAutorestOptions(): Promise<AutorestOptions> {
79
81
  coreHttpCompatMode,
80
82
  dependencyInfo,
81
83
  useLegacyLro,
82
- flavor
84
+ flavor,
85
+ moduleKind
83
86
  };
84
87
  }
85
88
 
@@ -399,3 +402,7 @@ async function getDependencyInfo(
399
402
  "Invalid dependency-info. Make sure that link and description are defined"
400
403
  );
401
404
  }
405
+
406
+ async function getModuleKind(host: AutorestExtensionHost): Promise<AutorestOptions["moduleKind"]> {
407
+ return (await host.getValue("module-kind")) || "cjs";
408
+ }
@@ -0,0 +1,35 @@
1
+ export type ModuleName =
2
+ | string
3
+ | {
4
+ esModulesName: string;
5
+ cjsName: string;
6
+ };
7
+
8
+ /**
9
+ * This is a helper function that gets the right import module depending on the type of
10
+ * library being generated.
11
+ *
12
+ * @param name The name of the module to import - this can be a string representing the base name of the module or an object with the cjsName and esModulesName properties. *
13
+ * @param moduleKind The type of module being generated. This can be either "cjs" or "esm".
14
+ *
15
+ * @example
16
+ *
17
+ * ```ts
18
+ * getImportModuleName("myModule", "cjs") // returns "myModule"
19
+ * getImportModuleName("myModule", "esm") // returns "myModule.js"
20
+ * getImportModuleName({ cjsName: "myModule", esModulesName: "myModule/index.js" }, "cjs") // returns "myModule"
21
+ * getImportModuleName({ cjsName: "myModule", esModulesName: "myModule/index.js" }, "esm") // returns "myModule/index.js"
22
+ */
23
+ export function getImportModuleName(
24
+ name: ModuleName,
25
+ moduleKind?: "cjs" | "esm"
26
+ ): string {
27
+ const cjsName = typeof name === "string" ? name : name.cjsName;
28
+ const esModulesName =
29
+ typeof name === "string" ? `${name}.js` : name.esModulesName;
30
+ if (moduleKind === "esm") {
31
+ return esModulesName;
32
+ }
33
+ // CJS is considered the default in autorest.typescript
34
+ return cjsName;
35
+ }