@tsed/platform-http 8.11.1 → 8.11.2-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (228) hide show
  1. package/lib/esm/common/builder/PlatformBuilder.js +0 -1
  2. package/lib/esm/common/builder/__fixtures__/PlatformCustom.js +0 -1
  3. package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js +0 -1
  4. package/lib/esm/common/config/interfaces/PlatformMulterSettings.js +0 -1
  5. package/lib/esm/common/config/interfaces/PlatformRouterSettings.js +0 -1
  6. package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js +0 -1
  7. package/lib/esm/common/config/services/PlatformConfiguration.js +0 -1
  8. package/lib/esm/common/constants/routerOptions.js +0 -1
  9. package/lib/esm/common/decorators/PlatformProvider.js +0 -1
  10. package/lib/esm/common/decorators/multer/multerFileSize.js +0 -1
  11. package/lib/esm/common/decorators/multer/multerOptions.js +0 -1
  12. package/lib/esm/common/decorators/multer/multipartFile.js +0 -1
  13. package/lib/esm/common/decorators/params/error.js +0 -1
  14. package/lib/esm/common/decorators/params/next.js +0 -1
  15. package/lib/esm/common/decorators/params/request.js +0 -1
  16. package/lib/esm/common/decorators/params/response.js +0 -1
  17. package/lib/esm/common/domain/AnyToPromiseWithCtx.js +0 -1
  18. package/lib/esm/common/domain/EndpointMetadata.js +0 -1
  19. package/lib/esm/common/domain/PlatformContext.js +0 -1
  20. package/lib/esm/common/domain/PlatformRouteDetails.js +0 -1
  21. package/lib/esm/common/exports.js +0 -1
  22. package/lib/esm/common/fn/adapter.js +0 -1
  23. package/lib/esm/common/fn/application.js +0 -1
  24. package/lib/esm/common/index.js +0 -1
  25. package/lib/esm/common/interfaces/AfterInit.js +0 -1
  26. package/lib/esm/common/interfaces/AfterListen.js +0 -1
  27. package/lib/esm/common/interfaces/AfterRoutesInit.js +0 -1
  28. package/lib/esm/common/interfaces/BeforeInit.js +0 -1
  29. package/lib/esm/common/interfaces/BeforeListen.js +0 -1
  30. package/lib/esm/common/interfaces/BeforeRoutesInit.js +0 -1
  31. package/lib/esm/common/interfaces/HandlerType.js +0 -1
  32. package/lib/esm/common/interfaces/IncomingEvent.js +0 -1
  33. package/lib/esm/common/interfaces/OnReady.js +0 -1
  34. package/lib/esm/common/interfaces/OnRequest.js +0 -1
  35. package/lib/esm/common/interfaces/OnResponse.js +0 -1
  36. package/lib/esm/common/interfaces/OnRoutesInit.js +0 -1
  37. package/lib/esm/common/interfaces/PlatformRouteOptions.js +0 -1
  38. package/lib/esm/common/interfaces/ResponseErrorObject.js +0 -1
  39. package/lib/esm/common/interfaces/Route.js +0 -1
  40. package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js +0 -1
  41. package/lib/esm/common/middlewares/PlatformMulterMiddleware.js +0 -1
  42. package/lib/esm/common/services/Platform.js +0 -1
  43. package/lib/esm/common/services/PlatformAdapter.js +0 -1
  44. package/lib/esm/common/services/PlatformApplication.js +0 -1
  45. package/lib/esm/common/services/PlatformHandler.js +0 -1
  46. package/lib/esm/common/services/PlatformRequest.js +0 -1
  47. package/lib/esm/common/services/PlatformResponse.js +0 -1
  48. package/lib/esm/common/utils/closeServer.js +0 -1
  49. package/lib/esm/common/utils/createContext.js +0 -1
  50. package/lib/esm/common/utils/createHttpServer.js +0 -1
  51. package/lib/esm/common/utils/createHttpsServer.js +0 -1
  52. package/lib/esm/common/utils/createInjector.js +0 -1
  53. package/lib/esm/common/utils/createServer.js +0 -1
  54. package/lib/esm/common/utils/ensureContext.js +0 -1
  55. package/lib/esm/common/utils/getConfiguration.js +0 -1
  56. package/lib/esm/common/utils/getStaticsOptions.js +0 -1
  57. package/lib/esm/common/utils/listenServer.js +0 -1
  58. package/lib/esm/common/utils/mapReturnedResponse.js +0 -1
  59. package/lib/esm/common/utils/printRoutes.js +0 -1
  60. package/lib/esm/common/utils/resolveControllers.js +0 -1
  61. package/lib/esm/common/utils/setResponseHeaders.js +0 -1
  62. package/lib/esm/index.js +0 -1
  63. package/lib/esm/testing/FakeAdapter.js +0 -1
  64. package/lib/esm/testing/FakeResponse.js +0 -1
  65. package/lib/esm/testing/PlatformTest.js +0 -1
  66. package/lib/esm/testing/index.js +0 -1
  67. package/package.json +20 -17
  68. package/.barrelsby.json +0 -5
  69. package/lib/esm/common/builder/PlatformBuilder.js.map +0 -1
  70. package/lib/esm/common/builder/__fixtures__/PlatformCustom.js.map +0 -1
  71. package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js.map +0 -1
  72. package/lib/esm/common/config/interfaces/PlatformMulterSettings.js.map +0 -1
  73. package/lib/esm/common/config/interfaces/PlatformRouterSettings.js.map +0 -1
  74. package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js.map +0 -1
  75. package/lib/esm/common/config/services/PlatformConfiguration.js.map +0 -1
  76. package/lib/esm/common/constants/routerOptions.js.map +0 -1
  77. package/lib/esm/common/decorators/PlatformProvider.js.map +0 -1
  78. package/lib/esm/common/decorators/multer/multerFileSize.js.map +0 -1
  79. package/lib/esm/common/decorators/multer/multerOptions.js.map +0 -1
  80. package/lib/esm/common/decorators/multer/multipartFile.js.map +0 -1
  81. package/lib/esm/common/decorators/params/error.js.map +0 -1
  82. package/lib/esm/common/decorators/params/next.js.map +0 -1
  83. package/lib/esm/common/decorators/params/request.js.map +0 -1
  84. package/lib/esm/common/decorators/params/response.js.map +0 -1
  85. package/lib/esm/common/domain/AnyToPromiseWithCtx.js.map +0 -1
  86. package/lib/esm/common/domain/EndpointMetadata.js.map +0 -1
  87. package/lib/esm/common/domain/PlatformContext.js.map +0 -1
  88. package/lib/esm/common/domain/PlatformRouteDetails.js.map +0 -1
  89. package/lib/esm/common/exports.js.map +0 -1
  90. package/lib/esm/common/fn/adapter.js.map +0 -1
  91. package/lib/esm/common/fn/application.js.map +0 -1
  92. package/lib/esm/common/index.js.map +0 -1
  93. package/lib/esm/common/interfaces/AfterInit.js.map +0 -1
  94. package/lib/esm/common/interfaces/AfterListen.js.map +0 -1
  95. package/lib/esm/common/interfaces/AfterRoutesInit.js.map +0 -1
  96. package/lib/esm/common/interfaces/BeforeInit.js.map +0 -1
  97. package/lib/esm/common/interfaces/BeforeListen.js.map +0 -1
  98. package/lib/esm/common/interfaces/BeforeRoutesInit.js.map +0 -1
  99. package/lib/esm/common/interfaces/HandlerType.js.map +0 -1
  100. package/lib/esm/common/interfaces/IncomingEvent.js.map +0 -1
  101. package/lib/esm/common/interfaces/OnReady.js.map +0 -1
  102. package/lib/esm/common/interfaces/OnRequest.js.map +0 -1
  103. package/lib/esm/common/interfaces/OnResponse.js.map +0 -1
  104. package/lib/esm/common/interfaces/OnRoutesInit.js.map +0 -1
  105. package/lib/esm/common/interfaces/PlatformRouteOptions.js.map +0 -1
  106. package/lib/esm/common/interfaces/ResponseErrorObject.js.map +0 -1
  107. package/lib/esm/common/interfaces/Route.js.map +0 -1
  108. package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js.map +0 -1
  109. package/lib/esm/common/middlewares/PlatformMulterMiddleware.js.map +0 -1
  110. package/lib/esm/common/services/Platform.js.map +0 -1
  111. package/lib/esm/common/services/PlatformAdapter.js.map +0 -1
  112. package/lib/esm/common/services/PlatformApplication.js.map +0 -1
  113. package/lib/esm/common/services/PlatformHandler.js.map +0 -1
  114. package/lib/esm/common/services/PlatformRequest.js.map +0 -1
  115. package/lib/esm/common/services/PlatformResponse.js.map +0 -1
  116. package/lib/esm/common/utils/closeServer.js.map +0 -1
  117. package/lib/esm/common/utils/createContext.js.map +0 -1
  118. package/lib/esm/common/utils/createHttpServer.js.map +0 -1
  119. package/lib/esm/common/utils/createHttpsServer.js.map +0 -1
  120. package/lib/esm/common/utils/createInjector.js.map +0 -1
  121. package/lib/esm/common/utils/createServer.js.map +0 -1
  122. package/lib/esm/common/utils/ensureContext.js.map +0 -1
  123. package/lib/esm/common/utils/getConfiguration.js.map +0 -1
  124. package/lib/esm/common/utils/getStaticsOptions.js.map +0 -1
  125. package/lib/esm/common/utils/listenServer.js.map +0 -1
  126. package/lib/esm/common/utils/mapReturnedResponse.js.map +0 -1
  127. package/lib/esm/common/utils/printRoutes.js.map +0 -1
  128. package/lib/esm/common/utils/resolveControllers.js.map +0 -1
  129. package/lib/esm/common/utils/setResponseHeaders.js.map +0 -1
  130. package/lib/esm/index.js.map +0 -1
  131. package/lib/esm/testing/FakeAdapter.js.map +0 -1
  132. package/lib/esm/testing/FakeResponse.js.map +0 -1
  133. package/lib/esm/testing/PlatformTest.js.map +0 -1
  134. package/lib/esm/testing/index.js.map +0 -1
  135. package/src/common/builder/PlatformBuilder.spec.ts +0 -331
  136. package/src/common/builder/PlatformBuilder.ts +0 -426
  137. package/src/common/builder/__fixtures__/PlatformCustom.ts +0 -39
  138. package/src/common/config/interfaces/PlatformJsonMapperSettings.ts +0 -11
  139. package/src/common/config/interfaces/PlatformMulterSettings.ts +0 -22
  140. package/src/common/config/interfaces/PlatformRouterSettings.ts +0 -11
  141. package/src/common/config/interfaces/PlatformStaticsSettings.ts +0 -23
  142. package/src/common/config/services/PlatformConfiguration.spec.ts +0 -181
  143. package/src/common/config/services/PlatformConfiguration.ts +0 -158
  144. package/src/common/constants/routerOptions.ts +0 -1
  145. package/src/common/decorators/PlatformProvider.ts +0 -14
  146. package/src/common/decorators/multer/multerFileSize.ts +0 -31
  147. package/src/common/decorators/multer/multerOptions.ts +0 -45
  148. package/src/common/decorators/multer/multipartFile.ts +0 -51
  149. package/src/common/decorators/params/error.spec.ts +0 -15
  150. package/src/common/decorators/params/error.ts +0 -16
  151. package/src/common/decorators/params/next.spec.ts +0 -15
  152. package/src/common/decorators/params/next.ts +0 -31
  153. package/src/common/decorators/params/request.spec.ts +0 -50
  154. package/src/common/decorators/params/request.ts +0 -86
  155. package/src/common/decorators/params/response.spec.ts +0 -37
  156. package/src/common/decorators/params/response.ts +0 -74
  157. package/src/common/domain/AnyToPromiseWithCtx.spec.ts +0 -20
  158. package/src/common/domain/AnyToPromiseWithCtx.ts +0 -30
  159. package/src/common/domain/EndpointMetadata.ts +0 -1
  160. package/src/common/domain/PlatformContext.spec.ts +0 -124
  161. package/src/common/domain/PlatformContext.ts +0 -136
  162. package/src/common/domain/PlatformRouteDetails.ts +0 -7
  163. package/src/common/exports.ts +0 -4
  164. package/src/common/fn/adapter.ts +0 -42
  165. package/src/common/fn/application.ts +0 -11
  166. package/src/common/index.ts +0 -61
  167. package/src/common/interfaces/AfterInit.ts +0 -3
  168. package/src/common/interfaces/AfterListen.ts +0 -3
  169. package/src/common/interfaces/AfterRoutesInit.ts +0 -3
  170. package/src/common/interfaces/BeforeInit.ts +0 -3
  171. package/src/common/interfaces/BeforeListen.ts +0 -3
  172. package/src/common/interfaces/BeforeRoutesInit.ts +0 -3
  173. package/src/common/interfaces/HandlerType.ts +0 -12
  174. package/src/common/interfaces/IncomingEvent.ts +0 -6
  175. package/src/common/interfaces/OnReady.ts +0 -3
  176. package/src/common/interfaces/OnRequest.ts +0 -5
  177. package/src/common/interfaces/OnResponse.ts +0 -5
  178. package/src/common/interfaces/OnRoutesInit.ts +0 -3
  179. package/src/common/interfaces/PlatformRouteOptions.ts +0 -17
  180. package/src/common/interfaces/ResponseErrorObject.ts +0 -8
  181. package/src/common/interfaces/Route.ts +0 -12
  182. package/src/common/middlewares/PlatformAcceptMimesMiddleware.spec.ts +0 -215
  183. package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +0 -34
  184. package/src/common/middlewares/PlatformMulterMiddleware.ts +0 -6
  185. package/src/common/services/Platform.spec.ts +0 -145
  186. package/src/common/services/Platform.ts +0 -79
  187. package/src/common/services/PlatformAdapter.ts +0 -102
  188. package/src/common/services/PlatformApplication.spec.ts +0 -89
  189. package/src/common/services/PlatformApplication.ts +0 -38
  190. package/src/common/services/PlatformHandler.spec.ts +0 -152
  191. package/src/common/services/PlatformHandler.ts +0 -137
  192. package/src/common/services/PlatformRequest.spec.ts +0 -185
  193. package/src/common/services/PlatformRequest.ts +0 -182
  194. package/src/common/services/PlatformResponse.spec.ts +0 -364
  195. package/src/common/services/PlatformResponse.ts +0 -393
  196. package/src/common/utils/closeServer.spec.ts +0 -11
  197. package/src/common/utils/closeServer.ts +0 -8
  198. package/src/common/utils/createContext.spec.ts +0 -111
  199. package/src/common/utils/createContext.ts +0 -52
  200. package/src/common/utils/createHttpServer.spec.ts +0 -75
  201. package/src/common/utils/createHttpServer.ts +0 -16
  202. package/src/common/utils/createHttpsServer.spec.ts +0 -74
  203. package/src/common/utils/createHttpsServer.ts +0 -17
  204. package/src/common/utils/createInjector.spec.ts +0 -42
  205. package/src/common/utils/createInjector.ts +0 -25
  206. package/src/common/utils/createServer.ts +0 -52
  207. package/src/common/utils/ensureContext.spec.ts +0 -41
  208. package/src/common/utils/ensureContext.ts +0 -17
  209. package/src/common/utils/getConfiguration.spec.ts +0 -163
  210. package/src/common/utils/getConfiguration.ts +0 -34
  211. package/src/common/utils/getStaticsOptions.spec.ts +0 -27
  212. package/src/common/utils/getStaticsOptions.ts +0 -24
  213. package/src/common/utils/listenServer.ts +0 -18
  214. package/src/common/utils/mapReturnedResponse.spec.ts +0 -35
  215. package/src/common/utils/mapReturnedResponse.ts +0 -10
  216. package/src/common/utils/printRoutes.spec.ts +0 -19
  217. package/src/common/utils/printRoutes.ts +0 -40
  218. package/src/common/utils/resolveControllers.spec.ts +0 -40
  219. package/src/common/utils/resolveControllers.ts +0 -54
  220. package/src/common/utils/setResponseHeaders.spec.ts +0 -84
  221. package/src/common/utils/setResponseHeaders.ts +0 -31
  222. package/src/index.ts +0 -2
  223. package/src/testing/FakeAdapter.ts +0 -69
  224. package/src/testing/FakeResponse.ts +0 -115
  225. package/src/testing/PlatformTest.spec.ts +0 -53
  226. package/src/testing/PlatformTest.ts +0 -161
  227. package/src/testing/index.ts +0 -5
  228. package/vitest.config.mts +0 -25
@@ -1,54 +0,0 @@
1
- import {isArray, isClass} from "@tsed/core";
2
- import {GlobalProviders, Provider, ProviderType, type TokenProvider, type TokenRoute} from "@tsed/di";
3
-
4
- const lookupProperties = ["mount", "imports"];
5
-
6
- /**
7
- * @ignore
8
- */
9
- function getTokens(config: any): {route?: string; token: TokenProvider}[] {
10
- if (!config) {
11
- return [];
12
- }
13
-
14
- if (isArray(config)) {
15
- return config.filter(isClass).map((value: any) => {
16
- return {token: value};
17
- });
18
- }
19
-
20
- return Object.keys(config).reduce((list: any[], route: string) => {
21
- return [
22
- ...list,
23
- ...[]
24
- .concat(config[route])
25
- .filter(isClass)
26
- .map((token) => ({
27
- route,
28
- token
29
- }))
30
- ];
31
- }, []);
32
- }
33
-
34
- /**
35
- * @ignore
36
- */
37
- function resolveRecursively(providers: {token: TokenProvider; route?: string}[]) {
38
- return providers
39
- .map(({token}) => GlobalProviders.get(token))
40
- .filter((provider) => provider?.type === ProviderType.MODULE && provider.configuration)
41
- .flatMap((provider: Provider) => resolveControllers(provider.configuration));
42
- }
43
-
44
- /**
45
- * Return controllers and is base route according to his configuration in module configuration.
46
- *
47
- * @param settings
48
- * @ignore
49
- */
50
- export function resolveControllers(settings: Partial<TsED.Configuration>): TokenRoute[] {
51
- const providers = lookupProperties.flatMap((property) => getTokens(settings[property]));
52
-
53
- return [...resolveRecursively(providers), ...providers].filter((provider) => !!provider.route) as any[];
54
- }
@@ -1,84 +0,0 @@
1
- import {EndpointMetadata, Get, Redirect, Returns} from "@tsed/schema";
2
-
3
- import {PlatformTest} from "../../testing/PlatformTest.js";
4
- import {setResponseHeaders} from "./setResponseHeaders.js";
5
-
6
- describe("setResponseHeaders", () => {
7
- beforeEach(() => PlatformTest.create());
8
- afterEach(() => PlatformTest.reset());
9
-
10
- it("should set headers, status and contentType", async () => {
11
- class Test {
12
- @Get("/")
13
- @(Returns(200).Header("x-header", "test"))
14
- test() {}
15
- }
16
-
17
- const ctx = PlatformTest.createRequestContext();
18
- ctx.endpoint = EndpointMetadata.get(Test, "test");
19
-
20
- // WHEN
21
- await setResponseHeaders(ctx);
22
-
23
- // THEN
24
- expect(ctx.response.getHeaders()).toEqual({"x-request-id": "id", "x-header": "test"});
25
- expect(ctx.response.statusCode).toEqual(200);
26
- });
27
- it("should not set headers automatically if the value isn't declared", async () => {
28
- class Test {
29
- @Get("/")
30
- @(Returns(200).Header("x-header"))
31
- test() {}
32
- }
33
-
34
- const ctx = PlatformTest.createRequestContext();
35
- ctx.endpoint = EndpointMetadata.get(Test, "test");
36
-
37
- // WHEN
38
- await setResponseHeaders(ctx);
39
-
40
- // THEN
41
- expect(ctx.response.getHeaders()).toEqual({"x-request-id": "id"});
42
- expect(ctx.response.statusCode).toEqual(200);
43
- });
44
-
45
- it("should redirect", async () => {
46
- class Test {
47
- @Get("/")
48
- @Redirect(301, "/path")
49
- test() {}
50
- }
51
-
52
- const ctx = PlatformTest.createRequestContext();
53
- ctx.endpoint = EndpointMetadata.get(Test, "test");
54
-
55
- vi.spyOn(ctx.response, "redirect").mockReturnValue(undefined as any);
56
-
57
- // WHEN
58
- await setResponseHeaders(ctx);
59
-
60
- // THEN
61
- expect(ctx.response.redirect).toHaveBeenCalledWith(301, "/path");
62
- });
63
-
64
- it("should do nothing when headers is already sent", async () => {
65
- class Test {
66
- @Get("/")
67
- @(Returns(200).Header("x-header", "test"))
68
- test() {}
69
- }
70
-
71
- const ctx = PlatformTest.createRequestContext();
72
- ctx.response.raw.headersSent = true;
73
-
74
- ctx.endpoint = EndpointMetadata.get(Test, "test");
75
-
76
- vi.spyOn(ctx.response.raw, "set");
77
-
78
- // WHEN
79
- await setResponseHeaders(ctx);
80
-
81
- // THEN
82
- return expect(ctx.response.raw.set).not.toHaveBeenCalled();
83
- });
84
- });
@@ -1,31 +0,0 @@
1
- import {PlatformContext} from "../domain/PlatformContext.js";
2
-
3
- /**
4
- * @ignore
5
- */
6
- export function setResponseHeaders(ctx: PlatformContext) {
7
- const {response, endpoint} = ctx;
8
- const {operation} = endpoint;
9
-
10
- if (ctx.isDone()) {
11
- return;
12
- }
13
-
14
- if (!response.hasStatus()) {
15
- // apply status only if the isn't already modified
16
- response.status(operation.getStatus());
17
- }
18
-
19
- const statusCode = response.statusCode;
20
- const headers = operation.getHeadersOf(statusCode);
21
-
22
- Object.entries(headers).forEach(([key, item]: any[]) => {
23
- if (!response.get(key) && item.example !== undefined) {
24
- response.setHeader(key, String(item.example));
25
- }
26
- });
27
-
28
- if (operation.isRedirection(statusCode)) {
29
- response.redirect(statusCode, response.get("location"));
30
- }
31
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from "./common/index.js";
2
- export * from "./testing/index.js";
@@ -1,69 +0,0 @@
1
- import {injectable, type ProviderOpts} from "@tsed/di";
2
- import {type PlatformContextHandler, PlatformLayer} from "@tsed/platform-router";
3
-
4
- import {
5
- PlatformAdapter,
6
- PlatformContext,
7
- type PlatformMulter,
8
- type PlatformMulterSettings,
9
- type PlatformStaticsOptions
10
- } from "../common/index.js";
11
-
12
- export class FakeAdapter extends PlatformAdapter<any> {
13
- readonly NAME: string = "FAKE_ADAPTER";
14
- providers: ProviderOpts[] = [];
15
-
16
- static createFakeRawDriver() {
17
- // istanbul ignore next
18
- function FakeRawDriver() {}
19
-
20
- // istanbul ignore next
21
- function use() {
22
- return this;
23
- }
24
-
25
- FakeRawDriver.use = use;
26
- FakeRawDriver.all = use;
27
- FakeRawDriver.get = use;
28
- FakeRawDriver.patch = use;
29
- FakeRawDriver.post = use;
30
- FakeRawDriver.put = use;
31
- FakeRawDriver.head = use;
32
- FakeRawDriver.delete = use;
33
- FakeRawDriver.options = use;
34
-
35
- return FakeRawDriver;
36
- }
37
-
38
- createApp(): {app: any; callback(): any} {
39
- const app = FakeAdapter.createFakeRawDriver();
40
- return {
41
- app,
42
- callback() {
43
- return app;
44
- }
45
- };
46
- }
47
-
48
- multipart(options: PlatformMulterSettings): PlatformMulter {
49
- return {} as any;
50
- }
51
-
52
- statics(endpoint: string, options: PlatformStaticsOptions): any {
53
- return {};
54
- }
55
-
56
- bodyParser(type: string): any {
57
- return () => {};
58
- }
59
-
60
- mapLayers(layers: PlatformLayer[]) {}
61
-
62
- mapHandler(handler: PlatformContextHandler<PlatformContext>) {
63
- return handler;
64
- }
65
-
66
- useContext() {}
67
- }
68
-
69
- injectable(PlatformAdapter).class(FakeAdapter);
@@ -1,115 +0,0 @@
1
- import {EventEmitter} from "node:events";
2
-
3
- import cookie from "cookie";
4
-
5
- export class FakeResponse extends EventEmitter {
6
- headers: Record<string, unknown> = {};
7
- locals: Record<string, unknown> = {};
8
- statusCode: number = 200;
9
- data: any;
10
-
11
- constructor(opts = {}) {
12
- super();
13
-
14
- Object.assign(this, opts);
15
- }
16
-
17
- append(field: string, val: string | string[]) {
18
- const prev = this.get(field);
19
- let value = val;
20
-
21
- if (prev) {
22
- // concat the new and prev vals
23
- value = Array.isArray(prev) ? prev.concat(val) : Array.isArray(val) ? [prev].concat(val) : [prev, val];
24
- }
25
-
26
- return this.set(field, value);
27
- }
28
-
29
- status(code: number) {
30
- this.statusCode = code;
31
- return this;
32
- }
33
-
34
- contentType(content: string) {
35
- this.set("content-type", content);
36
- }
37
-
38
- contentLength(content: number) {
39
- this.set("content-length", content);
40
- }
41
-
42
- cookie(name: string, value: string, options?: TsED.SetCookieOpts) {
43
- const opts = {...options};
44
-
45
- const val = typeof value === "object" ? "j:" + JSON.stringify(value) : String(value);
46
-
47
- if (opts.maxAge != null) {
48
- const maxAge = opts.maxAge - 0;
49
-
50
- if (!isNaN(maxAge)) {
51
- opts.expires = new Date(Date.now() + maxAge);
52
- opts.maxAge = Math.floor(maxAge / 1000);
53
- }
54
- }
55
-
56
- if (opts.path == null) {
57
- opts.path = "/";
58
- }
59
-
60
- this.append("Set-Cookie", cookie.serialize(name, String(val), opts));
61
-
62
- return this;
63
- }
64
-
65
- clearCookie(name: string, options?: TsED.SetCookieOpts) {
66
- const opts = {path: "/", ...options, expires: new Date(1)};
67
- delete opts.maxAge;
68
- return this.cookie(name, "", opts);
69
- }
70
-
71
- redirect(status: number, path: string) {
72
- this.statusCode = status;
73
- this.set("location", path);
74
- }
75
-
76
- location(path: string) {
77
- this.set("location", path);
78
- }
79
-
80
- get(key: string) {
81
- return this.headers[key.toLowerCase()];
82
- }
83
-
84
- getHeaders() {
85
- return this.headers;
86
- }
87
-
88
- set(key: string, value: any) {
89
- this.headers[key.toLowerCase()] = value;
90
- return this;
91
- }
92
-
93
- setHeader(key: string, value: any) {
94
- this.headers[key.toLowerCase()] = value;
95
- return this;
96
- }
97
-
98
- send(data: any) {
99
- this.data = data;
100
- }
101
-
102
- json(data: any) {
103
- this.data = data;
104
- }
105
-
106
- write(chunk: any) {
107
- this.emit("data", chunk);
108
- }
109
-
110
- end(data: any) {
111
- data !== undefined && (this.data = data);
112
-
113
- this.emit("end");
114
- }
115
- }
@@ -1,53 +0,0 @@
1
- import {Configuration, Controller, InjectorService} from "@tsed/di";
2
- import {Get} from "@tsed/schema";
3
-
4
- import {PlatformContext} from "../common/domain/PlatformContext.js";
5
- import {PlatformTest} from "./PlatformTest.js";
6
-
7
- @Configuration({})
8
- class Server {}
9
-
10
- @Controller("/")
11
- class MyController {
12
- @Get("/")
13
- get() {
14
- return "hello";
15
- }
16
- }
17
-
18
- describe("PlatformTest", () => {
19
- let testContext: any;
20
-
21
- beforeEach(() => {
22
- testContext = {};
23
- });
24
-
25
- beforeEach(PlatformTest.create);
26
- afterEach(PlatformTest.reset);
27
-
28
- it("should get symbol from injector", () => {
29
- expect(PlatformTest.get(InjectorService)).toBeInstanceOf(InjectorService);
30
- });
31
-
32
- describe("createRequestContext", () => {
33
- it("should return request context", () => {
34
- expect(PlatformTest.createRequestContext()).toBeInstanceOf(PlatformContext);
35
- });
36
- });
37
-
38
- describe("invoke", () => {
39
- it("should return request context", async () => {
40
- class Test {
41
- called: boolean;
42
-
43
- $onInit() {
44
- this.called = true;
45
-
46
- return Promise.resolve();
47
- }
48
- }
49
-
50
- expect(await PlatformTest.invoke(Test)).toEqual({called: true});
51
- });
52
- });
53
- });
@@ -1,161 +0,0 @@
1
- import type {IncomingMessage, RequestListener, ServerResponse} from "node:http";
2
-
3
- import {DITest, injector, InjectorService} from "@tsed/di";
4
- import accepts from "accepts";
5
-
6
- import {PlatformBuilder} from "../common/builder/PlatformBuilder.js";
7
- import {PlatformContext, PlatformContextOptions} from "../common/domain/PlatformContext.js";
8
- import {adapter} from "../common/index.js";
9
- import {PlatformAdapter, PlatformBuilderSettings} from "../common/services/PlatformAdapter.js";
10
- import {PlatformApplication} from "../common/services/PlatformApplication.js";
11
- import {createInjector} from "../common/utils/createInjector.js";
12
- import {getConfiguration} from "../common/utils/getConfiguration.js";
13
- import {FakeAdapter} from "./FakeAdapter.js";
14
- import {FakeResponse} from "./FakeResponse.js";
15
-
16
- /**
17
- * @platform
18
- */
19
- export class PlatformTest extends DITest {
20
- static async create(settings: Partial<TsED.Configuration> = {}) {
21
- settings.imports = [
22
- {
23
- token: PlatformAdapter,
24
- useClass: FakeAdapter
25
- },
26
- ...(settings.imports || [])
27
- ];
28
-
29
- PlatformTest.createInjector(getConfiguration(settings));
30
- await DITest.createContainer();
31
- }
32
-
33
- /**
34
- * Create a new injector with the right default services
35
- */
36
- static createInjector(settings: any = {}): InjectorService {
37
- return createInjector(DITest.configure({httpPort: false, httpsPort: false, ...settings}));
38
- }
39
-
40
- /**
41
- * Load the server silently without listening port and configure it on test profile.
42
- * @decorator
43
- * @param mod
44
- * @param listen
45
- * @param settings
46
- * @returns {Promise<void>}
47
- */
48
- static bootstrap(
49
- mod: any,
50
- {
51
- listen,
52
- ...settings
53
- }: Partial<
54
- PlatformBuilderSettings & {
55
- listen: boolean;
56
- }
57
- > = {}
58
- ): () => Promise<void> {
59
- return async function before(): Promise<void> {
60
- let instance: PlatformBuilder;
61
- // @ts-ignore
62
- settings = DITest.configure(settings);
63
-
64
- adapter(settings.adapter || adapter());
65
-
66
- const configuration = getConfiguration(settings, mod);
67
-
68
- instance = await PlatformBuilder.build(mod, configuration).bootstrap();
69
-
70
- await instance.listen(!!listen);
71
- };
72
- }
73
-
74
- /**
75
- * It injects services into the test function where you can alter, spy on, and manipulate them.
76
- *
77
- * The inject function has two parameters
78
- *
79
- * * an array of Service dependency injection tokens,
80
- * * a test function whose parameters correspond exactly to each item in the injection token array.
81
- *
82
- * @deprecated use PlatformTest.injector.invoke instead
83
- * @param targets
84
- * @param func
85
- */
86
- static inject<T>(targets: any[], func: (...args: any[]) => Promise<T> | T): () => Promise<T> {
87
- return async (): Promise<T> => {
88
- await PlatformTest.create();
89
-
90
- const inj: InjectorService = injector();
91
- const deps = [];
92
-
93
- for (const target of targets) {
94
- deps.push(inj.has(target) ? inj.get(target) : await inj.invoke(target));
95
- }
96
-
97
- return func(...deps);
98
- };
99
- }
100
-
101
- /**
102
- * Return the raw application (express or koa).
103
- * Use this callback with SuperTest.
104
- *
105
- * ```typescript
106
- * let request: SuperTest.Agent;
107
- * beforeEach(PlatformTest.bootstrap(Server, {
108
- * mount: {
109
- * "/rest": [ProductsController]
110
- * }
111
- * }));
112
- * beforeEach(() => {
113
- * request = SuperTest(PlatformTest.callback());
114
- * });
115
- * ```
116
- */
117
- static callback(): RequestListener<typeof IncomingMessage, typeof ServerResponse> {
118
- return injector().get<PlatformApplication>(PlatformApplication)?.callback() as any;
119
- }
120
-
121
- static createRequest(options: any = {}): any {
122
- return {
123
- headers: {},
124
- method: "GET",
125
- url: "/",
126
- query: {},
127
- get(key: string) {
128
- return this.headers[key.toLowerCase()];
129
- },
130
- accepts(mime?: string | string[]) {
131
- return accepts(this).types([].concat(mime as never));
132
- },
133
- ...options
134
- };
135
- }
136
-
137
- static createResponse(options: any = {}): any {
138
- return new FakeResponse(options);
139
- }
140
-
141
- static createRequestContext(options: Partial<PlatformContextOptions & any> = {}) {
142
- const event = {
143
- ...options.event,
144
- request: options?.request?.request || options?.event?.request || PlatformTest.createRequest(),
145
- response: options?.response?.response || options?.event?.response || PlatformTest.createResponse()
146
- };
147
-
148
- const $ctx = new PlatformContext({
149
- id: "id",
150
- url: "/",
151
- ...options,
152
- event
153
- });
154
-
155
- if (options.endpoint) {
156
- $ctx.endpoint = options.endpoint;
157
- }
158
-
159
- return $ctx;
160
- }
161
- }
@@ -1,5 +0,0 @@
1
- /**
2
- * @file Automatically generated by @tsed/barrels.
3
- */
4
- export * from "./FakeAdapter.js";
5
- export * from "./PlatformTest.js";
package/vitest.config.mts DELETED
@@ -1,25 +0,0 @@
1
- // @ts-ignore
2
- import {presets} from "@tsed/vitest/presets";
3
- import {defineConfig} from "vitest/config";
4
-
5
- export default defineConfig(
6
- {
7
- ...presets,
8
- test: {
9
- ...presets.test,
10
- coverage: {
11
- ...presets.test.coverage,
12
- exclude:[
13
- ...presets.test.coverage.exclude ||[],
14
- "**/Fake*"
15
- ],
16
- thresholds: {
17
- statements: 0,
18
- branches: 0,
19
- functions: 0,
20
- lines: 0
21
- }
22
- }
23
- }
24
- }
25
- );