@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,111 @@
1
+ import { NoTarget } from "@typespec/compiler";
2
+ import { reportDiagnostic } from "../lib.js";
3
+ import { UnreachableError } from "./error.js";
4
+ const DEFAULT_DECLARATION_OPTIONS = {
5
+ source: NoTarget,
6
+ resolutionStrategy: "shadow",
7
+ };
8
+ const NO_PARENT = {
9
+ declare() {
10
+ throw new UnreachableError("Cannot declare in the no-parent scope");
11
+ },
12
+ isDeclared() {
13
+ return false;
14
+ },
15
+ };
16
+ /**
17
+ * Create a new scope.
18
+ *
19
+ * @param ctx - the JS emitter context.
20
+ * @param parent - an optional parent scope for this scope. It will consider declarations in the parent scope for some conflicts.
21
+ */
22
+ export function createScope(ctx, parent = NO_PARENT) {
23
+ const ownDeclarations = new Set();
24
+ const self = {
25
+ declare(primaryName, options = {}) {
26
+ const { source: target, resolutionStrategy } = { ...DEFAULT_DECLARATION_OPTIONS, ...options };
27
+ if (!self.isDeclared(primaryName)) {
28
+ ownDeclarations.add(primaryName);
29
+ return primaryName;
30
+ }
31
+ // Apply resolution strategy
32
+ const resolutionStrategyName = typeof resolutionStrategy === "string" ? resolutionStrategy : resolutionStrategy.kind;
33
+ switch (resolutionStrategyName) {
34
+ case "none":
35
+ // Report diagnostic and return the name as is.
36
+ reportDiagnostic(ctx.program, {
37
+ code: "name-conflict",
38
+ format: {
39
+ name: primaryName,
40
+ },
41
+ target,
42
+ });
43
+ return primaryName;
44
+ case "shadow":
45
+ // Check to make sure this name isn't an own-declaration, and if not allow it, otherwise raise a diagnostic.
46
+ if (!ownDeclarations.has(primaryName)) {
47
+ ownDeclarations.add(primaryName);
48
+ return primaryName;
49
+ }
50
+ else {
51
+ reportDiagnostic(ctx.program, {
52
+ code: "name-conflict",
53
+ format: {
54
+ name: primaryName,
55
+ },
56
+ target,
57
+ });
58
+ return primaryName;
59
+ }
60
+ case "prefix": {
61
+ const { prefix = "_", repeated = false, shadow = true, } = resolutionStrategy;
62
+ let name = primaryName;
63
+ const isDeclared = shadow ? (name) => ownDeclarations.has(name) : self.isDeclared;
64
+ while (isDeclared(name)) {
65
+ name = prefix + name;
66
+ if (!repeated)
67
+ break;
68
+ }
69
+ if (isDeclared(name)) {
70
+ // We were not able to resolve the conflict with this strategy, so raise a diagnostic.
71
+ reportDiagnostic(ctx.program, {
72
+ code: "name-conflict",
73
+ format: {
74
+ name: name,
75
+ },
76
+ target,
77
+ });
78
+ return name;
79
+ }
80
+ ownDeclarations.add(name);
81
+ return name;
82
+ }
83
+ case "alt-name": {
84
+ const { altName } = resolutionStrategy;
85
+ if (!self.isDeclared(altName)) {
86
+ ownDeclarations.add(altName);
87
+ return altName;
88
+ }
89
+ // We were not able to resolve the conflict with this strategy, so raise a diagnostic.
90
+ reportDiagnostic(ctx.program, {
91
+ code: "name-conflict",
92
+ format: {
93
+ name: altName,
94
+ },
95
+ target,
96
+ });
97
+ return altName;
98
+ }
99
+ default:
100
+ throw new UnreachableError(`Unknown resolution strategy: ${resolutionStrategy}`, {
101
+ resolutionStrategyName,
102
+ });
103
+ }
104
+ },
105
+ isDeclared(name) {
106
+ return ownDeclarations.has(name) || parent.isDeclared(name);
107
+ },
108
+ };
109
+ return self;
110
+ }
111
+ //# sourceMappingURL=scope.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scope.js","sourceRoot":"","sources":["../../../src/util/scope.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAA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E,GAAG,kBAAuC,CAAC;oBAE5D,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;wBAC9B,eAAe,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;wBAC7B,OAAO,OAAO,CAAC;oBACjB,CAAC;oBAED,sFAAsF;oBACtF,gBAAgB,CAAC,GAAG,CAAC,OAAO,EAAE;wBAC5B,IAAI,EAAE,eAAe;wBACrB,MAAM,EAAE;4BACN,IAAI,EAAE,OAAO;yBACd;wBACD,MAAM;qBACP,CAAC,CAAC;oBAEH,OAAO,OAAO,CAAC;gBACjB,CAAC;gBACD;oBACE,MAAM,IAAI,gBAAgB,CAAC,gCAAgC,kBAAkB,EAAE,EAAE;wBAC/E,sBAAsB;qBACvB,CAAC,CAAC;YACP,CAAC;QACH,CAAC;QACD,UAAU,CAAC,IAAI;YACb,OAAO,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;QAC9D,CAAC;KACF,CAAC;IAEF,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { JsContext, Module } from "./ctx.js";
2
+ import { OnceQueue } from "./util/once-queue.js";
3
+ /**
4
+ * Writes the tree of modules to the output directory.
5
+ *
6
+ * @param ctx - The emitter context.
7
+ * @param baseOutputPath - The base output directory to write the module tree to.
8
+ * @param rootModule - The root module to begin emitting from.
9
+ * @param format - Whether to format the output using Prettier.
10
+ */
11
+ export declare function writeModuleTree(ctx: JsContext, baseOutputPath: string, rootModule: Module, format: boolean): Promise<void>;
12
+ /**
13
+ * Write a single module file to the output directory.
14
+ *
15
+ * @param ctx - The emitter context.
16
+ * @param baseOutputPath - The base output directory to write the module tree to.
17
+ * @param module - The module to write.
18
+ * @param queue - The queue of modules to write.
19
+ * @param format - Whether to format the output using Prettier.
20
+ * @param spit - The function used to write the file to disk (default: `ctx.program.host.writeFile`).
21
+ */
22
+ export declare function writeModuleFile(ctx: JsContext, baseOutputPath: string, module: Module, queue: OnceQueue<Module>, format: boolean, spit?: (path: string, contents: string) => Promise<void>): Promise<void>;
23
+ //# sourceMappingURL=write.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write.d.ts","sourceRoot":"","sources":["../../src/write.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAY,MAAM,UAAU,CAAC;AAGvD,OAAO,EAAE,SAAS,EAAmB,MAAM,sBAAsB,CAAC;AAQlE;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACnC,GAAG,EAAE,SAAS,EACd,cAAc,EAAE,MAAM,EACtB,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,OAAO,GACd,OAAO,CAAC,IAAI,CAAC,CAOf;AAED;;;;;;;;;GASG;AACH,wBAAsB,eAAe,CACnC,GAAG,EAAE,SAAS,EACd,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,SAAS,CAAC,MAAM,CAAC,EACxB,MAAM,EAAE,OAAO,EACf,IAAI,GAAE,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAGrD,GACA,OAAO,CAAC,IAAI,CAAC,CA8Bf"}
@@ -0,0 +1,62 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+ import { resolvePath } from "@typespec/compiler";
4
+ import { isModule } from "./ctx.js";
5
+ import { emitModuleBody } from "./common/namespace.js";
6
+ import { createOnceQueue } from "./util/once-queue.js";
7
+ import * as prettier from "prettier";
8
+ import { EOL } from "os";
9
+ import path from "path";
10
+ import { bifilter } from "./util/iter.js";
11
+ /**
12
+ * Writes the tree of modules to the output directory.
13
+ *
14
+ * @param ctx - The emitter context.
15
+ * @param baseOutputPath - The base output directory to write the module tree to.
16
+ * @param rootModule - The root module to begin emitting from.
17
+ * @param format - Whether to format the output using Prettier.
18
+ */
19
+ export async function writeModuleTree(ctx, baseOutputPath, rootModule, format) {
20
+ const queue = createOnceQueue(rootModule);
21
+ while (!queue.isEmpty()) {
22
+ const module = queue.take();
23
+ await writeModuleFile(ctx, baseOutputPath, module, queue, format);
24
+ }
25
+ }
26
+ /**
27
+ * Write a single module file to the output directory.
28
+ *
29
+ * @param ctx - The emitter context.
30
+ * @param baseOutputPath - The base output directory to write the module tree to.
31
+ * @param module - The module to write.
32
+ * @param queue - The queue of modules to write.
33
+ * @param format - Whether to format the output using Prettier.
34
+ * @param spit - The function used to write the file to disk (default: `ctx.program.host.writeFile`).
35
+ */
36
+ export async function writeModuleFile(ctx, baseOutputPath, module, queue, format, spit = async (name, contents) => {
37
+ await ctx.program.host.mkdirp(path.dirname(name));
38
+ await ctx.program.host.writeFile(name, contents);
39
+ }) {
40
+ const moduleText = [
41
+ "// Generated by Microsoft TypeSpec",
42
+ "",
43
+ ...emitModuleBody(ctx, module, queue),
44
+ ];
45
+ const [declaredModules, declaredText] = bifilter(module.declarations, isModule);
46
+ if (declaredText.length === 0) {
47
+ // Early exit to avoid writing an empty module.
48
+ return;
49
+ }
50
+ const isIndex = module.cursor.path.length === 0 || declaredModules.length > 0;
51
+ const moduleRelativePath = module.cursor.path.length > 0
52
+ ? module.cursor.path.join("/") + (isIndex ? "/index.ts" : ".ts")
53
+ : "index.ts";
54
+ const modulePath = resolvePath(baseOutputPath, moduleRelativePath);
55
+ const text = format
56
+ ? await prettier.format(moduleText.join(EOL), {
57
+ parser: "typescript",
58
+ })
59
+ : moduleText.join(EOL);
60
+ await spit(modulePath, text);
61
+ }
62
+ //# sourceMappingURL=write.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"write.js","sourceRoot":"","sources":["../../src/write.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,kCAAkC;AAElC,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAqB,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEvD,OAAO,EA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AE,QAAQ,CAAC,CAAC;IAEhF,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC9B,+CAA+C;QAC/C,OAAO;IACT,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,CAAC;IAE9E,MAAM,kBAAkB,GACtB,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC;QAC3B,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,CAAC;QAChE,CAAC,CAAC,UAAU,CAAC;IAEjB,MAAM,UAAU,GAAG,WAAW,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAC;IAEnE,MAAM,IAAI,GAAG,MAAM;QACjB,CAAC,CAAC,MAAM,QAAQ,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YAC1C,MAAM,EAAE,YAAY;SACrB,CAAC;QACJ,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEzB,MAAM,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1,83 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+
4
+ import { Module } from "../../src/ctx.js";
5
+
6
+ export let module: Module = undefined as any;
7
+
8
+ // prettier-ignore
9
+ const lines = [
10
+ "// Copyright (c) Microsoft Corporation",
11
+ "// Licensed under the MIT license.",
12
+ "",
13
+ "export interface HeaderValueParameters {",
14
+ " value: string;",
15
+ " verbatim: string;",
16
+ " params: { [k: string]: string };",
17
+ "}",
18
+ "",
19
+ "/**",
20
+ " * Parses a header value that may contain additional parameters (e.g. `text/html; charset=utf-8`).",
21
+ " * @param headerValueText - the text of the header value to parse",
22
+ " * @returns an object containing the value and a map of parameters",
23
+ " */",
24
+ "export function parseHeaderValueParameters<Header extends string | undefined>(",
25
+ " headerValueText: Header,",
26
+ "): undefined extends Header ? HeaderValueParameters | undefined : HeaderValueParameters {",
27
+ " if (headerValueText === undefined) {",
28
+ " return undefined as any;",
29
+ " }",
30
+ "",
31
+ " const idx = headerValueText.indexOf(\";\");",
32
+ " const [value, _paramsText] =",
33
+ " idx === -1",
34
+ " ? [headerValueText, \"\"]",
35
+ " : [headerValueText.slice(0, idx), headerValueText.slice(idx + 1)];",
36
+ "",
37
+ " let paramsText = _paramsText;",
38
+ "",
39
+ " // Parameters are a sequence of key=value pairs separated by semicolons, but the value may be quoted in which case it",
40
+ " // may contain semicolons. We use a regular expression to iteratively split the parameters into key=value pairs.",
41
+ " const params: { [k: string]: string } = {};",
42
+ "",
43
+ " let match;",
44
+ "",
45
+ " // TODO: may need to support ext-parameter (e.g. \"filename*=UTF-8''%e2%82%ac%20rates\" => { filename: \"€ rates\" }).",
46
+ " // By default we decoded everything as UTF-8, and non-UTF-8 agents are a dying breed, but we may need to support",
47
+ " // this for completeness. If we do support it, we'll prefer an ext-parameter over a regular parameter. Currently, we'll",
48
+ " // just treat them as separate keys and put the raw value in the parameter.",
49
+ " //",
50
+ " // https://datatracker.ietf.org/doc/html/rfc5987#section-3.2.1",
51
+ " while ((match = paramsText.match(/\\s*([^=]+)=(?:\"([^\"]+)\"|([^;]+));?/))) {",
52
+ " const [, key, quotedValue, unquotedValue] = match;",
53
+ "",
54
+ " params[key.trim()] = quotedValue ?? unquotedValue;",
55
+ "",
56
+ " paramsText = paramsText.slice(match[0].length);",
57
+ " }",
58
+ "",
59
+ " return {",
60
+ " value: value.trim(),",
61
+ " verbatim: headerValueText,",
62
+ " params,",
63
+ " };",
64
+ "}",
65
+ "",
66
+ ];
67
+
68
+ export async function createModule(parent: Module): Promise<Module> {
69
+ if (module) return module;
70
+
71
+ module = {
72
+ name: "header",
73
+ cursor: parent.cursor.enter("header"),
74
+ imports: [],
75
+ declarations: [],
76
+ };
77
+
78
+ module.declarations.push(lines);
79
+
80
+ parent.declarations.push(module);
81
+
82
+ return module;
83
+ }
@@ -0,0 +1,141 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+
4
+ import { Module } from "../../src/ctx.js";
5
+
6
+ export let module: Module = undefined as any;
7
+
8
+ // prettier-ignore
9
+ const lines = [
10
+ "// Copyright (c) Microsoft Corporation.",
11
+ "// Licensed under the MIT License.",
12
+ "",
13
+ "import { HttpContext } from \"./router.js\";",
14
+ "",
15
+ "export const HTTP_RESPONDER = Symbol.for(\"@typespec/http-server-js.HttpResponder\");",
16
+ "",
17
+ "/**",
18
+ " * A type that can respond to an HTTP request.",
19
+ " */",
20
+ "export interface HttpResponder {",
21
+ " /**",
22
+ " * A function that handles an HTTP request and response.",
23
+ " *",
24
+ " * @param context - The HTTP context.",
25
+ " */",
26
+ " [HTTP_RESPONDER]: (context: HttpContext) => void;",
27
+ "}",
28
+ "",
29
+ "/**",
30
+ " * Determines if a value is an HttpResponder.",
31
+ " * @param value - The value to check.",
32
+ " * @returns `true` if the value is an HttpResponder, otherwise `false`.",
33
+ " */",
34
+ "export function isHttpResponder(value: unknown): value is HttpResponder {",
35
+ " return (",
36
+ " typeof value === \"object\" &&",
37
+ " value !== null &&",
38
+ " HTTP_RESPONDER in value &&",
39
+ " typeof value[HTTP_RESPONDER] === \"function\"",
40
+ " );",
41
+ "}",
42
+ "",
43
+ "/**",
44
+ " * An Error that can respond to an HTTP request if thrown from a route handler.",
45
+ " */",
46
+ "export class HttpResponderError extends Error implements HttpResponder {",
47
+ " #statusCode: number;",
48
+ "",
49
+ " constructor(statusCode: number, message: string) {",
50
+ " super(message);",
51
+ " this.#statusCode = statusCode;",
52
+ " }",
53
+ "",
54
+ " [HTTP_RESPONDER](ctx: HttpContext): void {",
55
+ " ctx.response.statusCode = this.#statusCode;",
56
+ " ctx.response.setHeader(\"Content-Type\", \"text/plain\");",
57
+ " ctx.response.end(this.message);",
58
+ " }",
59
+ "}",
60
+ "",
61
+ "/**",
62
+ " * The requested resource was not found.",
63
+ " */",
64
+ "export class NotFoundError extends HttpResponderError {",
65
+ " constructor() {",
66
+ " super(404, \"Not Found\");",
67
+ " }",
68
+ "}",
69
+ "",
70
+ "/**",
71
+ " * The request was malformed.",
72
+ " */",
73
+ "export class BadRequestError extends HttpResponderError {",
74
+ " constructor() {",
75
+ " super(400, \"Bad Request\");",
76
+ " }",
77
+ "}",
78
+ "",
79
+ "/**",
80
+ " * The request is missing required authentication credentials.",
81
+ " */",
82
+ "export class UnauthorizedError extends HttpResponderError {",
83
+ " constructor() {",
84
+ " super(401, \"Unauthorized\");",
85
+ " }",
86
+ "}",
87
+ "",
88
+ "/**",
89
+ " * The request is missing required permissions.",
90
+ " */",
91
+ "export class ForbiddenError extends HttpResponderError {",
92
+ " constructor() {",
93
+ " super(403, \"Forbidden\");",
94
+ " }",
95
+ "}",
96
+ "",
97
+ "/**",
98
+ " * The request conflicts with the current state of the server.",
99
+ " */",
100
+ "export class ConflictError extends HttpResponderError {",
101
+ " constructor() {",
102
+ " super(409, \"Conflict\");",
103
+ " }",
104
+ "}",
105
+ "",
106
+ "/**",
107
+ " * The server encountered an unexpected condition that prevented it from fulfilling the request.",
108
+ " */",
109
+ "export class InternalServerError extends HttpResponderError {",
110
+ " constructor() {",
111
+ " super(500, \"Internal Server Error\");",
112
+ " }",
113
+ "}",
114
+ "",
115
+ "/**",
116
+ " * The server does not support the functionality required to fulfill the request.",
117
+ " */",
118
+ "export class NotImplementedError extends HttpResponderError {",
119
+ " constructor() {",
120
+ " super(501, \"Not Implemented\");",
121
+ " }",
122
+ "}",
123
+ "",
124
+ ];
125
+
126
+ export async function createModule(parent: Module): Promise<Module> {
127
+ if (module) return module;
128
+
129
+ module = {
130
+ name: "http",
131
+ cursor: parent.cursor.enter("http"),
132
+ imports: [],
133
+ declarations: [],
134
+ };
135
+
136
+ module.declarations.push(lines);
137
+
138
+ parent.declarations.push(module);
139
+
140
+ return module;
141
+ }
@@ -0,0 +1,27 @@
1
+ // Copyright (c) Microsoft Corporation
2
+ // Licensed under the MIT license.
3
+
4
+ import { Module } from "../../src/ctx.js";
5
+
6
+ export let module: Module = undefined as any;
7
+
8
+ export async function createModule(parent: Module): Promise<Module> {
9
+ if (module) return module;
10
+
11
+ module = {
12
+ name: "helpers",
13
+ cursor: parent.cursor.enter("helpers"),
14
+ imports: [],
15
+ declarations: [],
16
+ };
17
+
18
+ // Child modules
19
+ await import("./header.js").then((m) => m.createModule(module));
20
+ await import("./http.js").then((m) => m.createModule(module));
21
+ await import("./multipart.js").then((m) => m.createModule(module));
22
+ await import("./router.js").then((m) => m.createModule(module));
23
+
24
+ parent.declarations.push(module);
25
+
26
+ return module;
27
+ }