Skip to content

Commit

Permalink
fix: update deps and type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed May 26, 2021
1 parent a692c96 commit c9c88b1
Show file tree
Hide file tree
Showing 31 changed files with 82 additions and 104 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"jest": "^27.0.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.0.0",
"typescript": "^4.2.4"
"typescript": "^4.3.2"
},
"resolutions": {
"minimist": "1.2.5"
Expand Down
7 changes: 3 additions & 4 deletions packages/app/src/Route/Route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {ActionAny} from "@bunt/unit";
import {Ctor, ILogable, isFunction, isString} from "@bunt/util";
import {Payload} from "../Payload";
import {IRoute, IRouteMatcher, RouteFactory, RouteMatcherFactory, RouteRuleArg} from "./interfaces";
import {IRoute, IRouteMatcher, RouteFactory, RouteMatcherFactory, RouteRuleArg, RouteRuleVariants} from "./interfaces";
import {RouteRule} from "./RouteRule";

export class Route<A extends ActionAny> implements IRoute<A>, ILogable<{ route: string }> {
public readonly route: string;
public readonly action: Ctor<A>;
public readonly payload?: Payload<A>;
public readonly payload?: RouteRule<A>;
readonly #matcher: IRouteMatcher;

constructor(matcher: RouteMatcherFactory, action: Ctor<A>, rule: RouteRuleArg<A>) {
Expand Down Expand Up @@ -37,7 +36,7 @@ export class Route<A extends ActionAny> implements IRoute<A>, ILogable<{ route:
return this.#matcher.match(route);
}

private getRuleArgs(rule: string | RouteRule<A>): { route: string, payload?: Payload<A> } {
private getRuleArgs(rule: RouteRuleArg<A>): RouteRuleVariants<A> {
if (isString(rule)) {
return {route: rule, payload: undefined};
}
Expand Down
10 changes: 6 additions & 4 deletions packages/app/src/Route/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ActionAny, ActionContext, ActionCtor, ApplyContext, Context} from "@bunt/unit";
import {ActionAny, ActionContext, ActionCtor, ActionState, ApplyContext} from "@bunt/unit";
import {Ctor} from "@bunt/util";
import {IRequest} from "../interfaces";
import {Payload} from "../Payload";
Expand All @@ -19,9 +19,11 @@ export interface IRoute<A extends ActionAny> {

export type RouteMatcherFactory = IRouteMatcher | ((route: string) => IRouteMatcher);

export type RouteRuleArg<A extends ActionAny> = A extends ActionAny<any, infer S>
? S extends null ? string : RouteRule<A>
: string;
export type RouteRuleVariants<A extends ActionAny> = { route: string, payload: undefined }
| { route: string, payload: RouteRule<A> };

export type RouteRuleArg<A extends ActionAny> = ActionState<A> extends null
? string : RouteRule<A>;

export type RouteFactory = <A extends ActionAny>(action: Ctor<A>, rule: RouteRuleArg<A>) => Route<A>;

Expand Down
5 changes: 1 addition & 4 deletions packages/input/src/Assertion/AssertionListError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ export interface IReadableListError extends IReadableTypeError {
export class AssertionListError extends AssertionTypeError {
readonly #fields: IReadableListField[];

constructor(message: string,
type: TypeAbstract<any, any>,
payload: unknown,
fields: IReadableListField[]) {
constructor(message: string, type: TypeAbstract<any>, payload: unknown, fields: IReadableListField[]) {
super(message, type, payload);
this.#fields = fields;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Assertion/AssertionObjectError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AssertionObjectError extends AssertionTypeError {
readonly #fields: Record<string, any>;

constructor(message: string,
type: TypeAbstract<any, any>,
type: TypeAbstract<any>,
payload: unknown,
fields: Record<string, IReadableTypeError>) {
super(message, type, payload);
Expand Down
4 changes: 2 additions & 2 deletions packages/input/src/Assertion/AssertionTypeError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export interface IReadableTypeError {

export class AssertionTypeError extends Error implements IReadableError, ILogable<IReadableTypeError> {
readonly #payload: unknown;
readonly #type: TypeAbstract<any, any>;
readonly #type: TypeAbstract<any>;

constructor(message: string, type: TypeAbstract<any, any>, payload: unknown) {
constructor(message: string, type: TypeAbstract<any>, payload: unknown) {
super(message);
this.#payload = payload;
this.#type = type;
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Assertion/assertion.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TypeAbstract} from "../TypeAbstract";
import {AssertionTypeError} from "./AssertionTypeError";

export function assert(expr: unknown, type: TypeAbstract<any, any>, message: string, payload: unknown): asserts expr {
export function assert(expr: unknown, type: TypeAbstract<any>, message: string, payload: unknown): asserts expr {
if (!expr) {
throw new AssertionTypeError(message, type, payload);
}
Expand Down
9 changes: 3 additions & 6 deletions packages/input/src/SuperType.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {MayInput} from "./interfaces";
import {TypeAbstract} from "./TypeAbstract";

export abstract class SuperType<TValue, SValue,
TInput extends MayInput,
SInput extends MayInput> extends TypeAbstract<TValue, TInput> {
protected readonly type: TypeAbstract<SValue, SInput>;
export abstract class SuperType<TValue, SValue> extends TypeAbstract<TValue> {
protected readonly type: TypeAbstract<SValue>;

constructor(type: TypeAbstract<SValue, SInput>) {
constructor(type: TypeAbstract<SValue>) {
super();
this.type = type;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Type/Bool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isBoolean} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const Bool = new ScalarType<boolean, boolean>({
export const Bool = new ScalarType<boolean>({
name: "Bool",
validate(payload) {
this.assert(isBoolean(payload), `Wrong payload: ${this.name} expected`, payload);
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Type/DateTime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber, isString} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const DateTime = new ScalarType<Date, string | number>({
export const DateTime = new ScalarType<Date>({
name: "DateTime",
validate(payload) {
this.assert(isNumber(payload) || isString(payload), `Wrong payload: ${this.name} expected`, payload);
Expand Down
5 changes: 2 additions & 3 deletions packages/input/src/Type/Enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {isString, MayNullable, Promisify} from "@bunt/util";
import {MayInput} from "../interfaces";
import {isString, Promisify} from "@bunt/util";
import {TypeAbstract} from "../TypeAbstract";

export class Enum<T extends string | number> extends TypeAbstract<T> {
Expand All @@ -10,7 +9,7 @@ export class Enum<T extends string | number> extends TypeAbstract<T> {
this.#value = value;
}

public validate(input: MayNullable<MayInput>): Promisify<T> {
public validate(input: unknown): Promisify<T> {
this.assert(isString(input), "Wrong type", input);
this.assert(!/^\d+$/.test(input), "Wrong value", input);
this.assert(input in this.#value, "Wrong value", input);
Expand Down
6 changes: 3 additions & 3 deletions packages/input/src/Type/Fields.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {entriesReverse, isFunction, isInstanceOf, isObject, MayNullable} from "@bunt/util";
import {entriesReverse, isFunction, isInstanceOf, isObject} from "@bunt/util";
import {AssertionObjectError, AssertionTypeError, IReadableTypeError} from "../Assertion";
import {ObjectFields, ObjectTypeMerge} from "../interfaces";
import {TypeAbstract} from "../TypeAbstract";

export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TValue, Record<string, any>> {
export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TValue> {
readonly #fields: ObjectFields<TValue>;
readonly #name: string;

Expand Down Expand Up @@ -33,7 +33,7 @@ export class Fields<TValue extends Record<string, any>> extends TypeAbstract<TVa
);
}

public async validate(payload: MayNullable<Record<string, any>>): Promise<TValue> {
public async validate(payload: unknown): Promise<TValue> {
this.assert(isObject(payload), `Wrong payload: ${this.name} expected`, payload);

const entries: [string, any][] = [];
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Type/Float.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const Float = new ScalarType<number, number>({
export const Float = new ScalarType<number>({
name: "Float",
validate(payload) {
this.assert(isNumber(payload), `Wrong payload: ${this.name} expected`, payload);
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Type/Int.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const Int = new ScalarType<number, number>({
export const Int = new ScalarType<number>({
name: "Int",
validate(payload) {
this.assert(isNumber(payload), `Wrong payload: ${this.name} expected`, payload);
Expand Down
5 changes: 4 additions & 1 deletion packages/input/src/Type/JSONString.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import {isString} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const JSONString = new ScalarType<any, any>({
export const JSONString = new ScalarType<any>({
name: "JSON",
validate(payload) {
this.assert(isString(payload), "Wrong payload", payload);

return JSON.parse(payload);
},
});
8 changes: 3 additions & 5 deletions packages/input/src/Type/List.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {isArray, isInstanceOf, MayNullable} from "@bunt/util";
import {isArray, isInstanceOf} from "@bunt/util";
import {AssertionListError, AssertionTypeError, IReadableListField} from "../Assertion";
import {MayListInput} from "../interfaces";
import {SuperType} from "../SuperType";

export class List<TValue, TInput extends MayListInput = MayListInput>
extends SuperType<TValue[], TValue, Array<TInput>, TInput> {
public async validate(payload: MayNullable<TInput[]>): Promise<TValue[]> {
export class List<TValue> extends SuperType<TValue[], TValue> {
public async validate(payload: unknown): Promise<TValue[]> {
this.assert(isArray(payload), `Wrong payload: ${this.type.name}[] expected`, payload);

let index = 0;
Expand Down
9 changes: 4 additions & 5 deletions packages/input/src/Type/NonNull.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {isFunction, isNull, isUndefined, MayNullable, Promisify} from "@bunt/util";
import {MayInput} from "../interfaces";
import {isFunction, isNull, isUndefined, Promisify} from "@bunt/util";
import {SuperType} from "../SuperType";
import {TypeAbstract} from "../TypeAbstract";

export class NonNull<TValue, TInput extends MayInput> extends SuperType<TValue, TValue, TInput, TInput> {
export class NonNull<TValue> extends SuperType<TValue, TValue> {
readonly #defaultValue: TValue | (() => TValue);

constructor(type: TypeAbstract<TValue, TInput>, defaultValue: TValue | (() => TValue)) {
constructor(type: TypeAbstract<TValue>, defaultValue: TValue | (() => TValue)) {
super(type);
this.#defaultValue = defaultValue;
}

public validate(payload: MayNullable<TInput>): Promisify<TValue> {
public validate(payload: unknown): Promisify<TValue> {
if (isNull(payload) || isUndefined(payload)) {
if (isFunction(this.#defaultValue)) {
return this.#defaultValue();
Expand Down
7 changes: 3 additions & 4 deletions packages/input/src/Type/Nullable.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {isNull, isUndefined, MayNullable, Promisify} from "@bunt/util";
import {MayInput} from "../interfaces";
import {isNull, isUndefined, Promisify} from "@bunt/util";
import {SuperType} from "../SuperType";

export class Nullable<TValue, TInput extends MayInput> extends SuperType<TValue | undefined, TValue, TInput, TInput> {
public validate(payload: MayNullable<TInput>): Promisify<TValue | undefined> {
export class Nullable<TValue> extends SuperType<TValue | undefined, TValue> {
public validate(payload: unknown): Promisify<TValue | undefined> {
if (isNull(payload) || isUndefined(payload)) {
return undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/input/src/Type/Numeric.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {isNumber, isString} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const Numeric = new ScalarType<number, string | number>({
export const Numeric = new ScalarType<number>({
name: "Numeric",
validate(payload) {
this.assert(isNumber(payload) || isString(payload), `Wrong payload: ${this.name} expected`, payload);
Expand Down
15 changes: 7 additions & 8 deletions packages/input/src/Type/ScalarType.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {MayNullable, Promisify} from "@bunt/util";
import {MayInput} from "../interfaces";
import {Promisify} from "@bunt/util";
import {TypeAbstract} from "../TypeAbstract";

export interface IScalarType<TValue, TInput extends MayInput> {
export interface IScalarType<TValue> {
name: string;
validate: (this: ScalarType<TValue, TInput>, payload: MayNullable<TInput>) => Promisify<TValue>;
validate: (this: ScalarType<TValue>, payload: unknown) => Promisify<TValue>;
}

export class ScalarType<TValue, TInput extends MayInput> extends TypeAbstract<TValue, TInput> {
readonly #type: IScalarType<TValue, TInput>;
export class ScalarType<TValue> extends TypeAbstract<TValue> {
readonly #type: IScalarType<TValue>;

public constructor(type: IScalarType<TValue, TInput>) {
public constructor(type: IScalarType<TValue>) {
super();
this.#type = type;
}
Expand All @@ -19,7 +18,7 @@ export class ScalarType<TValue, TInput extends MayInput> extends TypeAbstract<TV
return this.#type.name;
}

public validate(payload: MayNullable<TInput>): Promisify<TValue> {
public validate(payload: unknown): Promisify<TValue> {
return this.#type.validate.call(this, payload);
}
}
3 changes: 2 additions & 1 deletion packages/input/src/Type/Text.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {isString} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const Text = new ScalarType<string, string>({
export const Text = new ScalarType<string>({
name: "Text",
validate(payload) {
this.assert(isString(payload), `Wrong payload: ${this.name} expected`, payload);

return payload;
},
});
7 changes: 4 additions & 3 deletions packages/input/src/Type/ToNumber.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import {isNumber, isString, MayNullable, Promisify} from "@bunt/util";
import {isNumber, isString, Promisify} from "@bunt/util";
import {SuperType} from "../SuperType";

export class ToNumber extends SuperType<number | string, number, number | string, number> {
public validate(payload: MayNullable<number | string>): Promisify<number> {
export class ToNumber extends SuperType<number, number> {
public validate(payload: unknown): Promisify<number> {
this.assert(isNumber(payload) || isString(payload), `Wrong payload type`, payload);

return this.type.validate(+payload);
}
}
3 changes: 2 additions & 1 deletion packages/input/src/Type/UUID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import {ScalarType} from "./ScalarType";

const RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89AB][0-9a-f]{3}-[0-9a-f]{12}$/i;

export const UUID = new ScalarType<string, string>({
export const UUID = new ScalarType<string>({
name: "UUID",
validate(payload) {
this.assert(isString(payload) && RE.test(payload), `Wrong payload: ${this.name} expected`, payload);

return payload;
},
});
15 changes: 7 additions & 8 deletions packages/input/src/Type/Union.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import {MayNullable, Promisify} from "@bunt/util";
import {MayInput} from "../interfaces";
import {Promisify} from "@bunt/util";
import {TypeAbstract} from "../TypeAbstract";

export type UnionSelector<TInput extends MayInput> =
(input: MayNullable<TInput>) => TypeAbstract<unknown> | undefined;
export type UnionSelector = (input: unknown) => TypeAbstract<unknown> | undefined;

export class Union<TValue, TInput extends MayInput = MayInput> extends TypeAbstract<TValue, TInput> {
readonly #selector: UnionSelector<TInput>;
export class Union<TValue> extends TypeAbstract<TValue> {
readonly #selector: UnionSelector;
readonly #name: string;

constructor(selector: UnionSelector<TInput>, name = "Union") {
constructor(selector: UnionSelector, name = "Union") {
super();
this.#selector = selector;
this.#name = name;
Expand All @@ -19,9 +17,10 @@ export class Union<TValue, TInput extends MayInput = MayInput> extends TypeAbstr
return this.#name;
}

public validate(input: MayNullable<TInput>): Promisify<TValue> {
public validate(input: unknown): Promisify<TValue> {
const type = this.#selector(input);
this.assert(!!type, `${this.name} detection was failed`, input);

return type.validate(input) as Promisify<TValue>;
}
}
2 changes: 1 addition & 1 deletion packages/input/src/Type/Varchar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface IVarchar {
readonly max?: number;
}

export class Varchar extends TypeAbstract<string, string> {
export class Varchar extends TypeAbstract<string> {
readonly #options: IVarchar;

constructor(options: IVarchar = {}) {
Expand Down
1 change: 0 additions & 1 deletion packages/input/src/Type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./Float";
export * from "./Int";
export * from "./ToNumber";
export * from "./List";
export * from "./NonNull";
export * from "./Nullable";
export * from "./NonNull";
export * from "./Fields";
Expand Down
7 changes: 3 additions & 4 deletions packages/input/src/TypeAbstract.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {MayNullable, Promisify} from "@bunt/util";
import {Promisify} from "@bunt/util";
import {assert} from "./Assertion";
import {MayInput} from "./interfaces";

export abstract class TypeAbstract<TValue, TInput extends MayInput = MayInput> {
export abstract class TypeAbstract<TValue> {
public get name(): string {
return this.constructor.name;
}

public abstract validate(input: MayNullable<TInput>): Promisify<TValue>;
public abstract validate(input: unknown): Promisify<TValue>;

public assert(expr: unknown, message: string, payload: unknown): asserts expr {
assert(expr, this, message, payload);
Expand Down
Loading

0 comments on commit c9c88b1

Please sign in to comment.