@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.
- 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,17 +0,0 @@
|
|
|
1
|
-
import {Type} from "@tsed/core";
|
|
2
|
-
import {PathType} from "@tsed/di";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @ignore
|
|
6
|
-
*/
|
|
7
|
-
export interface PlatformRouteOptions {
|
|
8
|
-
token?: Type<any>;
|
|
9
|
-
path?: PathType | undefined;
|
|
10
|
-
method?: string;
|
|
11
|
-
handlers: any[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @ignore
|
|
16
|
-
*/
|
|
17
|
-
export type PlatformRouteWithoutHandlers = Partial<Omit<PlatformRouteOptions, "handlers">>;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import {Provider, TokenProvider} from "@tsed/di";
|
|
2
|
-
|
|
3
|
-
export interface Route extends Record<string, any> {
|
|
4
|
-
route: string;
|
|
5
|
-
token: TokenProvider;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface RouteController {
|
|
9
|
-
route: string;
|
|
10
|
-
routes: Set<string>;
|
|
11
|
-
provider: Provider;
|
|
12
|
-
}
|
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import {catchError} from "@tsed/core";
|
|
2
|
-
import {AcceptMime, EndpointMetadata, Get} from "@tsed/schema";
|
|
3
|
-
|
|
4
|
-
import {PlatformTest} from "../../testing/index.js";
|
|
5
|
-
import {PlatformAcceptMimesMiddleware} from "./PlatformAcceptMimesMiddleware.js";
|
|
6
|
-
|
|
7
|
-
describe("PlatformMimesMiddleware", () => {
|
|
8
|
-
describe("when server has configuration", () => {
|
|
9
|
-
beforeEach(() =>
|
|
10
|
-
PlatformTest.create({
|
|
11
|
-
acceptMimes: ["application/json", "text"]
|
|
12
|
-
})
|
|
13
|
-
);
|
|
14
|
-
afterEach(() => PlatformTest.reset());
|
|
15
|
-
it("should accept type (application/json)", async () => {
|
|
16
|
-
class Test {
|
|
17
|
-
@Get("/")
|
|
18
|
-
@AcceptMime("application/json")
|
|
19
|
-
get() {}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
23
|
-
const request = PlatformTest.createRequest({
|
|
24
|
-
headers: {
|
|
25
|
-
accept: "application/json"
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
const ctx = PlatformTest.createRequestContext({
|
|
29
|
-
event: {request},
|
|
30
|
-
endpoint
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
vi.spyOn(request, "accepts");
|
|
34
|
-
|
|
35
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
36
|
-
|
|
37
|
-
middleware.use(ctx as never);
|
|
38
|
-
|
|
39
|
-
expect(request.accepts).toHaveBeenCalledWith(["application/json", "text"]);
|
|
40
|
-
});
|
|
41
|
-
it("should accept type (text)", async () => {
|
|
42
|
-
class Test {
|
|
43
|
-
@Get("/")
|
|
44
|
-
@AcceptMime("text")
|
|
45
|
-
get() {}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
49
|
-
const request: any = PlatformTest.createRequest({
|
|
50
|
-
headers: {
|
|
51
|
-
accept: "text/*, application/json"
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
const ctx = PlatformTest.createRequestContext({
|
|
55
|
-
event: {request},
|
|
56
|
-
endpoint
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
vi.spyOn(request, "accepts");
|
|
60
|
-
|
|
61
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
62
|
-
middleware.use(ctx as never);
|
|
63
|
-
|
|
64
|
-
expect(request.accepts).toHaveBeenCalledWith(["text", "application/json"]);
|
|
65
|
-
});
|
|
66
|
-
it("should accept type (text) without endpoint", async () => {
|
|
67
|
-
class Test {
|
|
68
|
-
@Get("/")
|
|
69
|
-
@AcceptMime("text")
|
|
70
|
-
get() {}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const request: any = PlatformTest.createRequest({
|
|
74
|
-
headers: {
|
|
75
|
-
accept: "text/*, application/json"
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
const ctx = PlatformTest.createRequestContext({
|
|
79
|
-
event: {request}
|
|
80
|
-
});
|
|
81
|
-
vi.spyOn(request, "accepts");
|
|
82
|
-
|
|
83
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
84
|
-
middleware.use(ctx as never);
|
|
85
|
-
|
|
86
|
-
expect(request.accepts).toHaveBeenCalledWith(["application/json", "text"]);
|
|
87
|
-
});
|
|
88
|
-
it("should refuse type", async () => {
|
|
89
|
-
class Test {
|
|
90
|
-
@Get("/")
|
|
91
|
-
@AcceptMime("application/json")
|
|
92
|
-
get() {}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
96
|
-
const request: any = PlatformTest.createRequest({
|
|
97
|
-
headers: {
|
|
98
|
-
accept: "application/xml"
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
const ctx = PlatformTest.createRequestContext({
|
|
102
|
-
event: {request},
|
|
103
|
-
endpoint
|
|
104
|
-
});
|
|
105
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
106
|
-
|
|
107
|
-
const error: any = catchError(() => middleware.use(ctx as never));
|
|
108
|
-
|
|
109
|
-
expect(error.message).toEqual("You must accept content-type application/json, text");
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
describe("when server hasn't configuration", () => {
|
|
113
|
-
beforeEach(() => PlatformTest.create());
|
|
114
|
-
afterEach(() => PlatformTest.reset());
|
|
115
|
-
it("should do noting", async () => {
|
|
116
|
-
class Test {
|
|
117
|
-
@Get("/")
|
|
118
|
-
get() {}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
122
|
-
const request: any = PlatformTest.createRequest({
|
|
123
|
-
headers: {
|
|
124
|
-
accept: "application/json"
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
const ctx = PlatformTest.createRequestContext({
|
|
128
|
-
event: {request},
|
|
129
|
-
endpoint
|
|
130
|
-
});
|
|
131
|
-
vi.spyOn(request, "accepts");
|
|
132
|
-
|
|
133
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
134
|
-
middleware.use(ctx as never);
|
|
135
|
-
|
|
136
|
-
return expect(request.accepts).not.toHaveBeenCalled();
|
|
137
|
-
});
|
|
138
|
-
it("should accept type (application/json)", async () => {
|
|
139
|
-
class Test {
|
|
140
|
-
@Get("/")
|
|
141
|
-
@AcceptMime("application/json")
|
|
142
|
-
get() {}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
146
|
-
const request: any = PlatformTest.createRequest({
|
|
147
|
-
headers: {
|
|
148
|
-
accept: "application/json"
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
const ctx = PlatformTest.createRequestContext({
|
|
152
|
-
event: {request},
|
|
153
|
-
endpoint
|
|
154
|
-
});
|
|
155
|
-
vi.spyOn(request, "accepts");
|
|
156
|
-
|
|
157
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
158
|
-
middleware.use(ctx as never);
|
|
159
|
-
|
|
160
|
-
expect(request.accepts).toHaveBeenCalledWith(["application/json"]);
|
|
161
|
-
});
|
|
162
|
-
it("should accept type (text)", async () => {
|
|
163
|
-
class Test {
|
|
164
|
-
@Get("/")
|
|
165
|
-
@AcceptMime("text")
|
|
166
|
-
get() {}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
170
|
-
const request: any = PlatformTest.createRequest({
|
|
171
|
-
headers: {
|
|
172
|
-
accept: "text/*, application/json"
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
const ctx = PlatformTest.createRequestContext({
|
|
176
|
-
event: {
|
|
177
|
-
request
|
|
178
|
-
},
|
|
179
|
-
endpoint
|
|
180
|
-
});
|
|
181
|
-
vi.spyOn(request, "accepts");
|
|
182
|
-
|
|
183
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
184
|
-
middleware.use(ctx as never);
|
|
185
|
-
|
|
186
|
-
expect(request.accepts).toHaveBeenCalledWith(["text"]);
|
|
187
|
-
});
|
|
188
|
-
it("should refuse type", async () => {
|
|
189
|
-
class Test {
|
|
190
|
-
@Get("/")
|
|
191
|
-
@AcceptMime("application/json")
|
|
192
|
-
get() {}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
const endpoint = EndpointMetadata.get(Test, "get");
|
|
196
|
-
const request: any = PlatformTest.createRequest({
|
|
197
|
-
headers: {
|
|
198
|
-
accept: "application/xml"
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
const ctx = PlatformTest.createRequestContext({
|
|
202
|
-
event: {request},
|
|
203
|
-
endpoint
|
|
204
|
-
});
|
|
205
|
-
|
|
206
|
-
vi.spyOn(request, "accepts");
|
|
207
|
-
|
|
208
|
-
const middleware = await PlatformTest.invoke<PlatformAcceptMimesMiddleware>(PlatformAcceptMimesMiddleware);
|
|
209
|
-
|
|
210
|
-
const error: any = catchError(() => middleware.use(ctx as never));
|
|
211
|
-
|
|
212
|
-
expect(error.message).toEqual("You must accept content-type application/json");
|
|
213
|
-
});
|
|
214
|
-
});
|
|
215
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import {uniq} from "@tsed/core";
|
|
2
|
-
import {constant, injectable, ProviderType} from "@tsed/di";
|
|
3
|
-
import {NotAcceptable} from "@tsed/exceptions";
|
|
4
|
-
import {MiddlewareMethods} from "@tsed/platform-middlewares";
|
|
5
|
-
import {Context} from "@tsed/platform-params";
|
|
6
|
-
import type {AlterEndpointHandlersArg} from "@tsed/platform-router";
|
|
7
|
-
import type {JsonOperationRoute} from "@tsed/schema";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @middleware
|
|
11
|
-
* @platform
|
|
12
|
-
*/
|
|
13
|
-
export class PlatformAcceptMimesMiddleware implements MiddlewareMethods {
|
|
14
|
-
protected acceptMimes = constant<string[]>("acceptMimes", []);
|
|
15
|
-
|
|
16
|
-
public $alterEndpointHandlers(handlers: AlterEndpointHandlersArg, operationRoute: JsonOperationRoute) {
|
|
17
|
-
const hasAcceptMimes = operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length;
|
|
18
|
-
return {
|
|
19
|
-
...handlers,
|
|
20
|
-
before: [hasAcceptMimes && PlatformAcceptMimesMiddleware, ...handlers.before].filter(Boolean) as any[]
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public use(@Context() ctx: Context): void {
|
|
25
|
-
const {endpoint, request} = ctx;
|
|
26
|
-
const mimes = uniq((endpoint?.get("acceptMimes") || []).concat(this.acceptMimes));
|
|
27
|
-
|
|
28
|
-
if (mimes.length && !request.accepts(mimes)) {
|
|
29
|
-
throw new NotAcceptable(mimes.join(", "));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
injectable(PlatformAcceptMimesMiddleware).type(ProviderType.MIDDLEWARE).priority(-10);
|
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import {nameOf} from "@tsed/core";
|
|
2
|
-
import {Controller} from "@tsed/di";
|
|
3
|
-
import {Get, Post} from "@tsed/schema";
|
|
4
|
-
|
|
5
|
-
import {PlatformTest} from "../../testing/PlatformTest.js";
|
|
6
|
-
import {application} from "../fn/application.js";
|
|
7
|
-
import {Platform} from "./Platform.js";
|
|
8
|
-
|
|
9
|
-
@Controller("/my-route")
|
|
10
|
-
class MyCtrl {
|
|
11
|
-
@Get("/")
|
|
12
|
-
get() {}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
@Controller("/my-sub-route")
|
|
16
|
-
class MySubCtrl {
|
|
17
|
-
@Get("/")
|
|
18
|
-
get() {}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
@Controller({
|
|
22
|
-
path: "/my-route",
|
|
23
|
-
children: [MySubCtrl]
|
|
24
|
-
})
|
|
25
|
-
class MyNestedCtrl {
|
|
26
|
-
@Get("/")
|
|
27
|
-
get() {}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
@Controller("/")
|
|
31
|
-
export class FlaggedCommentController {
|
|
32
|
-
@Get("/flag")
|
|
33
|
-
async list() {}
|
|
34
|
-
|
|
35
|
-
@Post("/:commentID/flag")
|
|
36
|
-
async create() {}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
@Controller({
|
|
40
|
-
path: "/comments",
|
|
41
|
-
children: [FlaggedCommentController]
|
|
42
|
-
})
|
|
43
|
-
export class CommentController {
|
|
44
|
-
@Get("/all")
|
|
45
|
-
get() {}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
@Controller({
|
|
49
|
-
path: "/domain/:contextID",
|
|
50
|
-
children: [CommentController]
|
|
51
|
-
})
|
|
52
|
-
export class DomainController {
|
|
53
|
-
@Get("/all")
|
|
54
|
-
get() {}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
@Controller({
|
|
58
|
-
path: "/platform/:platform",
|
|
59
|
-
children: [CommentController]
|
|
60
|
-
})
|
|
61
|
-
export class PlatformController {
|
|
62
|
-
@Get("/")
|
|
63
|
-
get() {}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
describe("Platform", () => {
|
|
67
|
-
beforeEach(PlatformTest.create);
|
|
68
|
-
afterEach(PlatformTest.reset);
|
|
69
|
-
afterEach(() => {
|
|
70
|
-
vi.resetAllMocks();
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
describe("addRoute", () => {
|
|
74
|
-
it("should add a route", async () => {
|
|
75
|
-
// GIVEN
|
|
76
|
-
const platform = await PlatformTest.get<Platform>(Platform);
|
|
77
|
-
|
|
78
|
-
vi.spyOn(application(), "use");
|
|
79
|
-
|
|
80
|
-
// WHEN
|
|
81
|
-
platform.addRoutes([{route: "/test", token: MyCtrl}]);
|
|
82
|
-
});
|
|
83
|
-
it("should add nested controllers (1)", async () => {
|
|
84
|
-
// GIVEN
|
|
85
|
-
const platform = await PlatformTest.get<Platform>(Platform);
|
|
86
|
-
|
|
87
|
-
vi.spyOn(application(), "use");
|
|
88
|
-
|
|
89
|
-
// WHEN
|
|
90
|
-
platform.addRoutes([{route: "/rest", token: MyNestedCtrl}]);
|
|
91
|
-
platform.addRoutes([{route: "/rest", token: MySubCtrl}]);
|
|
92
|
-
|
|
93
|
-
platform.getLayers();
|
|
94
|
-
const result = platform.getMountedControllers();
|
|
95
|
-
|
|
96
|
-
// THEN
|
|
97
|
-
expect(result.length).toEqual(2);
|
|
98
|
-
expect(result[0].route).toEqual("/rest");
|
|
99
|
-
expect(result[1].route).toEqual("/rest/my-route");
|
|
100
|
-
});
|
|
101
|
-
it("should add nested controllers (2)", async () => {
|
|
102
|
-
// GIVEN
|
|
103
|
-
const platform = await PlatformTest.get<Platform>(Platform);
|
|
104
|
-
|
|
105
|
-
vi.spyOn(application(), "use");
|
|
106
|
-
|
|
107
|
-
// WHEN
|
|
108
|
-
platform.addRoutes([{route: "/rest", token: DomainController}]);
|
|
109
|
-
platform.addRoutes([{route: "/rest", token: PlatformController}]);
|
|
110
|
-
|
|
111
|
-
platform.getLayers();
|
|
112
|
-
|
|
113
|
-
const result = platform.getMountedControllers();
|
|
114
|
-
|
|
115
|
-
// THEN
|
|
116
|
-
const data = result.map((c) => ({route: c.route, name: nameOf(c.provider)}));
|
|
117
|
-
expect(data).toEqual([
|
|
118
|
-
{
|
|
119
|
-
name: "Token:DomainController:DomainController",
|
|
120
|
-
route: "/rest"
|
|
121
|
-
},
|
|
122
|
-
{
|
|
123
|
-
name: "Token:CommentController:CommentController",
|
|
124
|
-
route: "/rest/domain/:contextID"
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
name: "Token:FlaggedCommentController:FlaggedCommentController",
|
|
128
|
-
route: "/rest/domain/:contextID/comments"
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
name: "Token:PlatformController:PlatformController",
|
|
132
|
-
route: "/rest"
|
|
133
|
-
},
|
|
134
|
-
{
|
|
135
|
-
name: "Token:CommentController:CommentController",
|
|
136
|
-
route: "/rest/platform/:platform"
|
|
137
|
-
},
|
|
138
|
-
{
|
|
139
|
-
name: "Token:FlaggedCommentController:FlaggedCommentController",
|
|
140
|
-
route: "/rest/platform/:platform/comments"
|
|
141
|
-
}
|
|
142
|
-
]);
|
|
143
|
-
});
|
|
144
|
-
});
|
|
145
|
-
});
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import {ControllerProvider, inject, injectable, injector, ProviderScope, TokenProvider} from "@tsed/di";
|
|
2
|
-
import {PlatformLayer, PlatformRouters} from "@tsed/platform-router";
|
|
3
|
-
|
|
4
|
-
import {application} from "../fn/application.js";
|
|
5
|
-
import {Route, RouteController} from "../interfaces/Route.js";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* `Platform` is used to provide all routes collected by annotation `@Controller`.
|
|
9
|
-
*
|
|
10
|
-
* @platform
|
|
11
|
-
*/
|
|
12
|
-
export class Platform {
|
|
13
|
-
readonly platformRouters = inject(PlatformRouters);
|
|
14
|
-
#layers: PlatformLayer[];
|
|
15
|
-
|
|
16
|
-
constructor() {
|
|
17
|
-
this.platformRouters.prebuild();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
get app() {
|
|
21
|
-
return application();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public addRoutes(routes: Route[]) {
|
|
25
|
-
routes.forEach((routeSettings) => {
|
|
26
|
-
this.addRoute(routeSettings.route, routeSettings.token);
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public addRoute(route: string, token: TokenProvider) {
|
|
31
|
-
const app = application();
|
|
32
|
-
const provider = injector().getProvider(token) as ControllerProvider;
|
|
33
|
-
|
|
34
|
-
if (!provider || provider.hasParent()) {
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const router = this.platformRouters.from(provider.token);
|
|
39
|
-
|
|
40
|
-
app.use(route, router);
|
|
41
|
-
|
|
42
|
-
return this;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public getLayers() {
|
|
46
|
-
this.#layers = this.#layers || this.platformRouters.getLayers(application());
|
|
47
|
-
|
|
48
|
-
return this.#layers;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Get all controllers mounted on the application.
|
|
53
|
-
* @returns {RouteController[]}
|
|
54
|
-
*/
|
|
55
|
-
public getMountedControllers() {
|
|
56
|
-
const controllers = this.getLayers().reduce((controllers, layer) => {
|
|
57
|
-
if (layer.isProvider()) {
|
|
58
|
-
const route = String(layer.getBasePath());
|
|
59
|
-
const key = `${layer.provider.toString()}:${route}`;
|
|
60
|
-
|
|
61
|
-
if (!controllers.has(key)) {
|
|
62
|
-
controllers.set(key, {
|
|
63
|
-
route,
|
|
64
|
-
routes: new Set(),
|
|
65
|
-
provider: layer.provider
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
controllers.get(key)!.routes.add(String(layer.path));
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return controllers;
|
|
73
|
-
}, new Map<string, RouteController>());
|
|
74
|
-
|
|
75
|
-
return [...controllers.values()];
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
injectable(Platform).scope(ProviderScope.SINGLETON);
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import {IncomingMessage, ServerResponse} from "node:http";
|
|
2
|
-
|
|
3
|
-
import {Type} from "@tsed/core";
|
|
4
|
-
import {configuration, constant, inject, injectable} from "@tsed/di";
|
|
5
|
-
import {$on} from "@tsed/hooks";
|
|
6
|
-
import type {PlatformMulter, PlatformMulterSettings} from "@tsed/platform-multer";
|
|
7
|
-
import {PlatformHandlerMetadata, PlatformLayer} from "@tsed/platform-router";
|
|
8
|
-
|
|
9
|
-
import {PlatformStaticsOptions} from "../config/interfaces/PlatformStaticsSettings.js";
|
|
10
|
-
import {application} from "../fn/application.js";
|
|
11
|
-
import {createHttpServer} from "../utils/createHttpServer.js";
|
|
12
|
-
import {createHttpsServer} from "../utils/createHttpsServer.js";
|
|
13
|
-
import {CreateServerReturn} from "../utils/createServer.js";
|
|
14
|
-
import {Platform} from "./Platform.js";
|
|
15
|
-
import {PlatformApplication} from "./PlatformApplication.js";
|
|
16
|
-
import {PlatformHandler} from "./PlatformHandler.js";
|
|
17
|
-
|
|
18
|
-
export abstract class PlatformAdapter<App = TsED.Application> {
|
|
19
|
-
abstract readonly NAME: string;
|
|
20
|
-
|
|
21
|
-
readonly app = inject<PlatformApplication<App>>(PlatformApplication);
|
|
22
|
-
|
|
23
|
-
constructor() {
|
|
24
|
-
const platformApp = inject(PlatformApplication);
|
|
25
|
-
|
|
26
|
-
const {app, callback} = this.createApp();
|
|
27
|
-
platformApp.rawApp = app as any;
|
|
28
|
-
platformApp.rawCallback = callback;
|
|
29
|
-
|
|
30
|
-
$on("$afterInvoke", PlatformAdapter, () => {
|
|
31
|
-
configuration().set("PLATFORM_NAME", constant("PLATFORM_NAME") || this.NAME);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
getServers(): CreateServerReturn[] {
|
|
36
|
-
const app = application<App>();
|
|
37
|
-
return [createHttpServer(app.callback()), createHttpsServer(app.callback())].filter(Boolean) as any[];
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
onInit(): Promise<void> | void {
|
|
41
|
-
return Promise.resolve();
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
beforeLoadRoutes(): Promise<void> | void {
|
|
45
|
-
return Promise.resolve();
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
afterLoadRoutes(): Promise<void> | void {
|
|
49
|
-
return Promise.resolve();
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* create initial context
|
|
54
|
-
*/
|
|
55
|
-
abstract useContext(): Promise<void> | void;
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Map router layer to the targeted framework
|
|
59
|
-
*/
|
|
60
|
-
abstract mapLayers(layer: PlatformLayer[]): void;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Map handler to the targeted framework
|
|
64
|
-
*/
|
|
65
|
-
mapHandler(handler: Function, layer: PlatformHandlerMetadata): Function {
|
|
66
|
-
return handler;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Return the app instance
|
|
71
|
-
*/
|
|
72
|
-
abstract createApp(): {app: App; callback(): (req: IncomingMessage, res: ServerResponse) => void};
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Return the statics middlewares
|
|
76
|
-
* @param endpoint
|
|
77
|
-
* @param options
|
|
78
|
-
*/
|
|
79
|
-
abstract statics(endpoint: string, options: PlatformStaticsOptions): any;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Return the multipart middleware
|
|
83
|
-
* @param options
|
|
84
|
-
* @deprecated use inject("MULTER_MODULE")?.get(options) instead
|
|
85
|
-
*/
|
|
86
|
-
multipart(options: PlatformMulterSettings): PlatformMulter {
|
|
87
|
-
return inject<any>(Symbol.for("MULTER_MODULE"))?.get(options);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Return the body parser for the given
|
|
92
|
-
* @param type
|
|
93
|
-
* @param opts
|
|
94
|
-
*/
|
|
95
|
-
abstract bodyParser(type: string, opts?: Record<string, any>): any;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export interface PlatformBuilderSettings<App = TsED.Application> extends Partial<TsED.Configuration> {
|
|
99
|
-
adapter?: Type<PlatformAdapter<App>>;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
injectable(PlatformAdapter).imports([PlatformApplication, Platform, PlatformHandler]).alias("PlatformAdapter");
|