From 998fb6212a546729e945db5b46268d895b23237c Mon Sep 17 00:00:00 2001 From: Romain Lenzotti Date: Sun, 24 Mar 2024 09:58:56 +0100 Subject: [PATCH] feat(platform-router): add PlatformHandlerMetadata.isRawMiddleware --- packages/platform/platform-fastify/package.json | 2 ++ .../src/domain/PlatformHandlerMetadata.spec.ts | 6 ++++++ .../platform-router/src/domain/PlatformHandlerMetadata.ts | 4 ++++ yarn.lock | 4 +++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/platform/platform-fastify/package.json b/packages/platform/platform-fastify/package.json index a1099ee8cc1..908e5f4cb06 100644 --- a/packages/platform/platform-fastify/package.json +++ b/packages/platform/platform-fastify/package.json @@ -70,9 +70,11 @@ "@tsed/di": "workspace:*", "@tsed/platform-test-sdk": "workspace:*", "@types/content-disposition": "^0.5.4", + "barrelsby": "2.8.1", "cross-env": "7.0.3", "fastify": "4.23.2", "fastify-raw-body": ">=4.3.0", + "jest": "^29.2.0", "ts-node": "10.9.2" }, "peerDependencies": { diff --git a/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.spec.ts b/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.spec.ts index 46eefdc0414..73c91a6e7e3 100644 --- a/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.spec.ts +++ b/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.spec.ts @@ -32,6 +32,7 @@ describe("PlatformHandlerMetadata", () => { expect(handlerMetadata.hasNextFunction).toEqual(false); expect(handlerMetadata.hasErrorParam).toEqual(false); expect(handlerMetadata.toString()).toEqual(""); + expect(handlerMetadata.isRawMiddleware()).toEqual(false); }); }); describe("from function", () => { @@ -48,6 +49,7 @@ describe("PlatformHandlerMetadata", () => { expect(handlerMetadata.hasNextFunction).toEqual(true); expect(handlerMetadata.hasErrorParam).toEqual(false); expect(handlerMetadata.toString()).toEqual("handler"); + expect(handlerMetadata.isRawMiddleware()).toEqual(true); }); }); describe("from function err", () => { @@ -65,6 +67,7 @@ describe("PlatformHandlerMetadata", () => { expect(handlerMetadata.hasErrorParam).toEqual(true); expect(handlerMetadata.propertyKey).toBeUndefined(); expect(handlerMetadata.toString()).toEqual("handler"); + expect(handlerMetadata.isRawMiddleware()).toEqual(true); }); }); describe("from function without nextFn", () => { @@ -83,6 +86,7 @@ describe("PlatformHandlerMetadata", () => { expect(handlerMetadata.hasErrorParam).toEqual(false); expect(handlerMetadata.propertyKey).toBeUndefined(); expect(handlerMetadata.toString()).toEqual("handler"); + expect(handlerMetadata.isRawMiddleware()).toEqual(true); }); }); describe("from endpoint/middleware with injection", () => { @@ -116,6 +120,7 @@ describe("PlatformHandlerMetadata", () => { expect(handlerMetadata.getParams()[0].paramType).toEqual("REQUEST"); expect(handlerMetadata.getParams()[1].paramType).toEqual("NEXT_FN"); + expect(handlerMetadata.isRawMiddleware()).toEqual(false); }); }); @@ -144,6 +149,7 @@ describe("PlatformHandlerMetadata", () => { expect(handlerMetadata.hasErrorParam).toEqual(true); expect(handlerMetadata.propertyKey).toEqual("use"); expect(handlerMetadata.toString()).toEqual("Test.use"); + expect(handlerMetadata.isRawMiddleware()).toEqual(false); }); }); }); diff --git a/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.ts b/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.ts index 28990248ebf..3136d59dffa 100644 --- a/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.ts +++ b/packages/platform/platform-router/src/domain/PlatformHandlerMetadata.ts @@ -138,6 +138,10 @@ export class PlatformHandlerMetadata { return this.type === PlatformHandlerType.RESPONSE_FN; } + public isRawMiddleware() { + return !this.isInjectable() && (this.type === PlatformHandlerType.RAW_FN || this.type === PlatformHandlerType.RAW_ERR_FN); + } + toString() { return [nameOf(this.target), this.propertyKey].filter(Boolean).join("."); } diff --git a/yarn.lock b/yarn.lock index 52da7d1d309..13d50fcfbcc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7323,10 +7323,12 @@ __metadata: "@tsed/di": "workspace:*" "@tsed/platform-test-sdk": "workspace:*" "@types/content-disposition": "npm:^0.5.4" + barrelsby: "npm:2.8.1" content-disposition: "npm:>=0.5.4" cross-env: "npm:7.0.3" fastify: "npm:4.23.2" fastify-raw-body: "npm:>=4.3.0" + jest: "npm:^29.2.0" ts-node: "npm:10.9.2" tslib: "npm:2.6.1" peerDependencies: @@ -11274,7 +11276,7 @@ __metadata: languageName: node linkType: hard -"barrelsby@npm:^2.8.1": +"barrelsby@npm:2.8.1, barrelsby@npm:^2.8.1": version: 2.8.1 resolution: "barrelsby@npm:2.8.1" dependencies: