Skip to content

Commit

Permalink
Merge refactoring-handler-orchestration
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Sep 12, 2020
2 parents 35ccc80 + 750c6bb commit 1a096b9
Show file tree
Hide file tree
Showing 50 changed files with 991 additions and 1,092 deletions.
6 changes: 3 additions & 3 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"lcov"
],
"check-coverage": true,
"lines": 99.73,
"lines": 99.8,
"statements": 99.81,
"functions": 99.62,
"branches": 88.45
"functions": 99.69,
"branches": 88.54
}
15 changes: 2 additions & 13 deletions docs/docs/middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,13 @@ The decorator @@OverrideProvider@@ gives you the ability to override some intern

<<< @/docs/docs/snippets/middlewares/override-middleware.ts

Since v5.63.0, Ts.ED provide a PlatformResponseMiddleware (it replaces @@SendResponseMiddleware@@ and @@ResponseViewMiddleware@@),
to render a view and send the appropriate response to your consumer according to the executed endpoint.

You are able to override this middleware to change the initial behavior of this middleware.

<<< @/docs/docs/snippets/middlewares/override-platform-response-middleware.ts

Here we use the new [Platform API](/docs/platform-api.md) to write our middleware. By using @@Context@@ decorator and @@PlatformContext@@ class we can get some information:
Here we use the new [Platform API](/docs/platform-api.md) to write our middleware.
By using @@Context@@ decorator and @@PlatformContext@@ class we can get some information:

- The data returned by the last executed endpoint,
- The @@EndpointMetadata@@ itself,
- The @@PlatformRequest@@ and @@PlatformResponse@@ classes abstraction. These classes allow better code abstraction by exposing methods that are agnostic to Express.js.

::: warning
Ts.ED have is own @@GlobalErrorHandlerMiddleware@@. Override this middleware is not necessary, since you can create you own middleware and
register it in your server before the original GlobalErrorHandlerMiddleware. For more details, see our [Exceptions](/docs/exceptions.md#handle-all-errors) documentation page.
:::

::: tip
By default, the server imports automatically your middlewares matching with this rules `${rootDir}/middlewares/**/*.ts` (See [componentScan configuration](/configuration.md)).

Expand Down
9 changes: 7 additions & 2 deletions docs/docs/snippets/middlewares/override-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {OriginalMiddleware, OverrideProvider} from "@tsed/common";
import {Context, OriginalMiddleware, OverrideProvider} from "@tsed/common";

@OverrideProvider(OriginalMiddleware)
export class CustomMiddleware extends OriginalMiddleware {
public use() {
public use(@Context() ctx: Context) {
ctx.response; // Ts.ED response
ctx.request; // Ts.ED resquest
ctx.getResponse(); // return Express.js or Koa.js response
ctx.getRequest(); // return Express.js or Koa.js request

// Do something
return super.use();
}
Expand Down

This file was deleted.

17 changes: 0 additions & 17 deletions docs/docs/snippets/middlewares/override-response-view.ts

This file was deleted.

29 changes: 0 additions & 29 deletions docs/docs/snippets/middlewares/override-send-response.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/ajv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
"scripts": {
"build": "tsc --build tsconfig.compile.json"
"build": "tsc --build tsconfig.compile.json",
"tsc": "tsc --build tsconfig.check.json"
},
"dependencies": {
"tslib": "1.11.0"
Expand Down
10 changes: 0 additions & 10 deletions packages/common/src/mvc/errors/TemplateRenderingError.spec.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/common/src/mvc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export * from "./pipes/DeserializerPipe";
export * from "./services/ConverterService";

// errors
export * from "./errors/TemplateRenderingError";
export * from "./errors/RequiredValidationError";
export * from "./errors/ValidationError";

Expand Down
9 changes: 6 additions & 3 deletions packages/common/src/mvc/interfaces/HandlerType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export enum HandlerType {
FUNCTION = "function",
$CTX = "context",
CUSTOM = "custom",
ENDPOINT = "endpoint",
MIDDLEWARE = "middleware",
CONTROLLER = "controller"
ERR_MIDDLEWARE = "err:middleware",
CTX_FN = "context",
RAW_FN = "raw:middleware",
RAW_ERR_FN = "raw:err:middleware"
}
42 changes: 27 additions & 15 deletions packages/common/src/mvc/models/HandlerMetadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,26 @@ import {Controller, Err, Get, Middleware, Next, ParamMetadata, ParamTypes, Req}
import {expect} from "chai";
import {HandlerType} from "../../../src/mvc/interfaces/HandlerType";
import {HandlerMetadata} from "../../../src/mvc/models/HandlerMetadata";
import {useCtxHandler} from "../../platform/utils/useCtxHandler";

describe("HandlerMetadata", () => {
describe("from useCtxHandler", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
const handler = useCtxHandler((ctx: any) => {});
const options = {
target: handler
};
// WHEN
const handlerMetadata = new HandlerMetadata(options);

// THEN
expect(handlerMetadata.injectable).to.eq(false);
expect(handlerMetadata.type).to.eq(HandlerType.CTX_FN);
expect(handlerMetadata.hasNextFunction).to.eq(false);
expect(handlerMetadata.hasErrorParam).to.eq(false);
});
});
describe("from function", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
Expand All @@ -15,12 +33,11 @@ describe("HandlerMetadata", () => {

// THEN
expect(handlerMetadata.injectable).to.eq(false);
expect(handlerMetadata.type).to.eq(HandlerType.FUNCTION);
expect(handlerMetadata.type).to.eq(HandlerType.RAW_FN);
expect(handlerMetadata.hasNextFunction).to.eq(true);
expect(handlerMetadata.hasErrorParam).to.eq(false);
});
});

describe("from function err", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
Expand All @@ -32,7 +49,7 @@ describe("HandlerMetadata", () => {

// THEN
expect(handlerMetadata.injectable).to.eq(false);
expect(handlerMetadata.type).to.eq(HandlerType.FUNCTION);
expect(handlerMetadata.type).to.eq(HandlerType.RAW_ERR_FN);
expect(handlerMetadata.hasNextFunction).to.eq(true);
expect(handlerMetadata.hasErrorParam).to.eq(true);
expect(handlerMetadata.propertyKey).to.eq(undefined);
Expand All @@ -57,7 +74,6 @@ describe("HandlerMetadata", () => {
]);
});
});

describe("from function without nextFn", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
Expand All @@ -70,13 +86,12 @@ describe("HandlerMetadata", () => {

// THEN
expect(handlerMetadata.injectable).to.eq(false);
expect(handlerMetadata.type).to.eq(HandlerType.FUNCTION);
expect(handlerMetadata.type).to.eq(HandlerType.RAW_FN);
expect(handlerMetadata.hasNextFunction).to.eq(false);
expect(handlerMetadata.hasErrorParam).to.eq(false);
expect(handlerMetadata.propertyKey).to.eq(undefined);
});
});

describe("from endpoint/middleware without injection", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
Expand All @@ -89,20 +104,19 @@ describe("HandlerMetadata", () => {
const options = {
target: Test,
propertyKey: "test",
type: HandlerType.CONTROLLER
type: HandlerType.ENDPOINT
};
// WHEN
const handlerMetadata = new HandlerMetadata(options);

// THEN
expect(handlerMetadata.injectable).to.eq(false);
expect(handlerMetadata.type).to.eq(HandlerType.CONTROLLER);
expect(handlerMetadata.type).to.eq(HandlerType.ENDPOINT);
expect(handlerMetadata.hasNextFunction).to.eq(true);
expect(handlerMetadata.hasErrorParam).to.eq(false);
expect(handlerMetadata.propertyKey).to.eq("test");
});
});

describe("from endpoint/middleware with injection", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
Expand All @@ -115,14 +129,14 @@ describe("HandlerMetadata", () => {
const options = {
target: Test,
propertyKey: "test",
type: HandlerType.CONTROLLER
type: HandlerType.ENDPOINT
};
// WHEN
const handlerMetadata = new HandlerMetadata(options);

// THEN
expect(handlerMetadata.injectable).to.eq(true);
expect(handlerMetadata.type).to.eq(HandlerType.CONTROLLER);
expect(handlerMetadata.type).to.eq(HandlerType.ENDPOINT);
expect(handlerMetadata.hasNextFunction).to.eq(true);
expect(handlerMetadata.hasErrorParam).to.eq(false);
expect(handlerMetadata.propertyKey).to.eq("test");
Expand All @@ -131,7 +145,6 @@ describe("HandlerMetadata", () => {
expect(handlerMetadata.getParams()[1].paramType).to.deep.eq("NEXT_FN");
});
});

describe("from middleware without injection and error", () => {
it("should create a new handlerMetadata with right metadata", () => {
// GIVEN
Expand All @@ -150,13 +163,12 @@ describe("HandlerMetadata", () => {

// THEN
expect(handlerMetadata.injectable).to.eq(false);
expect(handlerMetadata.type).to.eq(HandlerType.MIDDLEWARE);
expect(handlerMetadata.type).to.eq(HandlerType.RAW_ERR_FN);
expect(handlerMetadata.hasNextFunction).to.eq(true);
expect(handlerMetadata.hasErrorParam).to.eq(true);
expect(handlerMetadata.propertyKey).to.eq("use");
});
});

describe("from middleware with injection and error", () => {
it("should create a new handlerMetadata with right metadata", () => {
// WHEN
Expand All @@ -175,7 +187,7 @@ describe("HandlerMetadata", () => {

// THEN
expect(handlerMetadata.injectable).to.eq(true);
expect(handlerMetadata.type).to.eq(HandlerType.MIDDLEWARE);
expect(handlerMetadata.type).to.eq(HandlerType.ERR_MIDDLEWARE);
expect(handlerMetadata.hasNextFunction).to.eq(true);
expect(handlerMetadata.hasErrorParam).to.eq(true);
expect(handlerMetadata.propertyKey).to.eq("use");
Expand Down
Loading

0 comments on commit 1a096b9

Please sign in to comment.