@azure-tools/typespec-ts 0.30.1 → 0.31.0-alpha.20240723.1

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 (184) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/src/contextManager.d.ts +4 -0
  3. package/dist/src/contextManager.d.ts.map +1 -1
  4. package/dist/src/contextManager.js.map +1 -1
  5. package/dist/src/framework/declaration.d.ts +9 -0
  6. package/dist/src/framework/declaration.d.ts.map +1 -0
  7. package/dist/src/framework/declaration.js +28 -0
  8. package/dist/src/framework/declaration.js.map +1 -0
  9. package/dist/src/framework/hooks/binder.d.ts +25 -0
  10. package/dist/src/framework/hooks/binder.d.ts.map +1 -0
  11. package/dist/src/framework/hooks/binder.js +168 -0
  12. package/dist/src/framework/hooks/binder.js.map +1 -0
  13. package/dist/src/framework/hooks/sdkTypes.d.ts +12 -0
  14. package/dist/src/framework/hooks/sdkTypes.d.ts.map +1 -0
  15. package/dist/src/framework/hooks/sdkTypes.js +67 -0
  16. package/dist/src/framework/hooks/sdkTypes.js.map +1 -0
  17. package/dist/src/framework/reference.d.ts +2 -0
  18. package/dist/src/framework/reference.d.ts.map +1 -0
  19. package/dist/src/framework/reference.js +8 -0
  20. package/dist/src/framework/reference.js.map +1 -0
  21. package/dist/src/framework/refkey.d.ts +7 -0
  22. package/dist/src/framework/refkey.d.ts.map +1 -0
  23. package/dist/src/framework/refkey.js +22 -0
  24. package/dist/src/framework/refkey.js.map +1 -0
  25. package/dist/src/framework/sample.d.ts +2 -0
  26. package/dist/src/framework/sample.d.ts.map +1 -0
  27. package/dist/src/framework/sample.js +71 -0
  28. package/dist/src/framework/sample.js.map +1 -0
  29. package/dist/src/framework/utils/tsmorph-helpers.d.ts +2 -0
  30. package/dist/src/framework/utils/tsmorph-helpers.d.ts.map +1 -0
  31. package/dist/src/framework/utils/tsmorph-helpers.js +2 -0
  32. package/dist/src/framework/utils/tsmorph-helpers.js.map +1 -0
  33. package/dist/src/index.d.ts.map +1 -1
  34. package/dist/src/index.js +7 -8
  35. package/dist/src/index.js.map +1 -1
  36. package/dist/src/lib.d.ts +10 -1
  37. package/dist/src/lib.d.ts.map +1 -1
  38. package/dist/src/lib.js +6 -0
  39. package/dist/src/lib.js.map +1 -1
  40. package/dist/src/modular/buildClassicalClient.d.ts.map +1 -1
  41. package/dist/src/modular/buildClassicalClient.js +6 -5
  42. package/dist/src/modular/buildClassicalClient.js.map +1 -1
  43. package/dist/src/modular/buildClientContext.d.ts.map +1 -1
  44. package/dist/src/modular/buildClientContext.js +8 -4
  45. package/dist/src/modular/buildClientContext.js.map +1 -1
  46. package/dist/src/modular/buildCodeModel.d.ts.map +1 -1
  47. package/dist/src/modular/buildCodeModel.js +22 -11
  48. package/dist/src/modular/buildCodeModel.js.map +1 -1
  49. package/dist/src/modular/buildOperations.d.ts +1 -2
  50. package/dist/src/modular/buildOperations.d.ts.map +1 -1
  51. package/dist/src/modular/buildOperations.js +3 -9
  52. package/dist/src/modular/buildOperations.js.map +1 -1
  53. package/dist/src/modular/buildRootIndex.js +1 -1
  54. package/dist/src/modular/buildRootIndex.js.map +1 -1
  55. package/dist/src/modular/buildSerializeUtils.d.ts.map +1 -1
  56. package/dist/src/modular/buildSerializeUtils.js +2 -1
  57. package/dist/src/modular/buildSerializeUtils.js.map +1 -1
  58. package/dist/src/modular/helpers/clientHelpers.d.ts +4 -0
  59. package/dist/src/modular/helpers/clientHelpers.d.ts.map +1 -1
  60. package/dist/src/modular/helpers/clientHelpers.js +15 -1
  61. package/dist/src/modular/helpers/clientHelpers.js.map +1 -1
  62. package/dist/src/modular/helpers/namingHelpers.d.ts +1 -0
  63. package/dist/src/modular/helpers/namingHelpers.d.ts.map +1 -1
  64. package/dist/src/modular/helpers/namingHelpers.js +3 -1
  65. package/dist/src/modular/helpers/namingHelpers.js.map +1 -1
  66. package/dist/src/modular/helpers/operationHelpers.d.ts.map +1 -1
  67. package/dist/src/modular/helpers/operationHelpers.js +60 -40
  68. package/dist/src/modular/helpers/operationHelpers.js.map +1 -1
  69. package/dist/src/modular/helpers/typeHelpers.d.ts.map +1 -1
  70. package/dist/src/modular/helpers/typeHelpers.js +6 -1
  71. package/dist/src/modular/helpers/typeHelpers.js.map +1 -1
  72. package/dist/src/modular/modularCodeModel.d.ts +2 -0
  73. package/dist/src/modular/modularCodeModel.d.ts.map +1 -1
  74. package/dist/src/transform/transformApiVersionInfo.d.ts.map +1 -1
  75. package/dist/src/transform/transformApiVersionInfo.js +3 -5
  76. package/dist/src/transform/transformApiVersionInfo.js.map +1 -1
  77. package/dist/src/transform/transformHelperFunctionDetails.d.ts.map +1 -1
  78. package/dist/src/transform/transformHelperFunctionDetails.js +7 -10
  79. package/dist/src/transform/transformHelperFunctionDetails.js.map +1 -1
  80. package/dist/src/transform/transformParameters.d.ts.map +1 -1
  81. package/dist/src/transform/transformParameters.js +5 -7
  82. package/dist/src/transform/transformParameters.js.map +1 -1
  83. package/dist/src/transform/transformPaths.d.ts.map +1 -1
  84. package/dist/src/transform/transformPaths.js +4 -6
  85. package/dist/src/transform/transformPaths.js.map +1 -1
  86. package/dist/src/transform/transformResponses.d.ts.map +1 -1
  87. package/dist/src/transform/transformResponses.js +70 -16
  88. package/dist/src/transform/transformResponses.js.map +1 -1
  89. package/dist/src/transform/transformSchemas.d.ts.map +1 -1
  90. package/dist/src/transform/transformSchemas.js +5 -6
  91. package/dist/src/transform/transformSchemas.js.map +1 -1
  92. package/dist/src/transform/transformTelemetryInfo.d.ts.map +1 -1
  93. package/dist/src/transform/transformTelemetryInfo.js +3 -6
  94. package/dist/src/transform/transformTelemetryInfo.js.map +1 -1
  95. package/dist/src/transform/transfromRLCOptions.d.ts.map +1 -1
  96. package/dist/src/transform/transfromRLCOptions.js +5 -6
  97. package/dist/src/transform/transfromRLCOptions.js.map +1 -1
  98. package/dist/src/utils/modelUtils.d.ts +3 -3
  99. package/dist/src/utils/modelUtils.d.ts.map +1 -1
  100. package/dist/src/utils/modelUtils.js +42 -24
  101. package/dist/src/utils/modelUtils.js.map +1 -1
  102. package/dist/src/utils/operationUtil.d.ts +2 -2
  103. package/dist/src/utils/operationUtil.d.ts.map +1 -1
  104. package/dist/src/utils/operationUtil.js +5 -7
  105. package/dist/src/utils/operationUtil.js.map +1 -1
  106. package/dist/tsconfig.tsbuildinfo +1 -1
  107. package/package.json +23 -23
  108. package/src/contextManager.ts +4 -0
  109. package/src/framework/declaration.ts +100 -0
  110. package/src/framework/hooks/binder.ts +259 -0
  111. package/src/framework/hooks/sdkTypes.ts +101 -0
  112. package/src/framework/reference.ts +9 -0
  113. package/src/framework/refkey.ts +28 -0
  114. package/src/framework/sample.ts +92 -0
  115. package/src/framework/utils/tsmorph-helpers.ts +0 -0
  116. package/src/index.ts +8 -10
  117. package/src/lib.ts +6 -0
  118. package/src/modular/buildClassicalClient.ts +11 -4
  119. package/src/modular/buildClientContext.ts +11 -5
  120. package/src/modular/buildCodeModel.ts +25 -21
  121. package/src/modular/buildOperations.ts +13 -41
  122. package/src/modular/buildRootIndex.ts +1 -1
  123. package/src/modular/buildSerializeUtils.ts +2 -1
  124. package/src/modular/helpers/clientHelpers.ts +25 -1
  125. package/src/modular/helpers/namingHelpers.ts +4 -1
  126. package/src/modular/helpers/operationHelpers.ts +86 -45
  127. package/src/modular/helpers/typeHelpers.ts +6 -1
  128. package/src/modular/modularCodeModel.ts +2 -0
  129. package/src/transform/transformApiVersionInfo.ts +3 -6
  130. package/src/transform/transformHelperFunctionDetails.ts +9 -9
  131. package/src/transform/transformParameters.ts +5 -6
  132. package/src/transform/transformPaths.ts +5 -9
  133. package/src/transform/transformResponses.ts +92 -19
  134. package/src/transform/transformSchemas.ts +6 -5
  135. package/src/transform/transformTelemetryInfo.ts +3 -5
  136. package/src/transform/transfromRLCOptions.ts +5 -10
  137. package/src/utils/modelUtils.ts +49 -39
  138. package/src/utils/operationUtil.ts +6 -12
  139. package/dist/src/modular/serialization/collectionUtils.d.ts +0 -3
  140. package/dist/src/modular/serialization/collectionUtils.d.ts.map +0 -1
  141. package/dist/src/modular/serialization/collectionUtils.js +0 -17
  142. package/dist/src/modular/serialization/collectionUtils.js.map +0 -1
  143. package/dist/src/modular/serialization/index.d.ts +0 -5
  144. package/dist/src/modular/serialization/index.d.ts.map +0 -1
  145. package/dist/src/modular/serialization/index.js +0 -155
  146. package/dist/src/modular/serialization/index.js.map +0 -1
  147. package/dist/src/modular/serialization/operationHelpers.d.ts +0 -37
  148. package/dist/src/modular/serialization/operationHelpers.d.ts.map +0 -1
  149. package/dist/src/modular/serialization/operationHelpers.js +0 -457
  150. package/dist/src/modular/serialization/operationHelpers.js.map +0 -1
  151. package/dist/src/modular/serialization/serializeArray.d.ts +0 -5
  152. package/dist/src/modular/serialization/serializeArray.d.ts.map +0 -1
  153. package/dist/src/modular/serialization/serializeArray.js +0 -33
  154. package/dist/src/modular/serialization/serializeArray.js.map +0 -1
  155. package/dist/src/modular/serialization/serializeDateTime.d.ts +0 -4
  156. package/dist/src/modular/serialization/serializeDateTime.d.ts.map +0 -1
  157. package/dist/src/modular/serialization/serializeDateTime.js +0 -19
  158. package/dist/src/modular/serialization/serializeDateTime.js.map +0 -1
  159. package/dist/src/modular/serialization/serializeHeaders.d.ts +0 -7
  160. package/dist/src/modular/serialization/serializeHeaders.d.ts.map +0 -1
  161. package/dist/src/modular/serialization/serializeHeaders.js +0 -44
  162. package/dist/src/modular/serialization/serializeHeaders.js.map +0 -1
  163. package/dist/src/modular/serialization/serializers.d.ts +0 -17
  164. package/dist/src/modular/serialization/serializers.d.ts.map +0 -1
  165. package/dist/src/modular/serialization/serializers.js +0 -175
  166. package/dist/src/modular/serialization/serializers.js.map +0 -1
  167. package/dist/src/modular/serialization/util.d.ts +0 -27
  168. package/dist/src/modular/serialization/util.d.ts.map +0 -1
  169. package/dist/src/modular/serialization/util.js +0 -115
  170. package/dist/src/modular/serialization/util.js.map +0 -1
  171. package/dist/src/reify/index.d.ts +0 -12
  172. package/dist/src/reify/index.d.ts.map +0 -1
  173. package/dist/src/reify/index.js +0 -22
  174. package/dist/src/reify/index.js.map +0 -1
  175. package/src/modular/serialization/collectionUtils.ts +0 -20
  176. package/src/modular/serialization/index.ts +0 -267
  177. package/src/modular/serialization/operationHelpers.ts +0 -659
  178. package/src/modular/serialization/serializeArray.ts +0 -51
  179. package/src/modular/serialization/serializeDateTime.ts +0 -22
  180. package/src/modular/serialization/serializeHeaders.ts +0 -64
  181. package/src/modular/serialization/serializers.ts +0 -304
  182. package/src/modular/serialization/util.ts +0 -171
  183. package/src/modular/static/karma.conf.cjs +0 -120
  184. package/src/reify/index.ts +0 -41
@@ -1,22 +0,0 @@
1
- import { SdkDatetimeType } from "@azure-tools/typespec-client-generator-core";
2
- import { UsageFlags } from "@typespec/compiler";
3
- import { SerializeTypeOptions } from "./serializers.js";
4
-
5
- export function serializeDatetime(
6
- options: SerializeTypeOptions<SdkDatetimeType>
7
- ): string {
8
- const { functionType, type, valueExpr } = options;
9
- if (functionType === UsageFlags.Input) {
10
- switch (type.encode) {
11
- case "rfc7231":
12
- return `(${valueExpr}).toUTCString()`;
13
- case "unixTimestamp":
14
- return `(${valueExpr}).getTime()`;
15
- case "rfc3339":
16
- default:
17
- return `(${valueExpr}).toISOString()`;
18
- }
19
- } else {
20
- return `new Date(${valueExpr})`;
21
- }
22
- }
@@ -1,64 +0,0 @@
1
- import {
2
- SdkHeaderParameter,
3
- UsageFlags
4
- } from "@azure-tools/typespec-client-generator-core";
5
- import { SerializeTypeOptions, serializeType } from "./serializers.js";
6
- import { SerializerOutput } from "./util.js";
7
- import { getCollectionSeparator } from "./collectionUtils.js";
8
- import { serializeArray } from "./serializeArray.js";
9
- import { isNumericTypeKind } from "../helpers/typeHelpers.js";
10
-
11
- export function serializeHeader(
12
- options: SerializeTypeOptions<
13
- SdkHeaderParameter & {
14
- kind: "header";
15
- }
16
- >
17
- ): SerializerOutput {
18
- const { type, functionType, valueExpr } = options;
19
-
20
- // We need to handle arrays differently than other types.
21
- // For headers arrays are serialized as a single string with a separator.
22
- if (type.type.kind === "array") {
23
- const separator = JSON.stringify(
24
- getCollectionSeparator(type.collectionFormat)
25
- );
26
-
27
- if (functionType === UsageFlags.Input) {
28
- // Serialization
29
- // Here we need to turn an array into a string with a separator.
30
- // We can use the serializeArray function to do this.
31
- const serializedArray = serializeArray({
32
- ...options,
33
- type: type.type
34
- });
35
-
36
- // Now that we have serialized the array, we can join the elements with the separator.
37
- return `${serializedArray}.join(${separator})`;
38
- } else {
39
- // Deserialization
40
- // Here we have a string that we need to split into an array.
41
- const array = `${valueExpr}.split(${separator})`;
42
-
43
- // No need to parse if the elements ar strings
44
- if (type.type.valueType.kind === "string") {
45
- return array;
46
- }
47
-
48
- if (
49
- isNumericTypeKind(type.type.valueType.kind) ||
50
- type.type.valueType.kind === "boolean"
51
- ) {
52
- return `${array}.map(e => JSON.parse(e))`;
53
- }
54
-
55
- return `${array}.map(e => ${serializeType({
56
- ...options,
57
- type: type.type.valueType,
58
- valueExpr: "e"
59
- })})`;
60
- }
61
- }
62
-
63
- return serializeType({ ...options, type: type.type });
64
- }
@@ -1,304 +0,0 @@
1
- import { ImportType } from "@azure-tools/rlc-common";
2
- import {
3
- SdkConstantType,
4
- SdkContext,
5
- SdkEnumType,
6
- SdkModelPropertyType,
7
- SdkModelType,
8
- SdkType,
9
- SdkUnionType
10
- } from "@azure-tools/typespec-client-generator-core";
11
- import { getDiscriminator, UsageFlags } from "@typespec/compiler";
12
- import _ from "lodash";
13
- import * as Reify from "../../reify/index.js";
14
- import {
15
- getEncodingFormat,
16
- getParameterTypePropertyName,
17
- getReturnTypePropertyName,
18
- isDefined,
19
- SerializerMap,
20
- SerializerOutput
21
- } from "./util.js";
22
- import { serializeHeader } from "./serializeHeaders.js";
23
- import { serializeArray } from "./serializeArray.js";
24
- import { serializeDatetime } from "./serializeDateTime.js";
25
-
26
- export interface SerializeTypeOptions<
27
- TCGCType extends SdkType | SdkModelPropertyType
28
- > {
29
- dpgContext: SdkContext;
30
- functionType: UsageFlags;
31
- serializerMap?: SerializerMap;
32
- type: TCGCType;
33
- valueExpr: string;
34
- importCallback: (importType: ImportType, importedName: string) => void;
35
- }
36
-
37
- const placeholder = <T>(_?: T) => {
38
- return '(()=>{throw Error("Not implemented.")})()';
39
- };
40
-
41
- export function serializeType<TCGCType extends SdkType | SdkModelPropertyType>(
42
- options: SerializeTypeOptions<TCGCType>
43
- ): SerializerOutput {
44
- const { functionType, serializerMap, type, valueExpr } = options;
45
- const modularTypeName = (type as { name?: string }).name;
46
- const serializerMetadata =
47
- isDefined(modularTypeName) && serializerMap?.[modularTypeName];
48
-
49
- if (serializerMetadata) {
50
- const functionName =
51
- functionType === UsageFlags.Input
52
- ? serializerMetadata?.serializerFunctionName
53
- : serializerMetadata?.deserializerFunctionName;
54
-
55
- return `${
56
- functionName ??
57
- (functionType === UsageFlags.Input
58
- ? "MISSING_SERIALIZER"
59
- : "MISSING_DESERIALIZER")
60
- }((${valueExpr}))`;
61
- }
62
-
63
- return getSerializeHandler(type.kind)?.(options) ?? valueExpr;
64
- }
65
-
66
- function getSerializeHandler<TCGCType extends SdkType | SdkModelPropertyType>(
67
- kind: TCGCType["kind"]
68
- ): ((options: SerializeTypeOptions<TCGCType>) => SerializerOutput) | undefined {
69
- type SimpleType =
70
- | "any"
71
- | "armId"
72
- | "azureLocation"
73
- | "boolean"
74
- | "constant"
75
- | "decimal"
76
- | "decimal128"
77
- | "enum"
78
- | "eTag"
79
- | "float"
80
- | "float32"
81
- | "float64"
82
- | "guid"
83
- | "int16"
84
- | "int32"
85
- | "int64"
86
- | "int8"
87
- | "integer"
88
- | "ipAddress"
89
- | "ipV4Address"
90
- | "ipV6Address"
91
- | "numeric"
92
- | "password"
93
- | "safeint"
94
- | "string"
95
- | "uint16"
96
- | "uint32"
97
- | "uint64"
98
- | "uint8"
99
- | "uri"
100
- | "url"
101
- | "uuid"
102
- | "nullable";
103
-
104
- type SerializeHandler<
105
- TCGCTypeKind extends (SdkType | SdkModelPropertyType)["kind"]
106
- > = {
107
- [K in TCGCTypeKind]: (
108
- options: SerializeTypeOptions<
109
- (SdkType | SdkModelPropertyType) & { kind: K }
110
- >
111
- ) => SerializerOutput;
112
- };
113
- type SerializeHandlerMap = Partial<SerializeHandler<SimpleType>> &
114
- SerializeHandler<
115
- Exclude<(SdkType | SdkModelPropertyType)["kind"], SimpleType>
116
- >;
117
-
118
- const handlers: SerializeHandlerMap = {
119
- array: serializeArray,
120
- bytes: serializeByteArray,
121
- enumvalue: (options) =>
122
- serializeType({ ...options, type: options.type.valueType }),
123
- model: serializeModelPropertiesInline,
124
- offsetDateTime: serializeDatetime,
125
- plainDate: ({ valueExpr }) => `(${valueExpr}).toDateString()`,
126
- plainTime: ({ valueExpr }) => `(${valueExpr}).toTimeString()`,
127
- property: serializeModelProperty,
128
- // TODO: This is the function that emits the body of a function meant to serialize a type union.
129
- // It's implemented as a switch statement. This is fine for a function body. However,
130
- // `getSerializeHandler` is designed to always return something that can go in expression
131
- // position. We may or may not decide to emit a function to serialize anonymous unions using the
132
- // TCGC generated name as its identifier. If we don't, a copy of this function will need to be
133
- // adapted to expression position (e.g. nested ternary statements). As it stands, this function
134
- // should never actually be dispatched, and should be removed if we find it to be unnecessary.
135
- union: serializeUnionInline,
136
- utcDateTime: serializeDatetime,
137
-
138
- duration: placeholder,
139
- tuple: placeholder,
140
- dict: placeholder,
141
- credential: placeholder,
142
-
143
- endpoint: placeholder,
144
- method: placeholder,
145
- query: placeholder,
146
- path: placeholder,
147
- body: placeholder,
148
- header: serializeHeader
149
- };
150
- const handler = handlers[kind];
151
- return handler as any;
152
- }
153
-
154
- function serializeByteArray(
155
- options: SerializeTypeOptions<SdkType & { kind: "bytes" }>
156
- ): string {
157
- const { functionType, type, valueExpr, importCallback } = options;
158
-
159
- if (type.encode === "binary") {
160
- return valueExpr;
161
- }
162
-
163
- const format = getEncodingFormat(functionType, type);
164
- const args = [valueExpr, `"${format}"`].join(", ");
165
-
166
- if (functionType === UsageFlags.Input) {
167
- importCallback("coreUtil", "uint8ArrayToString");
168
- return `uint8ArrayToString(${args})`;
169
- }
170
- importCallback("coreUtil", "stringToUint8Array");
171
- return `(typeof (${valueExpr}) === 'string')
172
- ? (stringToUint8Array(${args}))
173
- : (${valueExpr})`;
174
- }
175
-
176
- export function serializeModelPropertiesInline(
177
- options: SerializeTypeOptions<SdkModelType>
178
- ): string {
179
- const { dpgContext, functionType, type, valueExpr } = options;
180
- const props = type.properties.filter((p) => p.kind === "property");
181
- const serializedParents = type.baseModel
182
- ? `...(${serializeType({ ...options, type: type.baseModel })})`
183
- : undefined;
184
- const serializedProps = props.map((prop) => {
185
- const id = getParameterTypePropertyName(dpgContext, functionType, prop);
186
- return serializeModelProperty({
187
- ...options,
188
- type: prop,
189
- valueExpr: `${valueExpr}["${id}"]`
190
- });
191
- });
192
- const body = [`...(${valueExpr})`, serializedParents, ...serializedProps]
193
- .filter(isDefined)
194
- .join(",\n");
195
- return `{${body}}`;
196
- }
197
-
198
- export function serializeUnionInline(
199
- options: SerializeTypeOptions<SdkUnionType>
200
- ): string {
201
- const { dpgContext, type, valueExpr } = options;
202
- const discriminator = getDiscriminator(dpgContext.program, type.__raw!)
203
- ?.propertyName;
204
- if (!discriminator) {
205
- return placeholder();
206
- }
207
- const baseType = type.values.find((v) => v.kind !== "constant")!;
208
- const variants = type.values.filter(
209
- (v) => v !== baseType
210
- ) as SdkConstantType[];
211
- // TODO: Used to catch corner cases during testing. Remove this before shipping.
212
- variants
213
- .filter((v) => v.kind !== "constant")
214
- .forEach((v) => {
215
- throw Error(
216
- `TODO: Handle non-constant non-base variant of union ${type.name} ` +
217
- `with type name ${v.name} and kind ${v.kind}`
218
- );
219
- });
220
- const cases = variants
221
- .map((variant) => {
222
- `${variant.value}: ${serializeType({
223
- ...options,
224
- type: variant
225
- })}`;
226
- })
227
- .join(",\n");
228
- // TODO: Non-discriminated unions have UB at the moment, as opposed to having good messaging that
229
- // they're not supported
230
- return `({${cases}})[(${valueExpr})["${discriminator}"]] ?? (${serializeType({
231
- ...options,
232
- type: baseType
233
- })})`;
234
- }
235
-
236
- export function serializeEnumFunctionBody(
237
- options: SerializeTypeOptions<SdkEnumType>
238
- ): string {
239
- const { type, valueExpr } = options;
240
- const defaultBody: Reify.Statement[] = [{ kind: "return", value: valueExpr }];
241
-
242
- const cases = _(type.values)
243
- .map((value): [string | undefined, Reify.Statement[]] => {
244
- const variantId = `${type.name}.${value.name}`;
245
- const variantBody: Reify.ReturnStatement[] = [
246
- {
247
- kind: "return",
248
- value: serializeType({ ...options, type: value.valueType })
249
- }
250
- ];
251
- return [variantId, variantBody];
252
- })
253
- .concat([[undefined, defaultBody]])
254
- .reduce((acc, [id, body]) => {
255
- const ids = acc.get(body) ?? new Set();
256
- ids.add(id);
257
- acc.set(body, ids);
258
- return acc;
259
- }, new Map<Reify.Statement[], Set<string | undefined>>());
260
-
261
- const functionBody: Reify.SwitchReturn = {
262
- kind: "switch return",
263
- expr: valueExpr,
264
- cases
265
- };
266
-
267
- return Reify.reifySwitchReturn(functionBody);
268
- }
269
-
270
- function serializeModelProperty(
271
- options: SerializeTypeOptions<SdkModelPropertyType>
272
- ): string {
273
- const { type, valueExpr } = options;
274
-
275
- const propDecl = getPropertyDeclaration(options);
276
-
277
- return type.optional
278
- ? `...((${valueExpr}) === undefined ? {} : {${propDecl}})`
279
- : `${propDecl}`;
280
- }
281
-
282
- function getPropertyDeclaration(
283
- options: SerializeTypeOptions<SdkModelPropertyType>
284
- ): string {
285
- const { dpgContext, functionType, type } = options;
286
-
287
- const id = getReturnTypePropertyName(dpgContext, functionType, type);
288
- const propInitializer = getPropertyInitializer(options);
289
-
290
- return `"${id}": (${propInitializer})`;
291
- }
292
-
293
- function getPropertyInitializer(
294
- options: SerializeTypeOptions<SdkModelPropertyType>
295
- ): string {
296
- const { type } = options;
297
-
298
- const propSerializer = serializeType({
299
- ...options,
300
- type: { ...type.type, nullable: false, optional: false }
301
- });
302
-
303
- return propSerializer;
304
- }
@@ -1,171 +0,0 @@
1
- import {
2
- getLibraryName,
3
- getWireName,
4
- SdkEnumType,
5
- SdkModelPropertyType,
6
- SdkModelType,
7
- SdkType,
8
- SdkUnionType,
9
- UsageFlags as TCGCUsageFlags
10
- } from "@azure-tools/typespec-client-generator-core";
11
- import { UsageFlags } from "@typespec/compiler";
12
- import { toPascalCase } from "../../utils/casingUtils.js";
13
- import { SdkContext } from "../../utils/interfaces.js";
14
-
15
- type ModularTypeName = string;
16
- export type SerializeFunctionType = SdkModelType | SdkUnionType | SdkEnumType;
17
- export type SerializerMap = Record<ModularTypeName, SerializerMetadata>;
18
- export type SerializerOutput = string;
19
-
20
- interface SerializerMetadata {
21
- rlcTypeName?: string;
22
- rlcTypeAlias?: string;
23
- type: SerializeFunctionType;
24
- serializerFunctionName?: string;
25
- deserializerFunctionName?: string;
26
- }
27
-
28
- const supportedFormats = ["base64url", "base64", "byte"];
29
- export function getEncodingFormat(
30
- functionType: UsageFlags,
31
- type: SdkType & { kind: "bytes" }
32
- ) {
33
- if (functionType === UsageFlags.Output) {
34
- return type.encode;
35
- }
36
-
37
- return supportedFormats.find((format) => format === type.encode) ?? "base64";
38
- }
39
-
40
- const dispatch: {
41
- [Kind in SdkType["kind"]]: (
42
- type: SdkType & { kind: Kind }
43
- ) => string | undefined;
44
- } = {
45
- string: (_) => "string",
46
- boolean: (_) => "boolean",
47
- bytes: (_) => "Uint8Array",
48
- model: (type) => type.name,
49
- union: (type) => type.name,
50
- enum: (type) => type.name,
51
- plainDate: (_) => "Date",
52
- plainTime: (_) => "Date",
53
- any: (_) => "any",
54
- numeric: (_) => "number",
55
- integer: (_) => "number",
56
- safeint: (_) => "number",
57
- int8: (_) => "number",
58
- int16: (_) => "number",
59
- int32: (_) => "number",
60
- int64: (_) => "number",
61
- uint8: (_) => "number",
62
- uint16: (_) => "number",
63
- uint32: (_) => "number",
64
- uint64: (_) => "number",
65
- float: (_) => "number",
66
- float32: (_) => "number",
67
- float64: (_) => "number",
68
- decimal: (_) => "number",
69
- decimal128: (_) => "number",
70
- password: (_) => "string",
71
- guid: (_) => "string",
72
- url: (_) => "string",
73
- uri: (_) => "string",
74
- ipAddress: (_) => "string",
75
- uuid: (_) => "string",
76
- ipV4Address: (_) => "string",
77
- ipV6Address: (_) => "string",
78
- eTag: (_) => "string",
79
- armId: (_) => "string",
80
- azureLocation: (_) => "string",
81
- utcDateTime: (_) => "Date",
82
- offsetDateTime: (_) => "Date",
83
- duration: (_) => "FIXME",
84
- array: (type) => {
85
- const valueTypeId = getModularTypeId(type.valueType);
86
- return valueTypeId ? `Array<${valueTypeId}>` : undefined;
87
- },
88
- tuple: (type) => {
89
- const elementIds = type.values.map(getModularTypeId);
90
- if (!type.values.length || elementIds.some((id) => !id)) {
91
- return;
92
- }
93
- return `[${elementIds.join(", ")}]`;
94
- },
95
- dict: (type) => {
96
- const keyId = getModularTypeId(type.keyType);
97
- const valueId = getModularTypeId(type.valueType);
98
- if (!keyId || !valueId) {
99
- return;
100
- }
101
- const valueType = valueId;
102
- return `Record<${keyId}, ${valueType}>`;
103
- },
104
- enumvalue: (_) => "FIXME",
105
- constant: (_) => "FIXME",
106
- credential: (_) => "FIXME",
107
- endpoint: (_) => "string",
108
- nullable: (_) => "null"
109
- };
110
-
111
- export function getModularTypeId<T extends SdkType>(
112
- type: T
113
- ): string | undefined {
114
- const callback = dispatch[type.kind] as (type: T) => string | undefined;
115
- return callback(type);
116
- }
117
-
118
- export function getParameterTypePropertyName(
119
- dpgContext: SdkContext,
120
- functionType: UsageFlags,
121
- p: SdkModelPropertyType
122
- ): string {
123
- return functionType === UsageFlags.Output
124
- ? getWireName(dpgContext, p.__raw!)
125
- : getLibraryName(dpgContext, p.__raw!);
126
- }
127
-
128
- export function getReturnTypePropertyName(
129
- dpgContext: SdkContext,
130
- functionType: UsageFlags,
131
- p: SdkModelPropertyType
132
- ): string {
133
- return functionType === UsageFlags.Output
134
- ? getLibraryName(dpgContext, p.__raw!)
135
- : getWireName(dpgContext, p.__raw!);
136
- }
137
-
138
- export function getRLCTypeId(
139
- dpgContext: SdkContext,
140
- type: SdkType & { name?: string }
141
- ) {
142
- const modularTypeId = getModularTypeId(type);
143
- const usage = getUsage(dpgContext, type);
144
- const useOutputModel =
145
- usage & UsageFlags.Output && !(usage & UsageFlags.Input);
146
- return useOutputModel ? toPascalCase(`${modularTypeId} Rest`) : modularTypeId;
147
- }
148
-
149
- export function getUsage(
150
- // @ts-expect-error: To be removed with the below TODO
151
- dpgContext: SdkContext,
152
- // @ts-expect-error: Ditto
153
- type: SdkType
154
- ): TCGCUsageFlags {
155
- return toDpgUsageFlags(
156
- //TODO: Currently disregards usage when calculating whether or not to emit the function. This
157
- //doesn't affect client API surface.
158
- TCGCUsageFlags.Input | TCGCUsageFlags.Output
159
- );
160
- }
161
-
162
- function toDpgUsageFlags(usage: TCGCUsageFlags): UsageFlags {
163
- return (
164
- (usage & TCGCUsageFlags.Input ? UsageFlags.Input : 0) |
165
- (usage & TCGCUsageFlags.Output ? UsageFlags.Output : 0)
166
- );
167
- }
168
-
169
- export function isDefined<T>(thing: T | undefined | null): thing is T {
170
- return typeof thing !== "undefined" && thing !== null;
171
- }
@@ -1,120 +0,0 @@
1
- // Copyright (c) Microsoft Corporation. All rights reserved.
2
- // Licensed under the MIT License.
3
-
4
- // https://github.com/karma-runner/karma-chrome-launcher
5
- process.env.CHROME_BIN = require("puppeteer").executablePath();
6
- require("dotenv").config();
7
-
8
- module.exports = function (config) {
9
- config.set({
10
- // base path that will be used to resolve all patterns (eg. files, exclude)
11
- basePath: "./",
12
-
13
- // frameworks to use
14
- // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
15
- frameworks: ["mocha"],
16
-
17
- plugins: [
18
- "karma-mocha",
19
- "karma-mocha-reporter",
20
- "karma-chrome-launcher",
21
- "karma-firefox-launcher",
22
- "karma-env-preprocessor",
23
- "karma-coverage",
24
- "karma-sourcemap-loader",
25
- "karma-junit-reporter"
26
- ],
27
-
28
- // list of files / patterns to load in the browser
29
- files: [
30
- "dist-test/index.browser.js",
31
- {
32
- pattern: "dist-test/index.browser.js.map",
33
- type: "html",
34
- included: false,
35
- served: true
36
- }
37
- ],
38
-
39
- // list of files / patterns to exclude
40
- exclude: [],
41
-
42
- // preprocess matching files before serving them to the browser
43
- // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
44
- preprocessors: {
45
- "**/*.js": ["sourcemap", "env"]
46
- // IMPORTANT: COMMENT following line if you want to debug in your browsers!!
47
- // Preprocess source file to calculate code coverage, however this will make source file unreadable
48
- //"dist-test/index.browser.js": ["coverage"]
49
- },
50
-
51
- // inject following environment values into browser testing with window.__env__
52
- // environment values MUST be exported or set with same console running "karma start"
53
- // https://www.npmjs.com/package/karma-env-preprocessor
54
- envPreprocessor: ["TEST_MODE"],
55
-
56
- // test results reporter to use
57
- // possible values: 'dots', 'progress'
58
- // available reporters: https://npmjs.org/browse/keyword/karma-reporter
59
- reporters: ["mocha", "coverage", "junit"],
60
-
61
- coverageReporter: {
62
- // specify a common output directory
63
- dir: "coverage-browser/",
64
- reporters: [
65
- { type: "json", subdir: ".", file: "coverage.json" },
66
- { type: "lcovonly", subdir: ".", file: "lcov.info" },
67
- { type: "html", subdir: "html" },
68
- { type: "cobertura", subdir: ".", file: "cobertura-coverage.xml" }
69
- ]
70
- },
71
-
72
- junitReporter: {
73
- outputDir: "", // results will be saved as $outputDir/$browserName.xml
74
- outputFile: "test-results.browser.xml", // if included, results will be saved as $outputDir/$browserName/$outputFile
75
- suite: "", // suite will become the package name attribute in xml testsuite element
76
- useBrowserName: false, // add browser name to report and classes names
77
- nameFormatter: undefined, // function (browser, result) to customize the name attribute in xml testcase element
78
- classNameFormatter: undefined, // function (browser, result) to customize the classname attribute in xml testcase element
79
- properties: {} // key value pair of properties to add to the <properties> section of the report
80
- },
81
-
82
- // web server port
83
- port: 9876,
84
-
85
- // enable / disable colors in the output (reporters and logs)
86
- colors: true,
87
-
88
- // level of logging
89
- // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
90
- logLevel: config.LOG_INFO,
91
-
92
- // enable / disable watching file and executing tests whenever any file changes
93
- autoWatch: false,
94
-
95
- // start these browsers
96
- // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
97
- // 'ChromeHeadless', 'Chrome', 'Firefox', 'Edge', 'IE'
98
- browsers: ["ChromeHeadless"],
99
-
100
- // Continuous Integration mode
101
- // if true, Karma captures browsers, runs the tests and exits
102
- singleRun: true,
103
-
104
- // Concurrency level
105
- // how many browser should be started simultaneous
106
- concurrency: 1,
107
-
108
- browserNoActivityTimeout: 600000,
109
- browserDisconnectTimeout: 10000,
110
- browserDisconnectTolerance: 3,
111
-
112
- client: {
113
- mocha: {
114
- // change Karma's debug.html to the mocha web reporter
115
- reporter: "html",
116
- timeout: "600000"
117
- }
118
- }
119
- });
120
- };