@domain-first/types 1.0.0 → 1.0.2

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 CHANGED
@@ -5,7 +5,13 @@
5
5
  </p>
6
6
 
7
7
  <p align="center">
8
- Type-safe Entities and Value Objects with Standard Schema validation.
8
+ Type-safe Entities and Value Objects powered by Standard Schema.
9
+ </p>
10
+
11
+ <p align="center">
12
+ <img src="https://badge.fury.io/js/@domain-first%2Ftypes.svg" alt="version">
13
+ <img src="https://img.shields.io/bundlephobia/minzip/%40domain-first%2Ftypes" alt="size">
14
+ <img src="https://img.shields.io/npm/l/%40domain-first%2Ftypes" 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 { defineValueObject, defineEntity } from "@domain-first/types";
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>(UserId.is),
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
- // identifier
40
- new UserId("user-1"),
41
- // model
42
- { name: "First User" },
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
  ```
@@ -1,3 +1,2 @@
1
1
  export * from './async-schema-in-sync-parsing-error';
2
2
  export * from './invalid-data-parsing-error';
3
- export * from './structured-clone-not-found-error';
package/dist/index.cjs CHANGED
@@ -30,7 +30,6 @@ __webpack_require__.r(__webpack_exports__);
30
30
  __webpack_require__.d(__webpack_exports__, {
31
31
  AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
32
32
  InvalidDataParsingError: ()=>InvalidDataParsingError,
33
- StructuredCloneNotFoundError: ()=>StructuredCloneNotFoundError,
34
33
  defineValueObject: ()=>defineValueObject
35
34
  });
36
35
  const errors_namespaceObject = require("@domain-first/errors");
@@ -40,9 +39,6 @@ const AsyncSchemaInSyncParsingError = (0, errors_namespaceObject.defineErrorClas
40
39
  const InvalidDataParsingError = (0, errors_namespaceObject.defineErrorClass)({
41
40
  code: 'INVALID_DATA_PARSING'
42
41
  });
43
- const StructuredCloneNotFoundError = (0, errors_namespaceObject.defineErrorClass)({
44
- code: 'STRUCTURED_CLONE_NOT_FOUND'
45
- });
46
42
  function parseSync(schema, value) {
47
43
  const result = schema['~standard'].validate(value);
48
44
  if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
@@ -82,12 +78,10 @@ const defineValueObject = (schema)=>{
82
78
  };
83
79
  exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
84
80
  exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
85
- exports.StructuredCloneNotFoundError = __webpack_exports__.StructuredCloneNotFoundError;
86
81
  exports.defineValueObject = __webpack_exports__.defineValueObject;
87
82
  for(var __rspack_i in __webpack_exports__)if (-1 === [
88
83
  "AsyncSchemaInSyncParsingError",
89
84
  "InvalidDataParsingError",
90
- "StructuredCloneNotFoundError",
91
85
  "defineValueObject"
92
86
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
93
87
  Object.defineProperty(exports, '__esModule', {
package/dist/index.js CHANGED
@@ -5,9 +5,6 @@ const AsyncSchemaInSyncParsingError = defineErrorClass({
5
5
  const InvalidDataParsingError = defineErrorClass({
6
6
  code: 'INVALID_DATA_PARSING'
7
7
  });
8
- const StructuredCloneNotFoundError = defineErrorClass({
9
- code: 'STRUCTURED_CLONE_NOT_FOUND'
10
- });
11
8
  function parseSync(schema, value) {
12
9
  const result = schema['~standard'].validate(value);
13
10
  if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
@@ -45,4 +42,4 @@ const defineValueObject = (schema)=>{
45
42
  }
46
43
  return ValueObject;
47
44
  };
48
- export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, StructuredCloneNotFoundError, defineValueObject };
45
+ export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, defineValueObject };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain-first/types",
3
- "description": "Type-safe Entities and Value Objects with Standard Schema validation",
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.0",
13
+ "version": "1.0.2",
14
14
  "type": "module",
15
15
  "repository": {
16
16
  "type": "git",
@@ -1,46 +0,0 @@
1
- export declare const StructuredCloneNotFoundError: {
2
- new (details: Record<string, any>, options?: ErrorOptions): {
3
- readonly metadata: import("@domain-first/errors").PlainPrimitivesObject & {
4
- code: string;
5
- };
6
- readonly details: Record<string, any>;
7
- readonly formattedDetails: import("@domain-first/errors").PlainPrimitivesObject;
8
- readonly serialized: import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
9
- code: string;
10
- }>;
11
- readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
12
- code: string;
13
- }>;
14
- readonly serializedFull: import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
15
- code: string;
16
- }>;
17
- name: string;
18
- message: string;
19
- stack?: string;
20
- cause?: unknown;
21
- };
22
- matches: (target: unknown) => boolean | null;
23
- is: (target: unknown) => target is {
24
- readonly metadata: import("@domain-first/errors").PlainPrimitivesObject & {
25
- code: string;
26
- };
27
- readonly details: Record<string, any>;
28
- get formattedDetails(): import("@domain-first/errors").PlainPrimitivesObject;
29
- get serialized(): import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
30
- code: string;
31
- }>;
32
- get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
33
- code: string;
34
- }>;
35
- get serializedFull(): import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
36
- code: string;
37
- }>;
38
- name: string;
39
- message: string;
40
- stack?: string;
41
- cause?: unknown;
42
- };
43
- captureStackTrace(targetObject: object, constructorOpt?: Function): void;
44
- prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
45
- stackTraceLimit: number;
46
- };