@tsed/platform-http 8.11.0 → 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.
- package/lib/esm/common/builder/PlatformBuilder.js +0 -1
- package/lib/esm/common/builder/__fixtures__/PlatformCustom.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformMulterSettings.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformRouterSettings.js +0 -1
- package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js +0 -1
- package/lib/esm/common/config/services/PlatformConfiguration.js +0 -1
- package/lib/esm/common/constants/routerOptions.js +0 -1
- package/lib/esm/common/decorators/PlatformProvider.js +0 -1
- package/lib/esm/common/decorators/multer/multerFileSize.js +0 -1
- package/lib/esm/common/decorators/multer/multerOptions.js +0 -1
- package/lib/esm/common/decorators/multer/multipartFile.js +0 -1
- package/lib/esm/common/decorators/params/error.js +0 -1
- package/lib/esm/common/decorators/params/next.js +0 -1
- package/lib/esm/common/decorators/params/request.js +0 -1
- package/lib/esm/common/decorators/params/response.js +0 -1
- package/lib/esm/common/domain/AnyToPromiseWithCtx.js +0 -1
- package/lib/esm/common/domain/EndpointMetadata.js +0 -1
- package/lib/esm/common/domain/PlatformContext.js +0 -1
- package/lib/esm/common/domain/PlatformRouteDetails.js +0 -1
- package/lib/esm/common/exports.js +0 -1
- package/lib/esm/common/fn/adapter.js +0 -1
- package/lib/esm/common/fn/application.js +0 -1
- package/lib/esm/common/index.js +0 -1
- package/lib/esm/common/interfaces/AfterInit.js +0 -1
- package/lib/esm/common/interfaces/AfterListen.js +0 -1
- package/lib/esm/common/interfaces/AfterRoutesInit.js +0 -1
- package/lib/esm/common/interfaces/BeforeInit.js +0 -1
- package/lib/esm/common/interfaces/BeforeListen.js +0 -1
- package/lib/esm/common/interfaces/BeforeRoutesInit.js +0 -1
- package/lib/esm/common/interfaces/HandlerType.js +0 -1
- package/lib/esm/common/interfaces/IncomingEvent.js +0 -1
- package/lib/esm/common/interfaces/OnReady.js +0 -1
- package/lib/esm/common/interfaces/OnRequest.js +0 -1
- package/lib/esm/common/interfaces/OnResponse.js +0 -1
- package/lib/esm/common/interfaces/OnRoutesInit.js +0 -1
- package/lib/esm/common/interfaces/PlatformRouteOptions.js +0 -1
- package/lib/esm/common/interfaces/ResponseErrorObject.js +0 -1
- package/lib/esm/common/interfaces/Route.js +0 -1
- package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js +0 -1
- package/lib/esm/common/middlewares/PlatformMulterMiddleware.js +0 -1
- package/lib/esm/common/services/Platform.js +0 -1
- package/lib/esm/common/services/PlatformAdapter.js +0 -1
- package/lib/esm/common/services/PlatformApplication.js +0 -1
- package/lib/esm/common/services/PlatformHandler.js +0 -1
- package/lib/esm/common/services/PlatformRequest.js +0 -1
- package/lib/esm/common/services/PlatformResponse.js +0 -1
- package/lib/esm/common/utils/closeServer.js +0 -1
- package/lib/esm/common/utils/createContext.js +0 -1
- package/lib/esm/common/utils/createHttpServer.js +0 -1
- package/lib/esm/common/utils/createHttpsServer.js +0 -1
- package/lib/esm/common/utils/createInjector.js +0 -1
- package/lib/esm/common/utils/createServer.js +0 -1
- package/lib/esm/common/utils/ensureContext.js +0 -1
- package/lib/esm/common/utils/getConfiguration.js +0 -1
- package/lib/esm/common/utils/getStaticsOptions.js +0 -1
- package/lib/esm/common/utils/listenServer.js +0 -1
- package/lib/esm/common/utils/mapReturnedResponse.js +0 -1
- package/lib/esm/common/utils/printRoutes.js +0 -1
- package/lib/esm/common/utils/resolveControllers.js +0 -1
- package/lib/esm/common/utils/setResponseHeaders.js +0 -1
- package/lib/esm/index.js +0 -1
- package/lib/esm/testing/FakeAdapter.js +0 -1
- package/lib/esm/testing/FakeResponse.js +0 -1
- package/lib/esm/testing/PlatformTest.js +0 -1
- package/lib/esm/testing/index.js +0 -1
- package/package.json +20 -17
- package/.barrelsby.json +0 -5
- package/lib/esm/common/builder/PlatformBuilder.js.map +0 -1
- package/lib/esm/common/builder/__fixtures__/PlatformCustom.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformJsonMapperSettings.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformMulterSettings.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformRouterSettings.js.map +0 -1
- package/lib/esm/common/config/interfaces/PlatformStaticsSettings.js.map +0 -1
- package/lib/esm/common/config/services/PlatformConfiguration.js.map +0 -1
- package/lib/esm/common/constants/routerOptions.js.map +0 -1
- package/lib/esm/common/decorators/PlatformProvider.js.map +0 -1
- package/lib/esm/common/decorators/multer/multerFileSize.js.map +0 -1
- package/lib/esm/common/decorators/multer/multerOptions.js.map +0 -1
- package/lib/esm/common/decorators/multer/multipartFile.js.map +0 -1
- package/lib/esm/common/decorators/params/error.js.map +0 -1
- package/lib/esm/common/decorators/params/next.js.map +0 -1
- package/lib/esm/common/decorators/params/request.js.map +0 -1
- package/lib/esm/common/decorators/params/response.js.map +0 -1
- package/lib/esm/common/domain/AnyToPromiseWithCtx.js.map +0 -1
- package/lib/esm/common/domain/EndpointMetadata.js.map +0 -1
- package/lib/esm/common/domain/PlatformContext.js.map +0 -1
- package/lib/esm/common/domain/PlatformRouteDetails.js.map +0 -1
- package/lib/esm/common/exports.js.map +0 -1
- package/lib/esm/common/fn/adapter.js.map +0 -1
- package/lib/esm/common/fn/application.js.map +0 -1
- package/lib/esm/common/index.js.map +0 -1
- package/lib/esm/common/interfaces/AfterInit.js.map +0 -1
- package/lib/esm/common/interfaces/AfterListen.js.map +0 -1
- package/lib/esm/common/interfaces/AfterRoutesInit.js.map +0 -1
- package/lib/esm/common/interfaces/BeforeInit.js.map +0 -1
- package/lib/esm/common/interfaces/BeforeListen.js.map +0 -1
- package/lib/esm/common/interfaces/BeforeRoutesInit.js.map +0 -1
- package/lib/esm/common/interfaces/HandlerType.js.map +0 -1
- package/lib/esm/common/interfaces/IncomingEvent.js.map +0 -1
- package/lib/esm/common/interfaces/OnReady.js.map +0 -1
- package/lib/esm/common/interfaces/OnRequest.js.map +0 -1
- package/lib/esm/common/interfaces/OnResponse.js.map +0 -1
- package/lib/esm/common/interfaces/OnRoutesInit.js.map +0 -1
- package/lib/esm/common/interfaces/PlatformRouteOptions.js.map +0 -1
- package/lib/esm/common/interfaces/ResponseErrorObject.js.map +0 -1
- package/lib/esm/common/interfaces/Route.js.map +0 -1
- package/lib/esm/common/middlewares/PlatformAcceptMimesMiddleware.js.map +0 -1
- package/lib/esm/common/middlewares/PlatformMulterMiddleware.js.map +0 -1
- package/lib/esm/common/services/Platform.js.map +0 -1
- package/lib/esm/common/services/PlatformAdapter.js.map +0 -1
- package/lib/esm/common/services/PlatformApplication.js.map +0 -1
- package/lib/esm/common/services/PlatformHandler.js.map +0 -1
- package/lib/esm/common/services/PlatformRequest.js.map +0 -1
- package/lib/esm/common/services/PlatformResponse.js.map +0 -1
- package/lib/esm/common/utils/closeServer.js.map +0 -1
- package/lib/esm/common/utils/createContext.js.map +0 -1
- package/lib/esm/common/utils/createHttpServer.js.map +0 -1
- package/lib/esm/common/utils/createHttpsServer.js.map +0 -1
- package/lib/esm/common/utils/createInjector.js.map +0 -1
- package/lib/esm/common/utils/createServer.js.map +0 -1
- package/lib/esm/common/utils/ensureContext.js.map +0 -1
- package/lib/esm/common/utils/getConfiguration.js.map +0 -1
- package/lib/esm/common/utils/getStaticsOptions.js.map +0 -1
- package/lib/esm/common/utils/listenServer.js.map +0 -1
- package/lib/esm/common/utils/mapReturnedResponse.js.map +0 -1
- package/lib/esm/common/utils/printRoutes.js.map +0 -1
- package/lib/esm/common/utils/resolveControllers.js.map +0 -1
- package/lib/esm/common/utils/setResponseHeaders.js.map +0 -1
- package/lib/esm/index.js.map +0 -1
- package/lib/esm/testing/FakeAdapter.js.map +0 -1
- package/lib/esm/testing/FakeResponse.js.map +0 -1
- package/lib/esm/testing/PlatformTest.js.map +0 -1
- package/lib/esm/testing/index.js.map +0 -1
- package/src/common/builder/PlatformBuilder.spec.ts +0 -331
- package/src/common/builder/PlatformBuilder.ts +0 -426
- package/src/common/builder/__fixtures__/PlatformCustom.ts +0 -39
- package/src/common/config/interfaces/PlatformJsonMapperSettings.ts +0 -11
- package/src/common/config/interfaces/PlatformMulterSettings.ts +0 -22
- package/src/common/config/interfaces/PlatformRouterSettings.ts +0 -11
- package/src/common/config/interfaces/PlatformStaticsSettings.ts +0 -23
- package/src/common/config/services/PlatformConfiguration.spec.ts +0 -181
- package/src/common/config/services/PlatformConfiguration.ts +0 -158
- package/src/common/constants/routerOptions.ts +0 -1
- package/src/common/decorators/PlatformProvider.ts +0 -14
- package/src/common/decorators/multer/multerFileSize.ts +0 -31
- package/src/common/decorators/multer/multerOptions.ts +0 -45
- package/src/common/decorators/multer/multipartFile.ts +0 -51
- package/src/common/decorators/params/error.spec.ts +0 -15
- package/src/common/decorators/params/error.ts +0 -16
- package/src/common/decorators/params/next.spec.ts +0 -15
- package/src/common/decorators/params/next.ts +0 -31
- package/src/common/decorators/params/request.spec.ts +0 -50
- package/src/common/decorators/params/request.ts +0 -86
- package/src/common/decorators/params/response.spec.ts +0 -37
- package/src/common/decorators/params/response.ts +0 -74
- package/src/common/domain/AnyToPromiseWithCtx.spec.ts +0 -20
- package/src/common/domain/AnyToPromiseWithCtx.ts +0 -30
- package/src/common/domain/EndpointMetadata.ts +0 -1
- package/src/common/domain/PlatformContext.spec.ts +0 -124
- package/src/common/domain/PlatformContext.ts +0 -136
- package/src/common/domain/PlatformRouteDetails.ts +0 -7
- package/src/common/exports.ts +0 -4
- package/src/common/fn/adapter.ts +0 -42
- package/src/common/fn/application.ts +0 -11
- package/src/common/index.ts +0 -61
- package/src/common/interfaces/AfterInit.ts +0 -3
- package/src/common/interfaces/AfterListen.ts +0 -3
- package/src/common/interfaces/AfterRoutesInit.ts +0 -3
- package/src/common/interfaces/BeforeInit.ts +0 -3
- package/src/common/interfaces/BeforeListen.ts +0 -3
- package/src/common/interfaces/BeforeRoutesInit.ts +0 -3
- package/src/common/interfaces/HandlerType.ts +0 -12
- package/src/common/interfaces/IncomingEvent.ts +0 -6
- package/src/common/interfaces/OnReady.ts +0 -3
- package/src/common/interfaces/OnRequest.ts +0 -5
- package/src/common/interfaces/OnResponse.ts +0 -5
- package/src/common/interfaces/OnRoutesInit.ts +0 -3
- package/src/common/interfaces/PlatformRouteOptions.ts +0 -17
- package/src/common/interfaces/ResponseErrorObject.ts +0 -8
- package/src/common/interfaces/Route.ts +0 -12
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.spec.ts +0 -215
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +0 -34
- package/src/common/middlewares/PlatformMulterMiddleware.ts +0 -6
- package/src/common/services/Platform.spec.ts +0 -145
- package/src/common/services/Platform.ts +0 -79
- package/src/common/services/PlatformAdapter.ts +0 -102
- package/src/common/services/PlatformApplication.spec.ts +0 -89
- package/src/common/services/PlatformApplication.ts +0 -38
- package/src/common/services/PlatformHandler.spec.ts +0 -152
- package/src/common/services/PlatformHandler.ts +0 -137
- package/src/common/services/PlatformRequest.spec.ts +0 -185
- package/src/common/services/PlatformRequest.ts +0 -182
- package/src/common/services/PlatformResponse.spec.ts +0 -364
- package/src/common/services/PlatformResponse.ts +0 -393
- package/src/common/utils/closeServer.spec.ts +0 -11
- package/src/common/utils/closeServer.ts +0 -8
- package/src/common/utils/createContext.spec.ts +0 -111
- package/src/common/utils/createContext.ts +0 -52
- package/src/common/utils/createHttpServer.spec.ts +0 -75
- package/src/common/utils/createHttpServer.ts +0 -16
- package/src/common/utils/createHttpsServer.spec.ts +0 -74
- package/src/common/utils/createHttpsServer.ts +0 -17
- package/src/common/utils/createInjector.spec.ts +0 -42
- package/src/common/utils/createInjector.ts +0 -25
- package/src/common/utils/createServer.ts +0 -52
- package/src/common/utils/ensureContext.spec.ts +0 -41
- package/src/common/utils/ensureContext.ts +0 -17
- package/src/common/utils/getConfiguration.spec.ts +0 -163
- package/src/common/utils/getConfiguration.ts +0 -34
- package/src/common/utils/getStaticsOptions.spec.ts +0 -27
- package/src/common/utils/getStaticsOptions.ts +0 -24
- package/src/common/utils/listenServer.ts +0 -18
- package/src/common/utils/mapReturnedResponse.spec.ts +0 -35
- package/src/common/utils/mapReturnedResponse.ts +0 -10
- package/src/common/utils/printRoutes.spec.ts +0 -19
- package/src/common/utils/printRoutes.ts +0 -40
- package/src/common/utils/resolveControllers.spec.ts +0 -40
- package/src/common/utils/resolveControllers.ts +0 -54
- package/src/common/utils/setResponseHeaders.spec.ts +0 -84
- package/src/common/utils/setResponseHeaders.ts +0 -31
- package/src/index.ts +0 -2
- package/src/testing/FakeAdapter.ts +0 -69
- package/src/testing/FakeResponse.ts +0 -115
- package/src/testing/PlatformTest.spec.ts +0 -53
- package/src/testing/PlatformTest.ts +0 -161
- package/src/testing/index.ts +0 -5
- 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,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
|
-
}
|
package/src/testing/index.ts
DELETED
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
|
-
);
|