@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,57 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { parseCase } from "../util/case.js";
4
+ import { emitDocumentation } from "./documentation.js";
5
+ import { emitTypeReference } from "./reference.js";
6
+ /**
7
+ * Emit an inline union type. This will automatically import any referenced types that are part of the union.
8
+ *
9
+ * @param ctx - The emitter context.
10
+ * @param variants - The variants of the union.
11
+ * @param module - The module that this union is written into.
12
+ * @returns a string that can be used as a type reference
13
+ */
14
+ export function emitUnionType(ctx, variants, module) {
15
+ // Treat empty unions as never so that we always return a good type reference here.
16
+ if (variants.length === 0)
17
+ return "never";
18
+ const variantTypes = [];
19
+ for (const [_, v] of variants.entries()) {
20
+ const name = emitTypeReference(ctx, v.type, v, module);
21
+ variantTypes.push(name);
22
+ // if (isImportableType(ctx, v.type)) {
23
+ // module.imports.push({
24
+ // binder: [name],
25
+ // from: createOrGetModuleForNamespace(ctx, v.type.namespace!),
26
+ // });
27
+ // }
28
+ }
29
+ return variantTypes.join(" | ");
30
+ }
31
+ /**
32
+ * Emits a union type declaration as an alias.
33
+ *
34
+ * This is rare in TypeScript, but may occur in some niche cases where an alias is desirable.
35
+ *
36
+ * @param ctx - The emitter context.
37
+ * @param union - The union to emit.
38
+ * @param module - The module that this union declaration is written into.
39
+ * @param altName - An alternative name to use for the union if it is not named.
40
+ */
41
+ export function* emitUnion(ctx, union, module, altName) {
42
+ const name = union.name ? parseCase(union.name).pascalCase : altName;
43
+ const isPartialSynthetic = union.kind === "partialUnion";
44
+ if (name === undefined) {
45
+ throw new Error("Internal Error: Union name is undefined");
46
+ }
47
+ if (!isPartialSynthetic)
48
+ yield* emitDocumentation(ctx, union);
49
+ const variants = isPartialSynthetic
50
+ ? union.variants.map((v) => [v.name, v])
51
+ : union.variants.entries();
52
+ const variantTypes = [...variants].map(([_, v]) => emitTypeReference(ctx, v.type, v, module, {
53
+ altName: name + parseCase(String(v.name)).pascalCase,
54
+ }));
55
+ yield `export type ${name} = ${variantTypes.join(" | ")};`;
56
+ }
57
+ //# sourceMappingURL=union.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"union.js","sourceRoot":"","sources":["../../../src/common/union.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAIlC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAEnD;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,GAAc,EAAE,QAAwB,EAAE,MAAc;IACpF,mFAAmF;IACnF,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,OAAO,CAAC;IAE1C,MAAM,YAAY,GAAa,EAAE,CAAC;IAElC,KAAK,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,IAAI,QAAQ,CAAC,OAAO,EAAE,EAAE,CAAC;QACxC,MAAM,IAAI,GAAG,iBAAiB,CAAC,GAAG,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,MAAM,CAAC,CAAC;QAEvD,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAExB,uCAAuC;QACvC,0BAA0B;QAC1B,sBAAsB;QACtB,mEAAmE;QACnE,QAAQ;QACR,IAAI;IACN,CAAC;IAED,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;AAClC,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ec/compiler";
2
+ import { SerializableType } from "./common/serialization/index.js";
3
+ import { JsEmitterOptions } from "./lib.js";
4
+ import { OnceQueue } from "./util/once-queue.js";
5
+ export type DeclarationType = Model | Enum | Union | Interface | Scalar;
6
+ /**
7
+ * Determines whether or not a type is importable into a JavaScript module.
8
+ *
9
+ * i.e. whether or not it is declared as a named symbol within the module.
10
+ *
11
+ * In TypeScript, unions are rendered inline, so they are not ordinarily
12
+ * considered importable.
13
+ *
14
+ * @param ctx - The JS emitter context.
15
+ * @param t - the type to test
16
+ * @returns `true` if the type is an importable declaration, `false` otherwise.
17
+ */
18
+ export declare function isImportableType(ctx: JsContext, t: Type): t is DeclarationType;
19
+ /**
20
+ * Stores stateful information consumed and modified by the JavaScript server
21
+ * emitter.
22
+ */
23
+ export interface JsContext {
24
+ /**
25
+ * The TypeSpec Program that this emitter instance operates over.
26
+ */
27
+ program: Program;
28
+ /**
29
+ * The emitter options.
30
+ */
31
+ options: JsEmitterOptions;
32
+ /**
33
+ * The global (root) namespace of the program.
34
+ */
35
+ globalNamespace: Namespace;
36
+ /**
37
+ * The service definition to use for emit.
38
+ */
39
+ service: Service;
40
+ /**
41
+ * A queue of all types to be included in the emit tree. This queue
42
+ * automatically deduplicates types, so if a type is added multiple times it
43
+ * will only be visited once.
44
+ */
45
+ typeQueue: OnceQueue<DeclarationType>;
46
+ /**
47
+ * A list of synthetic types (anonymous types that are given names) that are
48
+ * included in the emit tree.
49
+ */
50
+ synthetics: Synthetic[];
51
+ /**
52
+ * A cache of names given to synthetic types. These names may be used to avoid
53
+ * emitting the same synthetic type multiple times.
54
+ */
55
+ syntheticNames: Map<DeclarationType, string>;
56
+ /**
57
+ * The root module for the emit tree.
58
+ */
59
+ rootModule: Module;
60
+ /**
61
+ * The parent of the generated module.
62
+ */
63
+ srcModule: Module;
64
+ /**
65
+ * The module that contains all generated code.
66
+ */
67
+ generatedModule: Module;
68
+ /**
69
+ * A map relating each namespace to the module that contains its declarations.
70
+ *
71
+ * @see createOrGetModuleForNamespace
72
+ */
73
+ namespaceModules: Map<Namespace, Module>;
74
+ /**
75
+ * The module that contains all synthetic types.
76
+ */
77
+ syntheticModule: Module;
78
+ /**
79
+ * The root module for all named declarations of types referenced by the program.
80
+ */
81
+ modelsModule: Module;
82
+ /**
83
+ * The module within `models` that maps to the global namespace.
84
+ */
85
+ globalNamespaceModule: Module;
86
+ /**
87
+ * A map of all types that require serialization code to the formats they require.
88
+ */
89
+ serializations: OnceQueue<SerializableType>;
90
+ gensym: (name: string) => string;
91
+ }
92
+ export declare function createInitialContext(program: Program, options: JsEmitterOptions): Promise<JsContext | undefined>;
93
+ /**
94
+ * A synthetic type that is not directly represented with a name in the TypeSpec program.
95
+ */
96
+ export type Synthetic = AnonymousSynthetic | PartialUnionSynthetic;
97
+ /**
98
+ * An ordinary, anonymous type that is given a name.
99
+ */
100
+ export interface AnonymousSynthetic {
101
+ kind: "anonymous";
102
+ name: string;
103
+ underlying: DeclarationType;
104
+ }
105
+ /**
106
+ * A partial union with a name for the given variants.
107
+ */
108
+ export interface PartialUnionSynthetic {
109
+ kind: "partialUnion";
110
+ name: string;
111
+ variants: UnionVariant[];
112
+ }
113
+ /**
114
+ * Adds all pending declarations from the type queue to the module tree.
115
+ *
116
+ * The JavaScript emitter is lazy, and sometimes emitter components may visit
117
+ * types that are not yet declared. This function ensures that all types
118
+ * reachable from existing declarations are complete.
119
+ *
120
+ * @param ctx - The JavaScript emitter context.
121
+ */
122
+ export declare function completePendingDeclarations(ctx: JsContext): void;
123
+ /**
124
+ * A declaration within a module. This may be a string (i.e. a line), an array of
125
+ * strings (emitted as multiple lines), or another module (emitted as a nested module).
126
+ */
127
+ export type ModuleBodyDeclaration = string[] | string | Module;
128
+ /**
129
+ * A type-guard that checks whether or not a given value is a module.
130
+ * @returns `true` if the value is a module, `false` otherwise.
131
+ */
132
+ export declare function isModule(value: unknown): value is Module;
133
+ /**
134
+ * Creates a new module with the given name and attaches it to the parent module.
135
+ *
136
+ * Optionally, a namespace may be associated with the module. This namespace is
137
+ * _NOT_ stored in the context (this function does not use the JsContext), and
138
+ * is only stored as metadata within the module. To associate a module with a
139
+ * namespace inside the context, use `createOrGetModuleForNamespace`.
140
+ *
141
+ * The module is automatically declared as a declaration within its parent
142
+ * module.
143
+ *
144
+ * @param name - The name of the module.
145
+ * @param parent - The parent module to attach the new module to.
146
+ * @param namespace - an optional TypeSpec Namespace to associate with the module
147
+ * @returns the newly created module
148
+ */
149
+ export declare function createModule(name: string, parent: Module, namespace?: Namespace): Module;
150
+ /**
151
+ * The type of a binding for an import statement. Either:
152
+ *
153
+ * - A string beginning with `* as` followed by the name of the binding, which
154
+ * imports all exports from the module as a single object.
155
+ * - A binding name, which imports the default export of the module.
156
+ * - An array of strings, each of which is a named import from the module.
157
+ */
158
+ export type ImportBinder = string | string[];
159
+ /**
160
+ * An object representing a ECMAScript module import declaration.
161
+ */
162
+ export interface Import {
163
+ /**
164
+ * The binder to define the import as.
165
+ */
166
+ binder: ImportBinder;
167
+ /**
168
+ * Where to import from. This is either a literal string (which will be used verbatim), or Module object, which will
169
+ * be resolved to a relative file path.
170
+ */
171
+ from: Module | string;
172
+ }
173
+ /**
174
+ * An output module within the module tree.
175
+ */
176
+ export interface Module {
177
+ /**
178
+ * The name of the module, which should be suitable for use as the basename of
179
+ * a file and as an identifier.
180
+ */
181
+ name: string;
182
+ /**
183
+ * The cursor for the module, which assists navigation and relative path
184
+ * computation between modules.
185
+ */
186
+ readonly cursor: PathCursor;
187
+ /**
188
+ * An optional namespace for the module. This is not used by the code writer,
189
+ * but is used to track dependencies between TypeSpec namespaces and create
190
+ * imports between them.
191
+ */
192
+ namespace?: Namespace;
193
+ /**
194
+ * A list of imports that the module requires.
195
+ */
196
+ imports: Import[];
197
+ /**
198
+ * A list of declarations within the module.
199
+ */
200
+ declarations: ModuleBodyDeclaration[];
201
+ }
202
+ /**
203
+ * A cursor that assists in navigating the module tree and computing relative
204
+ * paths between modules.
205
+ */
206
+ export interface PathCursor {
207
+ /**
208
+ * The path to this cursor. This is an array of strings that represents the
209
+ * path from the root module to another module.
210
+ */
211
+ readonly path: string[];
212
+ /**
213
+ * The parent cursor of this cursor (equivalent to moving up one level in the
214
+ * module tree). If this cursor is the root cursor, this property is `undefined`.
215
+ */
216
+ readonly parent: PathCursor | undefined;
217
+ /**
218
+ * Returns a new cursor that includes the given path components appended to
219
+ * this cursor's path.
220
+ *
221
+ * @param path - the path to append to this cursor
222
+ */
223
+ enter(...path: string[]): PathCursor;
224
+ /**
225
+ * Computes a relative path from this cursor to another cursor, using the string `up`
226
+ * to navigate upwards one level in the path. This is similar to `path.relative` when
227
+ * working with file paths, but operates over PathCursor objects.
228
+ *
229
+ * @param to - the cursor to compute the path to
230
+ * @param up - the string to use to move up a level in the path (defaults to "..")
231
+ */
232
+ relativePath(to: PathCursor, up?: string): string[];
233
+ }
234
+ /**
235
+ * Create a new cursor with the given path.
236
+ *
237
+ * @param base - the base path of this cursor
238
+ * @returns
239
+ */
240
+ export declare function createPathCursor(...base: string[]): PathCursor;
241
+ export declare function gensym(ctx: JsContext, name: string): string;
242
+ //# sourceMappingURL=ctx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx.d.ts","sourceRoot":"","sources":["../../src/ctx.ts"],"names":[],"mappings":"AAGA,OAAO,EAEL,IAAI,EACJ,SAAS,EAIT,KAAK,EACL,SAAS,EAET,OAAO,EACP,MAAM,EACN,OAAO,EACP,IAAI,EACJ,KAAK,EACL,YAAY,EACb,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAC;AAEnE,OAAO,EAAE,gBAAgB,EAAoB,MAAM,UAAU,CAAC;AAG9D,OAAO,EAAmB,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAIlE,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,SAAS,GAAG,MAAM,CAAC;AAExE;;;;;;;;;;;GAWG;AACH,wBAA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,CAAC,MAAM,EAAE,UAAU,GAAG,SAAS,CAAC;IAExC;;;;;OAKG;IACH,KAAK,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,UAAU,CAAC;IAErC;;;;;;;OAOG;IACH,YAAY,CAAC,EAAE,EAAE,UAAU,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;CACrD;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,IAAI,EAAE,MAAM,EAAE,GAAG,UAAU,CA4B9D;AAqBD,wBAAgB,MAAM,CAAC,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAS3D"}
@@ -0,0 +1,211 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { compilerAssert, isArrayModelType, isRecordModelType, listServices, NoTarget, } from "@typespec/compiler";
4
+ import { emitDeclaration } from "./common/declaration.js";
5
+ import { createOrGetModuleForNamespace } from "./common/namespace.js";
6
+ import { emitUnion } from "./common/union.js";
7
+ import { reportDiagnostic } from "./lib.js";
8
+ import { parseCase } from "./util/case.js";
9
+ import { UnimplementedError } from "./util/error.js";
10
+ import { createOnceQueue } from "./util/once-queue.js";
11
+ import { createModule as initializeHelperModule } from "../generated-defs/helpers/index.js";
12
+ /**
13
+ * Determines whether or not a type is importable into a JavaScript module.
14
+ *
15
+ * i.e. whether or not it is declared as a named symbol within the module.
16
+ *
17
+ * In TypeScript, unions are rendered inline, so they are not ordinarily
18
+ * considered importable.
19
+ *
20
+ * @param ctx - The JS emitter context.
21
+ * @param t - the type to test
22
+ * @returns `true` if the type is an importable declaration, `false` otherwise.
23
+ */
24
+ export function isImportableType(ctx, t) {
25
+ return ((t.kind === "Model" &&
26
+ !isArrayModelType(ctx.program, t) &&
27
+ !isRecordModelType(ctx.program, t)) ||
28
+ t.kind === "Enum" ||
29
+ t.kind === "Interface");
30
+ }
31
+ export async function createInitialContext(program, options) {
32
+ const services = listServices(program);
33
+ if (services.length === 0) {
34
+ reportDiagnostic(program, {
35
+ code: "no-services-in-program",
36
+ target: NoTarget,
37
+ messageId: "default",
38
+ });
39
+ return undefined;
40
+ }
41
+ else if (services.length > 1) {
42
+ throw new UnimplementedError("multiple service definitions per program.");
43
+ }
44
+ const [service] = services;
45
+ const serviceModuleName = parseCase(service.type.name).snakeCase;
46
+ const rootCursor = createPathCursor();
47
+ const globalNamespace = program.getGlobalNamespaceType();
48
+ // Root module for emit.
49
+ const rootModule = {
50
+ name: serviceModuleName,
51
+ cursor: rootCursor,
52
+ imports: [],
53
+ declarations: [],
54
+ };
55
+ const srcModule = createModule("src", rootModule);
56
+ const generatedModule = createModule("generated", srcModule);
57
+ // This has the side effect of setting the `module` property of all helpers.
58
+ // Don't do anything with the emitter code before this is called.
59
+ await initializeHelperModule(generatedModule);
60
+ // Module for all models, including synthetic and all.
61
+ const modelsModule = createModule("models", generatedModule);
62
+ // Module for all types in all namespaces.
63
+ const allModule = createModule("all", modelsModule, globalNamespace);
64
+ // Module for all synthetic (named ad-hoc) types.
65
+ const syntheticModule = createModule("synthetic", modelsModule);
66
+ const jsCtx = {
67
+ program,
68
+ options,
69
+ globalNamespace,
70
+ service,
71
+ typeQueue: createOnceQueue(),
72
+ synthetics: [],
73
+ syntheticNames: new Map(),
74
+ rootModule,
75
+ srcModule,
76
+ generatedModule,
77
+ namespaceModules: new Map([[globalNamespace, allModule]]),
78
+ syntheticModule,
79
+ modelsModule,
80
+ globalNamespaceModule: allModule,
81
+ serializations: createOnceQueue(),
82
+ gensym: (name) => {
83
+ return gensym(jsCtx, name);
84
+ },
85
+ };
86
+ return jsCtx;
87
+ }
88
+ /**
89
+ * Adds all pending declarations from the type queue to the module tree.
90
+ *
91
+ * The JavaScript emitter is lazy, and sometimes emitter components may visit
92
+ * types that are not yet declared. This function ensures that all types
93
+ * reachable from existing declarations are complete.
94
+ *
95
+ * @param ctx - The JavaScript emitter context.
96
+ */
97
+ export function completePendingDeclarations(ctx) {
98
+ // Add all pending declarations to the module tree.
99
+ while (!ctx.typeQueue.isEmpty() || ctx.synthetics.length > 0) {
100
+ while (!ctx.typeQueue.isEmpty()) {
101
+ const type = ctx.typeQueue.take();
102
+ compilerAssert(type.namespace !== undefined, "no namespace for declaration type", type);
103
+ const module = createOrGetModuleForNamespace(ctx, type.namespace);
104
+ module.declarations.push([...emitDeclaration(ctx, type, module)]);
105
+ }
106
+ while (ctx.synthetics.length > 0) {
107
+ const synthetic = ctx.synthetics.shift();
108
+ switch (synthetic.kind) {
109
+ case "anonymous": {
110
+ ctx.syntheticModule.declarations.push([
111
+ ...emitDeclaration(ctx, synthetic.underlying, ctx.syntheticModule, synthetic.name),
112
+ ]);
113
+ break;
114
+ }
115
+ case "partialUnion": {
116
+ ctx.syntheticModule.declarations.push([
117
+ ...emitUnion(ctx, synthetic, ctx.syntheticModule, synthetic.name),
118
+ ]);
119
+ break;
120
+ }
121
+ }
122
+ }
123
+ }
124
+ }
125
+ /**
126
+ * A type-guard that checks whether or not a given value is a module.
127
+ * @returns `true` if the value is a module, `false` otherwise.
128
+ */
129
+ export function isModule(value) {
130
+ return (typeof value === "object" &&
131
+ value !== null &&
132
+ "declarations" in value &&
133
+ Array.isArray(value.declarations));
134
+ }
135
+ /**
136
+ * Creates a new module with the given name and attaches it to the parent module.
137
+ *
138
+ * Optionally, a namespace may be associated with the module. This namespace is
139
+ * _NOT_ stored in the context (this function does not use the JsContext), and
140
+ * is only stored as metadata within the module. To associate a module with a
141
+ * namespace inside the context, use `createOrGetModuleForNamespace`.
142
+ *
143
+ * The module is automatically declared as a declaration within its parent
144
+ * module.
145
+ *
146
+ * @param name - The name of the module.
147
+ * @param parent - The parent module to attach the new module to.
148
+ * @param namespace - an optional TypeSpec Namespace to associate with the module
149
+ * @returns the newly created module
150
+ */
151
+ export function createModule(name, parent, namespace) {
152
+ const self = {
153
+ name,
154
+ cursor: parent.cursor.enter(name),
155
+ namespace,
156
+ imports: [],
157
+ declarations: [],
158
+ };
159
+ parent.declarations.push(self);
160
+ return self;
161
+ }
162
+ /**
163
+ * Create a new cursor with the given path.
164
+ *
165
+ * @param base - the base path of this cursor
166
+ * @returns
167
+ */
168
+ export function createPathCursor(...base) {
169
+ const self = {
170
+ path: base,
171
+ get parent() {
172
+ return self.path.length === 0 ? undefined : createPathCursor(...self.path.slice(0, -1));
173
+ },
174
+ enter(...path) {
175
+ return createPathCursor(...self.path, ...path);
176
+ },
177
+ relativePath(to, up = "..") {
178
+ const commonPrefix = getCommonPrefix(self.path, to.path);
179
+ const outputPath = [];
180
+ for (let i = 0; i < self.path.length - commonPrefix.length; i++) {
181
+ outputPath.push(up);
182
+ }
183
+ outputPath.push(...to.path.slice(commonPrefix.length));
184
+ return outputPath;
185
+ },
186
+ };
187
+ return self;
188
+ }
189
+ /**
190
+ * Compute the common prefix of two paths.
191
+ */
192
+ function getCommonPrefix(a, b) {
193
+ const prefix = [];
194
+ for (let i = 0; i < Math.min(a.length, b.length); i++) {
195
+ if (a[i] !== b[i]) {
196
+ break;
197
+ }
198
+ prefix.push(a[i]);
199
+ }
200
+ return prefix;
201
+ }
202
+ const SYM_TAB = new WeakMap();
203
+ export function gensym(ctx, name) {
204
+ let symTab = SYM_TAB.get(ctx.program);
205
+ if (symTab === undefined) {
206
+ symTab = { idx: 0 };
207
+ SYM_TAB.set(ctx.program, symTab);
208
+ }
209
+ return `__${name}_${symTab.idx++}`;
210
+ }
211
+ //# sourceMappingURL=ctx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../src/ctx.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAElC,OAAO,EACL,cAAc,EAGd,gBAAgB,EAChB,iBAAiB,EACjB,YAAY,EAGZ,QAAQ,GAOT,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC1D,OAAO,EAAE,6BAA6B,EAAE,MAAM,uBAAuB,CAAC;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AC;QACpB,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACnC,CAAC;IAED,OAAO,KAAK,IAAI,IAAI,MAAM,CAAC,GAAG,EAAE,EAAE,CAAC;AACrC,CAAC"}
@@ -0,0 +1,14 @@
1
+ export interface HeaderValueParameters {
2
+ value: string;
3
+ verbatim: string;
4
+ params: {
5
+ [k: string]: string;
6
+ };
7
+ }
8
+ /**
9
+ * Parses a header value that may contain additional parameters (e.g. `text/html; charset=utf-8`).
10
+ * @param headerValueText - the text of the header value to parse
11
+ * @returns an object containing the value and a map of parameters
12
+ */
13
+ export declare function parseHeaderValueParameters<Header extends string | undefined>(headerValueText: Header): undefined extends Header ? HeaderValueParameters | undefined : HeaderValueParameters;
14
+ //# sourceMappingURL=header.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"header.d.ts","sourceRoot":"","sources":["../../../src/helpers/header.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACjC;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,MAAM,SAAS,MAAM,GAAG,SAAS,EAC1E,eAAe,EAAE,MAAM,GACtB,SAAS,SAAS,MAAM,GAAG,qBAAqB,GAAG,SAAS,GAAG,qBAAqB,CAsCtF"}
@@ -0,0 +1,38 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ /**
4
+ * Parses a header value that may contain additional parameters (e.g. `text/html; charset=utf-8`).
5
+ * @param headerValueText - the text of the header value to parse
6
+ * @returns an object containing the value and a map of parameters
7
+ */
8
+ export function parseHeaderValueParameters(headerValueText) {
9
+ if (headerValueText === undefined) {
10
+ return undefined;
11
+ }
12
+ const idx = headerValueText.indexOf(";");
13
+ const [value, _paramsText] = idx === -1
14
+ ? [headerValueText, ""]
15
+ : [headerValueText.slice(0, idx), headerValueText.slice(idx + 1)];
16
+ let paramsText = _paramsText;
17
+ // Parameters are a sequence of key=value pairs separated by semicolons, but the value may be quoted in which case it
18
+ // may contain semicolons. We use a regular expression to iteratively split the parameters into key=value pairs.
19
+ const params = {};
20
+ let match;
21
+ // TODO: may need to support ext-parameter (e.g. "filename*=UTF-8''%e2%82%ac%20rates" => { filename: "€ rates" }).
22
+ // By default we decoded everything as UTF-8, and non-UTF-8 agents are a dying breed, but we may need to support
23
+ // this for completeness. If we do support it, we'll prefer an ext-parameter over a regular parameter. Currently, we'll
24
+ // just treat them as separate keys and put the raw value in the parameter.
25
+ //
26
+ // https://datatracker.ietf.org/doc/html/rfc5987#section-3.2.1
27
+ while ((match = paramsText.match(/\s*([^=]+)=(?:"([^"]+)"|([^;]+));?/))) {
28
+ const [, key, quotedValue, unquotedValue] = match;
29
+ params[key.trim()] = quotedValue ?? unquotedValue;
30
+ paramsText = paramsText.slice(match[0].length);
31
+ }
32
+ return {
33
+ value: value.trim(),
34
+ verbatim: headerValueText,
35
+ params,
36
+ };
37
+ }
38
+ //# sourceMappingURL=header.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"header.js","sourceRoot":"","sources":["../../../src/helpers/header.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAQlC;;;;GAIG;AACH,MAAM,UAAU,0BAA0B,CACxC,eAAuB;IAEvB,IAAI,eAAe,KAAK,SAAS,EAAE,CAAC;QAClC,OAAO,SAAgB,CAAC;IAC1B,CAAC;IAED,MAAM,GAAG,GAAG,eAAe,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACzC,MAAM,CAAC,KAAK,EAAE,WAAW,CAAC,GACxB,GAAG,KAAK,CAAC,CAAC;QACR,CAAC,CAAC,CAAC,eAAe,EAAE,EAAE,CAAC;QACvB,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,eAAe,CAAC,KAAK,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;IAEtE,IAAI,UAAU,GAAG,WAAW,CAAC;IAE7B,qHAAqH;IACrH,gHAAgH;IAChH,MAAM,MAAM,GAA4B,EAAE,CAAC;IAE3C,IAAI,KAAK,CAAC;IAEV,kHAAkH;IAClH,gHAAgH;IAChH,uHAAuH;IACvH,2EAA2E;IAC3E,EAAE;IACF,8DAA8D;IAC9D,OAAO,CAAC,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,oCAAoC,CAAC,CAAC,EAAE,CAAC;QACxE,MAAM,CAAC,EAAE,GAAG,EAAE,WAAW,EAAE,aAAa,CAAC,GAAG,KAAK,CAAC;QAElD,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,GAAG,WAAW,IAAI,aAAa,CAAC;QAElD,UAAU,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;IACjD,CAAC;IAED,OAAO;QACL,KAAK,EAAE,KAAK,CAAC,IAAI,EAAE;QACnB,QAAQ,EAAE,eAAe;QACzB,MAAM;KACP,CAAC;AACJ,CAAC"}
@@ -0,0 +1,70 @@
1
+ import { HttpContext } from "./router.js";
2
+ export declare const HTTP_RESPONDER: unique symbol;
3
+ /**
4
+ * A type that can respond to an HTTP request.
5
+ */
6
+ export interface HttpResponder {
7
+ /**
8
+ * A function that handles an HTTP request and response.
9
+ *
10
+ * @param context - The HTTP context.
11
+ */
12
+ [HTTP_RESPONDER]: (context: HttpContext) => void;
13
+ }
14
+ /**
15
+ * Determines if a value is an HttpResponder.
16
+ * @param value - The value to check.
17
+ * @returns `true` if the value is an HttpResponder, otherwise `false`.
18
+ */
19
+ export declare function isHttpResponder(value: unknown): value is HttpResponder;
20
+ /**
21
+ * An Error that can respond to an HTTP request if thrown from a route handler.
22
+ */
23
+ export declare class HttpResponderError extends Error implements HttpResponder {
24
+ #private;
25
+ constructor(statusCode: number, message: string);
26
+ [HTTP_RESPONDER](ctx: HttpContext): void;
27
+ }
28
+ /**
29
+ * The requested resource was not found.
30
+ */
31
+ export declare class NotFoundError extends HttpResponderError {
32
+ constructor();
33
+ }
34
+ /**
35
+ * The request was malformed.
36
+ */
37
+ export declare class BadRequestError extends HttpResponderError {
38
+ constructor();
39
+ }
40
+ /**
41
+ * The request is missing required authentication credentials.
42
+ */
43
+ export declare class UnauthorizedError extends HttpResponderError {
44
+ constructor();
45
+ }
46
+ /**
47
+ * The request is missing required permissions.
48
+ */
49
+ export declare class ForbiddenError extends HttpResponderError {
50
+ constructor();
51
+ }
52
+ /**
53
+ * The request conflicts with the current state of the server.
54
+ */
55
+ export declare class ConflictError extends HttpResponderError {
56
+ constructor();
57
+ }
58
+ /**
59
+ * The server encountered an unexpected condition that prevented it from fulfilling the request.
60
+ */
61
+ export declare class InternalServerError extends HttpResponderError {
62
+ constructor();
63
+ }
64
+ /**
65
+ * The server does not support the functionality required to fulfill the request.
66
+ */
67
+ export declare class NotImplementedError extends HttpResponderError {
68
+ constructor();
69
+ }
70
+ //# sourceMappingURL=http.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/helpers/http.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE1C,eAAO,MAAM,cAAc,eAAuD,CAAC;AAEnF;;GAEG;AACH,MAAM,WAAW,aAAa;IAC5B;;;;OAIG;IACH,CAAC,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,KAAK,IAAI,CAAC;CAClD;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAOtE;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,KAAM,YAAW,aAAa;;gBAGxD,UAAU,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;IAK/C,CAAC,cAAc,CAAC,CAAC,GAAG,EAAE,WAAW,GAAG,IAAI;CAKzC;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,kBAAkB;;CAIpD;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,kBAAkB;;CAItD;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,kBAAkB;;CAIxD;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,kBAAkB;;CAIrD;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,kBAAkB;;CAIpD;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,kBAAkB;;CAI1D;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,kBAAkB;;CAI1D"}