Skip to content

Commit

Permalink
Merge chore-clean-code
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed May 16, 2021
2 parents ca044a0 + d70dc3b commit c8c9f5f
Show file tree
Hide file tree
Showing 15 changed files with 18 additions and 175 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {AcceptMimesMiddleware, UseBefore} from "@tsed/common";
import {applyDecorators, StoreSet} from "@tsed/core";
import {useDecorators, StoreSet} from "@tsed/core";

export function AcceptMime(...mimes: string[]): Function {
return applyDecorators(
return useDecorators(
StoreSet(AcceptMimesMiddleware, mimes),
UseBefore(AcceptMimesMiddleware)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {UseAfter} from "@tsed/common";
import {applyDecorators} from "@tsed/core";
import {useDecorators} from "@tsed/core";

export function CustomStatus(code: number) {
return applyDecorators(
return useDecorators(
UseAfter((req: any, res: any, next: any) => {
res.status(code);
next();
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/snippets/pipes/pipes-decorator-with-options.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {RawPathParams, UsePipe} from "@tsed/common";
import {applyDecorators} from "@tsed/core";
import {useDecorators} from "@tsed/core";
import {PersonPipe} from "../services/PersonPipe";

export interface IUsePersonParamOptions {
optional?: boolean;
}

export function UsePersonParam(expression: string, options: IUsePersonParamOptions = {}): ParameterDecorator {
return applyDecorators(
return useDecorators(
RawPathParams(expression),
UsePipe(PersonPipe, options) // UsePipe accept second parameter to store your options
);
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/snippets/pipes/pipes-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {RawPathParams, UsePipe} from "@tsed/common";
import {applyDecorators} from "@tsed/core";
import {useDecorators} from "@tsed/core";
import {PersonPipe} from "../services/PersonPipe";

export function UsePersonParam(expression: string): ParameterDecorator {
return applyDecorators(
return useDecorators(
RawPathParams(expression),
UsePipe(PersonPipe)
);
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/snippets/pipes/use-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import {
UseDeserialization,
UseParamExpression,
UseParamType,
UseType,
UseValidation
} from "@tsed/common";
import {UseType} from "@tsed/common";
import {applyDecorators} from "@tsed/core";
import {useDecorators} from "@tsed/core";

export function UseParam(paramType: ParamTypes | string, options: IParamOptions<any> = {}): ParameterDecorator {
return applyDecorators(
return useDecorators(
UseParamType(paramType),
options.useType && UseType(options.useType),
options.expression && UseParamExpression(options.expression),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/interfaces/HashOf.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type HashOf<T> = {[key: string]: T};
export type HashOf<T> = Record<string, T>;
21 changes: 0 additions & 21 deletions packages/core/src/utils/applyBefore.spec.ts

This file was deleted.

16 changes: 0 additions & 16 deletions packages/core/src/utils/applyBefore.ts

This file was deleted.

76 changes: 1 addition & 75 deletions packages/core/src/utils/decorators/decoratorTypeOf.spec.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,9 @@
import {expect} from "chai";
import {decoratorTypeOf, getDecoratorType} from "./decoratorTypeOf";
import {decoratorTypeOf} from "./decoratorTypeOf";

class Test {}

describe("decoratorTypeOf()", () => {
describe("when short type", () => {
it("should return class", () => {
expect(getDecoratorType([Test])).to.equal("class");
});

it("should return property (static)", () => {
expect(getDecoratorType([Test, "props"])).to.equal("property");
});

it("should return property (instance)", () => {
expect(getDecoratorType([Test.prototype, "props"])).to.equal("property");
});

it("should return method (instance, getter)", () => {
expect(
decoratorTypeOf([
Test.prototype,
"props",
{
get: () => {}
}
])
).to.equal("property");
});

it("should return method (instance, setter)", () => {
expect(
decoratorTypeOf([
Test.prototype,
"props",
{
set: () => {}
}
])
).to.equal("property");
});

it("should return method (static)", () => {
expect(
decoratorTypeOf([
Test,
"props",
{
value: () => {}
}
])
).to.equal("method.static");
});

it("should return method (instance)", () => {
expect(
decoratorTypeOf([
Test.prototype,
"props",
{
value: () => {}
}
])
).to.equal("method");
});

it("should return params (static)", () => {
expect(decoratorTypeOf([Test, "props", 0])).to.equal("parameter.static");
});

it("should return params (instance)", () => {
expect(decoratorTypeOf([Test.prototype, "props", 0])).to.equal("parameter");
});

it("should return params (constructor)", () => {
expect(decoratorTypeOf([Test.prototype, undefined, 0])).to.equal("parameter.constructor");
});
});

it("should return class", () => {
expect(decoratorTypeOf([Test])).to.equal("class");
});
Expand Down
9 changes: 0 additions & 9 deletions packages/core/src/utils/decorators/decoratorTypeOf.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {DecoratorTypes} from "../../domain/DecoratorTypes";
import {classOf} from "../objects/classOf";

/**
* @deprecated Since 2020-11-28. Use decoratorTypeOf function.
*/
export function getDecoratorType(args: any[], longType = false): DecoratorTypes {
const type = decoratorTypeOf(args);

return longType ? type : ((type as string).split(".")[0] as any);
}

export function decoratorTypeOf(args: any[]): DecoratorTypes {
const [target, propertyKey, descriptor] = args;

Expand Down
28 changes: 1 addition & 27 deletions packages/core/src/utils/decorators/useDecorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {applyDecorators, Store, StoreFn, useDecorators} from "@tsed/core";
import {Store, StoreFn, useDecorators} from "@tsed/core";
import {expect} from "chai";
import {AnyDecorator} from "../../interfaces/AnyDecorator";

Expand Down Expand Up @@ -27,29 +27,3 @@ describe("useDecorators", () => {
expect(Store.from(Test).get("decorator2")).to.eq("test2");
});
});

describe("applyDecorators", () => {
function decorator1(value: any) {
return StoreFn((store) => {
store.set("decorator1", value);
});
}

function decorator2(value: any) {
return StoreFn((store) => {
store.set("decorator2", value);
});
}

function decorate() {
return applyDecorators(decorator1("test1"), decorator2("test2"));
}

@decorate()
class Test {}

it("should apply all decorators", () => {
expect(Store.from(Test).get("decorator1")).to.eq("test1");
expect(Store.from(Test).get("decorator2")).to.eq("test2");
});
});
9 changes: 0 additions & 9 deletions packages/core/src/utils/decorators/useDecorators.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import {DecoratorParameters} from "../../interfaces/DecoratorParameters";
import {AnyDecorator} from "../../interfaces/AnyDecorator";

/**
* @deprecated Since 2020-11-28. Use useDecorators function.
* @param decorators
* @ignore
*/
export function applyDecorators(...decorators: (any | ClassDecorator | MethodDecorator | PropertyDescriptor | ParameterDecorator)[]): any {
return useDecorators(...decorators);
}

export function useDecorators(...decorators: AnyDecorator[]): any {
return (...args: DecoratorParameters) => {
decorators
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./applyBefore";
export * from "./catchError";
export * from "./proxyDelegation";
export * from "./uniq";
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/utils/objects/deepExtends.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {HashOf} from "../../interfaces/HashOf";
import {classOf} from "./classOf";
import {isArrayOrArrayClass} from "./isArray";
import {isPrimitive} from "./isPrimitive";
import {objectKeys} from "./objectKeys";

export type DeepExtendsReducers = HashOf<(collection: any[], value: any) => any>;
export type DeepExtendsReducers = Record<string, (collection: any[], value: any) => any>;

function reducer() {
return (collection: any[], value: any) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/schema/src/domain/JsonResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {cleanObject, HashOf} from "@tsed/core";
import {cleanObject} from "@tsed/core";
import {OS3MediaType, OS3Response} from "@tsed/openspec";
import {JsonHeader, JsonSchemaOptions} from "../interfaces";
import {mapHeaders} from "../utils/mapHeaders";
Expand Down Expand Up @@ -44,13 +44,13 @@ export class JsonResponse extends JsonMap<JsonResponseOptions> {
return this;
}

headers(headers: HashOf<string | JsonHeader>): this {
headers(headers: Record<string, string | JsonHeader>): this {
this.set("headers", mapHeaders(headers));

return this;
}

content(content: HashOf<OS3MediaType<JsonSchema>>) {
content(content: Record<string, OS3MediaType<JsonSchema>>) {
this.set("content", toJsonMapCollection(content, JsonMedia));

return this;
Expand Down

0 comments on commit c8c9f5f

Please sign in to comment.