@domain-first/types 1.0.1 → 1.0.3
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/README.md +35 -11
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -5,7 +5,13 @@
|
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
|
-
Type-safe Entities and Value Objects
|
|
8
|
+
Type-safe Entities and Value Objects powered by Standard Schema.
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<img src="https://img.shields.io/npm/v/%40domain-first%2Ftypes?style=for-the-badge" alt="version">
|
|
13
|
+
<img src="https://img.shields.io/bundlephobia/minzip/%40domain-first%2Ftypes?style=for-the-badge" alt="size">
|
|
14
|
+
<img src="https://img.shields.io/npm/l/%40domain-first%2Ftypes?style=for-the-badge" alt="license">
|
|
9
15
|
</p>
|
|
10
16
|
|
|
11
17
|
# Installation
|
|
@@ -14,31 +20,49 @@
|
|
|
14
20
|
npm i @domain-first/types
|
|
15
21
|
```
|
|
16
22
|
|
|
23
|
+
# Motivation
|
|
24
|
+
|
|
25
|
+
Building Entities and Value Objects often involves repetitive boilerplate, such as validation, while each class still needs its own domain-specific behavior. Domain-First Types implements the shared parts while leaving domain-specific behavior to your classes.
|
|
26
|
+
|
|
17
27
|
# Quick Start
|
|
18
28
|
|
|
19
29
|
```ts
|
|
20
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
defineValueObject,
|
|
32
|
+
defineEntity,
|
|
33
|
+
InvalidDataParsingError,
|
|
34
|
+
} from "@domain-first/types";
|
|
21
35
|
// any schema library supporting Standard Schema can be used
|
|
22
36
|
import { z } from "zod";
|
|
23
37
|
|
|
24
|
-
// User Id (Value Object)
|
|
25
38
|
class UserId extends defineValueObject(
|
|
26
39
|
// model schema
|
|
27
40
|
z.string().nonempty(),
|
|
28
41
|
) {}
|
|
29
42
|
|
|
30
|
-
// User (Entity)
|
|
31
43
|
class User extends defineEntity(
|
|
32
44
|
// id schema
|
|
33
|
-
z.custom<UserId>(
|
|
45
|
+
z.custom<UserId>(
|
|
46
|
+
// every Entity and Value Object has a static `is` method
|
|
47
|
+
// for type-safe instance checks
|
|
48
|
+
UserId.is,
|
|
49
|
+
),
|
|
34
50
|
// model schema
|
|
35
51
|
z.object({ name: z.string().nonempty() }),
|
|
36
52
|
) {}
|
|
37
53
|
|
|
38
|
-
const user = new User(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
54
|
+
const user = new User(new UserId("user-1"), { name: "First User" });
|
|
55
|
+
|
|
56
|
+
console.log(user.id.model); // "user-1", deep readonly
|
|
57
|
+
console.log(user.model); // { name: "First User" }, deep readonly
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const invalidUserId = new UserId("");
|
|
61
|
+
} catch (e: unknown) {
|
|
62
|
+
// use static `is` method for error checks
|
|
63
|
+
if (InvalidDataParsingError.is(e)) {
|
|
64
|
+
console.error(e.details.parsingIssues);
|
|
65
|
+
console.error(e.details.value);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
44
68
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domain-first/types",
|
|
3
|
-
"description": "Type-safe Entities and Value Objects
|
|
3
|
+
"description": "Type-safe Entities and Value Objects powered by Standard Schema",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"entity",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"domain model",
|
|
11
11
|
"domain-first"
|
|
12
12
|
],
|
|
13
|
-
"version": "1.0.
|
|
13
|
+
"version": "1.0.3",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|