@need-code/nc-ddd 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 NeedCode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # nc-ddd
2
+
3
+ A set of base classes for implementing Domain-Driven Design patterns in TypeScript.
4
+
5
+ This library provides base classes for Value Objects, Entities, and Aggregate Roots, helping you to structure your domain logic in a clean and maintainable way. It uses `valibot` for validation and `@sweet-monads/either` for handling success and failure cases in a functional way.
6
+
7
+ ## Installation
8
+
9
+ Install the package and its peer dependencies using your favorite package manager:
10
+
11
+ ```bash
12
+ npm install nc-ddd @sweet-monads/either valibot
13
+ ```
14
+
15
+ ## Core Concepts
16
+
17
+ ### Value Object (VO)
18
+
19
+ A Value Object is an object that represents a descriptive aspect of the domain with no conceptual identity. VOs are immutable and are compared by their values.
20
+
21
+ The `VO` base class provides:
22
+ - A private constructor to enforce creation through a factory method.
23
+ - An `isEqual` method for structural comparison.
24
+ - A `value` getter to access the primitive value.
25
+
26
+ **Example: `CompanyNameVO`**
27
+
28
+ ```typescript
29
+ // example/value-objects/company/company-name.vo.ts
30
+ import * as v from "valibot";
31
+ import { VO } from "@/core/vo";
32
+ import { voFactory } from "@/helpers/vo-factory";
33
+
34
+ export const CompanyNameVOSchema = v.pipe(
35
+ v.string("Name must be a string"),
36
+ v.trim(),
37
+ v.nonEmpty("Name can`t be empty"),
38
+ );
39
+
40
+ export type CompanyNameVOProps = v.InferOutput<typeof CompanyNameVOSchema>;
41
+
42
+ export class CompanyNameVO extends VO<typeof CompanyNameVOSchema> {
43
+ private constructor(props: CompanyNameVOProps) {
44
+ super(props);
45
+ }
46
+ public static create = (val: v.InferInput<typeof CompanyNameVOSchema>) => {
47
+ return voFactory(val, CompanyNameVOSchema, (props) => new CompanyNameVO(props));
48
+ };
49
+ }
50
+ ```
51
+
52
+ ### Entity
53
+
54
+ An Entity is an object with a unique identity that persists over time. Entities are mutable and are compared by their identity.
55
+
56
+ The `Entity` base class provides:
57
+ - A protected constructor to enforce creation through factory methods.
58
+ - An `id` getter for easy access to the entity's identifier.
59
+ - An `isEqual` method that compares entities based on their ID.
60
+ - A `primitive` getter to get the primitive values of the entity's props.
61
+
62
+ **Example: `Employee` Entity**
63
+
64
+ ```typescript
65
+ // example/entities/employee.entity.ts
66
+ import { left, merge } from "@sweet-monads/either";
67
+ import { flatten, safeParse } from "valibot";
68
+ import { Entity } from "@/core/entity";
69
+ import { CreateEmployeeDTOSchema } from "../dtos/create-employee.dto";
70
+ import { EmployeeContactVO } from "../value-objects/employee/employee-contact.vo";
71
+ import { EmployeeInfoVO } from "../value-objects/employee/employee-info.vo";
72
+ import { EmployeeNameVO } from "../value-objects/employee/employee-name.vo";
73
+ import { EmployeeRoleVO } from "../value-objects/employee/employee-role.vo";
74
+ import { IdVO } from "../value-objects/id.vo";
75
+
76
+ export interface EmployeeProps {
77
+ id: IdVO;
78
+ name: EmployeeNameVO;
79
+ role: EmployeeRoleVO;
80
+ info: EmployeeInfoVO;
81
+ contacts: EmployeeContactVO[];
82
+ }
83
+
84
+ export class Employee extends Entity<EmployeeProps> {
85
+ private constructor(props: EmployeeProps) {
86
+ super(props);
87
+ }
88
+
89
+ public static create(rawDTO: unknown) {
90
+ const dtoResult = safeParse(CreateEmployeeDTOSchema, rawDTO);
91
+
92
+ if (!dtoResult.success) {
93
+ return left(flatten(dtoResult.issues).nested);
94
+ }
95
+
96
+ const props = dtoResult.output;
97
+
98
+ const id = IdVO.create(props.id);
99
+ const name = EmployeeNameVO.create(props.name);
100
+ const role = EmployeeRoleVO.create(props.role);
101
+ const info = EmployeeInfoVO.create(props.info);
102
+ const contacts = merge(props.contacts.map((c) => EmployeeContactVO.create(c)));
103
+
104
+ return merge([id, name, role, info, contacts]).map(([id, name, role, info, contacts]) => {
105
+ return new Employee({ id, name, role, info, contacts });
106
+ });
107
+ }
108
+ }
109
+ ```
110
+
111
+ ### Aggregate Root
112
+
113
+ An Aggregate Root is a specific type of Entity that acts as a gateway to a cluster of associated objects.
114
+
115
+ The `AgregateRoot` base class extends the `Entity` class and allows for more complex properties, including other Entities.
116
+
117
+ **Example: `Company` Aggregate Root**
118
+ ```typescript
119
+ // example/aggregates/company.aggregate.ts
120
+ import { left, merge } from "@sweet-monads/either";
121
+ import { flatten, safeParse } from "valibot";
122
+
123
+ import { AgregateRoot } from "@/core/agregate-root";
124
+ import { CreateCompanyDTOSchema } from "../dtos/create-company.dto";
125
+ import { Employee } from "../entities/employee.entity";
126
+ import { CompanyNameVO } from "../value-objects/company/company-name.vo";
127
+ import { IdVO } from "../value-objects/id.vo";
128
+
129
+ export interface CompanyProps {
130
+ id: IdVO;
131
+ companyName: CompanyNameVO;
132
+ employees: Employee[];
133
+ }
134
+
135
+ export class Company extends AgregateRoot<CompanyProps> {
136
+ private constructor(props: CompanyProps) {
137
+ super(props);
138
+ }
139
+
140
+ public static create(rawDTO: unknown) {
141
+ const dtoResult = safeParse(CreateCompanyDTOSchema, rawDTO);
142
+
143
+ if (!dtoResult.success) {
144
+ return left(flatten(dtoResult.issues).nested);
145
+ }
146
+
147
+ const props = dtoResult.output;
148
+
149
+ const id = IdVO.create(props.id);
150
+ const companyName = CompanyNameVO.create(props.companyName);
151
+ const employees = merge(props.employees.map((e) => Employee.create(e)));
152
+
153
+ return merge([id, companyName, employees]).map(([id, companyName, employees]) => {
154
+ return new Company({ id, companyName, employees });
155
+ });
156
+ }
157
+ }
158
+ ```
159
+
160
+ ## Usage
161
+
162
+ The `voFactory` helper and the static `create` methods on VOs, Entities, and Aggregate Roots return an `Either` monad from `@sweet-monads/either`. This allows you to handle successful creation and validation errors in a functional and type-safe way.
163
+
164
+ ```typescript
165
+ import { Company } from "./example/aggregates/company.aggregate";
166
+
167
+ const companyData = {
168
+ id: "d1a6b1b1-3b3b-4b3b-8b3b-1b1b1b1b1b1b",
169
+ companyName: "NeedCode",
170
+ employees: [
171
+ {
172
+ id: "a1a1a1a1-1a1a-1a1a-1a1a-1a1a1a1a1a1a",
173
+ name: "John Doe",
174
+ role: "admin",
175
+ info: "Lead developer",
176
+ contacts: [
177
+ { type: "email", value: "john.doe@example.com" },
178
+ { type: "phone", value: "71234567890" },
179
+ ],
180
+ },
181
+ ],
182
+ };
183
+
184
+ const companyResult = Company.create(companyData);
185
+
186
+ companyResult.map((company) => {
187
+ console.log("Company created successfully:", company.primitive);
188
+ });
189
+
190
+ companyResult.mapLeft((errors) => {
191
+ console.error("Failed to create company:", errors);
192
+ });
193
+ ```
194
+
195
+ ## Running Tests
196
+
197
+ To run the tests, use the following command:
198
+
199
+ ```bash
200
+ npm test
201
+ ```
@@ -0,0 +1,36 @@
1
+ import type * as v from "valibot";
2
+ import { Entity, type IEntityProps, type InferEntityProps } from "./entity";
3
+ import { VO } from "./vo";
4
+ type AllowedAgregateRootPropValue = VO<v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>> | Array<VO<v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>> | Entity<IEntityProps> | Array<Entity<IEntityProps>>;
5
+ export type InferPropType<T> = T extends ReadonlyArray<infer U> ? InferPropType<U>[] : T extends VO<infer S> ? v.InferInput<S> : T extends Entity<infer P> ? InferEntityProps<P> : never;
6
+ export type InferAgregateRootProps<T> = {
7
+ [K in keyof T]: InferPropType<T[K]>;
8
+ };
9
+ /**
10
+ * @class AgregateRoot
11
+ * @description Base class for aggregate roots in DDD. An aggregate root is a specific type of entity that acts as a gateway to a cluster of associated objects.
12
+ * @template Props - The properties of the aggregate root. Must include an 'id' property which is a Value Object.
13
+ */
14
+ export declare abstract class AgregateRoot<Props extends IEntityProps & Record<keyof Props, AllowedAgregateRootPropValue>> {
15
+ readonly props: Props;
16
+ /**
17
+ * The constructor is protected to enforce creation through factory methods in subclasses.
18
+ * @param props The properties of the aggregate root.
19
+ */
20
+ protected constructor(props: Props);
21
+ /**
22
+ * Getter for the aggregate root's identifier (ID).
23
+ * @returns {Props["id"]} The unique identifier of the aggregate root.
24
+ */
25
+ get id(): Props["id"];
26
+ get primitive(): InferAgregateRootProps<Props>;
27
+ /**
28
+ * Compares this aggregate root with another for equality.
29
+ * Aggregate roots are considered equal if they have the same identifier.
30
+ * @param other The other aggregate root to compare with.
31
+ * @returns {boolean} `true` if they are equal, `false` otherwise.
32
+ */
33
+ isEqual(other?: AgregateRoot<Props>): boolean;
34
+ }
35
+ export {};
36
+ //# sourceMappingURL=agregate-root.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agregate-root.d.ts","sourceRoot":"","sources":["../../src/core/agregate-root.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,CAAC,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AA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AC,KAAK,CAAC,GAAG,OAAO;CAerD"}
@@ -0,0 +1,60 @@
1
+ import { Entity } from "./entity";
2
+ import { VO } from "./vo";
3
+ /**
4
+ * @class AgregateRoot
5
+ * @description Base class for aggregate roots in DDD. An aggregate root is a specific type of entity that acts as a gateway to a cluster of associated objects.
6
+ * @template Props - The properties of the aggregate root. Must include an 'id' property which is a Value Object.
7
+ */
8
+ export class AgregateRoot {
9
+ props;
10
+ /**
11
+ * The constructor is protected to enforce creation through factory methods in subclasses.
12
+ * @param props The properties of the aggregate root.
13
+ */
14
+ constructor(props) {
15
+ this.props = props;
16
+ }
17
+ /**
18
+ * Getter for the aggregate root's identifier (ID).
19
+ * @returns {Props["id"]} The unique identifier of the aggregate root.
20
+ */
21
+ get id() {
22
+ return this.props.id;
23
+ }
24
+ get primitive() {
25
+ const toPrim = (p) => {
26
+ if (p instanceof Entity) {
27
+ return p.primitive;
28
+ }
29
+ if (p instanceof VO) {
30
+ return p.value;
31
+ }
32
+ return p;
33
+ };
34
+ return Object.fromEntries(Object.entries(this.props).map(([key, prop]) => {
35
+ if (Array.isArray(prop)) {
36
+ return [key, prop.map(toPrim)];
37
+ }
38
+ return [key, toPrim(prop)];
39
+ }));
40
+ }
41
+ /**
42
+ * Compares this aggregate root with another for equality.
43
+ * Aggregate roots are considered equal if they have the same identifier.
44
+ * @param other The other aggregate root to compare with.
45
+ * @returns {boolean} `true` if they are equal, `false` otherwise.
46
+ */
47
+ isEqual(other) {
48
+ if (other === null || other === undefined) {
49
+ return false;
50
+ }
51
+ if (this === other) {
52
+ return true;
53
+ }
54
+ if (!(other instanceof AgregateRoot)) {
55
+ return false;
56
+ }
57
+ return this.id.isEqual(other.id);
58
+ }
59
+ }
60
+ //# sourceMappingURL=agregate-root.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agregate-root.js","sourceRoot":"","sources":["../../src/core/agregate-root.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAA4C,MAAM,UAAU,CAAC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KAAK,YAAY,YAAY,CAAC,EAAE,CAAC;YACrC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACnC,CAAC;CACF"}
@@ -0,0 +1,38 @@
1
+ import type * as v from "valibot";
2
+ import type { VO } from "./vo";
3
+ type AllowedEntityPropValue = VO<v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>> | Array<VO<v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>>;
4
+ export type IEntityProps = {
5
+ id: VO<v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>;
6
+ };
7
+ export type InferValibot<T> = T extends ReadonlyArray<infer U> ? InferValibot<U>[] : T extends VO<infer S> ? v.InferInput<S> : never;
8
+ export type InferEntityProps<T> = {
9
+ [K in keyof T]: InferValibot<T[K]>;
10
+ };
11
+ /**
12
+ * @class Entity
13
+ * @description Base class for entities in DDD. An entity is an object with a unique identity that persists over time.
14
+ * @template Props - The properties of the entity. Must include an 'id' property which is a Value Object.
15
+ */
16
+ export declare abstract class Entity<Props extends IEntityProps & Record<keyof Props, AllowedEntityPropValue>> {
17
+ readonly props: Props;
18
+ /**
19
+ * The constructor is protected to enforce creation through factory methods in subclasses.
20
+ * @param props The properties of the entity.
21
+ */
22
+ protected constructor(props: Props);
23
+ /**
24
+ * Getter for the entity's identifier (ID).
25
+ * @returns {Props["id"]} The unique identifier of the entity.
26
+ */
27
+ get id(): Props["id"];
28
+ get primitive(): InferEntityProps<Props>;
29
+ /**
30
+ * Compares this entity with another for equality.
31
+ * Entities are considered equal if they have the same identifier.
32
+ * @param other The other entity to compare with.
33
+ * @returns {boolean} `true` if the entities are equal, `false` otherwise.
34
+ */
35
+ isEqual(other?: Entity<Props>): boolean;
36
+ }
37
+ export {};
38
+ //# sourceMappingURL=entity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity.d.ts","sourceRoot":"","sources":["../../src/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,gBAAgB,CAAC,KAAK,CAAC,CASvC;IAED;;;;;OAKG;IACI,OAAO,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,OAAO;CAe/C"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @class Entity
3
+ * @description Base class for entities in DDD. An entity is an object with a unique identity that persists over time.
4
+ * @template Props - The properties of the entity. Must include an 'id' property which is a Value Object.
5
+ */
6
+ export class Entity {
7
+ props;
8
+ /**
9
+ * The constructor is protected to enforce creation through factory methods in subclasses.
10
+ * @param props The properties of the entity.
11
+ */
12
+ constructor(props) {
13
+ this.props = props;
14
+ }
15
+ /**
16
+ * Getter for the entity's identifier (ID).
17
+ * @returns {Props["id"]} The unique identifier of the entity.
18
+ */
19
+ get id() {
20
+ return this.props.id;
21
+ }
22
+ get primitive() {
23
+ return Object.fromEntries(Object.entries(this.props).map(([k, v]) => {
24
+ if (Array.isArray(v)) {
25
+ return [k, v.map((i) => i.value)];
26
+ }
27
+ return [k, v.value];
28
+ }));
29
+ }
30
+ /**
31
+ * Compares this entity with another for equality.
32
+ * Entities are considered equal if they have the same identifier.
33
+ * @param other The other entity to compare with.
34
+ * @returns {boolean} `true` if the entities are equal, `false` otherwise.
35
+ */
36
+ isEqual(other) {
37
+ if (other === null || other === undefined) {
38
+ return false;
39
+ }
40
+ if (this === other) {
41
+ return true;
42
+ }
43
+ if (!(other instanceof Entity)) {
44
+ return false;
45
+ }
46
+ return this.id.isEqual(other.id);
47
+ }
48
+ }
49
+ //# sourceMappingURL=entity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"entity.js","sourceRoot":"","sources":["../../src/core/entity.ts"],"names":[],"mappings":"AAsBA;;;;GAIG;AACH,MAAM,OAAgB,MAAM;IAGV,KAAK,CAAQ;IAE7B;;;OAGG;IACH,YAAsB,KAAY;QAChC,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;IAED;;;OAGG;IACH,IAAI,EAAE;QACJ,OAAO,IAAI,CAAC,KAAK,CAAC,EAAiB,CAAC;IACtC,CAAC;IAED,IAAI,SAAS;QACX,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE;YACxC,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;gBACrB,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YACpC,CAAC;YACD,OAAO,CAAC,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;QACtB,CAAC,CAAC,CACwB,CAAC;IAC/B,CAAC;IAED;;;;;OAKG;IACI,OAAO,CAAC,KAAqB;QAClC,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YAC1C,OAAO,KAAK,CAAC;QACf,CAAC;QAED,IAAI,IAAI,KAAK,KAAK,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QACd,CAAC;QAED,IAAI,CAAC,CAAC,KAAK,YAAY,MAAM,CAAC,EAAE,CAAC;YAC/B,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACnC,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ import type * as v from "valibot";
2
+ export declare abstract class VO<Schema extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>> {
3
+ private _value;
4
+ protected constructor(props: v.InferOutput<Schema>);
5
+ isEqual(other: VO<Schema>): boolean;
6
+ get value(): v.InferOutput<Schema>;
7
+ }
8
+ //# sourceMappingURL=vo.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vo.d.ts","sourceRoot":"","sources":["../../src/core/vo.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,KAAK,CAAC,MAAM,SAAS,CAAC;AAElC,8BAAsB,EAAE,CAAC,MAAM,SAAS,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAC1F,OAAO,CAAC,MAAM,CAAwB;IAEtC,SAAS,aAAa,KAAK,EAAE,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC;IAIlD,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,MAAM,CAAC;IASzB,IAAI,KAAK,0BAER;CACF"}
@@ -0,0 +1,20 @@
1
+ import assert from "node:assert/strict";
2
+ export class VO {
3
+ _value;
4
+ constructor(props) {
5
+ this._value = props;
6
+ }
7
+ isEqual(other) {
8
+ try {
9
+ assert.deepStrictEqual(other.value, this.value);
10
+ return true;
11
+ }
12
+ catch {
13
+ return false;
14
+ }
15
+ }
16
+ get value() {
17
+ return this._value;
18
+ }
19
+ }
20
+ //# sourceMappingURL=vo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vo.js","sourceRoot":"","sources":["../../src/core/vo.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,oBAAoB,CAAC;AAGxC,MAAM,OAAgB,EAAE;IACd,MAAM,CAAwB;IAEtC,YAAsB,KAA4B;QAChD,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED,OAAO,CAAC,KAAiB;QACvB,IAAI,CAAC;YACH,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAChD,OAAO,IAAI,CAAC;QACd,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,KAAK,CAAC;QACf,CAAC;IACH,CAAC;IAED,IAAI,KAAK;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import * as v from "valibot";
2
+ import type { VO } from "@/core/vo";
3
+ export declare function voFactory<S extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>(value: unknown, schema: S, constructorFn: (props: v.InferOutput<S>) => VO<S>): import("@sweet-monads/either").Either<[string, ...string[]] | (Readonly<Partial<Record<S extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>> | v.BaseSchemaAsync<unknown, unknown, v.BaseIssue<unknown>> ? v.IssueDotPath<S> : string, [string, ...string[]]>>> extends infer T ? { [TKey in keyof T]: T[TKey]; } : never) | undefined, never> | import("@sweet-monads/either").Either<never, VO<S>>;
4
+ //# sourceMappingURL=vo-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vo-factory.d.ts","sourceRoot":"","sources":["../../src/helpers/vo-factory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAC7B,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,WAAW,CAAC;AAEpC,wBAAgB,SAAS,CAAC,CAAC,SAAS,CAAC,CAAC,UAAU,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,EACtF,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,CAAC,EACT,aAAa,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,iZAQlD"}
@@ -0,0 +1,11 @@
1
+ import { left, right } from "@sweet-monads/either";
2
+ import * as v from "valibot";
3
+ export function voFactory(value, schema, constructorFn) {
4
+ const parseResult = v.safeParse(schema, value);
5
+ if (!parseResult.success) {
6
+ const flattedErrors = v.flatten(parseResult.issues);
7
+ return left(flattedErrors.root || flattedErrors.nested || flattedErrors.other);
8
+ }
9
+ return right(constructorFn(parseResult.output));
10
+ }
11
+ //# sourceMappingURL=vo-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vo-factory.js","sourceRoot":"","sources":["../../src/helpers/vo-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,KAAK,CAAC,MAAM,SAAS,CAAC;AAG7B,MAAM,UAAU,SAAS,CACvB,KAAc,EACd,MAAS,EACT,aAAiD;IAEjD,MAAM,WAAW,GAAG,CAAC,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC/C,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QACzB,MAAM,aAAa,GAAG,CAAC,CAAC,OAAO,CAAgB,WAAW,CAAC,MAAM,CAAC,CAAC;QACnE,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,aAAa,CAAC,MAAM,IAAI,aAAa,CAAC,KAAK,CAAC,CAAC;IACjF,CAAC;IACD,OAAO,KAAK,CAAC,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;AAClD,CAAC"}
@@ -0,0 +1,5 @@
1
+ export * from "./core/vo";
2
+ export * from "./core/entity";
3
+ export * from "./core/agregate-root";
4
+ export * from "./helpers/vo-factory";
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./core/vo";
2
+ export * from "./core/entity";
3
+ export * from "./core/agregate-root";
4
+ export * from "./helpers/vo-factory";
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,sBAAsB,CAAC;AACrC,cAAc,sBAAsB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@need-code/nc-ddd",
3
+ "version": "0.0.1",
4
+ "description": "A set of base classes for implementing Domain-Driven Design patterns.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "exports": {
12
+ ".": {
13
+ "import": "./dist/index.js",
14
+ "require": "./dist/index.js",
15
+ "types": "./dist/index.d.ts"
16
+ }
17
+ },
18
+ "scripts": {
19
+ "test": "jest",
20
+ "check": "biome check",
21
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
22
+ "prepublishOnly": "npm run build"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/NeedCodeLab/nc-ddd.git"
27
+ },
28
+ "author": "Mikhail Kokorin",
29
+ "license": "MIT",
30
+ "bugs": {
31
+ "url": "https://github.com/NeedCodeLab/nc-ddd/issues"
32
+ },
33
+ "homepage": "https://github.com/NeedCodeLab/nc-ddd#readme",
34
+ "devDependencies": {
35
+ "@biomejs/biome": "^2.3.10",
36
+ "@types/jest": "^30.0.0",
37
+ "jest": "^30.2.0",
38
+ "ts-jest": "^29.4.6",
39
+ "typescript": "^5.9.3"
40
+ },
41
+ "peerDependencies": {
42
+ "@sweet-monads/either": ">=3.0.0",
43
+ "valibot": ">=1.0.0"
44
+ }
45
+ }