@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,184 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { getNamespaceFullName } from "@typespec/compiler";
4
+ import { createModule, isModule, } from "../ctx.js";
5
+ import { parseCase } from "../util/case.js";
6
+ import { UnimplementedError } from "../util/error.js";
7
+ import { cat, indent, isIterable } from "../util/iter.js";
8
+ import { emitOperationGroup } from "./interface.js";
9
+ /**
10
+ * Enqueue all declarations in the namespace to be included in the emit, recursively.
11
+ *
12
+ * @param ctx - The emitter context.
13
+ * @param namespace - The root namespace to begin traversing.
14
+ */
15
+ export function visitAllTypes(ctx, namespace) {
16
+ const { enums, interfaces, models, unions, namespaces, scalars, operations } = namespace;
17
+ for (const type of cat(enums.values(), interfaces.values(), models.values(), unions.values(), scalars.values())) {
18
+ ctx.typeQueue.add(type);
19
+ }
20
+ for (const ns of namespaces.values()) {
21
+ visitAllTypes(ctx, ns);
22
+ }
23
+ if (operations.size > 0) {
24
+ // If the operation has any floating operations in it, we will synthesize an interface for them in the parent module.
25
+ // This requires some special handling by other parts of the emitter to ensure that the interface for a namespace's
26
+ // own operations is properly imported.
27
+ if (!namespace.namespace) {
28
+ throw new UnimplementedError("no parent namespace in visitAllTypes");
29
+ }
30
+ const parentModule = createOrGetModuleForNamespace(ctx, namespace.namespace);
31
+ parentModule.declarations.push([
32
+ // prettier-ignore
33
+ `/** An interface representing the operations defined in the '${getNamespaceFullName(namespace)}' namespace. */`,
34
+ `export interface ${parseCase(namespace.name).pascalCase}<Context = unknown> {`,
35
+ ...indent(emitOperationGroup(ctx, operations.values(), parentModule)),
36
+ "}",
37
+ ]);
38
+ }
39
+ }
40
+ /**
41
+ * Create a module for a namespace, or get an existing module if one has already been created.
42
+ *
43
+ * @param ctx - The emitter context.
44
+ * @param namespace - The namespace to create a module for.
45
+ * @returns the module for the namespace.
46
+ */
47
+ export function createOrGetModuleForNamespace(ctx, namespace, root = ctx.globalNamespaceModule) {
48
+ if (ctx.namespaceModules.has(namespace)) {
49
+ return ctx.namespaceModules.get(namespace);
50
+ }
51
+ if (!namespace.namespace) {
52
+ throw new Error("UNREACHABLE: no parent namespace in createOrGetModuleForNamespace");
53
+ }
54
+ const parent = namespace.namespace === ctx.globalNamespace
55
+ ? root
56
+ : createOrGetModuleForNamespace(ctx, namespace.namespace);
57
+ const name = namespace.name === "TypeSpec" ? "typespec" : parseCase(namespace.name).kebabCase;
58
+ const module = createModule(name, parent, namespace);
59
+ ctx.namespaceModules.set(namespace, module);
60
+ return module;
61
+ }
62
+ /**
63
+ * Get a reference to the interface representing the namespace's floating operations.
64
+ *
65
+ * This does not check that such an interface actually exists, so it should only be called in situations where it is
66
+ * known to exist (for example, if an operation comes from the namespace).
67
+ *
68
+ * @param ctx - The emitter context.
69
+ * @param namespace - The namespace to get the interface reference for.
70
+ * @param module - The module the the reference will be written to.
71
+ */
72
+ export function emitNamespaceInterfaceReference(ctx, namespace, module) {
73
+ if (!namespace.namespace) {
74
+ throw new Error("UNREACHABLE: no parent namespace in emitNamespaceInterfaceReference");
75
+ }
76
+ const namespaceName = parseCase(namespace.name).pascalCase;
77
+ module.imports.push({
78
+ binder: [namespaceName],
79
+ from: createOrGetModuleForNamespace(ctx, namespace.namespace),
80
+ });
81
+ return namespaceName;
82
+ }
83
+ /**
84
+ * Emits a single declaration within a module. If the declaration is a module, it is enqueued for later processing.
85
+ *
86
+ * @param ctx - The emitter context.
87
+ * @param decl - The declaration to emit.
88
+ * @param queue - The queue to add the declaration to if it is a module.
89
+ */
90
+ function* emitModuleBodyDeclaration(ctx, decl, queue) {
91
+ if (isIterable(decl)) {
92
+ yield* decl;
93
+ }
94
+ else if (typeof decl === "string") {
95
+ yield* decl.split(/\r?\n/);
96
+ }
97
+ else {
98
+ if (decl.declarations.length > 0) {
99
+ queue.add(decl);
100
+ }
101
+ }
102
+ }
103
+ /**
104
+ * Gets a file path from a given module to another module.
105
+ */
106
+ function computeRelativeFilePath(from, to) {
107
+ const fromIsIndex = from.declarations.some((d) => isModule(d));
108
+ const toIsIndex = to.declarations.some((d) => isModule(d));
109
+ const relativePath = (fromIsIndex ? from.cursor : from.cursor.parent).relativePath(to.cursor);
110
+ if (relativePath.length === 0 && !toIsIndex)
111
+ throw new Error("UNREACHABLE: relativePath returned no fragments");
112
+ if (relativePath.length === 0)
113
+ return "./index.js";
114
+ const prefix = relativePath[0] === ".." ? "" : "./";
115
+ const suffix = toIsIndex ? "/index.js" : ".js";
116
+ return prefix + relativePath.join("/") + suffix;
117
+ }
118
+ /**
119
+ * Deduplicates, consolidates, and writes the import statements for a module.
120
+ */
121
+ function* writeImportsNormalized(ctx, module) {
122
+ const allTargets = new Set();
123
+ const importMap = new Map();
124
+ const starAsMap = new Map();
125
+ const extraStarAs = [];
126
+ for (const _import of module.imports) {
127
+ // check for same module and continue
128
+ if (_import.from === module)
129
+ continue;
130
+ const target = typeof _import.from === "string"
131
+ ? _import.from
132
+ : computeRelativeFilePath(module, _import.from);
133
+ allTargets.add(target);
134
+ if (typeof _import.binder === "string") {
135
+ if (starAsMap.has(target)) {
136
+ extraStarAs.push([_import.binder, target]);
137
+ }
138
+ else {
139
+ starAsMap.set(target, _import.binder);
140
+ }
141
+ }
142
+ else {
143
+ const binders = importMap.get(target) ?? new Set();
144
+ for (const binder of _import.binder) {
145
+ binders.add(binder);
146
+ }
147
+ importMap.set(target, binders);
148
+ }
149
+ }
150
+ for (const target of allTargets) {
151
+ const binders = importMap.get(target);
152
+ const starAs = starAsMap.get(target);
153
+ if (binders && starAs) {
154
+ yield `import ${starAs}, { ${[...binders].join(", ")} } from "${target}";`;
155
+ }
156
+ else if (binders) {
157
+ yield `import { ${[...binders].join(", ")} } from "${target}";`;
158
+ }
159
+ else if (starAs) {
160
+ yield `import ${starAs} from "${target}";`;
161
+ }
162
+ yield "";
163
+ }
164
+ for (const [binder, target] of extraStarAs) {
165
+ yield `import ${binder} from "${target}";`;
166
+ }
167
+ }
168
+ /**
169
+ * Emits the body of a module file.
170
+ *
171
+ * @param ctx - The emitter context.
172
+ * @param module - The module to emit.
173
+ * @param queue - The queue to add any submodules to for later processing.
174
+ */
175
+ export function* emitModuleBody(ctx, module, queue) {
176
+ yield* writeImportsNormalized(ctx, module);
177
+ if (module.imports.length > 0)
178
+ yield "";
179
+ for (const decl of module.declarations) {
180
+ yield* emitModuleBodyDeclaration(ctx, decl, queue);
181
+ yield "";
182
+ }
183
+ }
184
+ //# sourceMappingURL=namespace.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace.js","sourceRoot":"","sources":["../../../src/common/namespace.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAElC,OAAO,EAAa,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AACrE,OAAO,EAKL,YAAY,EACZ,QAAQ,GACT,MAAM,WAAW,CAAC;AACnB,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAE1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,GAAc,EAAE,SAAoB;IAChE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,SAAS,CAAC;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;QACnD,MAAM,EAAE,CAAC;IACX,CAAC;AACH,CAAC"}
@@ -0,0 +1,46 @@
1
+ import { DiagnosticTarget, IntrinsicType, LiteralType, Namespace, NoTarget, Type } from "@typespec/compiler";
2
+ import { JsContext, Module } from "../ctx.js";
3
+ export type NamespacedType = Extract<Type, {
4
+ namespace?: Namespace;
5
+ }>;
6
+ /**
7
+ * Options for emitting a type reference.
8
+ */
9
+ export interface EmitTypeReferenceOptions {
10
+ /**
11
+ * An optional alternative name to use for the type if it is not named.
12
+ */
13
+ altName?: string;
14
+ /**
15
+ * Require a declaration for types that may be represented anonymously.
16
+ */
17
+ requireDeclaration?: boolean;
18
+ }
19
+ /**
20
+ * Emits a reference to a host type.
21
+ *
22
+ * This function will automatically ensure that the referenced type is included in the emit graph, and will import the
23
+ * type into the current module if necessary.
24
+ *
25
+ * Optionally, a `preferredAlternativeName` may be supplied. This alternative name will be used if a declaration is
26
+ * required, but the type is anonymous. The alternative name can only be set once. If two callers provide different
27
+ * alternative names for the same anonymous type, the first one is used in all cases. If a declaration _is_ required,
28
+ * and no alternative name is supplied (or has been supplied in a prior call to `emitTypeReference`), this function will
29
+ * throw an error. Callers must be sure to provide an alternative name if the type _may_ have an unknown name. However,
30
+ * callers may know that they have previously emitted a reference to the type and provided an alternative name in that
31
+ * call, in which case the alternative name may be safely omitted.
32
+ *
33
+ * @param ctx - The emitter context.
34
+ * @param type - The type to emit a reference to.
35
+ * @param position - The syntactic position of the reference, for diagnostics.
36
+ * @param module - The module that the reference is being emitted into.
37
+ * @param preferredAlternativeName - An optional alternative name to use for the type if it is not named.
38
+ * @returns a string containing a reference to the TypeScript type that represents the given TypeSpec type.
39
+ */
40
+ export declare function emitTypeReference(ctx: JsContext, type: Type, position: DiagnosticTarget | typeof NoTarget, module: Module, options?: EmitTypeReferenceOptions): string;
41
+ export declare function escapeUnsafeChars(s: string): string;
42
+ export type JsTypeSpecLiteralType = LiteralType | (IntrinsicType & {
43
+ name: "null";
44
+ });
45
+ export declare function isValueLiteralType(t: Type): t is JsTypeSpecLiteralType;
46
+ //# sourceMappingURL=reference.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reference.d.ts","sourceRoot":"","sources":["../../../src/common/reference.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,WAAW,EACX,SAAS,EACT,QAAQ,EACR,IAAI,EAKL,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,SAAS,EAAE,MAAM,EAAoB,MAAM,WAAW,CAAC;AAShE,MAAM,MAAM,cAAc,GAAG,OAAO,CAAC,IAAI,EAAE;IAAE,SAAS,CAAC,EAAE,SAAS,CAAA;CAAE,CAAC,CAAC;AAEtE;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,SAAS,EACd,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,gBAAgB,GAAG,OAAO,QAAQ,EAC5C,MAAM,EAAE,MAAM,EACd,OAAO,GAAE,wBAA6B,GACrC,MAAM,CAyNR;AAgBD,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,UAE1C;AAED,MAAM,MAAM,qBAAqB,GAAG,WAAW,GAAG,CAAC,aAAa,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAErF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,IAAI,GAAG,CAAC,IAAI,qBAAqB,CAWtE"}
@@ -0,0 +1,243 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { NoTarget, compilerAssert, getEffectiveModelType, getFriendlyName, isArrayModelType, } from "@typespec/compiler";
4
+ import { isImportableType } from "../ctx.js";
5
+ import { reportDiagnostic } from "../lib.js";
6
+ import { parseCase } from "../util/case.js";
7
+ import { asArrayType, getArrayElementName } from "../util/pluralism.js";
8
+ import { emitModelLiteral, emitWellKnownModel, isWellKnownModel } from "./model.js";
9
+ import { createOrGetModuleForNamespace } from "./namespace.js";
10
+ import { getJsScalar } from "./scalar.js";
11
+ import { emitUnionType } from "./union.js";
12
+ /**
13
+ * Emits a reference to a host type.
14
+ *
15
+ * This function will automatically ensure that the referenced type is included in the emit graph, and will import the
16
+ * type into the current module if necessary.
17
+ *
18
+ * Optionally, a `preferredAlternativeName` may be supplied. This alternative name will be used if a declaration is
19
+ * required, but the type is anonymous. The alternative name can only be set once. If two callers provide different
20
+ * alternative names for the same anonymous type, the first one is used in all cases. If a declaration _is_ required,
21
+ * and no alternative name is supplied (or has been supplied in a prior call to `emitTypeReference`), this function will
22
+ * throw an error. Callers must be sure to provide an alternative name if the type _may_ have an unknown name. However,
23
+ * callers may know that they have previously emitted a reference to the type and provided an alternative name in that
24
+ * call, in which case the alternative name may be safely omitted.
25
+ *
26
+ * @param ctx - The emitter context.
27
+ * @param type - The type to emit a reference to.
28
+ * @param position - The syntactic position of the reference, for diagnostics.
29
+ * @param module - The module that the reference is being emitted into.
30
+ * @param preferredAlternativeName - An optional alternative name to use for the type if it is not named.
31
+ * @returns a string containing a reference to the TypeScript type that represents the given TypeSpec type.
32
+ */
33
+ export function emitTypeReference(ctx, type, position, module, options = {}) {
34
+ switch (type.kind) {
35
+ case "Scalar":
36
+ // Get the scalar and return it directly, as it is a primitive.
37
+ return getJsScalar(ctx.program, type, position);
38
+ case "Model": {
39
+ // First handle arrays.
40
+ if (isArrayModelType(ctx.program, type)) {
41
+ const argumentType = type.indexer.value;
42
+ const argTypeReference = emitTypeReference(ctx, argumentType, position, module, {
43
+ altName: options.altName && getArrayElementName(options.altName),
44
+ });
45
+ if (isImportableType(ctx, argumentType) && argumentType.namespace) {
46
+ module.imports.push({
47
+ binder: [argTypeReference],
48
+ from: createOrGetModuleForNamespace(ctx, argumentType.namespace),
49
+ });
50
+ }
51
+ return asArrayType(argTypeReference);
52
+ }
53
+ // Now other well-known models.
54
+ if (isWellKnownModel(ctx, type)) {
55
+ return emitWellKnownModel(ctx, type, module, options.altName);
56
+ }
57
+ // Try to reduce the model to an effective model if possible.
58
+ const effectiveModel = getEffectiveModelType(ctx.program, type);
59
+ if (effectiveModel.name === "") {
60
+ // We might have seen the model before and synthesized a declaration for it already.
61
+ if (ctx.syntheticNames.has(effectiveModel)) {
62
+ const name = ctx.syntheticNames.get(effectiveModel);
63
+ module.imports.push({
64
+ binder: [name],
65
+ from: ctx.syntheticModule,
66
+ });
67
+ return name;
68
+ }
69
+ // Require preferredAlternativeName at this point, as we have an anonymous model that we have not visited.
70
+ if (!options.altName) {
71
+ return emitModelLiteral(ctx, effectiveModel, module);
72
+ }
73
+ // Anonymous model, synthesize a new model with the preferredName
74
+ ctx.synthetics.push({
75
+ kind: "anonymous",
76
+ name: options.altName,
77
+ underlying: effectiveModel,
78
+ });
79
+ module.imports.push({
80
+ binder: [options.altName],
81
+ from: ctx.syntheticModule,
82
+ });
83
+ ctx.syntheticNames.set(effectiveModel, options.altName);
84
+ return options.altName;
85
+ }
86
+ else {
87
+ // The effective model is good for a declaration, so enqueue it.
88
+ ctx.typeQueue.add(effectiveModel);
89
+ }
90
+ const friendlyName = getFriendlyName(ctx.program, effectiveModel);
91
+ // The model may be a template instance, so we generate a name for it.
92
+ const templatedName = parseCase(friendlyName
93
+ ? friendlyName
94
+ : effectiveModel.templateMapper
95
+ ? effectiveModel
96
+ .templateMapper.args.map((a) => ("name" in a ? String(a.name) : ""))
97
+ .join("_") + effectiveModel.name
98
+ : effectiveModel.name);
99
+ if (!effectiveModel.namespace) {
100
+ throw new Error("UNREACHABLE: no parent namespace of named model in emitTypeReference");
101
+ }
102
+ const parentModule = createOrGetModuleForNamespace(ctx, effectiveModel.namespace);
103
+ module.imports.push({
104
+ binder: [templatedName.pascalCase],
105
+ from: parentModule,
106
+ });
107
+ return templatedName.pascalCase;
108
+ }
109
+ case "Union": {
110
+ if (type.variants.size === 0)
111
+ return "never";
112
+ else if (type.variants.size === 1)
113
+ return emitTypeReference(ctx, [...type.variants.values()][0], position, module, options);
114
+ if (options.requireDeclaration) {
115
+ if (type.name) {
116
+ const nameCase = parseCase(type.name);
117
+ ctx.typeQueue.add(type);
118
+ module.imports.push({
119
+ binder: [nameCase.pascalCase],
120
+ from: createOrGetModuleForNamespace(ctx, type.namespace),
121
+ });
122
+ return type.name;
123
+ }
124
+ else {
125
+ const existingSyntheticName = ctx.syntheticNames.get(type);
126
+ if (existingSyntheticName) {
127
+ module.imports.push({
128
+ binder: [existingSyntheticName],
129
+ from: ctx.syntheticModule,
130
+ });
131
+ return existingSyntheticName;
132
+ }
133
+ else {
134
+ const altName = options.altName;
135
+ if (!altName) {
136
+ throw new Error("UNREACHABLE: anonymous union without preferredAlternativeName");
137
+ }
138
+ ctx.synthetics.push({
139
+ kind: "anonymous",
140
+ name: altName,
141
+ underlying: type,
142
+ });
143
+ module.imports.push({
144
+ binder: [altName],
145
+ from: ctx.syntheticModule,
146
+ });
147
+ ctx.syntheticNames.set(type, altName);
148
+ return altName;
149
+ }
150
+ }
151
+ }
152
+ else {
153
+ return emitUnionType(ctx, [...type.variants.values()], module);
154
+ }
155
+ }
156
+ case "Enum": {
157
+ ctx.typeQueue.add(type);
158
+ const name = parseCase(type.name).pascalCase;
159
+ module.imports.push({
160
+ binder: [name],
161
+ from: createOrGetModuleForNamespace(ctx, type.namespace),
162
+ });
163
+ return name;
164
+ }
165
+ case "String":
166
+ return escapeUnsafeChars(JSON.stringify(type.value));
167
+ case "Number":
168
+ case "Boolean":
169
+ return String(type.value);
170
+ case "Intrinsic":
171
+ switch (type.name) {
172
+ case "never":
173
+ return "never";
174
+ case "null":
175
+ return "null";
176
+ case "void":
177
+ // It's a bit strange to have a void property, but it's possible, and TypeScript allows it. Void is simply
178
+ // only assignable from undefined or void itself.
179
+ return "void";
180
+ case "ErrorType":
181
+ compilerAssert(false, "ErrorType should not be encountered in emitTypeReference", position === NoTarget ? type : position);
182
+ return "unknown";
183
+ case "unknown":
184
+ return "unknown";
185
+ default:
186
+ reportDiagnostic(ctx.program, {
187
+ code: "unrecognized-intrinsic",
188
+ format: { intrinsic: type.name },
189
+ target: position,
190
+ });
191
+ return "unknown";
192
+ }
193
+ case "Interface": {
194
+ if (type.namespace === undefined) {
195
+ throw new Error("UNREACHABLE: unparented interface");
196
+ }
197
+ const typeName = parseCase(type.name).pascalCase;
198
+ ctx.typeQueue.add(type);
199
+ const parentModule = createOrGetModuleForNamespace(ctx, type.namespace);
200
+ module.imports.push({
201
+ binder: [typeName],
202
+ from: parentModule,
203
+ });
204
+ return typeName;
205
+ }
206
+ case "ModelProperty": {
207
+ // Forward to underlying type.
208
+ return emitTypeReference(ctx, type.type, position, module, options);
209
+ }
210
+ default:
211
+ throw new Error(`UNREACHABLE: ${type.kind}`);
212
+ }
213
+ }
214
+ const UNSAFE_CHAR_MAP = {
215
+ "<": "\\u003C",
216
+ ">": "\\u003E",
217
+ "/": "\\u002F",
218
+ "\\": "\\\\",
219
+ "\b": "\\b",
220
+ "\f": "\\f",
221
+ "\n": "\\n",
222
+ "\r": "\\r",
223
+ "\t": "\\t",
224
+ "\0": "\\0",
225
+ "\u2028": "\\u2028",
226
+ "\u2029": "\\u2029",
227
+ };
228
+ export function escapeUnsafeChars(s) {
229
+ return s.replace(/[<>/\\\b\f\n\r\t\0\u2028\u2029]/g, (x) => UNSAFE_CHAR_MAP[x]);
230
+ }
231
+ export function isValueLiteralType(t) {
232
+ switch (t.kind) {
233
+ case "String":
234
+ case "Number":
235
+ case "Boolean":
236
+ return true;
237
+ case "Intrinsic":
238
+ return t.name === "null";
239
+ default:
240
+ return false;
241
+ }
242
+ }
243
+ //# sourceMappingURL=reference.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reference.js","sourceRoot":"","sources":["../../../src/common/reference.ts"],"names":[],"mappings":"AAAA,sCAAsC;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C,CAAC;YAEH,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,KAAK,eAAe,CAAC,CAAC,CAAC;YACrB,8BAA8B;YAC9B,OAAO,iBAAiB,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QACtE,CAAC;QACD;YACE,MAAM,IAAI,KAAK,CAAC,gBAAgB,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;IACjD,CAAC;AACH,CAAC;AACD,MAAM,eAAe,GAA4B;IAC/C,GAAG,EAAE,SAAS;IACd,GAAG,EAAE,SAAS;IACd,GAAG,EAAE,SAAS;IACd,IAAI,EAAE,MAAM;IACZ,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,IAAI,EAAE,KAAK;IACX,QAAQ,EAAE,SAAS;IACnB,QAAQ,EAAE,SAAS;CACpB,CAAC;AAEF,MAAM,UAAU,iBAAiB,CAAC,CAAS;IACzC,OAAO,CAAC,CAAC,OAAO,CAAC,kCAAkC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC,CAAC;AAClF,CAAC;AAID,MAAM,UAAU,kBAAkB,CAAC,CAAO;IACxC,QAAQ,CAAC,CAAC,IAAI,EAAE,CAAC;QACf,KAAK,QAAQ,CAAC;QACd,KAAK,QAAQ,CAAC;QACd,KAAK,SAAS;YACZ,OAAO,IAAI,CAAC;QACd,KAAK,WAAW;YACd,OAAO,CAAC,CAAC,IAAI,KAAK,MAAM,CAAC;QAC3B;YACE,OAAO,KAAK,CAAC;IACjB,CAAC;AACH,CAAC"}
@@ -0,0 +1,50 @@
1
+ import { DiagnosticTarget, NoTarget, Program, Scalar } from "@typespec/compiler";
2
+ import { JsContext } from "../ctx.js";
3
+ /**
4
+ * Emits a declaration for a scalar type.
5
+ *
6
+ * This is rare in TypeScript, as the scalar will ordinarily be used inline, but may be desirable in some cases.
7
+ *
8
+ * @param ctx - The emitter context.
9
+ * @param scalar - The scalar to emit.
10
+ * @returns a string that declares an alias to the scalar type in TypeScript.
11
+ */
12
+ export declare function emitScalar(ctx: JsContext, scalar: Scalar): string;
13
+ /**
14
+ * Get the string parsing template for a given scalar.
15
+ *
16
+ * It is common that a scalar type is encoded as a string. For example, in HTTP path parameters or query parameters
17
+ * where the value may be an integer, but the APIs expose it as a string. In such cases the parse template may be
18
+ * used to coerce the string value to the correct scalar type.
19
+ *
20
+ * The result of this function contains the string "{}" exactly once, which should be replaced with the text of an
21
+ * expression evaluating to the string representation of the scalar.
22
+ *
23
+ * For example, scalars that are represented by JS `number` are parsed with the template `Number({})`, which will
24
+ * convert the string to a number.
25
+ *
26
+ * @param ctx - The emitter context.
27
+ * @param scalar - The scalar to parse from a string
28
+ * @returns a template expression string that can be used to parse a string into the scalar type.
29
+ */
30
+ export declare function parseTemplateForScalar(ctx: JsContext, scalar: Scalar): string;
31
+ /**
32
+ * Get the string encoding template for a given scalar.
33
+ * @param ctx
34
+ * @param scalar
35
+ */
36
+ export declare function encodeTemplateForScalar(ctx: JsContext, scalar: Scalar): string;
37
+ /**
38
+ * Gets a TypeScript type that can represent a given TypeSpec scalar.
39
+ *
40
+ * Scalar recognition is recursive. If a scalar is not recognized, we will treat it as its parent scalar and try again.
41
+ *
42
+ * If no scalar in the chain is recognized, it will be treated as `unknown` and a warning will be issued.
43
+ *
44
+ * @param program - The program that contains the scalar
45
+ * @param scalar - The scalar to get the TypeScript type for
46
+ * @param diagnosticTarget - Where to report a diagnostic if the scalar is not recognized.
47
+ * @returns a string containing a TypeScript type that can represent the scalar
48
+ */
49
+ export declare function getJsScalar(program: Program, scalar: Scalar, diagnosticTarget: DiagnosticTarget | typeof NoTarget): string;
50
+ //# sourceMappingURL=scalar.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scalar.d.ts","sourceRoot":"","sources":["../../../src/common/scalar.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAoB,MAAM,oBAAoB,CAAC;AACnG,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAMtC;;;;;;;;GAQG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAMjE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAe7E;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAe9E;AAmDD;;;;;;;;;;;GAWG;AACH,wBAAgB,WAAW,CACzB,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,QAAQ,GACnD,MAAM,CAwBR"}