@azure-tools/typespec-ts 0.33.0 → 0.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/dist/src/framework/declaration.js.map +1 -1
  3. package/dist/src/framework/dependency.d.ts +25 -0
  4. package/dist/src/framework/dependency.d.ts.map +1 -1
  5. package/dist/src/framework/hooks/binder.d.ts +1 -1
  6. package/dist/src/framework/hooks/binder.d.ts.map +1 -1
  7. package/dist/src/framework/hooks/binder.js +16 -12
  8. package/dist/src/framework/hooks/binder.js.map +1 -1
  9. package/dist/src/framework/hooks/sdkTypes.d.ts +4 -2
  10. package/dist/src/framework/hooks/sdkTypes.d.ts.map +1 -1
  11. package/dist/src/framework/hooks/sdkTypes.js +43 -8
  12. package/dist/src/framework/hooks/sdkTypes.js.map +1 -1
  13. package/dist/src/framework/refkey.js +1 -1
  14. package/dist/src/framework/refkey.js.map +1 -1
  15. package/dist/src/framework/sample.js +1 -1
  16. package/dist/src/framework/sample.js.map +1 -1
  17. package/dist/src/index.d.ts +2 -0
  18. package/dist/src/index.d.ts.map +1 -1
  19. package/dist/src/index.js +81 -24
  20. package/dist/src/index.js.map +1 -1
  21. package/dist/src/lib.d.ts +37 -3
  22. package/dist/src/lib.d.ts.map +1 -1
  23. package/dist/src/lib.js +26 -1
  24. package/dist/src/lib.js.map +1 -1
  25. package/dist/src/modular/buildClassicalClient.d.ts +2 -2
  26. package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
  27. package/dist/src/modular/buildClassicalClient.js +25 -61
  28. package/dist/src/modular/buildClassicalClient.js.map +1 -1
  29. package/dist/src/modular/buildClassicalOperationGroups.d.ts.map +1 -1
  30. package/dist/src/modular/buildClassicalOperationGroups.js +0 -7
  31. package/dist/src/modular/buildClassicalOperationGroups.js.map +1 -1
  32. package/dist/src/modular/buildClientContext.d.ts +7 -3
  33. package/dist/src/modular/buildClientContext.d.ts.map +1 -1
  34. package/dist/src/modular/buildClientContext.js +58 -38
  35. package/dist/src/modular/buildClientContext.js.map +1 -1
  36. package/dist/src/modular/buildCodeModel.d.ts +18 -0
  37. package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
  38. package/dist/src/modular/buildCodeModel.js +95 -42
  39. package/dist/src/modular/buildCodeModel.js.map +1 -1
  40. package/dist/src/modular/buildOperations.d.ts +3 -4
  41. package/dist/src/modular/buildOperations.d.ts.map +1 -1
  42. package/dist/src/modular/buildOperations.js +24 -45
  43. package/dist/src/modular/buildOperations.js.map +1 -1
  44. package/dist/src/modular/buildProjectFiles.d.ts.map +1 -1
  45. package/dist/src/modular/buildProjectFiles.js +3 -1
  46. package/dist/src/modular/buildProjectFiles.js.map +1 -1
  47. package/dist/src/modular/buildRestorePoller.js +1 -1
  48. package/dist/src/modular/buildRestorePoller.js.map +1 -1
  49. package/dist/src/modular/buildRootIndex.d.ts +5 -0
  50. package/dist/src/modular/buildRootIndex.d.ts.map +1 -1
  51. package/dist/src/modular/buildRootIndex.js +62 -17
  52. package/dist/src/modular/buildRootIndex.js.map +1 -1
  53. package/dist/src/modular/buildSubpathIndex.d.ts +1 -1
  54. package/dist/src/modular/buildSubpathIndex.d.ts.map +1 -1
  55. package/dist/src/modular/buildSubpathIndex.js +11 -10
  56. package/dist/src/modular/buildSubpathIndex.js.map +1 -1
  57. package/dist/src/modular/emitModels.d.ts +13 -28
  58. package/dist/src/modular/emitModels.d.ts.map +1 -1
  59. package/dist/src/modular/emitModels.js +435 -240
  60. package/dist/src/modular/emitModels.js.map +1 -1
  61. package/dist/src/modular/emitModelsOptions.d.ts +26 -0
  62. package/dist/src/modular/emitModelsOptions.d.ts.map +1 -0
  63. package/dist/src/modular/emitModelsOptions.js +106 -0
  64. package/dist/src/modular/emitModelsOptions.js.map +1 -0
  65. package/dist/src/modular/emitSamples.d.ts +7 -0
  66. package/dist/src/modular/emitSamples.d.ts.map +1 -0
  67. package/dist/src/modular/emitSamples.js +354 -0
  68. package/dist/src/modular/emitSamples.js.map +1 -0
  69. package/dist/src/modular/external-dependencies.d.ts +7 -0
  70. package/dist/src/modular/external-dependencies.d.ts.map +1 -1
  71. package/dist/src/modular/external-dependencies.js +58 -1
  72. package/dist/src/modular/external-dependencies.js.map +1 -1
  73. package/dist/src/modular/helpers/classicalOperationHelpers.d.ts.map +1 -1
  74. package/dist/src/modular/helpers/classicalOperationHelpers.js +2 -2
  75. package/dist/src/modular/helpers/classicalOperationHelpers.js.map +1 -1
  76. package/dist/src/modular/helpers/clientHelpers.d.ts +13 -6
  77. package/dist/src/modular/helpers/clientHelpers.d.ts.map +1 -1
  78. package/dist/src/modular/helpers/clientHelpers.js +117 -48
  79. package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
  80. package/dist/src/modular/helpers/namingHelpers.d.ts +3 -2
  81. package/dist/src/modular/helpers/namingHelpers.d.ts.map +1 -1
  82. package/dist/src/modular/helpers/namingHelpers.js +3 -0
  83. package/dist/src/modular/helpers/namingHelpers.js.map +1 -1
  84. package/dist/src/modular/helpers/operationHelpers.d.ts +9 -11
  85. package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
  86. package/dist/src/modular/helpers/operationHelpers.js +255 -408
  87. package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
  88. package/dist/src/modular/helpers/typeHelpers.d.ts +12 -0
  89. package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
  90. package/dist/src/modular/helpers/typeHelpers.js +30 -6
  91. package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
  92. package/dist/src/modular/modularCodeModel.d.ts +2 -1
  93. package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
  94. package/dist/src/modular/serialization/buildDeserializerFunction.d.ts +5 -0
  95. package/dist/src/modular/serialization/buildDeserializerFunction.d.ts.map +1 -0
  96. package/dist/src/modular/serialization/buildDeserializerFunction.js +327 -0
  97. package/dist/src/modular/serialization/buildDeserializerFunction.js.map +1 -0
  98. package/dist/src/modular/serialization/buildSerializerFunction.d.ts +4 -3
  99. package/dist/src/modular/serialization/buildSerializerFunction.d.ts.map +1 -1
  100. package/dist/src/modular/serialization/buildSerializerFunction.js +285 -102
  101. package/dist/src/modular/serialization/buildSerializerFunction.js.map +1 -1
  102. package/dist/src/modular/serialization/serializeUtils.d.ts +15 -0
  103. package/dist/src/modular/serialization/serializeUtils.d.ts.map +1 -0
  104. package/dist/src/modular/serialization/serializeUtils.js +102 -0
  105. package/dist/src/modular/serialization/serializeUtils.js.map +1 -0
  106. package/dist/src/modular/type-expressions/get-credential-expression.d.ts +3 -0
  107. package/dist/src/modular/type-expressions/get-credential-expression.d.ts.map +1 -0
  108. package/dist/src/modular/type-expressions/get-credential-expression.js +17 -0
  109. package/dist/src/modular/type-expressions/get-credential-expression.js.map +1 -0
  110. package/dist/src/modular/type-expressions/get-enum-expression.d.ts +6 -0
  111. package/dist/src/modular/type-expressions/get-enum-expression.d.ts.map +1 -0
  112. package/dist/src/modular/type-expressions/get-enum-expression.js +18 -0
  113. package/dist/src/modular/type-expressions/get-enum-expression.js.map +1 -0
  114. package/dist/src/modular/type-expressions/get-model-expression.d.ts +9 -0
  115. package/dist/src/modular/type-expressions/get-model-expression.d.ts.map +1 -0
  116. package/dist/src/modular/type-expressions/get-model-expression.js +42 -0
  117. package/dist/src/modular/type-expressions/get-model-expression.js.map +1 -0
  118. package/dist/src/modular/type-expressions/get-type-expression.d.ts +8 -0
  119. package/dist/src/modular/type-expressions/get-type-expression.d.ts.map +1 -0
  120. package/dist/src/modular/type-expressions/get-type-expression.js +94 -0
  121. package/dist/src/modular/type-expressions/get-type-expression.js.map +1 -0
  122. package/dist/src/modular/type-expressions/get-union-expression.d.ts +5 -0
  123. package/dist/src/modular/type-expressions/get-union-expression.d.ts.map +1 -0
  124. package/dist/src/modular/type-expressions/get-union-expression.js +13 -0
  125. package/dist/src/modular/type-expressions/get-union-expression.js.map +1 -0
  126. package/dist/src/modular/type-expressions/utils.d.ts +5 -0
  127. package/dist/src/modular/type-expressions/utils.d.ts.map +1 -0
  128. package/dist/src/modular/type-expressions/utils.js +9 -0
  129. package/dist/src/modular/type-expressions/utils.js.map +1 -0
  130. package/dist/src/transform/transform.js +1 -1
  131. package/dist/src/transform/transform.js.map +1 -1
  132. package/dist/src/transform/transformApiVersionInfo.js +1 -1
  133. package/dist/src/transform/transformApiVersionInfo.js.map +1 -1
  134. package/dist/src/transform/transformParameters.js +1 -1
  135. package/dist/src/transform/transformParameters.js.map +1 -1
  136. package/dist/src/transform/transformSchemas.d.ts.map +1 -1
  137. package/dist/src/transform/transformSchemas.js +3 -3
  138. package/dist/src/transform/transformSchemas.js.map +1 -1
  139. package/dist/src/utils/clientUtils.d.ts.map +1 -1
  140. package/dist/src/utils/clientUtils.js +1 -4
  141. package/dist/src/utils/clientUtils.js.map +1 -1
  142. package/dist/src/utils/credentialUtils.d.ts +5 -0
  143. package/dist/src/utils/credentialUtils.d.ts.map +1 -1
  144. package/dist/src/utils/credentialUtils.js +36 -4
  145. package/dist/src/utils/credentialUtils.js.map +1 -1
  146. package/dist/src/utils/modelUtils.d.ts.map +1 -1
  147. package/dist/src/utils/modelUtils.js +28 -43
  148. package/dist/src/utils/modelUtils.js.map +1 -1
  149. package/dist/tsconfig.tsbuildinfo +1 -1
  150. package/package.json +26 -26
  151. package/src/framework/declaration.ts +0 -1
  152. package/src/framework/dependency.ts +25 -0
  153. package/src/framework/hooks/binder.ts +19 -14
  154. package/src/framework/hooks/sdkTypes.ts +53 -13
  155. package/src/framework/refkey.ts +1 -1
  156. package/src/framework/sample.ts +1 -1
  157. package/src/index.ts +96 -25
  158. package/src/lib.ts +26 -3
  159. package/src/modular/buildClassicalClient.ts +30 -97
  160. package/src/modular/buildClassicalOperationGroups.ts +0 -14
  161. package/src/modular/buildClientContext.ts +90 -50
  162. package/src/modular/buildCodeModel.ts +118 -47
  163. package/src/modular/buildOperations.ts +41 -76
  164. package/src/modular/buildProjectFiles.ts +4 -2
  165. package/src/modular/buildRestorePoller.ts +1 -1
  166. package/src/modular/buildRootIndex.ts +75 -35
  167. package/src/modular/buildSubpathIndex.ts +13 -11
  168. package/src/modular/emitModels.ts +588 -326
  169. package/src/modular/emitModelsOptions.ts +161 -0
  170. package/src/modular/emitSamples.ts +481 -0
  171. package/src/modular/external-dependencies.ts +59 -1
  172. package/src/modular/helpers/classicalOperationHelpers.ts +6 -2
  173. package/src/modular/helpers/clientHelpers.ts +182 -76
  174. package/src/modular/helpers/namingHelpers.ts +14 -2
  175. package/src/modular/helpers/operationHelpers.ts +356 -638
  176. package/src/modular/helpers/typeHelpers.ts +38 -6
  177. package/src/modular/modularCodeModel.ts +3 -0
  178. package/src/modular/serialization/buildDeserializerFunction.ts +492 -0
  179. package/src/modular/serialization/buildSerializerFunction.ts +434 -141
  180. package/src/modular/serialization/serializeUtils.ts +143 -0
  181. package/src/modular/type-expressions/get-credential-expression.ts +18 -0
  182. package/src/modular/type-expressions/get-enum-expression.ts +28 -0
  183. package/src/modular/type-expressions/get-model-expression.ts +63 -0
  184. package/src/modular/type-expressions/get-type-expression.ts +111 -0
  185. package/src/modular/type-expressions/get-union-expression.ts +20 -0
  186. package/src/modular/type-expressions/utils.ts +15 -0
  187. package/src/transform/transform.ts +2 -2
  188. package/src/transform/transformApiVersionInfo.ts +2 -2
  189. package/src/transform/transformParameters.ts +1 -1
  190. package/src/transform/transformSchemas.ts +9 -8
  191. package/src/utils/clientUtils.ts +2 -12
  192. package/src/utils/credentialUtils.ts +43 -3
  193. package/src/utils/modelUtils.ts +32 -41
  194. package/static/static-helpers/serialization/serializers.ts +60 -0
  195. package/dist/src/modular/buildSerializeUtils.d.ts +0 -14
  196. package/dist/src/modular/buildSerializeUtils.d.ts.map +0 -1
  197. package/dist/src/modular/buildSerializeUtils.js +0 -534
  198. package/dist/src/modular/buildSerializeUtils.js.map +0 -1
  199. package/src/modular/buildSerializeUtils.ts +0 -749
@@ -1,3 +1,7 @@
1
+ import {
2
+ SdkBodyParameter,
3
+ SdkType
4
+ } from "@azure-tools/typespec-client-generator-core";
1
5
  import { Type } from "../modularCodeModel.js";
2
6
 
3
7
  /**
@@ -14,14 +18,10 @@ export interface TypeMetadata {
14
18
  // Mapping of simple types to their TypeScript equivalents.
15
19
  const simpleTypeMap: Record<string, TypeMetadata> = {
16
20
  Key: {
17
- name: "KeyCredential",
18
- originModule: "@azure/core-auth",
19
- isRelative: false
21
+ name: "KeyCredential"
20
22
  },
21
23
  OAuth2: {
22
- name: "TokenCredential",
23
- originModule: "@azure/core-auth",
24
- isRelative: false
24
+ name: "TokenCredential"
25
25
  },
26
26
  boolean: { name: "boolean" },
27
27
  datetime: { name: "Date" },
@@ -306,3 +306,35 @@ export function isCredentialType(type: Type): boolean {
306
306
  (type.type === "combined" && (type.types?.every(isCredentialType) ?? false))
307
307
  );
308
308
  }
309
+
310
+ /**
311
+ * Builds a property name mapper between the serializedName and the name of the property.
312
+ * Return empty map if the type is not a model.
313
+ */
314
+ export function buildPropertyNameMapper(model: SdkType) {
315
+ const mapper = new Map<string, string>();
316
+ if (model.kind !== "model") {
317
+ return mapper;
318
+ }
319
+ for (const prop of model.properties) {
320
+ if (prop.kind !== "property") {
321
+ continue;
322
+ }
323
+
324
+ mapper.set(prop.serializedName, prop.name);
325
+ }
326
+ return mapper;
327
+ }
328
+
329
+ /**
330
+ * Checks if the body parameter is a spread parameter.
331
+ * @param body
332
+ * @returns
333
+ */
334
+ export function isSpreadBodyParameter(body: SdkBodyParameter) {
335
+ const methodParams = body.correspondingMethodParams;
336
+ return (
337
+ methodParams.length > 1 ||
338
+ (methodParams.length === 1 && methodParams[0]?.type !== body.type)
339
+ );
340
+ }
@@ -6,6 +6,8 @@ import {
6
6
  } from "@azure-tools/rlc-common";
7
7
  import {
8
8
  SdkBodyParameter,
9
+ SdkClientType,
10
+ SdkHttpOperation,
9
11
  SdkType
10
12
  } from "@azure-tools/typespec-client-generator-core";
11
13
  import { UsageFlags, Type as TypespecType } from "@typespec/compiler";
@@ -134,6 +136,7 @@ export interface Client {
134
136
  rlcClientName: string;
135
137
  subfolder?: string;
136
138
  rlcHelperDetails: HelperFunctionDetails;
139
+ tcgcClient: SdkClientType<SdkHttpOperation>;
137
140
  }
138
141
 
139
142
  export type ParameterLocation =
@@ -0,0 +1,492 @@
1
+ import { FunctionDeclarationStructure, StructureKind } from "ts-morph";
2
+ import {
3
+ SdkArrayType,
4
+ SdkDictionaryType,
5
+ SdkModelType,
6
+ SdkType,
7
+ SdkUnionType,
8
+ UsageFlags
9
+ } from "@azure-tools/typespec-client-generator-core";
10
+ import { toCamelCase, toPascalCase } from "../../utils/casingUtils.js";
11
+
12
+ import { SdkContext } from "../../utils/interfaces.js";
13
+ import { getResponseMapping } from "../helpers/operationHelpers.js";
14
+ import { getType } from "../buildCodeModel.js";
15
+ import { normalizeModelName } from "../emitModels.js";
16
+ import { NameType } from "@azure-tools/rlc-common";
17
+ import { isAzureCoreErrorType } from "../../utils/modelUtils.js";
18
+ import {
19
+ isDiscriminatedUnion,
20
+ isSupportedSerializeType,
21
+ ModelSerializeOptions
22
+ } from "./serializeUtils.js";
23
+
24
+ export function buildModelDeserializer(
25
+ context: SdkContext,
26
+ type: SdkType,
27
+ skipDiscriminatedUnion = false,
28
+ nameOnly: boolean = false
29
+ ): FunctionDeclarationStructure | undefined | string {
30
+ // const modelTcgcType = getTcgcType(type) as SdkModelType;
31
+ if (!isSupportedSerializeType(type)) {
32
+ return undefined;
33
+ }
34
+ if (type.kind === "model" || type.kind === "union" || type.kind === "enum") {
35
+ if (
36
+ !type.usage ||
37
+ (type.usage !== undefined &&
38
+ (type.usage & UsageFlags.Output) !== UsageFlags.Output)
39
+ ) {
40
+ return undefined;
41
+ }
42
+ if (!type.name) {
43
+ // TODO: https://github.com/Azure/typespec-azure/issues/1713 and https://github.com/microsoft/typespec/issues/4815
44
+ // throw new Error(`NYI Serialization of anonymous types`);
45
+ return undefined;
46
+ }
47
+ if (isAzureCoreErrorType(context.program, type.__raw!)) {
48
+ return undefined;
49
+ }
50
+ }
51
+
52
+ if (
53
+ !isDiscriminatedUnion(type) &&
54
+ type.kind === "model" &&
55
+ type.discriminatorProperty
56
+ ) {
57
+ return buildPolymorphicDeserializer(context, type, nameOnly);
58
+ }
59
+
60
+ if (isDiscriminatedUnion(type) && !skipDiscriminatedUnion) {
61
+ return buildDiscriminatedUnionDeserializer(context, type, nameOnly);
62
+ }
63
+
64
+ switch (type.kind) {
65
+ case "model":
66
+ return buildModelTypeDeserializer(context, type, {
67
+ nameOnly,
68
+ skipDiscriminatedUnionSuffix: skipDiscriminatedUnion
69
+ });
70
+ case "union": // for non-discriminated union, we just return whatever we get
71
+ return buildUnionDeserializer(context, type, nameOnly);
72
+ case "dict":
73
+ return buildDictTypeDeserializer(context, type, nameOnly);
74
+ case "array":
75
+ return buildArrayTypeDeserializer(context, type, nameOnly);
76
+ default:
77
+ return undefined;
78
+ }
79
+ }
80
+
81
+ function hasAdditionalProperties(type: SdkType | undefined) {
82
+ if (
83
+ !type ||
84
+ !(
85
+ "additionalProperties" in type ||
86
+ (type.kind === "model" && hasAdditionalProperties(type.baseModel))
87
+ )
88
+ ) {
89
+ return false;
90
+ }
91
+
92
+ if (type.additionalProperties) {
93
+ return true;
94
+ }
95
+
96
+ if (type.baseModel) {
97
+ return hasAdditionalProperties(type.baseModel);
98
+ }
99
+
100
+ return false;
101
+ }
102
+
103
+ function buildPolymorphicDeserializer(
104
+ context: SdkContext,
105
+ type: SdkModelType,
106
+ nameOnly: boolean
107
+ ): string;
108
+ function buildPolymorphicDeserializer(
109
+ context: SdkContext,
110
+ type: SdkModelType
111
+ ): FunctionDeclarationStructure | undefined;
112
+ function buildPolymorphicDeserializer(
113
+ context: SdkContext,
114
+ type: SdkModelType,
115
+ nameOnly = false
116
+ ): FunctionDeclarationStructure | undefined | string {
117
+ if (!type.name) {
118
+ throw new Error(`NYI Serialization of anonymous types`);
119
+ }
120
+ const deserializeFunctionName = `${normalizeModelName(
121
+ context,
122
+ type,
123
+ NameType.Operation
124
+ )}Deserializer`;
125
+ if (nameOnly) {
126
+ return deserializeFunctionName;
127
+ }
128
+ const deserializerFunction: FunctionDeclarationStructure = {
129
+ kind: StructureKind.Function,
130
+ name: deserializeFunctionName,
131
+ isExported: true,
132
+ parameters: [
133
+ {
134
+ name: "item",
135
+ type: "any"
136
+ }
137
+ ],
138
+ returnType: normalizeModelName(context, type),
139
+ statements: []
140
+ };
141
+ if (!type.discriminatorProperty) {
142
+ return;
143
+ }
144
+ const statements: string[] = [];
145
+
146
+ const subTypes = type.discriminatedSubtypes;
147
+ if (!subTypes) {
148
+ return;
149
+ }
150
+
151
+ const cases: string[] = [];
152
+ Object.keys(subTypes).forEach((discriminatedValue) => {
153
+ const subType = subTypes[discriminatedValue];
154
+ if (
155
+ !subType?.usage ||
156
+ (subType?.usage !== undefined &&
157
+ (subType.usage & UsageFlags.Output) !== UsageFlags.Output)
158
+ ) {
159
+ return;
160
+ }
161
+ const union = subType?.discriminatedSubtypes ? "Union" : "";
162
+ if (!subType || subType?.name) {
163
+ throw new Error(`NYI Serialization of anonymous types`);
164
+ }
165
+ const subTypeName = `${toPascalCase(subType.name)}${union}`;
166
+ const subtypeDeserializerName = toCamelCase(`${subTypeName}Deserializer`);
167
+
168
+ cases.push(`
169
+ case "${discriminatedValue}":
170
+ return ${subtypeDeserializerName}(item as ${subTypeName});
171
+ `);
172
+ });
173
+
174
+ statements.push(`
175
+ switch (item.${type.discriminatorProperty.name}) {
176
+ ${cases.join("\n")}
177
+ default:
178
+ return item;
179
+ }
180
+ `);
181
+ deserializerFunction.statements = statements.join("\n");
182
+ return deserializerFunction;
183
+ }
184
+
185
+ function buildDiscriminatedUnionDeserializer(
186
+ context: SdkContext,
187
+ type: SdkModelType,
188
+ nameOnly: boolean
189
+ ): string;
190
+ function buildDiscriminatedUnionDeserializer(
191
+ context: SdkContext,
192
+ type: SdkModelType
193
+ ): FunctionDeclarationStructure | undefined;
194
+ function buildDiscriminatedUnionDeserializer(
195
+ context: SdkContext,
196
+ type: SdkModelType,
197
+ nameOnly = false
198
+ ): FunctionDeclarationStructure | undefined | string {
199
+ if (!type.name) {
200
+ throw new Error(`NYI Serialization of anonymous types`);
201
+ }
202
+ const cases: string[] = [];
203
+ const output: string[] = [];
204
+ const deserializeFunctionName = `${normalizeModelName(
205
+ context,
206
+ type,
207
+ NameType.Operation
208
+ )}Deserializer`;
209
+ if (nameOnly) {
210
+ return deserializeFunctionName;
211
+ }
212
+ const baseDeserializerName = `${normalizeModelName(
213
+ context,
214
+ type,
215
+ NameType.Operation,
216
+ true
217
+ )}Deserializer`;
218
+ for (const key in type.discriminatedSubtypes) {
219
+ const subType = type.discriminatedSubtypes[key]!;
220
+ if (
221
+ !subType.usage ||
222
+ (subType.usage !== undefined &&
223
+ (subType.usage & UsageFlags.Output) !== UsageFlags.Output)
224
+ ) {
225
+ continue;
226
+ }
227
+ const discriminatedValue = subType.discriminatorValue!;
228
+ const union = subType.discriminatedSubtypes ? "Union" : "";
229
+ const subTypeName = `${toPascalCase(subType.name)}${union}`;
230
+ const subtypeDeserializerName = toCamelCase(`${subTypeName}Deserializer`);
231
+
232
+ cases.push(`
233
+ case "${discriminatedValue}":
234
+ return ${subtypeDeserializerName}(item as ${subTypeName});
235
+ `);
236
+ }
237
+ output.push(`
238
+ switch (item.${type.discriminatorProperty?.name}) {
239
+ ${cases.join("\n")}
240
+ default:
241
+ return ${baseDeserializerName}(item);
242
+ }
243
+ `);
244
+
245
+ const deserializerFunction: FunctionDeclarationStructure = {
246
+ kind: StructureKind.Function,
247
+ name: deserializeFunctionName,
248
+ isExported: true,
249
+ parameters: [
250
+ {
251
+ name: "item",
252
+ type: "any"
253
+ }
254
+ ],
255
+ returnType: normalizeModelName(context, type),
256
+ statements: output.join("\n")
257
+ };
258
+ return deserializerFunction;
259
+ }
260
+
261
+ function buildUnionDeserializer(
262
+ context: SdkContext,
263
+ type: SdkUnionType,
264
+ nameOnly: boolean
265
+ ): string;
266
+ function buildUnionDeserializer(
267
+ context: SdkContext,
268
+ type: SdkUnionType
269
+ ): FunctionDeclarationStructure;
270
+ function buildUnionDeserializer(
271
+ context: SdkContext,
272
+ type: SdkUnionType,
273
+ nameOnly = false
274
+ ): FunctionDeclarationStructure | string | undefined {
275
+ if (!type.name) {
276
+ throw new Error(`NYI Serialization of anonymous types`);
277
+ }
278
+ const deserializerFunctionName = `${normalizeModelName(
279
+ context,
280
+ type,
281
+ NameType.Operation
282
+ )}Deserializer`;
283
+ if (nameOnly) {
284
+ return deserializerFunctionName;
285
+ }
286
+ const deserializerFunction: FunctionDeclarationStructure = {
287
+ kind: StructureKind.Function,
288
+ name: deserializerFunctionName,
289
+ isExported: true,
290
+ parameters: [
291
+ {
292
+ name: "item",
293
+ type: "any"
294
+ }
295
+ ],
296
+ returnType: normalizeModelName(context, type),
297
+ statements: ["return item;"]
298
+ };
299
+ return deserializerFunction;
300
+ }
301
+
302
+ function buildModelTypeDeserializer(
303
+ context: SdkContext,
304
+ type: SdkModelType,
305
+ options: ModelSerializeOptions = {
306
+ nameOnly: false,
307
+ skipDiscriminatedUnionSuffix: false
308
+ }
309
+ ): FunctionDeclarationStructure | string | undefined {
310
+ if (!type.name) {
311
+ throw new Error(`NYI Deserialization of anonymous types`);
312
+ }
313
+ const deserializerFunctionName = `${normalizeModelName(
314
+ context,
315
+ type,
316
+ NameType.Operation,
317
+ options.skipDiscriminatedUnionSuffix
318
+ )}Deserializer`;
319
+ if (options.nameOnly) {
320
+ return deserializerFunctionName;
321
+ }
322
+ const deserializerFunction: FunctionDeclarationStructure = {
323
+ kind: StructureKind.Function,
324
+ name: deserializerFunctionName,
325
+ isExported: true,
326
+ parameters: [
327
+ {
328
+ name: "item",
329
+ type: "any"
330
+ }
331
+ ],
332
+ returnType: normalizeModelName(
333
+ context,
334
+ type,
335
+ NameType.Interface,
336
+ options.skipDiscriminatedUnionSuffix
337
+ ),
338
+ statements: ["return item;"]
339
+ };
340
+ const nullabilityPrefix = "";
341
+
342
+ // This is only handling the compatibility mode, will need to update when we handle additionalProperties property.
343
+ const additionalPropertiesSpread = hasAdditionalProperties(type)
344
+ ? "...item,"
345
+ : "";
346
+
347
+ const propertiesStr = getResponseMapping(
348
+ context,
349
+ getType(context, type.__raw!),
350
+ "item"
351
+ );
352
+ const propertiesDeserialization = propertiesStr.filter((p) => p.trim());
353
+
354
+ const output = [];
355
+
356
+ // don't emit a serializer if there is nothing to serialize
357
+ if (propertiesDeserialization.length || additionalPropertiesSpread) {
358
+ const fnBody = `{
359
+ ${additionalPropertiesSpread}
360
+ ${nullabilityPrefix} ${propertiesDeserialization.join(",\n")}
361
+ }`;
362
+ output.push(`
363
+ return ${fnBody}
364
+ `);
365
+ } else {
366
+ output.push(`
367
+ return item;
368
+ `);
369
+ }
370
+ deserializerFunction.statements = output;
371
+ return deserializerFunction;
372
+ }
373
+
374
+ function buildDictTypeDeserializer(
375
+ context: SdkContext,
376
+ type: SdkDictionaryType,
377
+ nameOnly: boolean
378
+ ): string;
379
+ function buildDictTypeDeserializer(
380
+ context: SdkContext,
381
+ type: SdkDictionaryType
382
+ ): FunctionDeclarationStructure | undefined;
383
+ function buildDictTypeDeserializer(
384
+ context: SdkContext,
385
+ type: SdkDictionaryType,
386
+ nameOnly = false
387
+ ): FunctionDeclarationStructure | undefined | string {
388
+ const valueDeserializer = buildModelDeserializer(
389
+ context,
390
+ type.valueType,
391
+ false,
392
+ true
393
+ );
394
+ if (!valueDeserializer) {
395
+ return undefined;
396
+ }
397
+ if (!isSupportedSerializeType(type.valueType)) {
398
+ return undefined;
399
+ }
400
+
401
+ if (typeof valueDeserializer !== "string") {
402
+ return undefined;
403
+ }
404
+ const valueTypeName = toCamelCase(
405
+ valueDeserializer ? valueDeserializer.replace("Deserializer", "") : ""
406
+ );
407
+ const deserializerFunctionName = `${valueTypeName}RecordDeserializer`;
408
+ if (nameOnly) {
409
+ return deserializerFunctionName;
410
+ }
411
+ const deserializerFunction: FunctionDeclarationStructure = {
412
+ kind: StructureKind.Function,
413
+ name: `${valueTypeName}RecordDeserializer`,
414
+ isExported: true,
415
+ parameters: [
416
+ {
417
+ name: "item",
418
+ type: "Record<string, any>"
419
+ }
420
+ ],
421
+ returnType: `Record<string, ${normalizeModelName(context, type.valueType as any) ?? "any"}>`,
422
+ statements: [
423
+ `
424
+ const result: Record<string, any> = {};
425
+ Object.keys(item).map((key) => {
426
+ result[key] = !item[key]? item[key]: ${valueDeserializer}(item[key])
427
+ });
428
+ return result;
429
+ `
430
+ ]
431
+ };
432
+ return deserializerFunction;
433
+ }
434
+
435
+ function buildArrayTypeDeserializer(
436
+ context: SdkContext,
437
+ type: SdkArrayType,
438
+ nameOnly: boolean
439
+ ): string;
440
+ function buildArrayTypeDeserializer(
441
+ context: SdkContext,
442
+ type: SdkArrayType
443
+ ): FunctionDeclarationStructure | undefined;
444
+ function buildArrayTypeDeserializer(
445
+ context: SdkContext,
446
+ type: SdkArrayType,
447
+ nameOnly = false
448
+ ): FunctionDeclarationStructure | undefined | string {
449
+ const valueDeserializer = buildModelDeserializer(
450
+ context,
451
+ type.valueType,
452
+ false,
453
+ true
454
+ );
455
+ if (!valueDeserializer) {
456
+ return undefined;
457
+ }
458
+ if (!isSupportedSerializeType(type.valueType)) {
459
+ return undefined;
460
+ }
461
+
462
+ if (typeof valueDeserializer !== "string") {
463
+ return undefined;
464
+ }
465
+ const valueTypeName = toCamelCase(
466
+ valueDeserializer ? valueDeserializer.replace("Deserializer", "") : ""
467
+ );
468
+ const deserializerFunctionName = `${valueTypeName}ArrayDeserializer`;
469
+ if (nameOnly) {
470
+ return deserializerFunctionName;
471
+ }
472
+ const serializerFunction: FunctionDeclarationStructure = {
473
+ kind: StructureKind.Function,
474
+ name: deserializerFunctionName,
475
+ isExported: true,
476
+ parameters: [
477
+ {
478
+ name: "result",
479
+ type: `Array<${normalizeModelName(context, type.valueType as any) ?? "any"}>`
480
+ }
481
+ ],
482
+ returnType: "any[]",
483
+ statements: [
484
+ `
485
+ return result.map((item) => {
486
+ return ${valueDeserializer}(item)
487
+ });
488
+ `
489
+ ]
490
+ };
491
+ return serializerFunction;
492
+ }