@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,32 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { File as RLCFile, RLCModel, RLCSampleGroup } from "./interfaces.js";
5
- import { sampleTemplate } from "./static/sample-template.js";
6
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
7
- // @ts-ignore: to fix the handlebars issue
8
- import { joinPaths } from "@typespec/compiler";
9
- import hbs from "handlebars";
10
-
11
- // Build sample files for the model based on the sample groups
12
- export function buildSamples(model: RLCModel) {
13
- if (!model.options || !model.options.packageDetails) {
14
- return;
15
- }
16
- const sampleGroups: RLCSampleGroup[] | undefined = model.sampleGroups;
17
- if (!sampleGroups || sampleGroups.length === 0) {
18
- return;
19
- }
20
- const sampleFiles: RLCFile[] = [];
21
- for (const sampleGroup of sampleGroups) {
22
- const sampleGroupFileContents = hbs.compile(sampleTemplate, {
23
- noEscape: true,
24
- });
25
- const filePath = joinPaths("samples-dev", `${sampleGroup.filename}.ts`);
26
- sampleFiles.push({
27
- path: filePath,
28
- content: sampleGroupFileContents(sampleGroup),
29
- });
30
- }
31
- return sampleFiles;
32
- }
@@ -1,79 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { joinPaths } from "@typespec/compiler";
5
- import { Project } from "ts-morph";
6
- import {
7
- buildObjectAliases,
8
- buildObjectInterfaces,
9
- buildPolymorphicAliases,
10
- } from "./build-object-types.js";
11
- import { getImportSpecifier } from "./helpers/imports-util.js";
12
- import { RLCModel, SchemaContext } from "./interfaces.js";
13
-
14
- /**
15
- * Generates types to represent schema definitions in the swagger
16
- */
17
- export function buildSchemaTypes(model: RLCModel) {
18
- const { srcPath } = model;
19
- const project = new Project({ useInMemoryFileSystem: true });
20
- let filePath = joinPaths(srcPath, `models.ts`);
21
- const inputModelFile = generateModelFiles(model, project, filePath, [SchemaContext.Input]);
22
- filePath = joinPaths(srcPath, `outputModels.ts`);
23
- const outputModelFile = generateModelFiles(model, project, filePath, [
24
- SchemaContext.Output,
25
- SchemaContext.Exception,
26
- ]);
27
- return { inputModelFile, outputModelFile };
28
- }
29
-
30
- export function generateModelFiles(
31
- model: RLCModel,
32
- project: Project,
33
- filePath: string,
34
- schemaContext: SchemaContext[],
35
- ) {
36
- // Track models that need to be imported
37
- const importedModels = new Set<string>();
38
- const objectsDefinitions = buildObjectInterfaces(model, importedModels, schemaContext);
39
-
40
- const objectTypeAliases = buildPolymorphicAliases(model, schemaContext);
41
-
42
- const objectAliases = buildObjectAliases(model, importedModels, schemaContext);
43
-
44
- if (objectTypeAliases.length || objectsDefinitions.length || objectAliases.length) {
45
- const modelsFile = project.createSourceFile(filePath, undefined, {
46
- overwrite: true,
47
- });
48
-
49
- modelsFile.addInterfaces(objectsDefinitions);
50
- modelsFile.addTypeAliases(objectTypeAliases);
51
- modelsFile.addTypeAliases(objectAliases);
52
- if (importedModels.size > 0) {
53
- modelsFile.addImportDeclarations([
54
- {
55
- isTypeOnly: true,
56
- namedImports: [...Array.from(importedModels || [])],
57
- moduleSpecifier: getImportSpecifier("restClient", model.importInfo.runtimeImports),
58
- },
59
- ]);
60
- }
61
- // Add NodeReadableStream import if binary types are used in models.
62
- // platform-types.ts is generated directly under src/ (no static-helpers/
63
- // subdirectory) to match the RLC design where all output lives in src/.
64
- // The platform-types static helper resolves NodeReadableStream to
65
- // NodeJS.ReadableStream on Node and `never` on browser/react-native, so the
66
- // union arm drops out naturally in non-Node builds.
67
- if (modelsFile.getFullText().includes("NodeReadableStream")) {
68
- modelsFile.addImportDeclarations([
69
- {
70
- isTypeOnly: true,
71
- namedImports: ["NodeReadableStream"],
72
- moduleSpecifier: "#platform/platform-types",
73
- },
74
- ]);
75
- }
76
- return { path: filePath, content: modelsFile.getFullText() };
77
- }
78
- return undefined;
79
- }
@@ -1,49 +0,0 @@
1
- import { joinPaths } from "@typespec/compiler";
2
- import { RLCModel } from "./interfaces.js";
3
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
4
- // @ts-ignore: to fix the handlebars issue
5
- import hbs from "handlebars";
6
- import {
7
- hasCsvCollection,
8
- hasMultiCollection,
9
- hasPipeCollection,
10
- hasSsvCollection,
11
- hasTsvCollection,
12
- } from "./helpers/operation-helpers.js";
13
- import {
14
- buildCsvCollectionContent,
15
- buildMultiCollectionContent,
16
- buildPipeCollectionContent,
17
- buildSsvCollectionContent,
18
- buildTsvCollectionContent,
19
- } from "./static/serialize-helper.js";
20
-
21
- export function buildSerializeHelper(model: RLCModel) {
22
- let serializeHelperContent = "";
23
- if (hasMultiCollection(model)) {
24
- serializeHelperContent += "\n" + buildMultiCollectionContent;
25
- }
26
- if (hasPipeCollection(model)) {
27
- serializeHelperContent += "\n" + buildPipeCollectionContent;
28
- }
29
- if (hasSsvCollection(model)) {
30
- serializeHelperContent += "\n" + buildSsvCollectionContent;
31
- }
32
- if (hasTsvCollection(model)) {
33
- serializeHelperContent += "\n" + buildTsvCollectionContent;
34
- }
35
- if (hasCsvCollection(model)) {
36
- serializeHelperContent += "\n" + buildCsvCollectionContent;
37
- }
38
- if (serializeHelperContent !== "") {
39
- const readmeFileContents = hbs.compile(serializeHelperContent, {
40
- noEscape: true,
41
- });
42
- const { srcPath } = model;
43
- return {
44
- path: joinPaths(srcPath, "serializeHelper.ts"),
45
- content: readmeFileContents({}),
46
- };
47
- }
48
- return undefined;
49
- }
@@ -1,51 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { joinPaths } from "@typespec/compiler";
5
- import { Project } from "ts-morph";
6
- import { NameType, normalizeName } from "./helpers/name-utils.js";
7
- import { getRelativePartFromSrcPath } from "./helpers/path-utils.js";
8
- import { RLCModel } from "./interfaces.js";
9
-
10
- const batchOutputFolder: [string, string, string][] = [];
11
-
12
- export function buildTopLevelIndex(model: RLCModel) {
13
- if (!model.options) {
14
- return undefined;
15
- }
16
- const project = new Project({ useInMemoryFileSystem: true });
17
- const { srcPath } = model;
18
- const { multiClient } = model.options;
19
- const batch = model.options.batch;
20
- if (srcPath) {
21
- const clientName = model.libraryName;
22
- const moduleName = normalizeName(clientName, NameType.File);
23
- const relativePath = "./" + getRelativePartFromSrcPath(srcPath, model.options.isModularLibrary);
24
- batchOutputFolder.push([relativePath, clientName, moduleName]);
25
- }
26
- if (multiClient && batch && batch.length > 1 && batchOutputFolder.length === batch.length) {
27
- const indexFile = project.createSourceFile("index.ts", undefined, {
28
- overwrite: true,
29
- });
30
- const allModules: string[] = [];
31
- batchOutputFolder.forEach((item) => {
32
- indexFile.addImportDeclaration({
33
- isTypeOnly: true,
34
- namespaceImport: item[1],
35
- moduleSpecifier: `${item[0]}/index.js`,
36
- });
37
- allModules.push(item[1]);
38
- });
39
- indexFile.addExportDeclaration({
40
- namedExports: [...allModules],
41
- });
42
- const content = indexFile.getFullText();
43
- const filePath = joinPaths(
44
- srcPath.substring(0, srcPath.lastIndexOf("src") + 4),
45
- model.options.isModularLibrary ? "rest" : "",
46
- `index.ts`,
47
- );
48
- return { path: filePath, content };
49
- }
50
- return undefined;
51
- }
@@ -1,8 +0,0 @@
1
- export function getRelativePartFromSrcPath(srcPath: string, isModularLibrary: boolean = false) {
2
- const sep = "/";
3
- let relativePart = srcPath.substring(srcPath.indexOf(sep + "src") + 4);
4
- if (isModularLibrary) {
5
- relativePart = relativePart.substring(srcPath.indexOf(sep + "rest"), +5);
6
- }
7
- return relativePart.startsWith(sep) ? relativePart.substring(1) : relativePart;
8
- }
@@ -1,58 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { InterfaceDeclarationStructure, MethodSignatureStructure, OptionalKind } from "ts-morph";
5
- import { PathMetadata, Paths } from "../interfaces.js";
6
- import { NameType, normalizeName } from "./name-utils.js";
7
- import { buildMethodDefinitions } from "./operation-helpers.js";
8
-
9
- export function generateMethodShortcuts(
10
- paths: Paths,
11
- ): OptionalKind<InterfaceDeclarationStructure>[] {
12
- const keys: Record<string, OptionalKind<MethodSignatureStructure>[]> = {};
13
- for (const path in paths) {
14
- const pathMetadata = paths[path];
15
- if (!pathMetadata) {
16
- continue;
17
- }
18
- const groupName = pathMetadata.operationGroupName;
19
- const definitions = buildOperationDefinitions(pathMetadata);
20
- if (!keys[groupName]) {
21
- keys[groupName] = definitions;
22
- } else {
23
- keys[groupName] = [...keys[groupName], ...definitions];
24
- }
25
- }
26
-
27
- const interfaces: OptionalKind<InterfaceDeclarationStructure>[] = [];
28
-
29
- for (const interfaceName in keys) {
30
- const methods = keys[interfaceName];
31
- interfaces.push({
32
- name: `${interfaceName}Operations`,
33
- methods: methods,
34
- isExported: true,
35
- docs: [`Contains operations for ${interfaceName} operations`],
36
- });
37
- }
38
-
39
- return interfaces;
40
- }
41
-
42
- function buildOperationDefinitions(path: PathMetadata): OptionalKind<MethodSignatureStructure>[] {
43
- let ops: OptionalKind<MethodSignatureStructure>[] = [];
44
-
45
- for (const verb in path.methods) {
46
- const methods = path.methods[verb];
47
- if (!methods) {
48
- continue;
49
- }
50
- for (const method of methods) {
51
- const name = normalizeName(method.operationName, NameType.Property);
52
- const pathParams = path.pathParameters;
53
- const methodDefinitions = buildMethodDefinitions({ [name]: [method] }, pathParams);
54
- ops = [...ops, ...methodDefinitions];
55
- }
56
- }
57
- return ops;
58
- }
@@ -1,234 +0,0 @@
1
- // Copyright (c) Microsoft Corporation.
2
- // Licensed under the MIT License.
3
-
4
- import { getImmediateParentsNames } from "../build-object-types.js";
5
- import {
6
- ArraySchema,
7
- DictionarySchema,
8
- ObjectSchema,
9
- Schema,
10
- SchemaContext,
11
- } from "../interfaces.js";
12
- import {
13
- TypeScriptType,
14
- getArrayObjectType,
15
- getNativeArrayType,
16
- getRecordType,
17
- getUnionType,
18
- isArrayObject,
19
- isNativeArray,
20
- leaveBracket,
21
- leaveStringQuotes,
22
- toTypeScriptTypeFromName,
23
- toTypeScriptTypeFromSchema,
24
- } from "./type-util.js";
25
-
26
- /**
27
- * Generate parameter type value for the given type and parameter name
28
- * @param type the typescript type
29
- * @param parameterName the parameter name
30
- * @param schemaMap the schema info to help generate the value
31
- * @param path optional path to help detect self reference
32
- * @param _allowMockValue the flag to indicate whether to allow mock value, currently we always generate mock value
33
- * @returns
34
- */
35
- export function generateParameterTypeValue(
36
- type: string,
37
- parameterName: string,
38
- schemaMap: Map<string, Schema>,
39
- path: Set<string> = new Set(),
40
- _allowMockValue = true,
41
- ): string | undefined {
42
- type = leaveBracket(type?.trim());
43
- let tsType: TypeScriptType | undefined;
44
- // Give priority to suggest the ts-type from schema
45
- if (schemaMap.has(type)) {
46
- tsType = toTypeScriptTypeFromSchema(schemaMap.get(type)!);
47
- }
48
- // Fallback to suggest ts-type from the type iteself
49
- if (!tsType) {
50
- tsType = toTypeScriptTypeFromName(type);
51
- }
52
- switch (tsType) {
53
- case TypeScriptType.string:
54
- return `"{Your ${leaveStringQuotes(parameterName)}}"`;
55
- case TypeScriptType.number:
56
- return "123";
57
- case TypeScriptType.boolean:
58
- return "true";
59
- case TypeScriptType.date:
60
- return "new Date()";
61
- case TypeScriptType.unknown:
62
- return `"Unknown Type"`;
63
- case TypeScriptType.object: {
64
- return generateObjectValues(type, parameterName, schemaMap, path);
65
- }
66
- case TypeScriptType.array: {
67
- return generateArrayValues(type, parameterName, schemaMap, path);
68
- }
69
- case TypeScriptType.record: {
70
- return generateRecordValues(type, parameterName, schemaMap, path);
71
- }
72
- case TypeScriptType.enum: {
73
- return mockEnumValues(type, parameterName, schemaMap, path);
74
- }
75
- case TypeScriptType.union: {
76
- return mockUnionValues(type, parameterName, schemaMap, path);
77
- }
78
- case TypeScriptType.constant:
79
- return type;
80
- }
81
- return `undefined /**FIXME */`;
82
- }
83
-
84
- function mockEnumValues(
85
- type: string,
86
- parameterName: string,
87
- schemaMap: Map<string, Schema>,
88
- path: Set<string> = new Set(),
89
- ) {
90
- const schema = schemaMap.get(type);
91
- if (schema && schema.enum && schema.enum.length > 0) {
92
- return schema.enum[0].type;
93
- }
94
- return generateParameterTypeValue(getUnionType(type), parameterName, schemaMap, path);
95
- }
96
-
97
- function mockUnionValues(
98
- type: string,
99
- parameterName: string,
100
- schemaMap: Map<string, Schema>,
101
- path: Set<string> = new Set(),
102
- ) {
103
- const schema = schemaMap.get(type);
104
- if (schema && schema.enum && schema.enum.length > 0) {
105
- addToSchemaMap(schemaMap, schema.enum[0]);
106
- return generateParameterTypeValue(
107
- getAccurateTypeName(schema.enum[0]) ?? schema.enum[0],
108
- parameterName,
109
- schemaMap,
110
- path,
111
- );
112
- }
113
- return generateParameterTypeValue(getUnionType(type), parameterName, schemaMap, path);
114
- }
115
-
116
- function generateRecordValues(
117
- type: string,
118
- parameterName: string,
119
- schemaMap: Map<string, Schema>,
120
- path: Set<string> = new Set(),
121
- ) {
122
- let recordType = getRecordType(type);
123
- const schema = schemaMap.get(type) as DictionarySchema;
124
- if (schema && schema.additionalProperties) {
125
- recordType = getAccurateTypeName(schema.additionalProperties);
126
- addToSchemaMap(schemaMap, schema.additionalProperties);
127
- }
128
-
129
- return recordType
130
- ? `{"key": ${generateParameterTypeValue(recordType, parameterName, schemaMap, path)}}`
131
- : `{}`;
132
- }
133
-
134
- function generateArrayValues(
135
- type: string,
136
- parameterName: string,
137
- schemaMap: Map<string, Schema>,
138
- path: Set<string> = new Set(),
139
- ) {
140
- let arrayType;
141
- const schema = schemaMap.get(type) as ArraySchema;
142
- if (schema && schema.items) {
143
- arrayType = getAccurateTypeName(schema.items);
144
- addToSchemaMap(schemaMap, schema.items);
145
- } else if (isArrayObject(type)) {
146
- arrayType = getArrayObjectType(type);
147
- } else if (isNativeArray(type)) {
148
- arrayType = getNativeArrayType(type);
149
- }
150
- const itemValue = arrayType
151
- ? generateParameterTypeValue(arrayType, parameterName, schemaMap, path)
152
- : undefined;
153
- return itemValue ? `[${itemValue}]` : `[]`;
154
- }
155
-
156
- function generateObjectValues(
157
- type: string,
158
- _parameterName: string,
159
- schemaMap: Map<string, Schema>,
160
- path: Set<string> = new Set(),
161
- ) {
162
- if (path.has(type)) {
163
- // skip generating if self referenced
164
- return `{} as any /**FIXME */`;
165
- }
166
- path.add(type);
167
- // Extract all properties from the schema
168
- const allProperties = getAllProperties(schemaMap.get(type), schemaMap);
169
- const values = extractObjectProperties(allProperties, schemaMap, path);
170
-
171
- path.delete(type);
172
- return `{${values.join(", ")}}`;
173
- }
174
-
175
- function getAllProperties(
176
- schema?: ObjectSchema,
177
- schemaMap: Map<string, Schema> = new Map(),
178
- ): Map<string, Schema> {
179
- const propertiesMap: Map<string, Schema> = new Map();
180
- if (!schema) {
181
- return new Map();
182
- }
183
- getImmediateParentsNames(schema, [SchemaContext.Input])?.forEach((p) => {
184
- const parentProperties = getAllProperties(schemaMap.get(p), schemaMap);
185
- for (const prop of parentProperties.keys()) {
186
- propertiesMap.set(prop, parentProperties.get(prop)!);
187
- }
188
- });
189
- for (const prop in schema.properties) {
190
- const propValue = schema.properties[prop];
191
- if (propValue) {
192
- propertiesMap.set(prop, propValue);
193
- }
194
- }
195
- return propertiesMap;
196
- }
197
-
198
- function extractObjectProperties(
199
- properties: Map<string, Schema>,
200
- schemaMap: Map<string, Schema> = new Map(),
201
- path: Set<string> = new Set(),
202
- ) {
203
- const values: string[] = [];
204
- for (const name of properties.keys()) {
205
- const property = properties.get(name);
206
- if (!property || property.readOnly || property.type === "never") {
207
- continue;
208
- }
209
- addToSchemaMap(schemaMap, property);
210
- values.push(
211
- `${name}: ` +
212
- generateParameterTypeValue(getAccurateTypeName(property), name, schemaMap, path),
213
- );
214
- }
215
- return values;
216
- }
217
-
218
- function getAccurateTypeName(schema: Schema) {
219
- // For extensible enum, fallback to use the type
220
- if (schema.typeName === "string" && schema.enum && schema.enum.length > 0) {
221
- return schema.type;
222
- }
223
- return schema.typeName ?? schema.type;
224
- }
225
-
226
- function addToSchemaMap(schemaMap: Map<string, Schema>, schema: Schema) {
227
- const type = getAccurateTypeName(schema);
228
- if (!type) {
229
- return;
230
- }
231
- if (!schemaMap.has(type) && !["string", "number", "boolean"].includes(schema.type)) {
232
- schemaMap.set(type, schema);
233
- }
234
- }