@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,89 +0,0 @@
|
|
|
1
|
-
import {configuration} from "@tsed/di";
|
|
2
|
-
|
|
3
|
-
import {PlatformTest} from "../../testing/PlatformTest.js";
|
|
4
|
-
import {createContext} from "../utils/createContext.js";
|
|
5
|
-
import {PlatformApplication} from "./PlatformApplication.js";
|
|
6
|
-
import {PlatformHandler} from "./PlatformHandler.js";
|
|
7
|
-
|
|
8
|
-
vi.mock("../utils/createContext");
|
|
9
|
-
|
|
10
|
-
function createDriver() {
|
|
11
|
-
return {
|
|
12
|
-
use: vi.fn(),
|
|
13
|
-
all: vi.fn(),
|
|
14
|
-
get: vi.fn(),
|
|
15
|
-
post: vi.fn(),
|
|
16
|
-
put: vi.fn(),
|
|
17
|
-
delete: vi.fn(),
|
|
18
|
-
patch: vi.fn(),
|
|
19
|
-
head: vi.fn(),
|
|
20
|
-
options: vi.fn()
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async function getPlatformApp() {
|
|
25
|
-
const platformHandler = {
|
|
26
|
-
createHandler: vi.fn().mockImplementation((o) => o)
|
|
27
|
-
};
|
|
28
|
-
const platformApp = await PlatformTest.invoke<PlatformApplication<any>>(PlatformApplication, [
|
|
29
|
-
{
|
|
30
|
-
token: PlatformHandler,
|
|
31
|
-
use: platformHandler
|
|
32
|
-
}
|
|
33
|
-
]);
|
|
34
|
-
configuration().logger = {};
|
|
35
|
-
platformApp.rawApp = createDriver() as any;
|
|
36
|
-
|
|
37
|
-
return {platformApp, platformHandler};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
describe("PlatformApplication", () => {
|
|
41
|
-
beforeEach(PlatformTest.create);
|
|
42
|
-
afterEach(PlatformTest.reset);
|
|
43
|
-
|
|
44
|
-
describe("getApp()", () => {
|
|
45
|
-
it("should return app", async () => {
|
|
46
|
-
// GIVEN
|
|
47
|
-
const {platformApp} = await getPlatformApp();
|
|
48
|
-
|
|
49
|
-
// WHEN
|
|
50
|
-
expect(platformApp.getApp()).toEqual(platformApp.rawApp);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe("statics()", () => {
|
|
55
|
-
it("should call statics", async () => {
|
|
56
|
-
// GIVEN
|
|
57
|
-
const {platformApp} = await getPlatformApp();
|
|
58
|
-
|
|
59
|
-
vi.spyOn(console, "warn");
|
|
60
|
-
|
|
61
|
-
// WHEN
|
|
62
|
-
platformApp.statics("/", {root: "/root"});
|
|
63
|
-
});
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
describe("callback()", () => {
|
|
67
|
-
it("should return the callback", async () => {
|
|
68
|
-
const $ctx = {
|
|
69
|
-
runInContext: vi.fn().mockImplementation((cb) => {
|
|
70
|
-
cb();
|
|
71
|
-
})
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const {platformApp} = await getPlatformApp();
|
|
75
|
-
|
|
76
|
-
(createContext as any).mockReturnValue(() => Promise.resolve($ctx));
|
|
77
|
-
|
|
78
|
-
// WHEN
|
|
79
|
-
const callback = vi.fn();
|
|
80
|
-
|
|
81
|
-
vi.spyOn(platformApp, "rawCallback").mockReturnValue(callback);
|
|
82
|
-
|
|
83
|
-
await platformApp.callback({} as any, {} as any);
|
|
84
|
-
|
|
85
|
-
expect(platformApp.rawCallback).toHaveBeenCalledWith();
|
|
86
|
-
expect(callback).toHaveBeenCalledWith({}, {});
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
});
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {IncomingMessage, ServerResponse} from "node:http";
|
|
2
|
-
|
|
3
|
-
import {injectable, ProviderScope} from "@tsed/di";
|
|
4
|
-
import {PlatformRouter} from "@tsed/platform-router";
|
|
5
|
-
|
|
6
|
-
declare global {
|
|
7
|
-
namespace TsED {
|
|
8
|
-
// @ts-ignore
|
|
9
|
-
export interface Application {}
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* `PlatformApplication` is used to provide all routes collected by annotation `@Controller`.
|
|
15
|
-
*
|
|
16
|
-
* @platform
|
|
17
|
-
*/
|
|
18
|
-
export class PlatformApplication<App = TsED.Application> extends PlatformRouter {
|
|
19
|
-
rawApp: App;
|
|
20
|
-
|
|
21
|
-
rawCallback(): any {}
|
|
22
|
-
|
|
23
|
-
getApp(): App {
|
|
24
|
-
return this.rawApp;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
callback(): (req: IncomingMessage, res: ServerResponse) => any;
|
|
28
|
-
callback(req: IncomingMessage, res: ServerResponse): any;
|
|
29
|
-
callback(req?: IncomingMessage, res?: ServerResponse) {
|
|
30
|
-
if (req && res) {
|
|
31
|
-
return this.callback()(req, res);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return this.rawCallback();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
injectable(PlatformApplication).scope(ProviderScope.SINGLETON).alias("PlatformApplication");
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import {Injectable, runInContext} from "@tsed/di";
|
|
2
|
-
import {PlatformHandlerMetadata, PlatformHandlerType} from "@tsed/platform-router";
|
|
3
|
-
import {EndpointMetadata, Get, View} from "@tsed/schema";
|
|
4
|
-
|
|
5
|
-
import {PlatformTest} from "../../testing/PlatformTest.js";
|
|
6
|
-
import {PlatformHandler} from "./PlatformHandler.js";
|
|
7
|
-
|
|
8
|
-
function getServiceFixture() {
|
|
9
|
-
const service = PlatformTest.get<PlatformHandler>(PlatformHandler);
|
|
10
|
-
return {service};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
describe("PlatformHandler", () => {
|
|
14
|
-
beforeEach(() => PlatformTest.create());
|
|
15
|
-
afterEach(() => PlatformTest.create());
|
|
16
|
-
|
|
17
|
-
describe("createHandler()", () => {
|
|
18
|
-
it("should create handler", async () => {
|
|
19
|
-
const {service} = getServiceFixture();
|
|
20
|
-
const handler = vi.fn();
|
|
21
|
-
const metadata = new PlatformHandlerMetadata({
|
|
22
|
-
handler,
|
|
23
|
-
type: PlatformHandlerType.CTX_FN
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
27
|
-
|
|
28
|
-
const result = service.createHandler(metadata);
|
|
29
|
-
|
|
30
|
-
expect(typeof result).toEqual("function");
|
|
31
|
-
|
|
32
|
-
await result($ctx);
|
|
33
|
-
|
|
34
|
-
expect(handler).toHaveBeenCalledWith($ctx);
|
|
35
|
-
});
|
|
36
|
-
it("should create handler from provider", async () => {
|
|
37
|
-
const {service} = getServiceFixture();
|
|
38
|
-
|
|
39
|
-
@Injectable()
|
|
40
|
-
class Test {
|
|
41
|
-
@Get("/")
|
|
42
|
-
get() {
|
|
43
|
-
return "test";
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
PlatformTest.injector.addProvider(Test);
|
|
48
|
-
|
|
49
|
-
const metadata = new PlatformHandlerMetadata({
|
|
50
|
-
provider: PlatformTest.injector.getProvider(Test),
|
|
51
|
-
propertyKey: "get",
|
|
52
|
-
type: PlatformHandlerType.ENDPOINT
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
56
|
-
$ctx.endpoint = EndpointMetadata.get(Test, "get");
|
|
57
|
-
|
|
58
|
-
const result = service.createHandler(metadata);
|
|
59
|
-
|
|
60
|
-
expect(typeof result).toEqual("function");
|
|
61
|
-
|
|
62
|
-
await result($ctx);
|
|
63
|
-
|
|
64
|
-
expect($ctx.data).toEqual("test");
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
describe("createCustomHandler()", () => {
|
|
68
|
-
it("should create handler", async () => {
|
|
69
|
-
@Injectable()
|
|
70
|
-
class TestService {
|
|
71
|
-
@View("test")
|
|
72
|
-
use() {
|
|
73
|
-
return Promise.resolve("hello");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const {service} = getServiceFixture();
|
|
78
|
-
|
|
79
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
80
|
-
const testService = await PlatformTest.invoke<TestService>(TestService)!;
|
|
81
|
-
const provider = PlatformTest.injector.getProvider(TestService)!;
|
|
82
|
-
|
|
83
|
-
vi.spyOn(testService, "use").mockResolvedValue("hello");
|
|
84
|
-
|
|
85
|
-
const result = service.createCustomHandler(provider, "use");
|
|
86
|
-
|
|
87
|
-
expect(result).toBeInstanceOf(Function);
|
|
88
|
-
|
|
89
|
-
await runInContext($ctx, () => result($ctx));
|
|
90
|
-
|
|
91
|
-
expect($ctx.data).toEqual("hello");
|
|
92
|
-
expect(testService.use).toHaveBeenCalledWith($ctx);
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
describe("onRequest()", () => {
|
|
96
|
-
it("should do nothing when the request is done", async () => {
|
|
97
|
-
const {service} = getServiceFixture();
|
|
98
|
-
const handler = vi.fn();
|
|
99
|
-
|
|
100
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
101
|
-
$ctx.next = vi.fn();
|
|
102
|
-
|
|
103
|
-
await $ctx.finish();
|
|
104
|
-
|
|
105
|
-
$ctx.handlerMetadata = new PlatformHandlerMetadata({
|
|
106
|
-
handler
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
await service.onRequest(handler, $ctx);
|
|
110
|
-
|
|
111
|
-
expect(handler).not.toHaveBeenCalled();
|
|
112
|
-
});
|
|
113
|
-
it("should apply headers and status", async () => {
|
|
114
|
-
@Injectable()
|
|
115
|
-
class TestService {
|
|
116
|
-
@Get("/")
|
|
117
|
-
use() {
|
|
118
|
-
return Promise.resolve("hello");
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const {service} = getServiceFixture();
|
|
123
|
-
const handler = vi.fn().mockResolvedValue({
|
|
124
|
-
status: 201,
|
|
125
|
-
statusText: "CREATED",
|
|
126
|
-
headers: {
|
|
127
|
-
test: "test"
|
|
128
|
-
},
|
|
129
|
-
data: "data"
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
133
|
-
$ctx.next = vi.fn();
|
|
134
|
-
$ctx.endpoint = EndpointMetadata.get(TestService, "use");
|
|
135
|
-
|
|
136
|
-
$ctx.handlerMetadata = new PlatformHandlerMetadata({
|
|
137
|
-
handler: TestService.prototype.use
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
vi.spyOn(service, "flush").mockResolvedValue();
|
|
141
|
-
|
|
142
|
-
await service.onRequest(handler, $ctx);
|
|
143
|
-
|
|
144
|
-
expect(handler).toHaveBeenCalled();
|
|
145
|
-
expect($ctx.response.statusCode).toEqual(201);
|
|
146
|
-
expect($ctx.response.getHeaders()).toEqual({
|
|
147
|
-
test: "test",
|
|
148
|
-
"x-request-id": "id"
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
});
|
|
152
|
-
});
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import {AnyPromiseResult, AnyToPromiseStatus, catchAsyncError} from "@tsed/core";
|
|
2
|
-
import {inject, injectable, Provider, ProviderScope} from "@tsed/di";
|
|
3
|
-
import {$alter} from "@tsed/hooks";
|
|
4
|
-
import {PlatformExceptions} from "@tsed/platform-exceptions";
|
|
5
|
-
import {PlatformParams, PlatformParamsCallback} from "@tsed/platform-params";
|
|
6
|
-
import {PlatformResponseFilter} from "@tsed/platform-response-filter";
|
|
7
|
-
import {
|
|
8
|
-
AlterEndpointHandlersArg,
|
|
9
|
-
PlatformHandlerMetadata,
|
|
10
|
-
PlatformHandlerType,
|
|
11
|
-
PlatformRouters,
|
|
12
|
-
useResponseHandler
|
|
13
|
-
} from "@tsed/platform-router";
|
|
14
|
-
import {JsonOperationRoute} from "@tsed/schema";
|
|
15
|
-
|
|
16
|
-
import {AnyToPromiseWithCtx} from "../domain/AnyToPromiseWithCtx.js";
|
|
17
|
-
import {PlatformContext} from "../domain/PlatformContext.js";
|
|
18
|
-
import {setResponseHeaders} from "../utils/setResponseHeaders.js";
|
|
19
|
-
import {PlatformAdapter} from "./PlatformAdapter.js";
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Platform Handler abstraction layer. Wrap original class method to a pure platform handler (Express, Koa, etc...).
|
|
23
|
-
* @platform
|
|
24
|
-
*/
|
|
25
|
-
export class PlatformHandler {
|
|
26
|
-
protected responseFilter = inject(PlatformResponseFilter);
|
|
27
|
-
protected platformParams = inject(PlatformParams);
|
|
28
|
-
protected platformExceptions = inject(PlatformExceptions);
|
|
29
|
-
protected platformRouters = inject(PlatformRouters);
|
|
30
|
-
|
|
31
|
-
constructor() {
|
|
32
|
-
// configure the router module
|
|
33
|
-
this.platformRouters.hooks
|
|
34
|
-
.on("alterEndpointHandlers", (handlers: AlterEndpointHandlersArg, operationRoute: JsonOperationRoute) => {
|
|
35
|
-
handlers = $alter("$alterEndpointHandlers", handlers, [operationRoute]);
|
|
36
|
-
|
|
37
|
-
handlers.after.push(useResponseHandler(this.flush.bind(this)));
|
|
38
|
-
|
|
39
|
-
return handlers;
|
|
40
|
-
})
|
|
41
|
-
.on("alterHandler", (handlerMetadata: PlatformHandlerMetadata) => {
|
|
42
|
-
const handler = handlerMetadata.isInjectable() ? this.createHandler(handlerMetadata) : handlerMetadata.handler;
|
|
43
|
-
handlerMetadata.compiledHandler = handler;
|
|
44
|
-
return inject(PlatformAdapter).mapHandler(handler, handlerMetadata);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
createHandler(handlerMetadata: PlatformHandlerMetadata): any {
|
|
49
|
-
const handler = this.platformParams.compileHandler(handlerMetadata);
|
|
50
|
-
|
|
51
|
-
return ($ctx: PlatformContext) => {
|
|
52
|
-
$ctx.handlerMetadata = handlerMetadata;
|
|
53
|
-
|
|
54
|
-
return this.onRequest(handler, $ctx);
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @param provider
|
|
60
|
-
* @param propertyKey
|
|
61
|
-
*/
|
|
62
|
-
createCustomHandler(provider: Provider, propertyKey: string) {
|
|
63
|
-
const metadata = new PlatformHandlerMetadata({
|
|
64
|
-
provider,
|
|
65
|
-
type: PlatformHandlerType.CUSTOM,
|
|
66
|
-
propertyKey
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
return this.createHandler(metadata);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Call handler when a request his handle
|
|
74
|
-
*/
|
|
75
|
-
async onRequest(handler: PlatformParamsCallback, $ctx: PlatformContext) {
|
|
76
|
-
const {handlerMetadata} = $ctx;
|
|
77
|
-
|
|
78
|
-
if (handlerMetadata.type === PlatformHandlerType.CTX_FN) {
|
|
79
|
-
return handler({$ctx});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const resolver = new AnyToPromiseWithCtx($ctx);
|
|
83
|
-
|
|
84
|
-
const response = await resolver.call(handler);
|
|
85
|
-
// Note: restore previous handler metadata (for OIDC)
|
|
86
|
-
$ctx.handlerMetadata = handlerMetadata;
|
|
87
|
-
|
|
88
|
-
if (response.state === AnyToPromiseStatus.RESOLVED && !$ctx.isDone()) {
|
|
89
|
-
return this.onResponse(response, $ctx);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
onResponse({status, data, headers}: AnyPromiseResult, $ctx: PlatformContext) {
|
|
94
|
-
if (status) {
|
|
95
|
-
$ctx.response.status(status);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
if (headers) {
|
|
99
|
-
$ctx.response.setHeaders(headers);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (data !== undefined) {
|
|
103
|
-
$ctx.data = data;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
$ctx.error = null;
|
|
107
|
-
|
|
108
|
-
// set headers each times that an endpoint is called
|
|
109
|
-
if ($ctx.handlerMetadata.isEndpoint()) {
|
|
110
|
-
setResponseHeaders($ctx);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Send the response to the consumer.
|
|
116
|
-
* @protected
|
|
117
|
-
* @param $ctx
|
|
118
|
-
*/
|
|
119
|
-
async flush($ctx: PlatformContext) {
|
|
120
|
-
if (!$ctx.error) {
|
|
121
|
-
$ctx.error = await catchAsyncError(async () => {
|
|
122
|
-
const {response} = $ctx;
|
|
123
|
-
|
|
124
|
-
if (!$ctx.isDone()) {
|
|
125
|
-
const data = await this.responseFilter.transform($ctx.data, $ctx as any);
|
|
126
|
-
response.body(data);
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
if ($ctx.error) {
|
|
132
|
-
this.platformExceptions.catch($ctx.error, $ctx);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
injectable(PlatformHandler).scope(ProviderScope.SINGLETON);
|
|
@@ -1,185 +0,0 @@
|
|
|
1
|
-
import {PlatformHandlerMetadata} from "@tsed/platform-router";
|
|
2
|
-
|
|
3
|
-
import {FakeAdapter} from "../../testing/index.js";
|
|
4
|
-
import {PlatformTest} from "../../testing/PlatformTest.js";
|
|
5
|
-
import {PlatformAdapter} from "./PlatformAdapter.js";
|
|
6
|
-
import {PlatformRequest} from "./PlatformRequest.js";
|
|
7
|
-
|
|
8
|
-
function createRequest() {
|
|
9
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
10
|
-
|
|
11
|
-
return {req: $ctx.request.request, request: $ctx.request};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
describe("PlatformRequest", () => {
|
|
15
|
-
beforeEach(() => PlatformTest.create());
|
|
16
|
-
afterEach(() => PlatformTest.reset());
|
|
17
|
-
|
|
18
|
-
it("should create a PlatformRequest instance", () => {
|
|
19
|
-
const request = PlatformTest.createRequest();
|
|
20
|
-
const response = PlatformTest.createResponse();
|
|
21
|
-
const $ctx = PlatformTest.createRequestContext({
|
|
22
|
-
event: {
|
|
23
|
-
request,
|
|
24
|
-
response
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
expect($ctx.request.raw).toEqual(request);
|
|
29
|
-
expect($ctx.request.response).toEqual($ctx.response);
|
|
30
|
-
expect($ctx.request.headers).toEqual({});
|
|
31
|
-
expect($ctx.request.get("host")).toEqual(undefined);
|
|
32
|
-
expect($ctx.request.getHeader("host")).toEqual(undefined);
|
|
33
|
-
expect($ctx.request.method).toEqual("GET");
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe("secure()", () => {
|
|
37
|
-
it("should return the secure request state (false)", () => {
|
|
38
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
39
|
-
|
|
40
|
-
$ctx.request.request.secure = false;
|
|
41
|
-
|
|
42
|
-
expect($ctx.request.secure).toEqual(false);
|
|
43
|
-
});
|
|
44
|
-
it("should return the secure request state (true)", () => {
|
|
45
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
46
|
-
|
|
47
|
-
$ctx.request.request.secure = true;
|
|
48
|
-
expect($ctx.request.secure).toEqual(true);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe("route()", () => {
|
|
53
|
-
it("should return route", () => {
|
|
54
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
55
|
-
$ctx.handlerMetadata = new PlatformHandlerMetadata({
|
|
56
|
-
handler: () => {}
|
|
57
|
-
});
|
|
58
|
-
$ctx.handlerMetadata.path = "/id";
|
|
59
|
-
|
|
60
|
-
expect($ctx.request.route).toEqual("/id");
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe("protocol()", () => {
|
|
65
|
-
it("should return the protocol request state (http)", () => {
|
|
66
|
-
const $ctx = PlatformTest.createRequestContext();
|
|
67
|
-
|
|
68
|
-
$ctx.request.request.protocol = "http";
|
|
69
|
-
|
|
70
|
-
expect($ctx.request.protocol).toEqual("http");
|
|
71
|
-
});
|
|
72
|
-
it("should return the protocol request state (https)", () => {
|
|
73
|
-
const {req, request} = createRequest();
|
|
74
|
-
|
|
75
|
-
req.protocol = "https";
|
|
76
|
-
expect(request.protocol).toEqual("https");
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
describe("host()", () => {
|
|
81
|
-
it("should return the host", () => {
|
|
82
|
-
const {request} = createRequest();
|
|
83
|
-
|
|
84
|
-
request.raw.headers["host"] = "host";
|
|
85
|
-
|
|
86
|
-
expect(request.host).toEqual("host");
|
|
87
|
-
});
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
describe("accepts()", () => {
|
|
91
|
-
it("should set the accepts header", () => {
|
|
92
|
-
const {req, request} = createRequest();
|
|
93
|
-
|
|
94
|
-
vi.spyOn(req, "accepts");
|
|
95
|
-
|
|
96
|
-
request.accepts("application/json");
|
|
97
|
-
|
|
98
|
-
expect(req.accepts).toHaveBeenCalledWith("application/json");
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
describe("rawBody()", () => {
|
|
103
|
-
it("should return the rawBody", () => {
|
|
104
|
-
const {req, request} = createRequest();
|
|
105
|
-
req.rawBody = "raw";
|
|
106
|
-
const raw = request.rawBody;
|
|
107
|
-
|
|
108
|
-
expect(raw).toEqual("raw");
|
|
109
|
-
});
|
|
110
|
-
it("should return the body", () => {
|
|
111
|
-
const {req, request} = createRequest();
|
|
112
|
-
req.body = "raw";
|
|
113
|
-
const raw = request.rawBody;
|
|
114
|
-
|
|
115
|
-
expect(raw).toEqual("raw");
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
describe("files()", () => {
|
|
120
|
-
it("should return the files", () => {
|
|
121
|
-
const {req, request} = createRequest();
|
|
122
|
-
req.files = "files";
|
|
123
|
-
|
|
124
|
-
expect(request.files).toEqual("files");
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
describe("getReq()", () => {
|
|
129
|
-
it("should return req", () => {
|
|
130
|
-
const {req, request} = createRequest();
|
|
131
|
-
|
|
132
|
-
expect(request.req).toEqual(req);
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
describe("getters", () => {
|
|
137
|
-
it("should return the expected data", () => {
|
|
138
|
-
const {request} = createRequest();
|
|
139
|
-
request.raw.originalUrl = "/";
|
|
140
|
-
request.raw.query =
|
|
141
|
-
request.raw.session =
|
|
142
|
-
request.raw.cookies =
|
|
143
|
-
request.raw.params =
|
|
144
|
-
request.raw.body =
|
|
145
|
-
{
|
|
146
|
-
obj: {
|
|
147
|
-
test: "testValue"
|
|
148
|
-
},
|
|
149
|
-
test: "testValue"
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
expect(request.url).toEqual("/");
|
|
153
|
-
expect(request.body).toEqual({
|
|
154
|
-
obj: {
|
|
155
|
-
test: "testValue"
|
|
156
|
-
},
|
|
157
|
-
test: "testValue"
|
|
158
|
-
});
|
|
159
|
-
expect(request.params).toEqual({
|
|
160
|
-
obj: {
|
|
161
|
-
test: "testValue"
|
|
162
|
-
},
|
|
163
|
-
test: "testValue"
|
|
164
|
-
});
|
|
165
|
-
expect(request.query).toEqual({
|
|
166
|
-
obj: {
|
|
167
|
-
test: "testValue"
|
|
168
|
-
},
|
|
169
|
-
test: "testValue"
|
|
170
|
-
});
|
|
171
|
-
expect(request.cookies).toEqual({
|
|
172
|
-
obj: {
|
|
173
|
-
test: "testValue"
|
|
174
|
-
},
|
|
175
|
-
test: "testValue"
|
|
176
|
-
});
|
|
177
|
-
expect(request.session).toEqual({
|
|
178
|
-
obj: {
|
|
179
|
-
test: "testValue"
|
|
180
|
-
},
|
|
181
|
-
test: "testValue"
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
});
|