@azure-tools/typespec-ts 0.18.0 → 0.19.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 (113) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/dist/src/index.d.ts.map +1 -1
  3. package/dist/src/index.js +36 -20
  4. package/dist/src/index.js.map +1 -1
  5. package/dist/src/lib.d.ts.map +1 -1
  6. package/dist/src/lib.js +4 -1
  7. package/dist/src/lib.js.map +1 -1
  8. package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
  9. package/dist/src/modular/buildClassicalClient.js +50 -51
  10. package/dist/src/modular/buildClassicalClient.js.map +1 -1
  11. package/dist/src/modular/buildClassicalOperationGroups.d.ts +4 -0
  12. package/dist/src/modular/buildClassicalOperationGroups.d.ts.map +1 -0
  13. package/dist/src/modular/buildClassicalOperationGroups.js +79 -0
  14. package/dist/src/modular/buildClassicalOperationGroups.js.map +1 -0
  15. package/dist/src/modular/buildClientContext.d.ts.map +1 -1
  16. package/dist/src/modular/buildClientContext.js +3 -2
  17. package/dist/src/modular/buildClientContext.js.map +1 -1
  18. package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
  19. package/dist/src/modular/buildCodeModel.js +69 -23
  20. package/dist/src/modular/buildCodeModel.js.map +1 -1
  21. package/dist/src/modular/buildOperations.d.ts +1 -1
  22. package/dist/src/modular/buildOperations.d.ts.map +1 -1
  23. package/dist/src/modular/buildOperations.js +21 -13
  24. package/dist/src/modular/buildOperations.js.map +1 -1
  25. package/dist/src/modular/buildProjectFiles.d.ts.map +1 -1
  26. package/dist/src/modular/buildProjectFiles.js +183 -73
  27. package/dist/src/modular/buildProjectFiles.js.map +1 -1
  28. package/dist/src/modular/buildRootIndex.d.ts.map +1 -1
  29. package/dist/src/modular/buildRootIndex.js +7 -5
  30. package/dist/src/modular/buildRootIndex.js.map +1 -1
  31. package/dist/src/modular/buildSubpathIndex.d.ts +5 -1
  32. package/dist/src/modular/buildSubpathIndex.d.ts.map +1 -1
  33. package/dist/src/modular/buildSubpathIndex.js +32 -4
  34. package/dist/src/modular/buildSubpathIndex.js.map +1 -1
  35. package/dist/src/modular/emitModels.d.ts.map +1 -1
  36. package/dist/src/modular/emitModels.js +3 -2
  37. package/dist/src/modular/emitModels.js.map +1 -1
  38. package/dist/src/modular/helpers/classicalOperationHelpers.d.ts +4 -0
  39. package/dist/src/modular/helpers/classicalOperationHelpers.d.ts.map +1 -0
  40. package/dist/src/modular/helpers/classicalOperationHelpers.js +127 -0
  41. package/dist/src/modular/helpers/classicalOperationHelpers.js.map +1 -0
  42. package/dist/src/modular/helpers/clientHelpers.d.ts +2 -1
  43. package/dist/src/modular/helpers/clientHelpers.d.ts.map +1 -1
  44. package/dist/src/modular/helpers/clientHelpers.js +3 -2
  45. package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
  46. package/dist/src/modular/helpers/namingHelpers.d.ts +2 -1
  47. package/dist/src/modular/helpers/namingHelpers.d.ts.map +1 -1
  48. package/dist/src/modular/helpers/namingHelpers.js +14 -0
  49. package/dist/src/modular/helpers/namingHelpers.js.map +1 -1
  50. package/dist/src/modular/helpers/operationHelpers.d.ts +4 -3
  51. package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
  52. package/dist/src/modular/helpers/operationHelpers.js +63 -48
  53. package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
  54. package/dist/src/modular/modularCodeModel.d.ts +4 -0
  55. package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
  56. package/dist/src/transform/transform.d.ts.map +1 -1
  57. package/dist/src/transform/transform.js +7 -5
  58. package/dist/src/transform/transform.js.map +1 -1
  59. package/dist/src/transform/transformParameters.d.ts +2 -2
  60. package/dist/src/transform/transformParameters.d.ts.map +1 -1
  61. package/dist/src/transform/transformParameters.js +2 -2
  62. package/dist/src/transform/transformParameters.js.map +1 -1
  63. package/dist/src/transform/transformResponses.d.ts +2 -2
  64. package/dist/src/transform/transformResponses.d.ts.map +1 -1
  65. package/dist/src/transform/transformResponses.js +2 -2
  66. package/dist/src/transform/transformResponses.js.map +1 -1
  67. package/dist/src/transform/transfromRLCOptions.d.ts.map +1 -1
  68. package/dist/src/transform/transfromRLCOptions.js +30 -4
  69. package/dist/src/transform/transfromRLCOptions.js.map +1 -1
  70. package/dist/src/utils/emitUtil.d.ts.map +1 -1
  71. package/dist/src/utils/emitUtil.js +12 -5
  72. package/dist/src/utils/emitUtil.js.map +1 -1
  73. package/dist/src/utils/modelUtils.d.ts.map +1 -1
  74. package/dist/src/utils/modelUtils.js +17 -9
  75. package/dist/src/utils/modelUtils.js.map +1 -1
  76. package/dist/src/utils/namespaceUtils.d.ts +6 -0
  77. package/dist/src/utils/namespaceUtils.d.ts.map +1 -0
  78. package/dist/src/utils/namespaceUtils.js +54 -0
  79. package/dist/src/utils/namespaceUtils.js.map +1 -0
  80. package/dist/src/utils/operationUtil.d.ts.map +1 -1
  81. package/dist/src/utils/operationUtil.js +9 -13
  82. package/dist/src/utils/operationUtil.js.map +1 -1
  83. package/dist/tsconfig.tsbuildinfo +1 -1
  84. package/package.json +11 -4
  85. package/src/index.ts +29 -12
  86. package/src/lib.ts +4 -1
  87. package/src/modular/buildClassicalClient.ts +90 -69
  88. package/src/modular/buildClassicalOperationGroups.ts +151 -0
  89. package/src/modular/buildClientContext.ts +3 -2
  90. package/src/modular/buildCodeModel.ts +82 -18
  91. package/src/modular/buildOperations.ts +41 -16
  92. package/src/modular/buildProjectFiles.ts +224 -87
  93. package/src/modular/buildRootIndex.ts +34 -5
  94. package/src/modular/buildSubpathIndex.ts +41 -6
  95. package/src/modular/emitModels.ts +9 -2
  96. package/src/modular/helpers/classicalOperationHelpers.ts +212 -0
  97. package/src/modular/helpers/clientHelpers.ts +7 -2
  98. package/src/modular/helpers/namingHelpers.ts +25 -1
  99. package/src/modular/helpers/operationHelpers.ts +101 -46
  100. package/src/modular/modularCodeModel.ts +4 -0
  101. package/src/transform/transform.ts +10 -6
  102. package/src/transform/transformParameters.ts +3 -3
  103. package/src/transform/transformResponses.ts +4 -4
  104. package/src/transform/transfromRLCOptions.ts +43 -4
  105. package/src/utils/emitUtil.ts +10 -4
  106. package/src/utils/modelUtils.ts +11 -1
  107. package/src/utils/namespaceUtils.ts +74 -0
  108. package/src/utils/operationUtil.ts +10 -19
  109. package/dist/src/modular/helpers/credentialHelpers.d.ts +0 -7
  110. package/dist/src/modular/helpers/credentialHelpers.d.ts.map +0 -1
  111. package/dist/src/modular/helpers/credentialHelpers.js +0 -32
  112. package/dist/src/modular/helpers/credentialHelpers.js.map +0 -1
  113. package/src/modular/helpers/credentialHelpers.ts +0 -40
@@ -50,7 +50,8 @@ import {
50
50
  HttpServer,
51
51
  isStatusCode,
52
52
  HttpOperation,
53
- getHttpOperation
53
+ getHttpOperation,
54
+ isSharedRoute
54
55
  } from "@typespec/http";
55
56
  import { getAddedOnVersions } from "@typespec/versioning";
56
57
  import {
@@ -99,6 +100,11 @@ import {
99
100
  } from "../utils/operationUtil.js";
100
101
  import { SdkContext } from "../utils/interfaces.js";
101
102
  import { Project } from "ts-morph";
103
+ import { buildRuntimeImports } from "@azure-tools/rlc-common";
104
+ import {
105
+ getModelNamespaceName,
106
+ getOperationNamespaceInterfaceName
107
+ } from "../utils/namespaceUtils.js";
102
108
 
103
109
  interface HttpServerParameter {
104
110
  type: "endpointPath";
@@ -769,6 +775,20 @@ function emitBasicOperation(
769
775
  );
770
776
  });
771
777
 
778
+ const namespaceHierarchies =
779
+ context.rlcOptions?.hierarchyClient === true
780
+ ? getOperationNamespaceInterfaceName(context, operation)
781
+ : [];
782
+
783
+ if (
784
+ namespaceHierarchies.length === 0 &&
785
+ context.rlcOptions?.hierarchyClient === false &&
786
+ operationGroupName !== "" &&
787
+ namespaceHierarchies[0] !== operationGroupName
788
+ ) {
789
+ namespaceHierarchies.push(operationGroupName);
790
+ }
791
+
772
792
  for (const param of httpOperation.parameters.parameters) {
773
793
  if (isIgnoredHeaderParam(param)) {
774
794
  continue;
@@ -783,7 +803,7 @@ function emitBasicOperation(
783
803
  // Set up responses for operation
784
804
  const responses: Response[] = [];
785
805
  const exceptions: Response[] = [];
786
- const isOverload: boolean = false;
806
+ const isOverload = isSharedRoute(context.program, operation);
787
807
  for (const response of httpOperation.responses) {
788
808
  for (const innerResponse of response.responses) {
789
809
  const emittedResponse: Response = emitResponse(
@@ -834,6 +854,7 @@ function emitBasicOperation(
834
854
  }
835
855
  }
836
856
  }
857
+
837
858
  const name = applyCasing(operation.name, { casing: CASING });
838
859
 
839
860
  /** handle name collision between operation name and parameter signature */
@@ -851,7 +872,7 @@ function emitBasicOperation(
851
872
  param.clientName = param.clientName + "Parameter";
852
873
  });
853
874
  return {
854
- name: name,
875
+ name: normalizeName(name, NameType.Operation, true),
855
876
  description: getDocStr(context.program, operation),
856
877
  summary: getSummary(context.program, operation) ?? "",
857
878
  url: httpOperation.path,
@@ -863,10 +884,11 @@ function emitBasicOperation(
863
884
  groupName: operationGroupName,
864
885
  addedOn: getAddedOnVersion(context.program, operation),
865
886
  discriminator: "basic",
866
- isOverload: false,
887
+ isOverload,
867
888
  overloads: [],
868
889
  apiVersions: [getAddedOnVersion(context.program, operation)],
869
- rlcResponse: rlcResponses?.[0]
890
+ rlcResponse: rlcResponses?.[0],
891
+ namespaceHierarchies
870
892
  };
871
893
  }
872
894
 
@@ -958,9 +980,20 @@ function emitModel(context: SdkContext, type: Model): Record<string, any> {
958
980
  getProjectedName(context.program, type, "javascript") ??
959
981
  getProjectedName(context.program, type, "client") ??
960
982
  getFriendlyName(context.program, type);
983
+ const fullNamespaceName =
984
+ getModelNamespaceName(context, type.namespace!)
985
+ .map((nsName) => {
986
+ return normalizeName(nsName, NameType.Interface);
987
+ })
988
+ .join("") +
989
+ (effectiveName ? effectiveName : getName(context.program, type));
961
990
  let modelName =
962
991
  overridedModelName ??
963
- (effectiveName ? effectiveName : getName(context.program, type));
992
+ (context.rlcOptions?.enableModelNamespace
993
+ ? fullNamespaceName
994
+ : effectiveName
995
+ ? effectiveName
996
+ : getName(context.program, type));
964
997
  if (
965
998
  !overridedModelName &&
966
999
  type.templateMapper &&
@@ -1419,6 +1452,10 @@ function emitOperationGroups(
1419
1452
  rlcModels: RLCModel
1420
1453
  ): OperationGroup[] {
1421
1454
  const operationGroups: OperationGroup[] = [];
1455
+ const groupMapping: Map<string, OperationGroup> = new Map<
1456
+ string,
1457
+ OperationGroup
1458
+ >();
1422
1459
  for (const operationGroup of listOperationGroups(context, client)) {
1423
1460
  const operations: HrlcOperation[] = [];
1424
1461
  const name = operationGroup.type.name;
@@ -1428,27 +1465,53 @@ function emitOperationGroups(
1428
1465
  )) {
1429
1466
  operations.push(emitOperation(context, operation, name, rlcModels));
1430
1467
  }
1431
- operationGroups.push({
1432
- className: name,
1433
- propertyName: name,
1434
- operations: operations
1435
- });
1468
+ if (operations.length > 0) {
1469
+ addHierarchyOperationGroup(operations, groupMapping);
1470
+ }
1436
1471
  }
1437
1472
  const clientOperations: HrlcOperation[] = [];
1438
1473
  for (const operation of listOperationsInOperationGroup(context, client)) {
1439
1474
  clientOperations.push(emitOperation(context, operation, "", rlcModels));
1440
1475
  }
1441
1476
  if (clientOperations.length > 0) {
1442
- operationGroups.push({
1443
- className: "",
1444
- propertyName: "",
1445
- operations: clientOperations
1446
- });
1477
+ addHierarchyOperationGroup(clientOperations, groupMapping);
1478
+ }
1479
+
1480
+ groupMapping.forEach((value) => {
1481
+ operationGroups.push(value);
1482
+ });
1483
+ if (
1484
+ context.rlcOptions?.hierarchyClient === false &&
1485
+ context.rlcOptions?.enableOperationGroup
1486
+ ) {
1487
+ resolveConflictIfExist(operationGroups);
1447
1488
  }
1448
- resolveConflictIfExist(operationGroups);
1449
1489
  return operationGroups;
1450
1490
  }
1451
1491
 
1492
+ function addHierarchyOperationGroup(
1493
+ operations: HrlcOperation[],
1494
+ groupMapping: Map<string, OperationGroup>
1495
+ ): OperationGroup[] {
1496
+ if (operations.length > 0) {
1497
+ operations.forEach((op) => {
1498
+ const groupName = op.namespaceHierarchies.join("") ?? "";
1499
+ if (!groupMapping.has(groupName)) {
1500
+ groupMapping.set(groupName, {
1501
+ className: groupName,
1502
+ propertyName: groupName,
1503
+ operations: [op],
1504
+ namespaceHierarchies: op.namespaceHierarchies
1505
+ });
1506
+ } else {
1507
+ groupMapping.get(groupName)!.operations.push(op);
1508
+ }
1509
+ });
1510
+ return [...groupMapping.values()];
1511
+ }
1512
+ return [];
1513
+ }
1514
+
1452
1515
  function resolveConflictIfExist(operationGroups: OperationGroup[]) {
1453
1516
  if (operationGroups.length < 2) {
1454
1517
  return;
@@ -1705,7 +1768,8 @@ export function emitCodeModel(
1705
1768
  subnamespaceToClients: {},
1706
1769
  clients: [],
1707
1770
  types: [],
1708
- project
1771
+ project,
1772
+ runtimeImports: buildRuntimeImports(dpgContext.rlcOptions?.branded ?? true)
1709
1773
  };
1710
1774
 
1711
1775
  typesMap.clear();
@@ -11,6 +11,7 @@ import { Client, ModularCodeModel, Operation } from "./modularCodeModel.js";
11
11
  import { isRLCMultiEndpoint } from "../utils/clientUtils.js";
12
12
  import { getDocsFromDescription } from "./helpers/docsHelpers.js";
13
13
  import { SdkContext } from "../utils/interfaces.js";
14
+ import { getImportSpecifier } from "@azure-tools/rlc-common";
14
15
 
15
16
  /**
16
17
  * This function creates a file under /api for each operation group.
@@ -26,23 +27,34 @@ export function buildOperationFiles(
26
27
  const operationFiles = [];
27
28
  for (const operationGroup of client.operationGroups) {
28
29
  const importSet: Map<string, Set<string>> = new Map<string, Set<string>>();
29
- const fileName = operationGroup.className
30
- ? `${normalizeName(operationGroup.className, NameType.File)}`
31
- : // When the program has no operation groups defined all operations are put
32
- // into a nameless operation group. We'll call this operations.
33
- "operations";
30
+ const operationFileName =
31
+ operationGroup.className && operationGroup.namespaceHierarchies.length > 0
32
+ ? `${operationGroup.namespaceHierarchies
33
+ .map((hierarchy) => {
34
+ return normalizeName(hierarchy, NameType.File);
35
+ })
36
+ .join("/")}/index`
37
+ : // When the program has no operation groups defined all operations are put
38
+ // into a nameless operation group. We'll call this operations.
39
+ "operations";
34
40
 
35
41
  const subfolder = client.subfolder;
36
42
  const srcPath = codeModel.modularOptions.sourceRoot;
37
43
  const operationGroupFile = codeModel.project.createSourceFile(
38
44
  `${srcPath}/${
39
45
  subfolder && subfolder !== "" ? subfolder + "/" : ""
40
- }api/${fileName}.ts`
46
+ }api/${operationFileName}.ts`
41
47
  );
42
48
 
43
49
  // Import models used from ./models.ts
44
50
  // We SHOULD keep this because otherwise ts-morph will "helpfully" try to import models from the rest layer when we call fixMissingImports().
45
- importModels(srcPath, operationGroupFile, codeModel.project, subfolder);
51
+ importModels(
52
+ srcPath,
53
+ operationGroupFile,
54
+ codeModel.project,
55
+ subfolder,
56
+ operationGroup.namespaceHierarchies.length
57
+ );
46
58
 
47
59
  const namedImports: string[] = [];
48
60
  let clientType = "Client";
@@ -54,7 +66,9 @@ export function buildOperationFiles(
54
66
  }
55
67
  operationGroupFile.addImportDeclarations([
56
68
  {
57
- moduleSpecifier: `../../rest/${subfolder}/index.js`,
69
+ moduleSpecifier: `../${"../".repeat(
70
+ operationGroup.namespaceHierarchies.length
71
+ )}rest/${subfolder}/index.js`,
58
72
  namedImports
59
73
  }
60
74
  ]);
@@ -68,7 +82,9 @@ export function buildOperationFiles(
68
82
  {
69
83
  moduleSpecifier: `${
70
84
  subfolder && subfolder !== "" ? "../" : ""
71
- }../rest/index.js`,
85
+ }${"../".repeat(
86
+ operationGroup.namespaceHierarchies.length + 1
87
+ )}rest/index.js`,
72
88
  namedImports
73
89
  }
74
90
  ]);
@@ -79,13 +95,15 @@ export function buildOperationFiles(
79
95
  dpgContext,
80
96
  o,
81
97
  clientType,
82
- importSet
98
+ importSet,
99
+ codeModel.runtimeImports
83
100
  );
84
101
  const deserializeOperationDeclaration = getDeserializePrivateFunction(
85
102
  o,
86
103
  isRLCMultiEndpoint(dpgContext),
87
104
  needUnexpectedHelper,
88
- importSet
105
+ importSet,
106
+ codeModel.runtimeImports
89
107
  );
90
108
  operationGroupFile.addFunctions([
91
109
  sendOperationDeclaration,
@@ -96,7 +114,10 @@ export function buildOperationFiles(
96
114
 
97
115
  operationGroupFile.addImportDeclarations([
98
116
  {
99
- moduleSpecifier: "@azure-rest/core-client",
117
+ moduleSpecifier: getImportSpecifier(
118
+ "restClient",
119
+ codeModel?.runtimeImports
120
+ ),
100
121
  namedImports: [
101
122
  "StreamableMethod",
102
123
  "operationOptionsToRequestParameters"
@@ -125,8 +146,12 @@ export function importModels(
125
146
  srcPath: string,
126
147
  sourceFile: SourceFile,
127
148
  project: Project,
128
- subfolder: string = ""
149
+ subfolder: string = "",
150
+ importLayer: number = 0
129
151
  ) {
152
+ const hasModelsImport = sourceFile.getImportDeclarations().some((i) => {
153
+ return i.getModuleSpecifierValue().endsWith(`models/models.js`);
154
+ });
130
155
  const modelsFile = project.getSourceFile(
131
156
  `${srcPath}/${
132
157
  subfolder && subfolder !== "" ? subfolder + "/" : ""
@@ -138,9 +163,9 @@ export function importModels(
138
163
  models.push(entry[0]);
139
164
  }
140
165
 
141
- if (models.length > 0) {
166
+ if (models.length > 0 && !hasModelsImport) {
142
167
  sourceFile.addImportDeclaration({
143
- moduleSpecifier: "../models/models.js",
168
+ moduleSpecifier: `${"../".repeat(importLayer + 1)}models/models.js`,
144
169
  namedImports: models
145
170
  });
146
171
  }
@@ -162,7 +187,7 @@ export function buildOperationOptions(
162
187
  .filter((p) => p.optional || p.clientDefaultValue);
163
188
  const options = [...optionalParameters];
164
189
 
165
- const name = getOperationOptionsName(operation);
190
+ const name = getOperationOptionsName(operation, true);
166
191
 
167
192
  sourceFile.addInterface({
168
193
  name,