@typespec/http-server-js 0.58.0-alpha.10-dev.3

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 (215) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/LICENSE +21 -0
  3. package/README.md +183 -0
  4. package/build-helpers.ts +170 -0
  5. package/dist/generated-defs/helpers/header.d.ts +4 -0
  6. package/dist/generated-defs/helpers/header.d.ts.map +1 -0
  7. package/dist/generated-defs/helpers/header.js +76 -0
  8. package/dist/generated-defs/helpers/header.js.map +1 -0
  9. package/dist/generated-defs/helpers/http.d.ts +4 -0
  10. package/dist/generated-defs/helpers/http.d.ts.map +1 -0
  11. package/dist/generated-defs/helpers/http.js +134 -0
  12. package/dist/generated-defs/helpers/http.js.map +1 -0
  13. package/dist/generated-defs/helpers/index.d.ts +4 -0
  14. package/dist/generated-defs/helpers/index.d.ts.map +1 -0
  15. package/dist/generated-defs/helpers/index.js +21 -0
  16. package/dist/generated-defs/helpers/index.js.map +1 -0
  17. package/dist/generated-defs/helpers/multipart.d.ts +4 -0
  18. package/dist/generated-defs/helpers/multipart.d.ts.map +1 -0
  19. package/dist/generated-defs/helpers/multipart.js +249 -0
  20. package/dist/generated-defs/helpers/multipart.js.map +1 -0
  21. package/dist/generated-defs/helpers/router.d.ts +4 -0
  22. package/dist/generated-defs/helpers/router.d.ts.map +1 -0
  23. package/dist/generated-defs/helpers/router.js +259 -0
  24. package/dist/generated-defs/helpers/router.js.map +1 -0
  25. package/dist/src/common/declaration.d.ts +13 -0
  26. package/dist/src/common/declaration.d.ts.map +1 -0
  27. package/dist/src/common/declaration.js +45 -0
  28. package/dist/src/common/declaration.js.map +1 -0
  29. package/dist/src/common/documentation.d.ts +12 -0
  30. package/dist/src/common/documentation.d.ts.map +1 -0
  31. package/dist/src/common/documentation.js +21 -0
  32. package/dist/src/common/documentation.js.map +1 -0
  33. package/dist/src/common/enum.d.ts +10 -0
  34. package/dist/src/common/enum.d.ts.map +1 -0
  35. package/dist/src/common/enum.js +21 -0
  36. package/dist/src/common/enum.js.map +1 -0
  37. package/dist/src/common/interface.d.ts +50 -0
  38. package/dist/src/common/interface.d.ts.map +1 -0
  39. package/dist/src/common/interface.js +194 -0
  40. package/dist/src/common/interface.js.map +1 -0
  41. package/dist/src/common/model.d.ts +26 -0
  42. package/dist/src/common/model.d.ts.map +1 -0
  43. package/dist/src/common/model.js +115 -0
  44. package/dist/src/common/model.js.map +1 -0
  45. package/dist/src/common/namespace.d.ts +38 -0
  46. package/dist/src/common/namespace.d.ts.map +1 -0
  47. package/dist/src/common/namespace.js +184 -0
  48. package/dist/src/common/namespace.js.map +1 -0
  49. package/dist/src/common/reference.d.ts +46 -0
  50. package/dist/src/common/reference.d.ts.map +1 -0
  51. package/dist/src/common/reference.js +243 -0
  52. package/dist/src/common/reference.js.map +1 -0
  53. package/dist/src/common/scalar.d.ts +50 -0
  54. package/dist/src/common/scalar.d.ts.map +1 -0
  55. package/dist/src/common/scalar.js +144 -0
  56. package/dist/src/common/scalar.js.map +1 -0
  57. package/dist/src/common/serialization/index.d.ts +11 -0
  58. package/dist/src/common/serialization/index.d.ts.map +1 -0
  59. package/dist/src/common/serialization/index.js +72 -0
  60. package/dist/src/common/serialization/index.js.map +1 -0
  61. package/dist/src/common/serialization/json.d.ts +6 -0
  62. package/dist/src/common/serialization/json.d.ts.map +1 -0
  63. package/dist/src/common/serialization/json.js +341 -0
  64. package/dist/src/common/serialization/json.js.map +1 -0
  65. package/dist/src/common/union.d.ts +23 -0
  66. package/dist/src/common/union.d.ts.map +1 -0
  67. package/dist/src/common/union.js +57 -0
  68. package/dist/src/common/union.js.map +1 -0
  69. package/dist/src/ctx.d.ts +242 -0
  70. package/dist/src/ctx.d.ts.map +1 -0
  71. package/dist/src/ctx.js +211 -0
  72. package/dist/src/ctx.js.map +1 -0
  73. package/dist/src/helpers/header.d.ts +14 -0
  74. package/dist/src/helpers/header.d.ts.map +1 -0
  75. package/dist/src/helpers/header.js +38 -0
  76. package/dist/src/helpers/header.js.map +1 -0
  77. package/dist/src/helpers/http.d.ts +70 -0
  78. package/dist/src/helpers/http.d.ts.map +1 -0
  79. package/dist/src/helpers/http.js +86 -0
  80. package/dist/src/helpers/http.js.map +1 -0
  81. package/dist/src/helpers/multipart.d.ts +26 -0
  82. package/dist/src/helpers/multipart.d.ts.map +1 -0
  83. package/dist/src/helpers/multipart.js +182 -0
  84. package/dist/src/helpers/multipart.js.map +1 -0
  85. package/dist/src/helpers/router.d.ts +176 -0
  86. package/dist/src/helpers/router.d.ts.map +1 -0
  87. package/dist/src/helpers/router.js +55 -0
  88. package/dist/src/helpers/router.js.map +1 -0
  89. package/dist/src/http/index.d.ts +24 -0
  90. package/dist/src/http/index.d.ts.map +1 -0
  91. package/dist/src/http/index.js +52 -0
  92. package/dist/src/http/index.js.map +1 -0
  93. package/dist/src/http/server/index.d.ts +11 -0
  94. package/dist/src/http/server/index.d.ts.map +1 -0
  95. package/dist/src/http/server/index.js +413 -0
  96. package/dist/src/http/server/index.js.map +1 -0
  97. package/dist/src/http/server/multipart.d.ts +16 -0
  98. package/dist/src/http/server/multipart.d.ts.map +1 -0
  99. package/dist/src/http/server/multipart.js +214 -0
  100. package/dist/src/http/server/multipart.js.map +1 -0
  101. package/dist/src/http/server/router.d.ts +15 -0
  102. package/dist/src/http/server/router.d.ts.map +1 -0
  103. package/dist/src/http/server/router.js +459 -0
  104. package/dist/src/http/server/router.js.map +1 -0
  105. package/dist/src/index.d.ts +5 -0
  106. package/dist/src/index.d.ts.map +1 -0
  107. package/dist/src/index.js +38 -0
  108. package/dist/src/index.js.map +1 -0
  109. package/dist/src/lib.d.ts +141 -0
  110. package/dist/src/lib.d.ts.map +1 -0
  111. package/dist/src/lib.js +116 -0
  112. package/dist/src/lib.js.map +1 -0
  113. package/dist/src/scripts/scaffold/bin.d.mts +14 -0
  114. package/dist/src/scripts/scaffold/bin.d.mts.map +1 -0
  115. package/dist/src/scripts/scaffold/bin.mjs +559 -0
  116. package/dist/src/scripts/scaffold/bin.mjs.map +1 -0
  117. package/dist/src/testing/index.d.ts +3 -0
  118. package/dist/src/testing/index.d.ts.map +1 -0
  119. package/dist/src/testing/index.js +6 -0
  120. package/dist/src/testing/index.js.map +1 -0
  121. package/dist/src/util/case.d.ts +81 -0
  122. package/dist/src/util/case.d.ts.map +1 -0
  123. package/dist/src/util/case.js +111 -0
  124. package/dist/src/util/case.js.map +1 -0
  125. package/dist/src/util/differentiate.d.ts +251 -0
  126. package/dist/src/util/differentiate.d.ts.map +1 -0
  127. package/dist/src/util/differentiate.js +580 -0
  128. package/dist/src/util/differentiate.js.map +1 -0
  129. package/dist/src/util/error.d.ts +13 -0
  130. package/dist/src/util/error.d.ts.map +1 -0
  131. package/dist/src/util/error.js +25 -0
  132. package/dist/src/util/error.js.map +1 -0
  133. package/dist/src/util/extends.d.ts +10 -0
  134. package/dist/src/util/extends.d.ts.map +1 -0
  135. package/dist/src/util/extends.js +31 -0
  136. package/dist/src/util/extends.js.map +1 -0
  137. package/dist/src/util/iter.d.ts +39 -0
  138. package/dist/src/util/iter.d.ts.map +1 -0
  139. package/dist/src/util/iter.js +72 -0
  140. package/dist/src/util/iter.js.map +1 -0
  141. package/dist/src/util/keywords.d.ts +10 -0
  142. package/dist/src/util/keywords.d.ts.map +1 -0
  143. package/dist/src/util/keywords.js +85 -0
  144. package/dist/src/util/keywords.js.map +1 -0
  145. package/dist/src/util/name.d.ts +12 -0
  146. package/dist/src/util/name.d.ts.map +1 -0
  147. package/dist/src/util/name.js +26 -0
  148. package/dist/src/util/name.js.map +1 -0
  149. package/dist/src/util/once-queue.d.ts +24 -0
  150. package/dist/src/util/once-queue.d.ts.map +1 -0
  151. package/dist/src/util/once-queue.js +34 -0
  152. package/dist/src/util/once-queue.js.map +1 -0
  153. package/dist/src/util/openapi3.d.ts +23 -0
  154. package/dist/src/util/openapi3.d.ts.map +1 -0
  155. package/dist/src/util/openapi3.js +40 -0
  156. package/dist/src/util/openapi3.js.map +1 -0
  157. package/dist/src/util/pluralism.d.ts +23 -0
  158. package/dist/src/util/pluralism.d.ts.map +1 -0
  159. package/dist/src/util/pluralism.js +36 -0
  160. package/dist/src/util/pluralism.js.map +1 -0
  161. package/dist/src/util/scope.d.ts +85 -0
  162. package/dist/src/util/scope.d.ts.map +1 -0
  163. package/dist/src/util/scope.js +111 -0
  164. package/dist/src/util/scope.js.map +1 -0
  165. package/dist/src/write.d.ts +23 -0
  166. package/dist/src/write.d.ts.map +1 -0
  167. package/dist/src/write.js +62 -0
  168. package/dist/src/write.js.map +1 -0
  169. package/generated-defs/helpers/header.ts +83 -0
  170. package/generated-defs/helpers/http.ts +141 -0
  171. package/generated-defs/helpers/index.ts +27 -0
  172. package/generated-defs/helpers/multipart.ts +256 -0
  173. package/generated-defs/helpers/router.ts +266 -0
  174. package/package.json +71 -0
  175. package/src/common/declaration.ts +52 -0
  176. package/src/common/documentation.ts +26 -0
  177. package/src/common/enum.ts +28 -0
  178. package/src/common/interface.ts +264 -0
  179. package/src/common/model.ts +160 -0
  180. package/src/common/namespace.ts +243 -0
  181. package/src/common/reference.ts +319 -0
  182. package/src/common/scalar.ts +173 -0
  183. package/src/common/serialization/index.ts +124 -0
  184. package/src/common/serialization/json.ts +444 -0
  185. package/src/common/union.ts +76 -0
  186. package/src/ctx.ts +497 -0
  187. package/src/helpers/header.ts +55 -0
  188. package/src/helpers/http.ts +113 -0
  189. package/src/helpers/multipart.ts +228 -0
  190. package/src/helpers/router.ts +238 -0
  191. package/src/http/index.ts +81 -0
  192. package/src/http/server/index.ts +548 -0
  193. package/src/http/server/multipart.ts +272 -0
  194. package/src/http/server/router.ts +686 -0
  195. package/src/index.ts +56 -0
  196. package/src/lib.ts +130 -0
  197. package/src/scripts/scaffold/bin.mts +781 -0
  198. package/src/testing/index.ts +10 -0
  199. package/src/util/case.ts +182 -0
  200. package/src/util/differentiate.ts +957 -0
  201. package/src/util/error.ts +28 -0
  202. package/src/util/extends.ts +43 -0
  203. package/src/util/iter.ts +85 -0
  204. package/src/util/keywords.ts +90 -0
  205. package/src/util/name.ts +33 -0
  206. package/src/util/once-queue.ts +55 -0
  207. package/src/util/openapi3.ts +53 -0
  208. package/src/util/pluralism.ts +37 -0
  209. package/src/util/scope.ts +211 -0
  210. package/src/write.ts +88 -0
  211. package/temp/tsconfig.tsbuildinfo +1 -0
  212. package/test/header.test.ts +26 -0
  213. package/test/multipart.test.ts +169 -0
  214. package/tsconfig.json +10 -0
  215. package/vitest.config.ts +4 -0
@@ -0,0 +1,144 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { formatDiagnostic } from "@typespec/compiler";
4
+ import { reportDiagnostic } from "../lib.js";
5
+ import { parseCase } from "../util/case.js";
6
+ import { UnimplementedError } from "../util/error.js";
7
+ import { getFullyQualifiedTypeName } from "../util/name.js";
8
+ /**
9
+ * Emits a declaration for a scalar type.
10
+ *
11
+ * This is rare in TypeScript, as the scalar will ordinarily be used inline, but may be desirable in some cases.
12
+ *
13
+ * @param ctx - The emitter context.
14
+ * @param scalar - The scalar to emit.
15
+ * @returns a string that declares an alias to the scalar type in TypeScript.
16
+ */
17
+ export function emitScalar(ctx, scalar) {
18
+ const jsScalar = getJsScalar(ctx.program, scalar, scalar.node.id);
19
+ const name = parseCase(scalar.name).pascalCase;
20
+ return `type ${name} = ${jsScalar};`;
21
+ }
22
+ /**
23
+ * Get the string parsing template for a given scalar.
24
+ *
25
+ * It is common that a scalar type is encoded as a string. For example, in HTTP path parameters or query parameters
26
+ * where the value may be an integer, but the APIs expose it as a string. In such cases the parse template may be
27
+ * used to coerce the string value to the correct scalar type.
28
+ *
29
+ * The result of this function contains the string "{}" exactly once, which should be replaced with the text of an
30
+ * expression evaluating to the string representation of the scalar.
31
+ *
32
+ * For example, scalars that are represented by JS `number` are parsed with the template `Number({})`, which will
33
+ * convert the string to a number.
34
+ *
35
+ * @param ctx - The emitter context.
36
+ * @param scalar - The scalar to parse from a string
37
+ * @returns a template expression string that can be used to parse a string into the scalar type.
38
+ */
39
+ export function parseTemplateForScalar(ctx, scalar) {
40
+ const jsScalar = getJsScalar(ctx.program, scalar, scalar);
41
+ switch (jsScalar) {
42
+ case "string":
43
+ return "{}";
44
+ case "number":
45
+ return "Number({})";
46
+ case "bigint":
47
+ return "BigInt({})";
48
+ case "Uint8Array":
49
+ return "Buffer.from({}, 'base64')";
50
+ default:
51
+ throw new UnimplementedError(`parse template for scalar '${jsScalar}'`);
52
+ }
53
+ }
54
+ /**
55
+ * Get the string encoding template for a given scalar.
56
+ * @param ctx
57
+ * @param scalar
58
+ */
59
+ export function encodeTemplateForScalar(ctx, scalar) {
60
+ const jsScalar = getJsScalar(ctx.program, scalar, scalar);
61
+ switch (jsScalar) {
62
+ case "string":
63
+ return "{}";
64
+ case "number":
65
+ return "String({})";
66
+ case "bigint":
67
+ return "String({})";
68
+ case "Uint8Array":
69
+ return "{}.toString('base64')";
70
+ default:
71
+ throw new UnimplementedError(`encode template for scalar '${jsScalar}'`);
72
+ }
73
+ }
74
+ const __JS_SCALARS_MAP = new Map();
75
+ function getScalarsMap(program) {
76
+ let scalars = __JS_SCALARS_MAP.get(program);
77
+ if (scalars === undefined) {
78
+ scalars = createScalarsMap(program);
79
+ __JS_SCALARS_MAP.set(program, scalars);
80
+ }
81
+ return scalars;
82
+ }
83
+ function createScalarsMap(program) {
84
+ const entries = [
85
+ [program.resolveTypeReference("TypeSpec.bytes"), "Uint8Array"],
86
+ [program.resolveTypeReference("TypeSpec.boolean"), "boolean"],
87
+ [program.resolveTypeReference("TypeSpec.string"), "string"],
88
+ [program.resolveTypeReference("TypeSpec.float32"), "number"],
89
+ [program.resolveTypeReference("TypeSpec.float64"), "number"],
90
+ [program.resolveTypeReference("TypeSpec.uint32"), "number"],
91
+ [program.resolveTypeReference("TypeSpec.uint16"), "number"],
92
+ [program.resolveTypeReference("TypeSpec.uint8"), "number"],
93
+ [program.resolveTypeReference("TypeSpec.int32"), "number"],
94
+ [program.resolveTypeReference("TypeSpec.int16"), "number"],
95
+ [program.resolveTypeReference("TypeSpec.int8"), "number"],
96
+ [program.resolveTypeReference("TypeSpec.safeint"), "number"],
97
+ [program.resolveTypeReference("TypeSpec.integer"), "bigint"],
98
+ [program.resolveTypeReference("TypeSpec.plainDate"), "Date"],
99
+ [program.resolveTypeReference("TypeSpec.plainTime"), "Date"],
100
+ [program.resolveTypeReference("TypeSpec.utcDateTime"), "Date"],
101
+ ];
102
+ for (const [[type, diagnostics]] of entries) {
103
+ if (!type) {
104
+ const diagnosticString = diagnostics.map((x) => formatDiagnostic(x)).join("\n");
105
+ throw new Error(`failed to construct TypeSpec -> JavaScript scalar map: ${diagnosticString}`);
106
+ }
107
+ else if (type.kind !== "Scalar") {
108
+ throw new Error(`type ${type.name ?? "<anonymous>"} is a '${type.kind}', expected 'scalar'`);
109
+ }
110
+ }
111
+ return new Map(entries.map(([[type], scalar]) => [type, scalar]));
112
+ }
113
+ /**
114
+ * Gets a TypeScript type that can represent a given TypeSpec scalar.
115
+ *
116
+ * Scalar recognition is recursive. If a scalar is not recognized, we will treat it as its parent scalar and try again.
117
+ *
118
+ * If no scalar in the chain is recognized, it will be treated as `unknown` and a warning will be issued.
119
+ *
120
+ * @param program - The program that contains the scalar
121
+ * @param scalar - The scalar to get the TypeScript type for
122
+ * @param diagnosticTarget - Where to report a diagnostic if the scalar is not recognized.
123
+ * @returns a string containing a TypeScript type that can represent the scalar
124
+ */
125
+ export function getJsScalar(program, scalar, diagnosticTarget) {
126
+ const scalars = getScalarsMap(program);
127
+ let _scalar = scalar;
128
+ while (_scalar !== undefined) {
129
+ const jsScalar = scalars.get(_scalar);
130
+ if (jsScalar !== undefined) {
131
+ return jsScalar;
132
+ }
133
+ _scalar = _scalar.baseScalar;
134
+ }
135
+ reportDiagnostic(program, {
136
+ code: "unrecognized-scalar",
137
+ target: diagnosticTarget,
138
+ format: {
139
+ scalar: getFullyQualifiedTypeName(scalar),
140
+ },
141
+ });
142
+ return "unknown";
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AC,IAAI,IAAI,aAAa,UAAU,IAAI,CAAC,IAAI,sBAAsB,CACrF,CAAC;QACJ,CAAC;IACH,CAAC;IAED,OAAO,IAAI,GAAG,CAAiB,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,EAAE,EAAE,CAAC,CAAC,IAAe,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;AAC/F,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,WAAW,CACzB,OAAgB,EAChB,MAAc,EACd,gBAAoD;IAEpD,MAAM,OAAO,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;IAEvC,IAAI,OAAO,GAAuB,MAAM,CAAC;IAEzC,OAAO,OAAO,KAAK,SAAS,EAAE,CAAC;QAC7B,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAEtC,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,OAAO,QAAQ,CAAC;QAClB,CAAC;QAED,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC;IAC/B,CAAC;IAED,gBAAgB,CAAC,OAAO,EAAE;QACxB,IAAI,EAAE,qBAAqB;QAC3B,MAAM,EAAE,gBAAgB;QACxB,MAAM,EAAE;YACN,MAAM,EAAE,yBAAyB,CAAC,MAAM,CAAC;SAC1C;KACF,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { Model, Scalar, Type, Union } from "@typespec/compiler";
2
+ import { JsContext } from "../../ctx.js";
3
+ export type SerializableType = Model | Scalar | Union;
4
+ export declare function isSerializableType(t: Type): t is SerializableType;
5
+ export type SerializationContentType = "application/json";
6
+ export declare function requireSerialization(ctx: JsContext, type: Type, contentType: SerializationContentType): void;
7
+ export interface SerializationContext extends JsContext {
8
+ }
9
+ export declare function emitSerialization(ctx: JsContext): void;
10
+ export declare function isSerializationRequired(ctx: JsContext, type: Type, serialization: SerializationContentType): boolean;
11
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/common/serialization/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAY,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAE,SAAS,EAAuC,MAAM,cAAc,CAAC;AAO9E,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,MAAM,GAAG,KAAK,CAAC;AAEtD,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,IAAI,gBAAgB,CAEjE;AAED,MAAM,MAAM,wBAAwB,GAAG,kBAAkB,CAAC;AAI1D,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,SAAS,EACd,IAAI,EAAE,IAAI,EACV,WAAW,EAAE,wBAAwB,GACpC,IAAI,CAeN;AAED,MAAM,WAAW,oBAAqB,SAAQ,SAAS;CAAG;AAE1D,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAsBtD;AAED,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,SAAS,EACd,IAAI,EAAE,IAAI,EACV,aAAa,EAAE,wBAAwB,GACtC,OAAO,CAQT"}
@@ -0,0 +1,72 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { NoTarget } from "@typespec/compiler";
4
+ import { completePendingDeclarations } from "../../ctx.js";
5
+ import { UnimplementedError } from "../../util/error.js";
6
+ import { indent } from "../../util/iter.js";
7
+ import { createOrGetModuleForNamespace } from "../namespace.js";
8
+ import { emitTypeReference } from "../reference.js";
9
+ import { emitJsonSerialization, requiresJsonSerialization } from "./json.js";
10
+ export function isSerializableType(t) {
11
+ return t.kind === "Model" || t.kind === "Scalar" || t.kind === "Union";
12
+ }
13
+ const _SERIALIZATIONS_MAP = new WeakMap();
14
+ export function requireSerialization(ctx, type, contentType) {
15
+ if (!isSerializableType(type)) {
16
+ throw new UnimplementedError(`no implementation of JSON serialization for type '${type.kind}'`);
17
+ }
18
+ let serializationsForType = _SERIALIZATIONS_MAP.get(type);
19
+ if (!serializationsForType) {
20
+ serializationsForType = new Set();
21
+ _SERIALIZATIONS_MAP.set(type, serializationsForType);
22
+ }
23
+ serializationsForType.add(contentType);
24
+ ctx.serializations.add(type);
25
+ }
26
+ export function emitSerialization(ctx) {
27
+ completePendingDeclarations(ctx);
28
+ const serializationContext = {
29
+ ...ctx,
30
+ };
31
+ while (!ctx.serializations.isEmpty()) {
32
+ const type = ctx.serializations.take();
33
+ const serializations = _SERIALIZATIONS_MAP.get(type);
34
+ const requiredSerializations = new Set([...serializations].filter((serialization) => isSerializationRequired(ctx, type, serialization)));
35
+ if (requiredSerializations.size > 0) {
36
+ emitSerializationsForType(serializationContext, type, serializations);
37
+ }
38
+ }
39
+ }
40
+ export function isSerializationRequired(ctx, type, serialization) {
41
+ switch (serialization) {
42
+ case "application/json": {
43
+ return requiresJsonSerialization(ctx, type);
44
+ }
45
+ default:
46
+ throw new Error(`Unreachable: serialization content type ${serialization}`);
47
+ }
48
+ }
49
+ function emitSerializationsForType(ctx, type, serializations) {
50
+ const isSynthetic = ctx.syntheticNames.has(type) || !type.namespace;
51
+ const module = isSynthetic
52
+ ? ctx.syntheticModule
53
+ : createOrGetModuleForNamespace(ctx, type.namespace);
54
+ const typeName = emitTypeReference(ctx, type, NoTarget, module);
55
+ const serializationCode = [`export const ${typeName} = {`];
56
+ for (const serialization of serializations) {
57
+ serializationCode.push(...indent(emitSerializationForType(ctx, type, serialization, module, typeName)));
58
+ }
59
+ serializationCode.push("} as const;");
60
+ module.declarations.push(serializationCode);
61
+ }
62
+ function* emitSerializationForType(ctx, type, contentType, module, typeName) {
63
+ switch (contentType) {
64
+ case "application/json": {
65
+ yield* emitJsonSerialization(ctx, type, module, typeName);
66
+ break;
67
+ }
68
+ default:
69
+ throw new Error(`Unreachable: serialization content type ${contentType}`);
70
+ }
71
+ }
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/common/serialization/index.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAElC,OAAO,EAAS,QAAQ,EAAuB,MAAM,oBAAoB,CAAC;AAC1E,OAAO,EAAqB,2BAA2B,EAAE,MAAM,cAAc,CAAC;AAC9E,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,6BAA6B,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,qBAAqB,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAI7E,MAAM,UAAU,kBAAkB,CAAC,CAAO;IACxC,OAAO,CAAC,CAAC,IAAI,KAAK,OAAO,IAAI,CAAC,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC;AACzE,CAAC;AAID,MAAM,mBAAmB,GAAG,IAAI,OAAO,EAAmD,CAAC;AAE3F,MAAM,UAAU,oBAAoB,CAClC,GAAc,EACd,IAAU,EACV,WAAqC;IAErC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,kBAAkB,CAAC,qDAAqD,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC;IAClG,CAAC;IAED,IAAI,qBAAqB,GAAG,mBAAmB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAE1D,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAC3B,qBAAqB,GAAG,IAAI,GAAG,EAAE,CAAC;QAClC,mBAAmB,CAAC,GAAG,CAAC,IAAI,EAAE,qBAAqB,CAAC,CAAC;IACvD,CAAC;IAED,qBA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I,CAAC,SAAS,CAAC;IAEpE,MAAM,MAAM,GAAG,WAAW;QACxB,CAAC,CAAC,GAAG,CAAC,eAAe;QACrB,CAAC,CAAC,6BAA6B,CAAC,GAAG,EAAE,IAAI,CAAC,SAAU,CAAC,CAAC;IAExD,MAAM,QAAQ,GAAG,iBAAiB,CAAC,GAAG,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,CAAC,CAAC;IAEhE,MAAM,iBAAiB,GAAG,CAAC,gBAAgB,QAAQ,MAAM,CAAC,CAAC;IAE3D,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;QAC3C,iBAAiB,CAAC,IAAI,CACpB,GAAG,MAAM,CAAC,wBAAwB,CAAC,GAAG,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC,CAChF,CAAC;IACJ,CAAC;IAED,iBAAiB,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IAEtC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;AAC9C,CAAC;AAED,QAAQ,CAAC,CAAC,wBAAwB,CAChC,GAAyB,EACzB,IAAsB,EACtB,WAAqC,EACrC,MAAc,EACd,QAAgB;IAEhB,QAAQ,WAAW,EAAE,CAAC;QACpB,KAAK,kBAAkB,CAAC,CAAC,CAAC;YACxB,KAAK,CAAC,CAAC,qBAAqB,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,CAAC;YAC1D,MAAM;QACR,CAAC;QACD;YACE,MAAM,IAAI,KAAK,CAAC,2CAA2C,WAA2B,EAAE,CAAC,CAAC;IAC9F,CAAC;AACH,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { Type } from "@typespec/compiler";
2
+ import { JsContext, Module } from "../../ctx.js";
3
+ import { SerializableType, SerializationContext } from "./index.js";
4
+ export declare function requiresJsonSerialization(ctx: JsContext, type: Type): boolean;
5
+ export declare function emitJsonSerialization(ctx: SerializationContext, type: SerializableType, module: Module, typeName: string): Iterable<string>;
6
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../../../src/common/serialization/json.ts"],"names":[],"mappings":"AAGA,OAAO,EAOL,IAAI,EAOL,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAOjD,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAwB,MAAM,YAAY,CAAC;AAO1F,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,IAAI,GAAG,OAAO,CA8C7E;AA6BD,wBAAiB,qBAAqB,CACpC,GAAG,EAAE,oBAAoB,EACzB,IAAI,EAAE,gBAAgB,EACtB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GACf,QAAQ,CAAC,MAAM,CAAC,CAQlB"}
@@ -0,0 +1,341 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { NoTarget, compilerAssert, getEncode, getProjectedName, isArrayModelType, isRecordModelType, resolveEncodedName, } from "@typespec/compiler";
4
+ import { getHeaderFieldOptions, getPathParamOptions, getQueryParamOptions } from "@typespec/http";
5
+ import { parseCase } from "../../util/case.js";
6
+ import { differentiateUnion, writeCodeTree } from "../../util/differentiate.js";
7
+ import { UnimplementedError } from "../../util/error.js";
8
+ import { indent } from "../../util/iter.js";
9
+ import { emitTypeReference, escapeUnsafeChars } from "../reference.js";
10
+ import { getJsScalar } from "../scalar.js";
11
+ import { requireSerialization } from "./index.js";
12
+ /**
13
+ * Memoization cache for requiresJsonSerialization.
14
+ */
15
+ const _REQUIRES_JSON_SERIALIZATION = new WeakMap();
16
+ export function requiresJsonSerialization(ctx, type) {
17
+ if (!isSerializable(type))
18
+ return false;
19
+ if (_REQUIRES_JSON_SERIALIZATION.has(type)) {
20
+ return _REQUIRES_JSON_SERIALIZATION.get(type);
21
+ }
22
+ // Assume the type is serializable until proven otherwise, in case this model is encountered recursively.
23
+ // This isn't an exactly correct algorithm, but in the recursive case it will at least produce something that
24
+ // is correct.
25
+ _REQUIRES_JSON_SERIALIZATION.set(type, true);
26
+ let requiresSerialization;
27
+ switch (type.kind) {
28
+ case "Model": {
29
+ if (isArrayModelType(ctx.program, type)) {
30
+ const argumentType = type.indexer.value;
31
+ requiresSerialization = requiresJsonSerialization(ctx, argumentType);
32
+ break;
33
+ }
34
+ requiresSerialization = [...type.properties.values()].some((property) => propertyRequiresJsonSerialization(ctx, property));
35
+ break;
36
+ }
37
+ case "Scalar": {
38
+ const scalar = getJsScalar(ctx.program, type, type);
39
+ requiresSerialization = scalar === "Uint8Array" || getEncode(ctx.program, type) !== undefined;
40
+ break;
41
+ }
42
+ case "Union": {
43
+ requiresSerialization = [...type.variants.values()].some((variant) => requiresJsonSerialization(ctx, variant));
44
+ break;
45
+ }
46
+ case "ModelProperty":
47
+ requiresSerialization = requiresJsonSerialization(ctx, type.type);
48
+ break;
49
+ }
50
+ _REQUIRES_JSON_SERIALIZATION.set(type, requiresSerialization);
51
+ return requiresSerialization;
52
+ }
53
+ function propertyRequiresJsonSerialization(ctx, property) {
54
+ return !!(isHttpMetadata(ctx, property) ||
55
+ getEncode(ctx.program, property) ||
56
+ resolveEncodedName(ctx.program, property, "application/json") !== property.name ||
57
+ getProjectedName(ctx.program, property, "json") ||
58
+ (isSerializable(property.type) && requiresJsonSerialization(ctx, property.type)));
59
+ }
60
+ function isHttpMetadata(ctx, property) {
61
+ return (getQueryParamOptions(ctx.program, property) !== undefined ||
62
+ getHeaderFieldOptions(ctx.program, property) !== undefined ||
63
+ getPathParamOptions(ctx.program, property) !== undefined);
64
+ }
65
+ function isSerializable(type) {
66
+ return (type.kind === "Model" ||
67
+ type.kind === "Scalar" ||
68
+ type.kind === "Union" ||
69
+ type.kind === "ModelProperty");
70
+ }
71
+ export function* emitJsonSerialization(ctx, type, module, typeName) {
72
+ yield `toJsonObject(input: ${typeName}): any {`;
73
+ yield* indent(emitToJson(ctx, type, module));
74
+ yield `},`;
75
+ yield `fromJsonObject(input: any): ${typeName} {`;
76
+ yield* indent(emitFromJson(ctx, type, module));
77
+ yield `},`;
78
+ }
79
+ function* emitToJson(ctx, type, module) {
80
+ switch (type.kind) {
81
+ case "Model": {
82
+ yield `return {`;
83
+ for (const property of type.properties.values()) {
84
+ const encodedName = getProjectedName(ctx.program, property, "json") ??
85
+ resolveEncodedName(ctx.program, property, "application/json") ??
86
+ property.name;
87
+ const expr = transposeExpressionToJson(ctx, property.type, `input.${property.name}`, module);
88
+ yield ` ${encodedName}: ${expr},`;
89
+ }
90
+ yield `};`;
91
+ return;
92
+ }
93
+ case "Scalar": {
94
+ yield `throw new Error("Unimplemented: scalar JSON serialization");`;
95
+ return;
96
+ }
97
+ case "Union": {
98
+ const codeTree = differentiateUnion(ctx, type);
99
+ yield* writeCodeTree(ctx, codeTree, {
100
+ subject: "input",
101
+ referenceModelProperty(p) {
102
+ return "input." + parseCase(p.name).camelCase;
103
+ },
104
+ renderResult(type) {
105
+ return [`return ${transposeExpressionToJson(ctx, type, "input", module)};`];
106
+ },
107
+ });
108
+ return;
109
+ }
110
+ }
111
+ }
112
+ function transposeExpressionToJson(ctx, type, expr, module) {
113
+ switch (type.kind) {
114
+ case "Model": {
115
+ if (isArrayModelType(ctx.program, type)) {
116
+ const argumentType = type.indexer.value;
117
+ if (requiresJsonSerialization(ctx, argumentType)) {
118
+ return `${expr}?.map((item) => ${transposeExpressionToJson(ctx, argumentType, "item", module)})`;
119
+ }
120
+ else {
121
+ return expr;
122
+ }
123
+ }
124
+ else if (isRecordModelType(ctx.program, type)) {
125
+ const argumentType = type.indexer.value;
126
+ if (requiresJsonSerialization(ctx, argumentType)) {
127
+ return `Object.fromEntries(Object.entries(${expr}).map(([key, value]) => [String(key), ${transposeExpressionToJson(ctx, argumentType, "value", module)}]))`;
128
+ }
129
+ else {
130
+ return expr;
131
+ }
132
+ }
133
+ else if (!requiresJsonSerialization(ctx, type)) {
134
+ return expr;
135
+ }
136
+ else {
137
+ requireSerialization(ctx, type, "application/json");
138
+ const typeReference = emitTypeReference(ctx, type, NoTarget, module);
139
+ return `${typeReference}.toJsonObject(${expr})`;
140
+ }
141
+ }
142
+ case "Scalar":
143
+ const scalar = getJsScalar(ctx.program, type, type);
144
+ switch (scalar) {
145
+ case "Uint8Array":
146
+ // Coerce to Buffer if we aren't given a buffer. This avoids having to do unholy things to
147
+ // convert through an intermediate and use globalThis.btoa. v8 does not support Uint8Array.toBase64
148
+ return `((${expr} instanceof Buffer) ? ${expr} : Buffer.from(${expr})).toString('base64')`;
149
+ default:
150
+ return expr;
151
+ }
152
+ case "Union":
153
+ if (!requiresJsonSerialization(ctx, type)) {
154
+ return expr;
155
+ }
156
+ else {
157
+ requireSerialization(ctx, type, "application/json");
158
+ const typeReference = emitTypeReference(ctx, type, NoTarget, module, {
159
+ altName: "WeirdUnion",
160
+ requireDeclaration: true,
161
+ });
162
+ return `${typeReference}.toJsonObject(${expr})`;
163
+ }
164
+ case "ModelProperty":
165
+ return transposeExpressionToJson(ctx, type.type, expr, module);
166
+ case "Intrinsic":
167
+ switch (type.name) {
168
+ case "void":
169
+ return "undefined";
170
+ case "null":
171
+ return "null";
172
+ case "ErrorType":
173
+ compilerAssert(false, "Encountered ErrorType in JSON serialization", type);
174
+ return expr;
175
+ case "never":
176
+ case "unknown":
177
+ default:
178
+ // Unhandled intrinsics will have been caught during type construction. We'll ignore this and
179
+ // just return the expr as-is.
180
+ return expr;
181
+ }
182
+ case "String":
183
+ case "Number":
184
+ case "Boolean":
185
+ return literalToExpr(type);
186
+ case "Interface":
187
+ case "Enum":
188
+ case "EnumMember":
189
+ case "TemplateParameter":
190
+ case "Namespace":
191
+ case "Operation":
192
+ case "StringTemplate":
193
+ case "StringTemplateSpan":
194
+ case "Tuple":
195
+ case "UnionVariant":
196
+ case "Function":
197
+ case "Decorator":
198
+ case "FunctionParameter":
199
+ case "Object":
200
+ case "Projection":
201
+ case "ScalarConstructor":
202
+ default:
203
+ throw new UnimplementedError(`transformJsonExprForType: ${type.kind}`);
204
+ }
205
+ }
206
+ function literalToExpr(type) {
207
+ switch (type.kind) {
208
+ case "String":
209
+ return escapeUnsafeChars(JSON.stringify(type.value));
210
+ case "Number":
211
+ case "Boolean":
212
+ return String(type.value);
213
+ }
214
+ }
215
+ function* emitFromJson(ctx, type, module) {
216
+ switch (type.kind) {
217
+ case "Model": {
218
+ yield `return {`;
219
+ for (const property of type.properties.values()) {
220
+ const encodedName = getProjectedName(ctx.program, property, "json") ??
221
+ resolveEncodedName(ctx.program, property, "application/json") ??
222
+ property.name;
223
+ const expr = transposeExpressionFromJson(ctx, property.type, `input["${encodedName}"]`, module);
224
+ yield ` ${property.name}: ${expr},`;
225
+ }
226
+ yield "};";
227
+ return;
228
+ }
229
+ case "Scalar": {
230
+ yield `throw new Error("Unimplemented: scalar JSON serialization");`;
231
+ return;
232
+ }
233
+ case "Union": {
234
+ const codeTree = differentiateUnion(ctx, type);
235
+ yield* writeCodeTree(ctx, codeTree, {
236
+ subject: "input",
237
+ referenceModelProperty(p) {
238
+ const jsonName = getProjectedName(ctx.program, p, "json") ??
239
+ resolveEncodedName(ctx.program, p, "application/json") ??
240
+ p.name;
241
+ return "input[" + JSON.stringify(jsonName) + "]";
242
+ },
243
+ renderResult(type) {
244
+ return [`return ${transposeExpressionFromJson(ctx, type, "input", module)};`];
245
+ },
246
+ });
247
+ return;
248
+ }
249
+ }
250
+ }
251
+ function transposeExpressionFromJson(ctx, type, expr, module) {
252
+ switch (type.kind) {
253
+ case "Model": {
254
+ if (isArrayModelType(ctx.program, type)) {
255
+ const argumentType = type.indexer.value;
256
+ if (requiresJsonSerialization(ctx, argumentType)) {
257
+ return `${expr}?.map((item: any) => ${transposeExpressionFromJson(ctx, argumentType, "item", module)})`;
258
+ }
259
+ else {
260
+ return expr;
261
+ }
262
+ }
263
+ else if (isRecordModelType(ctx.program, type)) {
264
+ const argumentType = type.indexer.value;
265
+ if (requiresJsonSerialization(ctx, argumentType)) {
266
+ return `Object.fromEntries(Object.entries(${expr}).map(([key, value]) => [key, ${transposeExpressionFromJson(ctx, argumentType, "value", module)}]))`;
267
+ }
268
+ else {
269
+ return expr;
270
+ }
271
+ }
272
+ else if (!requiresJsonSerialization(ctx, type)) {
273
+ return `${expr} as ${emitTypeReference(ctx, type, NoTarget, module)}`;
274
+ }
275
+ else {
276
+ requireSerialization(ctx, type, "application/json");
277
+ const typeReference = emitTypeReference(ctx, type, NoTarget, module);
278
+ return `${typeReference}.fromJsonObject(${expr})`;
279
+ }
280
+ }
281
+ case "Scalar":
282
+ const scalar = getJsScalar(ctx.program, type, type);
283
+ switch (scalar) {
284
+ case "Uint8Array":
285
+ return `Buffer.from(${expr}, 'base64')`;
286
+ default:
287
+ return expr;
288
+ }
289
+ case "Union":
290
+ if (!requiresJsonSerialization(ctx, type)) {
291
+ return expr;
292
+ }
293
+ else {
294
+ requireSerialization(ctx, type, "application/json");
295
+ const typeReference = emitTypeReference(ctx, type, NoTarget, module, {
296
+ altName: "WeirdUnion",
297
+ requireDeclaration: true,
298
+ });
299
+ return `${typeReference}.fromJsonObject(${expr})`;
300
+ }
301
+ case "ModelProperty":
302
+ return transposeExpressionFromJson(ctx, type.type, expr, module);
303
+ case "Intrinsic":
304
+ switch (type.name) {
305
+ case "ErrorType":
306
+ throw new Error("UNREACHABLE: ErrorType in JSON deserialization");
307
+ case "void":
308
+ return "undefined";
309
+ case "null":
310
+ return "null";
311
+ case "never":
312
+ case "unknown":
313
+ return expr;
314
+ default:
315
+ throw new Error(`Unreachable: intrinsic type ${type.name}`);
316
+ }
317
+ case "String":
318
+ case "Number":
319
+ case "Boolean":
320
+ return literalToExpr(type);
321
+ case "Interface":
322
+ case "Enum":
323
+ case "EnumMember":
324
+ case "TemplateParameter":
325
+ case "Namespace":
326
+ case "Operation":
327
+ case "StringTemplate":
328
+ case "StringTemplateSpan":
329
+ case "Tuple":
330
+ case "UnionVariant":
331
+ case "Function":
332
+ case "Decorator":
333
+ case "FunctionParameter":
334
+ case "Object":
335
+ case "Projection":
336
+ case "ScalarConstructor":
337
+ default:
338
+ throw new UnimplementedError(`transformJsonExprForType: ${type.kind}`);
339
+ }
340
+ }
341
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../../../../src/common/serialization/json.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAElC,OAAO,EAIL,QAAQ,EAIR,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAElG,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAA0C,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAE1F;;GAEG;AACH,MAAM,4BAA4B,GAAG,IAAI,OAAO,EAA6C,CAAC;AAE9F,MAAM,UAAU,yBAAyB,CAAC,GAAc,EAAE,IAAU;IAClE,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAExC,IAAI,4BAA4B,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QAC3C,OAAO,4BAA4B,CAAC,GAAG,CAAC,IAAI,CAAE,CAAC;IACjD,CAAC;I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ACd,MAAM,IAAI,KAAK,CAAC,gDAAgD,CAAC,CAAC;gBACpE,KAAK,MAAM;oBACT,OAAO,WAAW,CAAC;gBACrB,KAAK,MAAM;oBACT,OAAO,MAAM,CAAC;gBAChB,KAAK,OAAO,CAAC;gBACb,KAAK,SAAS;oBACZ,OAAO,IAAI,CAAC;gBACd;oBACE,MAAM,IAAI,KAAK,CACb,+BAAgC,IAAsC,CAAC,IAAI,EAAE,CAC9E,CAAC;YACN,CAAC;QACH,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ,CAAC;QACd,KAAK,SAAS;YACZ,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC;QAC7B,KAAK,WAAW,CAAC;QACjB,KAAK,MAAM,CAAC;QACZ,KAAK,YAAY,CAAC;QAClB,KAAK,mBAAmB,CAAC;QACzB,KAAK,WAAW,CAAC;QACjB,KAAK,WAAW,CAAC;QACjB,KAAK,gBAAgB,CAAC;QACtB,KAAK,oBAAoB,CAAC;QAC1B,KAAK,OAAO,CAAC;QACb,KAAK,cAAc,CAAC;QACpB,KAAK,UAAU,CAAC;QAChB,KAAK,WAAW,CAAC;QACjB,KAAK,mBAAmB,CAAC;QACzB,KAAK,QAAQ,CAAC;QACd,KAAK,YAAY,CAAC;QAClB,KAAK,mBAAmB,CAAC;QACzB;YACE,MAAM,IAAI,kBAAkB,CAAC,6BAA6B,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IAC3E,CAAC;AACH,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { Union, UnionVariant } from "@typespec/compiler";
2
+ import { JsContext, Module, PartialUnionSynthetic } from "../ctx.js";
3
+ /**
4
+ * Emit an inline union type. This will automatically import any referenced types that are part of the union.
5
+ *
6
+ * @param ctx - The emitter context.
7
+ * @param variants - The variants of the union.
8
+ * @param module - The module that this union is written into.
9
+ * @returns a string that can be used as a type reference
10
+ */
11
+ export declare function emitUnionType(ctx: JsContext, variants: UnionVariant[], module: Module): string;
12
+ /**
13
+ * Emits a union type declaration as an alias.
14
+ *
15
+ * This is rare in TypeScript, but may occur in some niche cases where an alias is desirable.
16
+ *
17
+ * @param ctx - The emitter context.
18
+ * @param union - The union to emit.
19
+ * @param module - The module that this union declaration is written into.
20
+ * @param altName - An alternative name to use for the union if it is not named.
21
+ */
22
+ export declare function emitUnion(ctx: JsContext, union: Union | PartialUnionSynthetic, module: Module, altName?: string): Iterable<string>;
23
+ //# sourceMappingURL=union.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../../src/common/union.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAKrE;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAoB9F;AAED;;;;;;;;;GASG;AACH,wBAAiB,SAAS,CACxB,GAAG,EAAE,SAAS,EACd,KAAK,EAAE,KAAK,GAAG,qBAAqB,EACpC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,GACf,QAAQ,CAAC,MAAM,CAAC,CAqBlB"}