Skip to content

Commit

Permalink
Merge 84c1e07 into e786bed
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita authored Sep 19, 2020
2 parents e786bed + 84c1e07 commit ef39251
Show file tree
Hide file tree
Showing 58 changed files with 2,031 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@
"check-coverage": true,
"lines": 99.86,
"statements": 99.86,
"functions": 99.82,
"branches": 88.64
"functions": 99.76,
"branches": 88.52
}
2 changes: 2 additions & 0 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
"private": false,
"keywords": [
"Express",
"Koa",
"TypeScript",
"typescript",
"Decorator",
"decorators",
"decorator",
"express",
"koa",
"Controller",
"Inject",
"ioc",
Expand Down
10 changes: 8 additions & 2 deletions packages/common/src/mvc/decorators/method/useAfter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,38 @@ class CustomMiddleware {
use() {}
}

class CustomMiddleware2 {
use() {}
}

describe("UseAfter()", () => {
describe("when the decorator is use on a class", () => {
it("should add the middleware on the use stack", () => {
// WHEN
@UseAfter(CustomMiddleware)
@UseAfter(CustomMiddleware2)
class Test {
test() {}
}

// THEN
const store = Store.from(Test).get("middlewares");

expect(store).to.deep.eq({useAfter: [CustomMiddleware]});
expect(store).to.deep.eq({useAfter: [CustomMiddleware, CustomMiddleware2]});
});
});
describe("when the decorator is use on a method", () => {
it("should add the middleware on the use stack", () => {
// WHEN
class Test {
@UseAfter(CustomMiddleware)
@UseAfter(CustomMiddleware2)
test() {}
}

// THEN
const endpoint = EndpointMetadata.get(Test, "test");
expect(endpoint.afterMiddlewares).to.deep.equal([CustomMiddleware]);
expect(endpoint.afterMiddlewares).to.deep.equal([CustomMiddleware, CustomMiddleware2]);
});
});
describe("when the decorator is use in another way", () => {
Expand Down
11 changes: 9 additions & 2 deletions packages/common/src/mvc/decorators/method/useAfter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,18 @@ export function UseAfter(...args: any[]): Function {
return JsonEntityFn((entity, parameters) => {
switch (entity.decoratorType) {
case DecoratorTypes.METHOD:
(entity as EndpointMetadata).after(args);
const endpoint = entity as EndpointMetadata;
endpoint.afterMiddlewares = args.concat(endpoint.afterMiddlewares);

break;

case DecoratorTypes.CLASS:
entity.store.merge("middlewares", {useAfter: args});
const middlewares = entity.store.get("middlewares") || {};

entity.store.set("middlewares", {
...middlewares,
useAfter: [...args, ...(middlewares.useAfter || [])]
});
break;

default:
Expand Down
10 changes: 8 additions & 2 deletions packages/common/src/mvc/decorators/method/useBefore.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@ class CustomMiddleware {
use() {}
}

class CustomMiddleware2 {
use() {}
}

describe("UseBefore()", () => {
describe("when the decorator is use on a class", () => {
it("should add the middleware on the use stack", () => {
// WHEN
@UseBefore(CustomMiddleware)
@UseBefore(CustomMiddleware2)
class Test {
test() {}
}

// THEN
const result = Store.from(Test).get("middlewares");

expect(result).to.deep.eq({useBefore: [CustomMiddleware]});
expect(result).to.deep.eq({useBefore: [CustomMiddleware, CustomMiddleware2]});
});
});
describe("when the decorator is use on a method", () => {
it("should add the middleware on the use stack", () => {
// WHEN
class Test {
@UseBefore(CustomMiddleware)
@UseBefore(CustomMiddleware2)
test() {}
}

const endpoint = EndpointMetadata.get(Test, "test");
// THEN
expect(endpoint.beforeMiddlewares).to.deep.eq([CustomMiddleware]);
expect(endpoint.beforeMiddlewares).to.deep.eq([CustomMiddleware, CustomMiddleware2]);
});
});
describe("when the decorator is use in another way", () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/common/src/mvc/decorators/method/useBefore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EndpointMetadata} from "../../models/EndpointMetadata";
import {DecoratorTypes, UnsupportedDecoratorType} from "@tsed/core";
import {JsonEntityFn} from "@tsed/schema";
import {EndpointMetadata} from "../../models/EndpointMetadata";

/**
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
Expand All @@ -26,11 +26,18 @@ export function UseBefore(...args: any[]): Function {
return JsonEntityFn((entity, parameters) => {
switch (entity.decoratorType) {
case DecoratorTypes.METHOD:
(entity as EndpointMetadata).before(args);
const endpoint = entity as EndpointMetadata;
endpoint.beforeMiddlewares = args.concat(endpoint.beforeMiddlewares);

break;

case DecoratorTypes.CLASS:
entity.store.merge("middlewares", {useBefore: args});
const middlewares = entity.store.get("middlewares") || {};

entity.store.set("middlewares", {
...middlewares,
useBefore: [...args, ...(middlewares.useBefore || [])]
});
break;

default:
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/mvc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ export * from "./errors/ValidationError";

// decorators
export * from "./decorators";
export * from "./utils/mapParamsOptions";
1 change: 0 additions & 1 deletion packages/common/src/platform/constants/abort.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/common/src/platform/constants/routerOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ROUTER_OPTIONS = Symbol.for("ROUTER_OPTIONS");
10 changes: 5 additions & 5 deletions packages/common/src/platform/domain/ControllerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Enumerable, NotEnumerable, Type} from "@tsed/core";
import {Provider, ProviderType} from "@tsed/di";
import {JsonEntityStore} from "@tsed/schema";
import {ControllerMiddlewares, EndpointMetadata} from "../../mvc";
import {ROUTER_OPTIONS} from "../constants/routerOptions";
import {PlatformRouterMethods} from "../interfaces/PlatformRouterMethods";

export interface IChildrenController extends Type<any> {
Expand Down Expand Up @@ -64,18 +65,17 @@ export class ControllerProvider<T = any> extends Provider<T> {

/**
*
* @returns {IRouterSettings}
*/
get routerOptions(): any {
return this.store.get("routerOptions") || {};
get routerOptions(): TsED.RouterOptions {
return this.store.get(ROUTER_OPTIONS) || ({} as any);
}

/**
*
* @param value
*/
set routerOptions(value: any) {
this.store.set("routerOptions", value);
set routerOptions(value: TsED.RouterOptions) {
this.store.set(ROUTER_OPTIONS, value);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/platform/domain/HandlerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {isPromise, isStream} from "@tsed/core";
import {InjectorService} from "@tsed/di";
import {isObservable} from "rxjs";
import {HandlerMetadata} from "../../mvc/models/HandlerMetadata";
import {ABORT} from "../constants/abort";
import {PlatformContext} from "./PlatformContext";

function isResponse(obj: any) {
Expand Down Expand Up @@ -183,7 +182,7 @@ export class HandlerContext {
} = this;

if (process) {
if (process === $ctx.getResponse() || process === ABORT) {
if (process === $ctx.getResponse()) {
// ABANDON
return this.cancel();
}
Expand Down
1 change: 1 addition & 0 deletions packages/common/src/platform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from "./decorators/context";
export * from "./decorators/multer";

// interfaces
export * from "./constants/routerOptions";
export * from "./interfaces/IRoute";
export * from "./interfaces/PlatformRouterMethods";

Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/platform/services/PlatformResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class PlatformResponse<T extends {[key: string]: any} = any> {
}

hasStatus() {
return this.statusCode === 200;
return this.statusCode !== 200;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/platform/utils/setResponseHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function setResponseHeaders(ctx: PlatformContext) {
return;
}

if (response.hasStatus()) {
if (!response.hasStatus()) {
// apply status only if the isn't already modified
response.status(operation.getStatus());
}
Expand Down
65 changes: 65 additions & 0 deletions packages/exceptions/src/constants/statuses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export const HTTP_STATUSES: any = {
"100": "Continue",
"101": "Switching Protocols",
"102": "Processing",
"103": "Early Hints",
"200": "OK",
"201": "Created",
"202": "Accepted",
"203": "Non-Authoritative Information",
"204": "No Content",
"205": "Reset Content",
"206": "Partial Content",
"207": "Multi-Status",
"208": "Already Reported",
"226": "IM Used",
"300": "Multiple Choices",
"301": "Moved Permanently",
"302": "Found",
"303": "See Other",
"304": "Not Modified",
"305": "Use Proxy",
"307": "Temporary Redirect",
"308": "Permanent Redirect",
"400": "Bad Request",
"401": "Unauthorized",
"402": "Payment Required",
"403": "Forbidden",
"404": "Not Found",
"405": "Method Not Allowed",
"406": "Not Acceptable",
"407": "Proxy Authentication Required",
"408": "Request Timeout",
"409": "Conflict",
"410": "Gone",
"411": "Length Required",
"412": "Precondition Failed",
"413": "Payload Too Large",
"414": "URI Too Long",
"415": "Unsupported Media Type",
"416": "Range Not Satisfiable",
"417": "Expectation Failed",
"418": "I'm a Teapot",
"421": "Misdirected Request",
"422": "Unprocessable Entity",
"423": "Locked",
"424": "Failed Dependency",
"425": "Too Early",
"426": "Upgrade Required",
"428": "Precondition Required",
"429": "Too Many Requests",
"431": "Request Header Fields Too Large",
"451": "Unavailable For Legal Reasons",
"500": "Internal Server Error",
"501": "Not Implemented",
"502": "Bad Gateway",
"503": "Service Unavailable",
"504": "Gateway Timeout",
"505": "HTTP Version Not Supported",
"506": "Variant Also Negotiates",
"507": "Insufficient Storage",
"508": "Loop Detected",
"509": "Bandwidth Limit Exceeded",
"510": "Not Extended",
"511": "Network Authentication Required"
};
1 change: 1 addition & 0 deletions packages/exceptions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./core";
export * from "./constants/statuses";
export * from "./clientErrors";
export * from "./redirections";
export * from "./serverErrors";
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ROUTER_OPTIONS} from "@tsed/common";
import {Store} from "@tsed/core";
import {CaseSensitive, MergeParams, Strict} from "@tsed/platform-express";
import {expect} from "chai";
Expand All @@ -8,23 +9,23 @@ describe("RouterSettings", () => {
describe("MergeParams", () => {
it("should call merge method for mergeParams options", () => {
MergeParams(true)(Test);
const store = Store.from(Test).get("routerOptions");
const store = Store.from(Test).get(ROUTER_OPTIONS);
expect(store.mergeParams).to.eq(true);
});
});

describe("CaseSensitive", () => {
it("should call merge method for mergeParams options", () => {
CaseSensitive(true)(Test);
const store = Store.from(Test).get("routerOptions");
const store = Store.from(Test).get(ROUTER_OPTIONS);
expect(store.caseSensitive).to.eq(true);
});
});

describe("Strict", () => {
it("should call merge method for mergeParams options", () => {
Strict(true)(Test);
const store = Store.from(Test).get("routerOptions");
const store = Store.from(Test).get(ROUTER_OPTIONS);
expect(store.strict).to.eq(true);
});
});
Expand Down
9 changes: 5 additions & 4 deletions packages/platform-express/src/decorators/routerSettings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {ROUTER_OPTIONS} from "@tsed/common";
import {StoreMerge} from "@tsed/core";
import {RouterOptions} from "express";

Expand All @@ -14,15 +15,15 @@ import {RouterOptions} from "express";
*
* Property | Description | Default
* ---|---|---
* caseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
* mergeParams | Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. | false
* strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
* caseSensitive | Enable case sensitivity. | Disabled by default, treating “/Foo” and “/foo” as the same.
* mergeParams | Preserve the req.params values from the parent router. If the parent and the child have conflicting param names, the child’s value take precedence. | false
* strict | Enable strict routing. | Disabled by default, “/foo” and “/foo/” are treated the same by the router.
*
* @returns {(target:any)=>void}
* @decorator
* @param routerOptions
* @express
*/
export function RouterSettings(routerOptions: RouterOptions): Function {
return StoreMerge("routerOptions", routerOptions);
return StoreMerge(ROUTER_OPTIONS, routerOptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe("PlatformExpressHandler", () => {

// WHEN
// @ts-ignore
const value = platformHandler.getArg(param.type, h);
const value = platformHandler.getArg(param.paramType, h);

// THEN
expect(value).to.deep.eq(request);
Expand Down Expand Up @@ -367,7 +367,7 @@ describe("PlatformExpressHandler", () => {
const value = platformHandler.getArg(param.paramType, h);

// THEN
expect(value).to.deep.eq(h.response.locals);
expect(value).to.deep.eq(h.getResponse().locals);
});

it("should return request by default", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function createResponse() {
return {res, response};
}

describe("PlatformResponse", () => {
describe("PlatformExpressResponse", () => {
beforeEach(() => PlatformTest.create());
afterEach(() => PlatformTest.reset());
it("should create a PlatformResponse instance", () => {
Expand Down
Loading

0 comments on commit ef39251

Please sign in to comment.