@tsed/platform-http 8.0.1 → 8.0.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.
- package/package.json +18 -16
- package/src/common/builder/PlatformBuilder.spec.ts +331 -0
- package/src/common/builder/PlatformBuilder.ts +426 -0
- package/src/common/builder/__fixtures__/PlatformCustom.ts +39 -0
- package/src/common/config/interfaces/PlatformJsonMapperSettings.ts +11 -0
- package/src/common/config/interfaces/PlatformMulterSettings.ts +21 -0
- package/src/common/config/interfaces/PlatformRouterSettings.ts +11 -0
- package/src/common/config/interfaces/PlatformStaticsSettings.ts +23 -0
- package/src/common/config/services/PlatformConfiguration.spec.ts +181 -0
- package/src/common/config/services/PlatformConfiguration.ts +157 -0
- package/src/common/constants/routerOptions.ts +1 -0
- package/src/common/decorators/PlatformProvider.ts +14 -0
- package/src/common/decorators/multer/multerFileSize.spec.ts +22 -0
- package/src/common/decorators/multer/multerFileSize.ts +32 -0
- package/src/common/decorators/multer/multerOptions.spec.ts +21 -0
- package/src/common/decorators/multer/multerOptions.ts +53 -0
- package/src/common/decorators/multer/multipartFile.spec.ts +259 -0
- package/src/common/decorators/multer/multipartFile.ts +88 -0
- package/src/common/decorators/params/error.spec.ts +15 -0
- package/src/common/decorators/params/error.ts +16 -0
- package/src/common/decorators/params/next.spec.ts +15 -0
- package/src/common/decorators/params/next.ts +31 -0
- package/src/common/decorators/params/request.spec.ts +49 -0
- package/src/common/decorators/params/request.ts +85 -0
- package/src/common/decorators/params/response.spec.ts +36 -0
- package/src/common/decorators/params/response.ts +73 -0
- package/src/common/domain/AnyToPromiseWithCtx.spec.ts +20 -0
- package/src/common/domain/AnyToPromiseWithCtx.ts +30 -0
- package/src/common/domain/EndpointMetadata.ts +1 -0
- package/src/common/domain/PlatformContext.spec.ts +124 -0
- package/src/common/domain/PlatformContext.ts +135 -0
- package/src/common/domain/PlatformRouteDetails.ts +7 -0
- package/src/common/exports.ts +4 -0
- package/src/common/fn/adapter.ts +23 -0
- package/src/common/fn/application.ts +11 -0
- package/src/common/index.ts +62 -0
- package/src/common/interfaces/AfterInit.ts +3 -0
- package/src/common/interfaces/AfterListen.ts +3 -0
- package/src/common/interfaces/AfterRoutesInit.ts +3 -0
- package/src/common/interfaces/BeforeInit.ts +3 -0
- package/src/common/interfaces/BeforeListen.ts +3 -0
- package/src/common/interfaces/BeforeRoutesInit.ts +3 -0
- package/src/common/interfaces/HandlerType.ts +12 -0
- package/src/common/interfaces/IncomingEvent.ts +6 -0
- package/src/common/interfaces/OnReady.ts +3 -0
- package/src/common/interfaces/OnRequest.ts +5 -0
- package/src/common/interfaces/OnResponse.ts +5 -0
- package/src/common/interfaces/OnRoutesInit.ts +3 -0
- package/src/common/interfaces/PlatformRouteOptions.ts +17 -0
- package/src/common/interfaces/ResponseErrorObject.ts +8 -0
- package/src/common/interfaces/Route.ts +12 -0
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.spec.ts +215 -0
- package/src/common/middlewares/PlatformAcceptMimesMiddleware.ts +24 -0
- package/src/common/middlewares/PlatformMulterMiddleware.spec.ts +97 -0
- package/src/common/middlewares/PlatformMulterMiddleware.ts +59 -0
- package/src/common/services/Platform.spec.ts +145 -0
- package/src/common/services/Platform.ts +79 -0
- package/src/common/services/PlatformAdapter.ts +89 -0
- package/src/common/services/PlatformApplication.spec.ts +101 -0
- package/src/common/services/PlatformApplication.ts +54 -0
- package/src/common/services/PlatformHandler.spec.ts +152 -0
- package/src/common/services/PlatformHandler.ts +140 -0
- package/src/common/services/PlatformMiddlewaresChain.spec.ts +96 -0
- package/src/common/services/PlatformMiddlewaresChain.ts +55 -0
- package/src/common/services/PlatformRequest.spec.ts +185 -0
- package/src/common/services/PlatformRequest.ts +181 -0
- package/src/common/services/PlatformResponse.spec.ts +344 -0
- package/src/common/services/PlatformResponse.ts +388 -0
- package/src/common/utils/closeServer.spec.ts +11 -0
- package/src/common/utils/closeServer.ts +7 -0
- package/src/common/utils/createContext.spec.ts +111 -0
- package/src/common/utils/createContext.ts +52 -0
- package/src/common/utils/createHttpServer.spec.ts +74 -0
- package/src/common/utils/createHttpServer.ts +15 -0
- package/src/common/utils/createHttpsServer.spec.ts +73 -0
- package/src/common/utils/createHttpsServer.ts +16 -0
- package/src/common/utils/createInjector.spec.ts +42 -0
- package/src/common/utils/createInjector.ts +55 -0
- package/src/common/utils/createServer.ts +51 -0
- package/src/common/utils/ensureContext.spec.ts +41 -0
- package/src/common/utils/ensureContext.ts +17 -0
- package/src/common/utils/getConfiguration.spec.ts +163 -0
- package/src/common/utils/getConfiguration.ts +34 -0
- package/src/common/utils/getStaticsOptions.spec.ts +27 -0
- package/src/common/utils/getStaticsOptions.ts +24 -0
- package/src/common/utils/listenServer.ts +17 -0
- package/src/common/utils/mapReturnedResponse.spec.ts +35 -0
- package/src/common/utils/mapReturnedResponse.ts +10 -0
- package/src/common/utils/printRoutes.spec.ts +19 -0
- package/src/common/utils/printRoutes.ts +40 -0
- package/src/common/utils/resolveControllers.spec.ts +40 -0
- package/src/common/utils/resolveControllers.ts +54 -0
- package/src/common/utils/setResponseHeaders.spec.ts +84 -0
- package/src/common/utils/setResponseHeaders.ts +31 -0
- package/src/index.ts +2 -0
- package/src/testing/FakeAdapter.ts +69 -0
- package/src/testing/FakeResponse.ts +72 -0
- package/src/testing/PlatformTest.spec.ts +53 -0
- package/src/testing/PlatformTest.ts +157 -0
- package/src/testing/index.ts +5 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {configuration, destroyInjector, injector, InjectorService} from "@tsed/di";
|
|
2
|
+
import Https from "https";
|
|
3
|
+
|
|
4
|
+
import {createHttpsServer} from "./createHttpsServer.js";
|
|
5
|
+
|
|
6
|
+
describe("createHttpsServer", () => {
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
vi.resetAllMocks();
|
|
9
|
+
});
|
|
10
|
+
afterEach(() => destroyInjector());
|
|
11
|
+
it("should create an instance of Https (Https port true)", async () => {
|
|
12
|
+
configuration().set("httpsPort", true);
|
|
13
|
+
|
|
14
|
+
const fn: any = vi.fn();
|
|
15
|
+
|
|
16
|
+
const listener = createHttpsServer(fn)!;
|
|
17
|
+
|
|
18
|
+
expect(!!injector().get(Https.Server)).toEqual(true);
|
|
19
|
+
expect(listener).toBeInstanceOf(Function);
|
|
20
|
+
|
|
21
|
+
const server = injector().get(Https.Server)!;
|
|
22
|
+
|
|
23
|
+
vi.spyOn(injector().logger, "info").mockReturnValue(undefined);
|
|
24
|
+
vi.spyOn(injector().logger, "debug").mockReturnValue(undefined);
|
|
25
|
+
vi.spyOn(server, "listen").mockReturnValue(undefined as never);
|
|
26
|
+
vi.spyOn(server, "address").mockReturnValue({port: 8089, address: "0.0.0.0"} as never);
|
|
27
|
+
vi.spyOn(server, "on").mockImplementation(((event: string, cb: any) => {
|
|
28
|
+
if (event === "listening") {
|
|
29
|
+
cb();
|
|
30
|
+
}
|
|
31
|
+
}) as never);
|
|
32
|
+
|
|
33
|
+
await listener();
|
|
34
|
+
|
|
35
|
+
expect(server.listen).toHaveBeenCalledWith(true, "0.0.0.0");
|
|
36
|
+
expect(injector().logger.info).toHaveBeenCalledWith("Listen server on https://0.0.0.0:8089");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should create a raw object (Https port false)", () => {
|
|
40
|
+
configuration().set("httpsPort", false);
|
|
41
|
+
|
|
42
|
+
const fn: any = vi.fn();
|
|
43
|
+
|
|
44
|
+
const listener = createHttpsServer(fn);
|
|
45
|
+
|
|
46
|
+
expect(injector().get(Https.Server)).toEqual(null);
|
|
47
|
+
|
|
48
|
+
expect(listener).toBeUndefined();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("should create an instance of Https (https port 0)", () => {
|
|
52
|
+
configuration().set("httpsPort", 0);
|
|
53
|
+
|
|
54
|
+
const fn: any = vi.fn();
|
|
55
|
+
|
|
56
|
+
const listener = createHttpsServer(fn);
|
|
57
|
+
|
|
58
|
+
expect(!!injector().get(Https.Server)).toEqual(true);
|
|
59
|
+
|
|
60
|
+
expect(listener).toBeInstanceOf(Function);
|
|
61
|
+
});
|
|
62
|
+
it("should create an instance of Https (https port + address)", () => {
|
|
63
|
+
configuration().set("httpsPort", "0.0.0.0:8080");
|
|
64
|
+
|
|
65
|
+
const fn: any = vi.fn();
|
|
66
|
+
|
|
67
|
+
const listener = createHttpsServer(fn);
|
|
68
|
+
|
|
69
|
+
expect(!!injector().get(Https.Server)).toEqual(true);
|
|
70
|
+
|
|
71
|
+
expect(listener).toBeInstanceOf(Function);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {configuration, constant, InjectorService} from "@tsed/di";
|
|
2
|
+
import Http from "http";
|
|
3
|
+
import Https from "https";
|
|
4
|
+
|
|
5
|
+
import {createServer} from "./createServer.js";
|
|
6
|
+
|
|
7
|
+
export function createHttpsServer(requestListener?: Http.RequestListener) {
|
|
8
|
+
const httpsOptions = configuration().get("httpsOptions");
|
|
9
|
+
|
|
10
|
+
return createServer({
|
|
11
|
+
type: "https",
|
|
12
|
+
token: Https.Server,
|
|
13
|
+
port: constant("httpsPort"),
|
|
14
|
+
server: () => Https.createServer(httpsOptions, requestListener)
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {Env} from "@tsed/core";
|
|
2
|
+
import {$log} from "@tsed/logger";
|
|
3
|
+
|
|
4
|
+
import {FakeAdapter} from "../../testing/FakeAdapter.js";
|
|
5
|
+
import {PlatformConfiguration} from "../config/services/PlatformConfiguration.js";
|
|
6
|
+
import {PlatformAdapter} from "../services/PlatformAdapter.js";
|
|
7
|
+
import {createInjector} from "./createInjector.js";
|
|
8
|
+
|
|
9
|
+
describe("createInjector", () => {
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
vi.spyOn($log, "stop").mockReturnValue(undefined as any);
|
|
12
|
+
});
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.resetAllMocks();
|
|
15
|
+
});
|
|
16
|
+
it("should create injector and stop logger in env Test", () => {
|
|
17
|
+
const settings = {
|
|
18
|
+
test: "test",
|
|
19
|
+
env: Env.TEST,
|
|
20
|
+
adapter: FakeAdapter
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const injector = createInjector(settings);
|
|
24
|
+
|
|
25
|
+
expect(injector.settings).toBeInstanceOf(PlatformConfiguration);
|
|
26
|
+
expect(injector.settings.get("test")).toEqual("test");
|
|
27
|
+
expect(injector.logger).toEqual($log);
|
|
28
|
+
expect(injector.get(PlatformAdapter)).toBeInstanceOf(FakeAdapter);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("should create injector", () => {
|
|
32
|
+
const settings = {
|
|
33
|
+
test: "test",
|
|
34
|
+
env: Env.PROD,
|
|
35
|
+
adapter: FakeAdapter
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const injector = createInjector(settings);
|
|
39
|
+
|
|
40
|
+
expect(injector.logger.stop).not.toHaveBeenCalled();
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {toMap} from "@tsed/core";
|
|
2
|
+
import {injector, ProviderOpts, setLoggerConfiguration} from "@tsed/di";
|
|
3
|
+
import {$log} from "@tsed/logger";
|
|
4
|
+
|
|
5
|
+
import {PlatformConfiguration} from "../config/services/PlatformConfiguration.js";
|
|
6
|
+
import {adapter as $adapter} from "../fn/adapter.js";
|
|
7
|
+
import {Platform} from "../services/Platform.js";
|
|
8
|
+
import {PlatformAdapter} from "../services/PlatformAdapter.js";
|
|
9
|
+
import {PlatformApplication} from "../services/PlatformApplication.js";
|
|
10
|
+
import {PlatformHandler} from "../services/PlatformHandler.js";
|
|
11
|
+
import {PlatformRequest} from "../services/PlatformRequest.js";
|
|
12
|
+
import {PlatformResponse} from "../services/PlatformResponse.js";
|
|
13
|
+
|
|
14
|
+
$log.name = "TSED";
|
|
15
|
+
|
|
16
|
+
const DEFAULT_PROVIDERS = [
|
|
17
|
+
{token: PlatformHandler},
|
|
18
|
+
{token: PlatformResponse},
|
|
19
|
+
{token: PlatformRequest},
|
|
20
|
+
{token: PlatformApplication},
|
|
21
|
+
{token: Platform}
|
|
22
|
+
];
|
|
23
|
+
|
|
24
|
+
export function createInjector(settings: Partial<TsED.Configuration>) {
|
|
25
|
+
const inj = injector();
|
|
26
|
+
inj.addProvider(PlatformConfiguration);
|
|
27
|
+
|
|
28
|
+
inj.settings = inj.invoke(PlatformConfiguration);
|
|
29
|
+
inj.logger = $log;
|
|
30
|
+
inj.settings.set(settings);
|
|
31
|
+
|
|
32
|
+
setLoggerConfiguration();
|
|
33
|
+
|
|
34
|
+
const adapterToken = $adapter(settings.adapter);
|
|
35
|
+
|
|
36
|
+
inj
|
|
37
|
+
.addProvider(PlatformAdapter, {
|
|
38
|
+
useClass: adapterToken
|
|
39
|
+
})
|
|
40
|
+
.alias(PlatformAdapter, "PlatformAdapter");
|
|
41
|
+
|
|
42
|
+
const adapter = inj.invoke(PlatformAdapter);
|
|
43
|
+
|
|
44
|
+
inj.settings.set("PLATFORM_NAME", settings.PLATFORM_NAME || adapter.NAME);
|
|
45
|
+
|
|
46
|
+
const instance = inj.get<PlatformAdapter>(PlatformAdapter)!;
|
|
47
|
+
|
|
48
|
+
toMap<any, ProviderOpts>(instance.providers, "token").forEach((provider, token) => {
|
|
49
|
+
inj.addProvider(token, provider);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
DEFAULT_PROVIDERS.map((provider) => inj.get(provider.token));
|
|
53
|
+
|
|
54
|
+
return inj;
|
|
55
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {getHostInfoFromPort, ReturnHostInfoFromPort} from "@tsed/core";
|
|
2
|
+
import {configuration, injector, logger, ProviderScope, TokenProvider} from "@tsed/di";
|
|
3
|
+
import Http from "http";
|
|
4
|
+
import Http2 from "http2";
|
|
5
|
+
import Https from "https";
|
|
6
|
+
|
|
7
|
+
import {listenServer} from "./listenServer.js";
|
|
8
|
+
|
|
9
|
+
export interface CreateServerOptions {
|
|
10
|
+
token: TokenProvider;
|
|
11
|
+
type: "http" | "https";
|
|
12
|
+
port: string | false | undefined;
|
|
13
|
+
listen?: (hostInfo: ReturnHostInfoFromPort) => Promise<any>;
|
|
14
|
+
server: () => Http.Server | Https.Server | Http2.Http2Server;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type CreateServerReturn = () => Promise<Http.Server | Https.Server | Http2.Http2Server>;
|
|
18
|
+
|
|
19
|
+
export function createServer({token, type, port, server: get, listen}: CreateServerOptions): undefined | CreateServerReturn {
|
|
20
|
+
const server = port !== false ? get() : null;
|
|
21
|
+
|
|
22
|
+
injector().addProvider(token, {
|
|
23
|
+
scope: ProviderScope.SINGLETON,
|
|
24
|
+
useValue: server
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
injector().invoke(token);
|
|
28
|
+
|
|
29
|
+
if (server) {
|
|
30
|
+
const hostInfo = getHostInfoFromPort(type, port);
|
|
31
|
+
|
|
32
|
+
return async () => {
|
|
33
|
+
const url = `${hostInfo.protocol}://${hostInfo.address}:${port}`;
|
|
34
|
+
logger().debug(`Start server on ${url}`);
|
|
35
|
+
|
|
36
|
+
await (listen ? listen(hostInfo) : listenServer(server, hostInfo));
|
|
37
|
+
|
|
38
|
+
const address = server.address();
|
|
39
|
+
|
|
40
|
+
if (address && typeof address !== "string") {
|
|
41
|
+
hostInfo.address = address.address;
|
|
42
|
+
hostInfo.port = address.port;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
logger().info(`Listen server on ${hostInfo.toString()}`);
|
|
46
|
+
configuration().set(`${type}Port`, `${hostInfo.address}:${hostInfo.port}`);
|
|
47
|
+
|
|
48
|
+
return server;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {runInContext} from "@tsed/di";
|
|
2
|
+
|
|
3
|
+
import {PlatformTest} from "../../testing/PlatformTest.js";
|
|
4
|
+
import {ensureContext} from "./ensureContext.js";
|
|
5
|
+
|
|
6
|
+
describe("ensureContext()", () => {
|
|
7
|
+
beforeEach(() => PlatformTest.create());
|
|
8
|
+
afterEach(() => PlatformTest.reset());
|
|
9
|
+
|
|
10
|
+
it("should return context (no async_hook)", async () => {
|
|
11
|
+
const $ctx = PlatformTest.createRequestContext();
|
|
12
|
+
const stub = vi.fn();
|
|
13
|
+
const req = {
|
|
14
|
+
$ctx
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
await ensureContext(req, stub);
|
|
18
|
+
|
|
19
|
+
expect(stub).toHaveBeenCalledWith($ctx);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("should return context (with async_hook)", async () => {
|
|
23
|
+
const $ctx = PlatformTest.createRequestContext();
|
|
24
|
+
const stub = vi.fn();
|
|
25
|
+
const req = {};
|
|
26
|
+
|
|
27
|
+
await runInContext($ctx, () => ensureContext(req, stub));
|
|
28
|
+
|
|
29
|
+
expect(stub).toHaveBeenCalledWith($ctx);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should return context (no async_hook, no context)", async () => {
|
|
33
|
+
const stub = vi.fn();
|
|
34
|
+
const fallback = vi.fn();
|
|
35
|
+
const req = {};
|
|
36
|
+
|
|
37
|
+
await ensureContext(req, stub, fallback);
|
|
38
|
+
|
|
39
|
+
expect(fallback).toHaveBeenCalledWith();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {getContext, runInContext} from "@tsed/di";
|
|
2
|
+
|
|
3
|
+
import {PlatformContext} from "../domain/PlatformContext.js";
|
|
4
|
+
|
|
5
|
+
export function ensureContext(request: any, cb: ($ctx: PlatformContext) => any, fallback?: () => void) {
|
|
6
|
+
const $ctx = getContext<PlatformContext>();
|
|
7
|
+
|
|
8
|
+
if ($ctx) {
|
|
9
|
+
return cb($ctx);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
if (!request?.$ctx && fallback) {
|
|
13
|
+
return fallback();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return runInContext(request.$ctx, () => cb(request.$ctx));
|
|
17
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import {Env} from "@tsed/core";
|
|
2
|
+
import {Configuration} from "@tsed/di";
|
|
3
|
+
|
|
4
|
+
import {getConfiguration} from "./getConfiguration.js";
|
|
5
|
+
|
|
6
|
+
class MyCtrl {}
|
|
7
|
+
|
|
8
|
+
describe("getConfiguration()", () => {
|
|
9
|
+
it("should return configuration (2)", () => {
|
|
10
|
+
@Configuration({
|
|
11
|
+
mount: {
|
|
12
|
+
"/v1": [MyCtrl]
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
class MyModule {}
|
|
16
|
+
|
|
17
|
+
expect(
|
|
18
|
+
getConfiguration(
|
|
19
|
+
{
|
|
20
|
+
mount: {
|
|
21
|
+
"/v2": [MyCtrl]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
MyModule
|
|
25
|
+
)
|
|
26
|
+
).toEqual({
|
|
27
|
+
$$resolved: true,
|
|
28
|
+
env: "test",
|
|
29
|
+
httpPort: 8080,
|
|
30
|
+
httpsPort: false,
|
|
31
|
+
logger: {
|
|
32
|
+
debug: false,
|
|
33
|
+
jsonIndentation: 2,
|
|
34
|
+
level: "off",
|
|
35
|
+
logRequest: true
|
|
36
|
+
},
|
|
37
|
+
mount: {
|
|
38
|
+
"/v1": [MyCtrl],
|
|
39
|
+
"/v2": [MyCtrl]
|
|
40
|
+
},
|
|
41
|
+
scopes: {
|
|
42
|
+
controller: "singleton"
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it("should load configuration for test", () => {
|
|
47
|
+
@Configuration({})
|
|
48
|
+
class App {}
|
|
49
|
+
|
|
50
|
+
const config = getConfiguration({}, App);
|
|
51
|
+
|
|
52
|
+
expect(config).toEqual({
|
|
53
|
+
$$resolved: true,
|
|
54
|
+
env: "test",
|
|
55
|
+
httpPort: 8080,
|
|
56
|
+
httpsPort: false,
|
|
57
|
+
logger: {
|
|
58
|
+
debug: false,
|
|
59
|
+
jsonIndentation: 2,
|
|
60
|
+
level: "off",
|
|
61
|
+
logRequest: true
|
|
62
|
+
},
|
|
63
|
+
mount: {},
|
|
64
|
+
scopes: {
|
|
65
|
+
controller: "singleton"
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
it("should load configuration for production", () => {
|
|
70
|
+
@Configuration({
|
|
71
|
+
env: Env.PROD
|
|
72
|
+
})
|
|
73
|
+
class App {}
|
|
74
|
+
|
|
75
|
+
const config = getConfiguration({}, App);
|
|
76
|
+
|
|
77
|
+
expect(config).toEqual({
|
|
78
|
+
$$resolved: true,
|
|
79
|
+
env: Env.PROD,
|
|
80
|
+
httpPort: 8080,
|
|
81
|
+
httpsPort: false,
|
|
82
|
+
logger: {
|
|
83
|
+
debug: false,
|
|
84
|
+
jsonIndentation: 2,
|
|
85
|
+
level: "info",
|
|
86
|
+
logRequest: true
|
|
87
|
+
},
|
|
88
|
+
mount: {},
|
|
89
|
+
scopes: {
|
|
90
|
+
controller: "singleton"
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
it("should load configuration with mount from config", () => {
|
|
95
|
+
@Configuration({
|
|
96
|
+
env: Env.PROD
|
|
97
|
+
})
|
|
98
|
+
class App {}
|
|
99
|
+
|
|
100
|
+
class Ctrl {}
|
|
101
|
+
|
|
102
|
+
const config = getConfiguration(
|
|
103
|
+
{
|
|
104
|
+
mount: {
|
|
105
|
+
"/": [Ctrl]
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
App
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(config).toEqual({
|
|
112
|
+
$$resolved: true,
|
|
113
|
+
env: Env.PROD,
|
|
114
|
+
httpPort: 8080,
|
|
115
|
+
httpsPort: false,
|
|
116
|
+
logger: {
|
|
117
|
+
debug: false,
|
|
118
|
+
jsonIndentation: 2,
|
|
119
|
+
level: "info",
|
|
120
|
+
logRequest: true
|
|
121
|
+
},
|
|
122
|
+
mount: {
|
|
123
|
+
"/": [Ctrl]
|
|
124
|
+
},
|
|
125
|
+
scopes: {
|
|
126
|
+
controller: "singleton"
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
it("should load configuration with mount from decorator", () => {
|
|
131
|
+
class Ctrl {}
|
|
132
|
+
|
|
133
|
+
@Configuration({
|
|
134
|
+
env: Env.PROD,
|
|
135
|
+
mount: {
|
|
136
|
+
"/": [Ctrl]
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
class App {}
|
|
140
|
+
|
|
141
|
+
let config = getConfiguration({}, App);
|
|
142
|
+
config = getConfiguration(config, App);
|
|
143
|
+
|
|
144
|
+
expect(config).toEqual({
|
|
145
|
+
$$resolved: true,
|
|
146
|
+
env: Env.PROD,
|
|
147
|
+
httpPort: 8080,
|
|
148
|
+
httpsPort: false,
|
|
149
|
+
logger: {
|
|
150
|
+
debug: false,
|
|
151
|
+
jsonIndentation: 2,
|
|
152
|
+
level: "info",
|
|
153
|
+
logRequest: true
|
|
154
|
+
},
|
|
155
|
+
mount: {
|
|
156
|
+
"/": [Ctrl]
|
|
157
|
+
},
|
|
158
|
+
scopes: {
|
|
159
|
+
controller: "singleton"
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {Env, Store, Type} from "@tsed/core";
|
|
2
|
+
import {mergeMount, ProviderScope, ProviderType} from "@tsed/di";
|
|
3
|
+
|
|
4
|
+
export function getConfiguration(configuration: any = {}, module: Type<any> | null = null) {
|
|
5
|
+
if (configuration.$$resolved) {
|
|
6
|
+
return configuration;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const store = module ? Store.from(module).get("configuration") || {} : {};
|
|
10
|
+
const env = configuration.env || store.env || (process.env.NODE_ENV as Env) || Env.DEV;
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
$$resolved: true,
|
|
14
|
+
env,
|
|
15
|
+
httpPort: 8080,
|
|
16
|
+
httpsPort: false,
|
|
17
|
+
...store,
|
|
18
|
+
...configuration,
|
|
19
|
+
scopes: {
|
|
20
|
+
[ProviderType.CONTROLLER]: ProviderScope.SINGLETON,
|
|
21
|
+
...store.scopes,
|
|
22
|
+
...configuration.scopes
|
|
23
|
+
},
|
|
24
|
+
mount: mergeMount(store.mount, configuration.mount),
|
|
25
|
+
logger: {
|
|
26
|
+
debug: false,
|
|
27
|
+
level: env === Env.TEST ? "off" : "info",
|
|
28
|
+
logRequest: true,
|
|
29
|
+
...store.logger,
|
|
30
|
+
...configuration.logger,
|
|
31
|
+
jsonIndentation: process.env.NODE_ENV === Env.PROD ? 0 : 2
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {getStaticsOptions} from "./getStaticsOptions.js";
|
|
2
|
+
|
|
3
|
+
describe("getStaticsOptions()", () => {
|
|
4
|
+
it("should return the statics files options", () => {
|
|
5
|
+
const result = getStaticsOptions({
|
|
6
|
+
"/": ["/root", {root: "/root2", test: "test", hook: "$beforeRoutesInit"}]
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
expect(result).toEqual([
|
|
10
|
+
{
|
|
11
|
+
options: {
|
|
12
|
+
hook: "$afterRoutesInit",
|
|
13
|
+
root: "/root"
|
|
14
|
+
},
|
|
15
|
+
path: "/"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
options: {
|
|
19
|
+
hook: "$beforeRoutesInit",
|
|
20
|
+
root: "/root2",
|
|
21
|
+
test: "test"
|
|
22
|
+
},
|
|
23
|
+
path: "/"
|
|
24
|
+
}
|
|
25
|
+
]);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import {getValue} from "@tsed/core";
|
|
2
|
+
|
|
3
|
+
import {PlatformStaticsOptions, PlatformStaticsSettings} from "../config/interfaces/PlatformStaticsSettings.js";
|
|
4
|
+
|
|
5
|
+
function mapOptions(options: any): any {
|
|
6
|
+
const opts: PlatformStaticsOptions = typeof options === "string" ? {root: options} : options;
|
|
7
|
+
return {
|
|
8
|
+
...opts,
|
|
9
|
+
hook: getValue(opts, "hook", "$afterRoutesInit")
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getStaticsOptions(statics?: PlatformStaticsSettings): {path: string; options: PlatformStaticsOptions}[] {
|
|
14
|
+
return Object.entries(statics || {}).reduce(
|
|
15
|
+
(statics, [path, items]) => {
|
|
16
|
+
[].concat(items as any).forEach((options) => {
|
|
17
|
+
statics.push({path, options: mapOptions(options)});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return statics;
|
|
21
|
+
},
|
|
22
|
+
[] as {path: string; options: PlatformStaticsOptions}[]
|
|
23
|
+
);
|
|
24
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {getHostInfoFromPort} from "@tsed/core";
|
|
2
|
+
import type Http from "http";
|
|
3
|
+
import type Http2 from "http2";
|
|
4
|
+
import type Https from "https";
|
|
5
|
+
|
|
6
|
+
export function listenServer(server: Http.Server | Https.Server | Http2.Http2Server, hostInfo: ReturnType<typeof getHostInfoFromPort>) {
|
|
7
|
+
const {address, port} = hostInfo;
|
|
8
|
+
|
|
9
|
+
const promise = new Promise((resolve, reject) => {
|
|
10
|
+
server.on("listening", resolve);
|
|
11
|
+
server.on("error", reject);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
server.listen(port, address as any);
|
|
15
|
+
|
|
16
|
+
return promise;
|
|
17
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {mapReturnedResponse} from "../index.js";
|
|
2
|
+
|
|
3
|
+
describe("mapReturnedResponse", () => {
|
|
4
|
+
it("should map the response (1)", () => {
|
|
5
|
+
expect(
|
|
6
|
+
mapReturnedResponse({
|
|
7
|
+
description: "description",
|
|
8
|
+
type: "type",
|
|
9
|
+
collection: "collection",
|
|
10
|
+
headers: "headers"
|
|
11
|
+
})
|
|
12
|
+
).toEqual({
|
|
13
|
+
description: "description",
|
|
14
|
+
type: "type",
|
|
15
|
+
collectionType: "collection",
|
|
16
|
+
headers: "headers"
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should map the response (2)", () => {
|
|
21
|
+
expect(
|
|
22
|
+
mapReturnedResponse({
|
|
23
|
+
description: "description",
|
|
24
|
+
use: "use",
|
|
25
|
+
collection: "collection",
|
|
26
|
+
headers: "headers"
|
|
27
|
+
})
|
|
28
|
+
).toEqual({
|
|
29
|
+
description: "description",
|
|
30
|
+
type: "use",
|
|
31
|
+
collectionType: "collection",
|
|
32
|
+
headers: "headers"
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {printRoutes} from "./printRoutes.js";
|
|
2
|
+
|
|
3
|
+
describe("printRoutes()", () => {
|
|
4
|
+
it("should return routes", () => {
|
|
5
|
+
const routes = [
|
|
6
|
+
{
|
|
7
|
+
method: "GET",
|
|
8
|
+
url: "/",
|
|
9
|
+
name: "name",
|
|
10
|
+
toJSON() {
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
// tslint:disable-next-line: no-unused-variable
|
|
17
|
+
expect(typeof printRoutes(routes as any)).toBe("string");
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {$log, colorize} from "@tsed/logger";
|
|
2
|
+
|
|
3
|
+
import {PlatformRouteDetails} from "../domain/PlatformRouteDetails.js";
|
|
4
|
+
|
|
5
|
+
export function printRoutes(routes: PlatformRouteDetails[]) {
|
|
6
|
+
const mapColor: {[key: string]: string} = {
|
|
7
|
+
GET: "green",
|
|
8
|
+
POST: "yellow",
|
|
9
|
+
PUT: "blue",
|
|
10
|
+
DELETE: "red",
|
|
11
|
+
PATCH: "magenta",
|
|
12
|
+
ALL: "cyan",
|
|
13
|
+
STATICS: "white"
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const list = routes.map((route) => {
|
|
17
|
+
const method = route.method.toUpperCase();
|
|
18
|
+
|
|
19
|
+
return {
|
|
20
|
+
...route,
|
|
21
|
+
method: {
|
|
22
|
+
length: method.length,
|
|
23
|
+
toString: () => {
|
|
24
|
+
return colorize(method, mapColor[method]);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const str = $log.drawTable(list, {
|
|
31
|
+
padding: 1,
|
|
32
|
+
header: {
|
|
33
|
+
method: "Method",
|
|
34
|
+
url: "Endpoint",
|
|
35
|
+
name: "Class method"
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return "\n" + str.trim();
|
|
40
|
+
}
|