diff --git a/packages/platform/common/src/builder/PlatformControllerBuilder.ts b/packages/platform/common/src/builder/PlatformControllerBuilder.ts index e3cf985063f..f56baf3be70 100644 --- a/packages/platform/common/src/builder/PlatformControllerBuilder.ts +++ b/packages/platform/common/src/builder/PlatformControllerBuilder.ts @@ -1,6 +1,6 @@ import {Type} from "@tsed/core"; import {GlobalProviders, InjectorService, ProviderType} from "@tsed/di"; -import {getOperationsRoutes, OperationMethods, EndpointMetadata} from "@tsed/schema"; +import {getOperationsRoutes, OperationMethods} from "@tsed/schema"; import {ControllerProvider} from "../domain/ControllerProvider"; import {PlatformRouter} from "../services/PlatformRouter"; import {PlatformMiddlewaresChain} from "../services/PlatformMiddlewaresChain"; @@ -71,7 +71,7 @@ export function buildRouter(injector: InjectorService, provider: ControllerProvi }); // build all endpoints and his middlewares - getOperationsRoutes(provider.token).forEach((operationRoute) => { + getOperationsRoutes(provider.token).forEach((operationRoute) => { const handlers = platformMiddlewaresChain?.get(provider, operationRoute); router.addRoute({ diff --git a/packages/platform/common/src/services/PlatformMiddlewaresChain.spec.ts b/packages/platform/common/src/services/PlatformMiddlewaresChain.spec.ts index be97b0c9e70..e814322b42a 100644 --- a/packages/platform/common/src/services/PlatformMiddlewaresChain.spec.ts +++ b/packages/platform/common/src/services/PlatformMiddlewaresChain.spec.ts @@ -45,12 +45,11 @@ describe("PlatformMiddlewaresChain", () => { const chain = platformMiddlewaresChain.get(provider, operationRoutes[1]); expect(chain[0].type).to.eq("context"); - expect(chain[1]).to.eq(PlatformAcceptMimesMiddleware); - expect(chain[2]).to.eq(endpoint.beforeMiddlewares[0]); - expect(chain[3]).to.eq(provider.middlewares.use[0]); - expect(chain[4]).to.eq(endpoint.middlewares[0]); - expect(chain[5]).to.eq(endpoint); - expect(chain[6]).to.eq(endpoint.afterMiddlewares[0]); - expect(chain[7]).to.eq(provider.middlewares.useAfter[0]); + expect(chain[1]).to.eq(endpoint.beforeMiddlewares[0]); + expect(chain[2]).to.eq(provider.middlewares.use[0]); + expect(chain[3]).to.eq(endpoint.middlewares[0]); + expect(chain[4]).to.eq(endpoint); + expect(chain[5]).to.eq(endpoint.afterMiddlewares[0]); + expect(chain[6]).to.eq(provider.middlewares.useAfter[0]); }); }); diff --git a/packages/platform/common/src/services/PlatformMiddlewaresChain.ts b/packages/platform/common/src/services/PlatformMiddlewaresChain.ts index b57ce6e0bb5..17cdef30405 100644 --- a/packages/platform/common/src/services/PlatformMiddlewaresChain.ts +++ b/packages/platform/common/src/services/PlatformMiddlewaresChain.ts @@ -1,6 +1,6 @@ -import {Injectable} from "@tsed/di"; +import {Constant, Injectable} from "@tsed/di"; import {ParamTypes} from "@tsed/platform-params"; -import {EndpointMetadata, JsonOperationRoute} from "@tsed/schema"; +import {JsonMethodStore, JsonOperationRoute} from "@tsed/schema"; import {ControllerProvider} from "../domain/ControllerProvider"; import {bindEndpointMiddleware} from "../middlewares/bindEndpointMiddleware"; import {PlatformAcceptMimesMiddleware} from "../middlewares/PlatformAcceptMimesMiddleware"; @@ -9,7 +9,10 @@ import {useCtxHandler} from "../utils/useCtxHandler"; @Injectable() export class PlatformMiddlewaresChain { - get(provider: ControllerProvider, operationRoute: JsonOperationRoute) { + @Constant("acceptMimes", []) + protected acceptMimes: string[]; + + get(provider: ControllerProvider, operationRoute: JsonOperationRoute) { const {endpoint} = operationRoute; const {beforeMiddlewares, middlewares: mldwrs, afterMiddlewares} = endpoint; @@ -19,8 +22,8 @@ export class PlatformMiddlewaresChain { return [ useCtxHandler(bindEndpointMiddleware(endpoint)), - PlatformAcceptMimesMiddleware, - this.uploadFile(operationRoute) && PlatformMulterMiddleware, + this.hasAcceptMimes(operationRoute) && PlatformAcceptMimesMiddleware, + operationRoute.has(ParamTypes.FILES) && PlatformMulterMiddleware, ...beforeMiddlewares, ...use, ...mldwrs, @@ -30,7 +33,7 @@ export class PlatformMiddlewaresChain { ].filter((item: any) => !!item); } - private uploadFile(operationRoute: JsonOperationRoute) { - return [...operationRoute.endpoint.children.values()].find((item) => item.paramType === ParamTypes.FILES); + protected hasAcceptMimes(operationRoute: JsonOperationRoute) { + return operationRoute.endpoint.acceptMimes.length || this.acceptMimes.length; } } diff --git a/packages/specs/schema/src/domain/JsonOperationRoute.spec.ts b/packages/specs/schema/src/domain/JsonOperationRoute.spec.ts index 06eebfcc0f4..6bf12baea59 100644 --- a/packages/specs/schema/src/domain/JsonOperationRoute.spec.ts +++ b/packages/specs/schema/src/domain/JsonOperationRoute.spec.ts @@ -1,4 +1,5 @@ import {Get, JsonEntityStore, JsonOperation, JsonOperationRoute, Name} from "@tsed/schema"; +import {BodyParams} from "@tsed/platform-params"; describe("JsonOperationRoute", () => { it("should create JsonOperationRoute instance", () => { @@ -66,4 +67,41 @@ describe("JsonOperationRoute", () => { expect(operationRoute.operation).toBeInstanceOf(JsonOperation); expect(operationRoute.operationId).toBe("get"); }); + it("should create operation with parameter", () => { + @Name("Testify") + class Test { + @Get("/") + @Name("getify") + get(@BodyParams() body: string) {} + } + + const endpoint = JsonEntityStore.fromMethod(Test, "get"); + const operationRoute = new JsonOperationRoute({ + token: Test, + endpoint, + operationPath: {method: "GET", path: "/"}, + basePath: "/base" + }); + + expect(operationRoute.operationPath).toEqual({ + method: "GET", + path: "/" + }); + expect(operationRoute.method).toEqual("GET"); + expect(operationRoute.path).toEqual("/"); + expect(operationRoute.fullPath).toEqual("/base/"); + expect(operationRoute.url).toEqual("/base/"); + expect(operationRoute.isFinal).toEqual(false); + expect(operationRoute.name).toEqual("Test.get()"); + expect(operationRoute.className).toEqual("Test"); + expect(operationRoute.methodClassName).toEqual("get"); + expect(operationRoute.propertyKey).toEqual("get"); + expect(operationRoute.propertyName).toEqual("get"); + expect(operationRoute.operation).toBeInstanceOf(JsonOperation); + expect(operationRoute.operationId).toBe("get"); + expect(operationRoute.paramsTypes).toEqual({ + BODY: true + }); + expect(operationRoute.has("BODY")).toEqual(true); + }); }); diff --git a/packages/specs/schema/src/domain/JsonOperationRoute.ts b/packages/specs/schema/src/domain/JsonOperationRoute.ts index 3bbd28ee909..0b7c4bfa3ef 100644 --- a/packages/specs/schema/src/domain/JsonOperationRoute.ts +++ b/packages/specs/schema/src/domain/JsonOperationRoute.ts @@ -1,17 +1,25 @@ import {Type} from "@tsed/core"; +import {JsonMethodStore} from "./JsonMethodStore"; import {JsonMethodPath, JsonOperation} from "./JsonOperation"; -import {JsonEntityStore} from "./JsonEntityStore"; import {concatPath} from "../utils/concatPath"; -import {JsonParameterStore} from "./JsonParameterStore"; -export class JsonOperationRoute { +export class JsonOperationRoute { readonly token: Type; readonly endpoint: Entity; readonly operationPath?: JsonMethodPath; readonly basePath?: string; + readonly paramsTypes: Record; constructor(options: Partial) { Object.assign(this, options); + + this.paramsTypes = [...this.endpoint.children.values()].reduce( + (obj, item) => ({ + ...obj, + [item.paramType]: true + }), + {} + ); } get url() { @@ -69,4 +77,8 @@ export class JsonOperationRoute( +export function getOperationsRoutes( token: Type | any, options: GetOperationsRoutesOptions = {} ): JsonOperationRoute[] {