Skip to content

Commit

Permalink
✨ [feat]: Move decorator types to its own namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
brunotot committed Sep 18, 2023
1 parent bec5e77 commit c7057b6
Show file tree
Hide file tree
Showing 65 changed files with 296 additions and 433 deletions.
14 changes: 1 addition & 13 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import { makeValidator } from "./src/decorators/decorator.facade";
import {
Decorator,
DecoratorContext,
DecoratorContextMetadata,
DecoratorImpartialProps,
DecoratorPartialProps,
DecoratorValueProps,
} from "./src/decorators/decorator.types";
import { Locale, Locales } from "./src/messages/message.types";
import { getLocale, setLocale } from "./src/messages/models/locale";
import EntityProcessor from "./src/reflection/models/entity.processor";
Expand All @@ -27,6 +19,7 @@ import {
getClassFieldNames,
} from "./src/reflection/service/reflection.service";
import TdvCore from "./src/types";
import Decorator from "./src/types/namespace/decorator.namespace";
import Validation from "./src/types/namespace/validation.namespace";
import Class from "./src/types/validation/class.type";
import DetailedErrors from "./src/types/validation/detailed-errors.type";
Expand All @@ -45,11 +38,6 @@ export interface PrimitiveSetAppend {}
export type {
Class,
Decorator,
DecoratorContext,
DecoratorContextMetadata,
DecoratorImpartialProps,
DecoratorPartialProps,
DecoratorValueProps,
DescriptorProps,
DetailedErrors,
Errors,
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
"private": false,
"scripts": {
"clean": "rm -rf dist",
"build": "npm run test && npm run clean && tsc && cp polyfill.d.ts ./dist && cp polyfill.d.ts ./dist/types",
"build:noTest": "npm run clean && tsc && cp polyfill.d.ts ./dist && cp polyfill.d.ts ./dist/types",
"build": "npm run test && npm run clean && tsc",
"build:noTest": "npm run clean && tsc",
"deploy:minor": "bash ../../scripts/deploy.sh core minor",
"deploy:major": "bash ../../scripts/deploy.sh core major",
"deploy:patch": "bash ../../scripts/deploy.sh core patch",
Expand Down
25 changes: 0 additions & 25 deletions packages/core/polyfill.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/core/polyfill.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/core/src/decorators/decorator.facade.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ValidationMetaService from "../reflection/service/impl/reflection.service.validation";
import Decorator from "../types/namespace/decorator.namespace";
import Validation from "../types/namespace/validation.namespace";
import { makeDecorator } from "./decorator.factory";
import { Decorator } from "./decorator.types";

export function makeValidator<T>({
groups,
isValid,
}: Validation.Builder<T>): Decorator<T> {
}: Validation.Builder<T>): Decorator.Type<T> {
return makeDecorator<T>((key, processor) => {
saveMetadata(processor, key, isValid, groups);
});
Expand Down
16 changes: 6 additions & 10 deletions packages/core/src/decorators/decorator.factory.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import ValidationMetaService from "../reflection/service/impl/reflection.service.validation";
import {
Decorator,
DecoratorContext,
DecoratorSupplier,
} from "./decorator.types";
import Decorator from "../types/namespace/decorator.namespace";

function isDecoratorsStage2(context: DecoratorContext) {
function isDecoratorsStage2(context: Decorator.Context) {
return typeof context === "string";
}

function makeDecoratorUsingStage2Strategy<T>(
target: any,
context: DecoratorContext,
decoratorSupplier: DecoratorSupplier<T>
context: Decorator.Context,
decoratorSupplier: Decorator.Supplier<T>
) {
const name = context as unknown as string;
const clazz = target.constructor;
Expand All @@ -24,8 +20,8 @@ function makeDecoratorUsingStage2Strategy<T>(
}

export function makeDecorator<T>(
decoratorSupplier: DecoratorSupplier<T>
): Decorator<T> {
decoratorSupplier: Decorator.Supplier<T>
): Decorator.Type<T> {
return function (target, context) {
// Hardcoded check for old TS decorators (stage 2 proposal)
if (isDecoratorsStage2(context)) {
Expand Down
49 changes: 0 additions & 49 deletions packages/core/src/decorators/decorator.types.ts

This file was deleted.

9 changes: 4 additions & 5 deletions packages/core/src/decorators/decorator.utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { hasValue } from "../shared";
import $ from "../types";
import Decorator from "../types/namespace/decorator.namespace";
import Validation from "../types/namespace/validation.namespace";
import { DecoratorPartialProps } from "./decorator.types";

export function extractMessage<T extends object>(
provider: DecoratorPartialProps<any, T>,
provider: Decorator.PartialProps<any, T>,
defaultMessage: string
) {
if (!provider) {
Expand All @@ -16,7 +15,7 @@ export function extractMessage<T extends object>(
}

export function extractGroups<T extends object>(
provider: DecoratorPartialProps<any, T>
provider: Decorator.PartialProps<any, T>
): Validation.Group[] {
return Array.isArray(provider)
? provider
Expand All @@ -33,5 +32,5 @@ export function evaluateNullableValidity<T>(
object: $.Objects.Optional<T>,
isValid: (value: T) => boolean
) {
return !hasValue(object) ? true : isValid(object);
return !$.Objects.hasValue(object) ? true : isValid(object);
}
25 changes: 0 additions & 25 deletions packages/core/src/global.d.ts

This file was deleted.

7 changes: 4 additions & 3 deletions packages/core/src/reflection/models/entity.processor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { deepEquals, hasErrors } from "../../shared";
import $ from "../../types/index";
import ns from "../../types/namespace/entity-processor.namespace";
import Validation from "../../types/namespace/validation.namespace";
import Class from "../../types/validation/class.type";
Expand Down Expand Up @@ -107,12 +107,13 @@ export default class EntityProcessor<TClass, TBody = TClass> {
cacheValue: K,
valueGetter: () => K
): K {
return cacheValue === undefined || !deepEquals(this.#cache.state, state)
return cacheValue === undefined ||
!$.Objects.deepEquals(this.#cache.state, state)
? valueGetter()
: cacheValue;
}

#isValid(errors: Errors<TClass>) {
return !hasErrors(errors);
return !$.Objects.hasErrors(errors);
}
}
6 changes: 3 additions & 3 deletions packages/core/src/reflection/service/reflection.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DecoratorContext } from "../../decorators/decorator.types";
import Decorator from "../../types/namespace/decorator.namespace";
import Class from "../../types/validation/class.type";

export function getClassFieldDescriptor<TClass>(
Expand Down Expand Up @@ -27,7 +27,7 @@ export function getClassFieldNames<TClass>(
return uniquePropsArray as (keyof TClass)[];
}

export type MetaStrategy = DecoratorContext | Class<any>;
export type MetaStrategy = Decorator.Context | Class<any>;

export function metadata(strategy: MetaStrategy): DecoratorMetadataObject {
if (isClass(strategy)) {
Expand All @@ -47,7 +47,7 @@ export default abstract class MetaService<Entry> {
#injectionKey: string;
#initial: () => Entry;
#class?: Class<any>;
protected context?: DecoratorContext;
protected context?: Decorator.Context;

constructor(
injectionKey: string,
Expand Down
Loading

0 comments on commit c7057b6

Please sign in to comment.