-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rework ValueObject to HObject (#55)
- Loading branch information
Showing
26 changed files
with
307 additions
and
306 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { | ||
JsonSerialize, | ||
LogicError, | ||
type HObjectType, | ||
type PlainParseError, | ||
type R | ||
} from '../../Util'; | ||
|
||
export type AnyValueObject = AbstractValueObject<any>; | ||
export type ValueObjectType<T extends AnyValueObject = AnyValueObject> = HObjectType<T>; | ||
|
||
export abstract class AbstractValueObject<T extends AnyValueObject> implements JsonSerialize { | ||
/** | ||
* Creates ValueObject from plain value | ||
* @param this | ||
* @param plain | ||
* @returns | ||
*/ | ||
public static parse<T>(this: (new (...args: any[]) => any), plain: unknown): R<T, PlainParseError> { | ||
throw new LogicError('Not implemented or AOT generated'); | ||
} | ||
|
||
public abstract equals(o: T): boolean; | ||
public abstract toString(): string; | ||
|
||
public toJSON(): any { | ||
throw new LogicError('Not implemented or AOT generated'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import { HObjectTypeMeta } from "../../Util"; | ||
import { StringValue } from "./StringValue"; | ||
import { ValueObject } from "./ValueObject"; | ||
|
||
@ValueObject('Account') | ||
export class AccountId extends StringValue<AccountId> {} | ||
export class AccountId extends StringValue<AccountId> { | ||
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Account', 'ValueObject', 'AccountId', AccountId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
import { StringValue } from './StringValue'; | ||
import * as crypto from 'crypto'; | ||
import { hash } from 'node:crypto'; | ||
import { Email } from './Email'; | ||
import { ValueObject } from './ValueObject'; | ||
import { RegexStringIdRawType } from './RegexStringValue'; | ||
import { HObjectTypeMeta } from "../../Util"; | ||
|
||
export type EmailHashRawType = RegexStringIdRawType; | ||
|
||
@ValueObject('Core') | ||
export class EmailHash extends StringValue<EmailHash> { | ||
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'EmailHash', EmailHash); | ||
|
||
public static createFromEmail(email: Email): EmailHash { | ||
return new EmailHash(crypto.createHash('sha1').update(email.v).digest('hex')); | ||
return new EmailHash(hash('sha1', email.v, "hex")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import { StringValue } from "./StringValue"; | ||
import { ValueObject } from "./ValueObject"; | ||
import { customAlphabet } from 'nanoid'; | ||
import { HObjectTypeMeta } from "../../Util"; | ||
|
||
const RefIdGenerator = customAlphabet('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-', 21); | ||
|
||
/** | ||
* Represents unique random string id. Generates nanoid - 21 characters | ||
*/ | ||
@ValueObject('Core') | ||
export class RefId extends StringValue<RefId> { | ||
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'RefId', RefId); | ||
|
||
public static gen(): RefId { | ||
return new RefId(RefIdGenerator()); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,47 @@ | ||
import { OK, Result } from '../../Util/Result'; | ||
import { AbstractValueObject } from './ValueObject'; | ||
import { StringValue } from './StringValue'; | ||
|
||
export type RegexStringIdRawType = string; | ||
import { LogicError, OK, PlainParseHelper, PlainParseIssue, type PlainParseError, type R } from '../../Util'; | ||
import { AbstractValueObject, type ValueObjectType } from "./AbstractValueObject"; | ||
|
||
export type RegexStringSubtype<T> = { | ||
new (value: string): T; | ||
new(value: string): T; | ||
getRegex(): RegExp; | ||
}; | ||
export abstract class RegexStringValue<T extends RegexStringValue<any>> extends StringValue<T> { | ||
public static checkRawValue<T>(this: RegexStringSubtype<T>, value: string): Result<boolean> { | ||
return this.getRegex().test(value) ? OK(true) : AbstractValueObject.invalidRaw(this, { raw: value }); | ||
} & ValueObjectType; | ||
|
||
export abstract class RegexStringValue<T extends RegexStringValue<any>> extends AbstractValueObject<T> { | ||
public constructor(public readonly v: string) { | ||
super(); | ||
} | ||
|
||
public static parse<T extends RegexStringValue<any>>(this: RegexStringSubtype<T>, plain: unknown): R<T, PlainParseError> { | ||
const parsed = PlainParseHelper.parseStringRegex(plain, this.getRegex()); | ||
if (parsed instanceof PlainParseIssue) { | ||
return PlainParseHelper.HObjectParseErr(this, [parsed]); | ||
} | ||
|
||
return OK(new this(parsed)); | ||
} | ||
|
||
public static getRegex(): RegExp { | ||
throw new LogicError("Must be implemented in value object class"); | ||
} | ||
|
||
/** | ||
* Creates instance without extra validation. | ||
* @param v | ||
* @returns | ||
*/ | ||
public static cs<T extends RegexStringValue<any>>(this: RegexStringSubtype<T>, v: string): T { | ||
return new (this as any)(v); | ||
} | ||
|
||
public equals(other: T): boolean { | ||
return this.v === other.v; | ||
} | ||
|
||
public toString(): string { | ||
return this.v; | ||
} | ||
|
||
public toJSON(): string { | ||
return this.v; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,42 @@ | ||
import { SimpleValueObject, SimpleValueObjectConstructor } from './SimpleValueObject'; | ||
import { HObjectTypeMeta, OK, PlainParseError, PlainParseHelper, PlainParseIssue, type R } from "../../Util"; | ||
import { AbstractValueObject, type ValueObjectType } from "./AbstractValueObject"; | ||
|
||
export type StringValueConstructor<T extends StringValue<any> = StringValue<any>> = SimpleValueObjectConstructor<T, string>; | ||
export class StringValue<T extends StringValue<any> = any> extends AbstractValueObject<T> { | ||
public static readonly HOBJ_META = HObjectTypeMeta.domain('Core', 'Core', 'ValueObject', 'String', StringValue); | ||
|
||
|
||
public constructor(public readonly v: string) { | ||
super(); | ||
} | ||
|
||
public static parse<T extends StringValue>(this: ValueObjectType<T>, plain: unknown): R<T, PlainParseError> { | ||
const parsed = PlainParseHelper.parseString(plain); | ||
if (parsed instanceof PlainParseIssue) { | ||
return PlainParseHelper.HObjectParseErr(this, [parsed]); | ||
} | ||
|
||
return OK(new this(parsed)); | ||
} | ||
|
||
/** | ||
* Creates instance without extra validation. | ||
* @param v | ||
* @returns | ||
*/ | ||
public static cs<T extends StringValue>(this: ValueObjectType<T>, v: string): T { | ||
return new (this as any)(v); | ||
} | ||
|
||
public equals(other: T): boolean { | ||
return this.v === other.v; | ||
} | ||
|
||
public toString(): string { | ||
return this.v; | ||
} | ||
|
||
public toJSON(): string { | ||
return this.v; | ||
} | ||
} | ||
|
||
export abstract class StringValue<T extends StringValue<any> = any> extends SimpleValueObject<T, string> {} |
Oops, something went wrong.