@domain-first/types 0.0.2 → 1.0.0

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
@@ -16,41 +16,29 @@ npm i @domain-first/types
16
16
 
17
17
  # Quick Start
18
18
 
19
- ## Value Object
20
-
21
19
  ```ts
22
- import { defineValueObjectClass } from "@domain-first/types";
20
+ import { defineValueObject, defineEntity } from "@domain-first/types";
21
+ // any schema library supporting Standard Schema can be used
23
22
  import { z } from "zod";
24
23
 
25
- /**
26
- * Create a value object by extending `defineValueObjectClass`.
27
- * The constructor validates input using your schema and exposes
28
- * validated data through `model`, exposed as a deeply readonly type.
29
- */
30
- class Pagination extends defineValueObjectClass(
31
- z.object({
32
- pageSize: z.int().positive(),
33
- pageIndex: z.int().positive(),
34
- }),
35
- ) {
36
- get nextPage(): Pagination {
37
- return new Pagination({
38
- ...this.model,
39
- pageIndex: this.model.pageIndex + 1,
40
- });
41
- }
42
- }
43
-
44
- const secondPage = new Pagination({
45
- pageSize: 10,
46
- pageIndex: 2,
47
- });
48
-
49
- secondPage.model.pageIndex;
50
- // 2
51
-
52
- const thirdPage = secondPage.nextPage;
53
-
54
- thirdPage.model;
55
- // { pageSize: 10, pageIndex: 3 }
24
+ // User Id (Value Object)
25
+ class UserId extends defineValueObject(
26
+ // model schema
27
+ z.string().nonempty(),
28
+ ) {}
29
+
30
+ // User (Entity)
31
+ class User extends defineEntity(
32
+ // id schema
33
+ z.custom<UserId>(UserId.is),
34
+ // model schema
35
+ z.object({ name: z.string().nonempty() }),
36
+ ) {}
37
+
38
+ const user = new User(
39
+ // identifier
40
+ new UserId("user-1"),
41
+ // model
42
+ { name: "First User" },
43
+ );
56
44
  ```
@@ -0,0 +1,21 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { type DeepReadonly } from './utils';
3
+ export declare const defineEntity: <IdSchema extends StandardSchemaV1, ModelSchema extends StandardSchemaV1>(idSchema: IdSchema, modelSchema: ModelSchema | ((isCurrentEntity: (target: unknown) => boolean) => ModelSchema)) => {
4
+ new (id: StandardSchemaV1.InferOutput<IdSchema>, model: StandardSchemaV1.InferOutput<ModelSchema>): {
5
+ get model(): DeepReadonly<StandardSchemaV1.InferOutput<ModelSchema>>;
6
+ get id(): DeepReadonly<StandardSchemaV1.InferOutput<IdSchema>>;
7
+ };
8
+ readonly idSchema: IdSchema;
9
+ readonly modelSchema: ModelSchema;
10
+ is<T extends abstract new (...args: any[]) => any>(this: T, target: unknown): target is InstanceType<T>;
11
+ };
12
+ export type InferEntityModel<T> = T extends {
13
+ modelSchema: StandardSchemaV1;
14
+ } ? StandardSchemaV1.InferOutput<T['modelSchema']> : T extends {
15
+ model: infer M;
16
+ } ? M : never;
17
+ export type InferEntityId<T> = T extends {
18
+ idSchema: StandardSchemaV1;
19
+ } ? StandardSchemaV1.InferOutput<T['idSchema']> : T extends {
20
+ id: infer M;
21
+ } ? M : never;
package/dist/index.cjs CHANGED
@@ -30,9 +30,8 @@ __webpack_require__.r(__webpack_exports__);
30
30
  __webpack_require__.d(__webpack_exports__, {
31
31
  AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
32
32
  InvalidDataParsingError: ()=>InvalidDataParsingError,
33
- S: ()=>S,
34
33
  StructuredCloneNotFoundError: ()=>StructuredCloneNotFoundError,
35
- defineValueObjectClass: ()=>defineValueObjectClass
34
+ defineValueObject: ()=>defineValueObject
36
35
  });
37
36
  const errors_namespaceObject = require("@domain-first/errors");
38
37
  const AsyncSchemaInSyncParsingError = (0, errors_namespaceObject.defineErrorClass)({
@@ -44,14 +43,6 @@ const InvalidDataParsingError = (0, errors_namespaceObject.defineErrorClass)({
44
43
  const StructuredCloneNotFoundError = (0, errors_namespaceObject.defineErrorClass)({
45
44
  code: 'STRUCTURED_CLONE_NOT_FOUND'
46
45
  });
47
- async function parseAsync(schema, value) {
48
- const result = await schema['~standard'].validate(value);
49
- if ('issues' in result && result.issues) throw new InvalidDataParsingError({
50
- parsingIssues: result.issues,
51
- value
52
- });
53
- return result.value;
54
- }
55
46
  function parseSync(schema, value) {
56
47
  const result = schema['~standard'].validate(value);
57
48
  if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
@@ -64,9 +55,7 @@ function parseSync(schema, value) {
64
55
  });
65
56
  return result.value;
66
57
  }
67
- class S {
68
- }
69
- const defineValueObjectClass = (schema)=>{
58
+ const defineValueObject = (schema)=>{
70
59
  const modelSymbol = Symbol();
71
60
  const classSymbol = Symbol();
72
61
  const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
@@ -81,10 +70,8 @@ const defineValueObjectClass = (schema)=>{
81
70
  value: true
82
71
  });
83
72
  }
84
- static is = (target)=>isThisClass(target);
85
- static async createWithAsyncSchema(data) {
86
- const parsedData = await parseAsync(ValueObject.schema, data);
87
- return new this(parsedData);
73
+ static is(target) {
74
+ return isThisClass(target);
88
75
  }
89
76
  get model() {
90
77
  const thisWithSymbol = this;
@@ -95,15 +82,13 @@ const defineValueObjectClass = (schema)=>{
95
82
  };
96
83
  exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
97
84
  exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
98
- exports.S = __webpack_exports__.S;
99
85
  exports.StructuredCloneNotFoundError = __webpack_exports__.StructuredCloneNotFoundError;
100
- exports.defineValueObjectClass = __webpack_exports__.defineValueObjectClass;
86
+ exports.defineValueObject = __webpack_exports__.defineValueObject;
101
87
  for(var __rspack_i in __webpack_exports__)if (-1 === [
102
88
  "AsyncSchemaInSyncParsingError",
103
89
  "InvalidDataParsingError",
104
- "S",
105
90
  "StructuredCloneNotFoundError",
106
- "defineValueObjectClass"
91
+ "defineValueObject"
107
92
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
108
93
  Object.defineProperty(exports, '__esModule', {
109
94
  value: true
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './errors';
2
+ export type { DeepReadonly } from './utils';
2
3
  export * from './value-object';
package/dist/index.js CHANGED
@@ -8,14 +8,6 @@ const InvalidDataParsingError = defineErrorClass({
8
8
  const StructuredCloneNotFoundError = defineErrorClass({
9
9
  code: 'STRUCTURED_CLONE_NOT_FOUND'
10
10
  });
11
- async function parseAsync(schema, value) {
12
- const result = await schema['~standard'].validate(value);
13
- if ('issues' in result && result.issues) throw new InvalidDataParsingError({
14
- parsingIssues: result.issues,
15
- value
16
- });
17
- return result.value;
18
- }
19
11
  function parseSync(schema, value) {
20
12
  const result = schema['~standard'].validate(value);
21
13
  if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
@@ -28,9 +20,7 @@ function parseSync(schema, value) {
28
20
  });
29
21
  return result.value;
30
22
  }
31
- class S {
32
- }
33
- const defineValueObjectClass = (schema)=>{
23
+ const defineValueObject = (schema)=>{
34
24
  const modelSymbol = Symbol();
35
25
  const classSymbol = Symbol();
36
26
  const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
@@ -45,10 +35,8 @@ const defineValueObjectClass = (schema)=>{
45
35
  value: true
46
36
  });
47
37
  }
48
- static is = (target)=>isThisClass(target);
49
- static async createWithAsyncSchema(data) {
50
- const parsedData = await parseAsync(ValueObject.schema, data);
51
- return new this(parsedData);
38
+ static is(target) {
39
+ return isThisClass(target);
52
40
  }
53
41
  get model() {
54
42
  const thisWithSymbol = this;
@@ -57,4 +45,4 @@ const defineValueObjectClass = (schema)=>{
57
45
  }
58
46
  return ValueObject;
59
47
  };
60
- export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, S, StructuredCloneNotFoundError, defineValueObjectClass };
48
+ export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, StructuredCloneNotFoundError, defineValueObject };
@@ -1,2 +1,2 @@
1
- export * from './parse-async';
2
1
  export * from './parse-sync';
2
+ export * from './types';
@@ -1,17 +1,11 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- import type { DeepReadonly } from './types';
3
- export declare abstract class S<Schema extends StandardSchemaV1> {
4
- abstract schema: Schema;
5
- }
6
- export declare const defineValueObjectClass: <Schema extends StandardSchemaV1>(schema: Schema | ((isCurrentValueObject: (target: unknown) => boolean) => Schema)) => {
2
+ import { type DeepReadonly } from './utils';
3
+ export declare const defineValueObject: <Schema extends StandardSchemaV1>(schema: Schema | ((isCurrentValueObject: (target: unknown) => boolean) => Schema)) => {
7
4
  new (data: StandardSchemaV1.InferOutput<Schema>): {
8
5
  get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
9
6
  };
10
7
  readonly schema: Schema;
11
- is: (target: unknown) => boolean;
12
- createWithAsyncSchema(data: StandardSchemaV1.InferInput<Schema>): Promise<{
13
- get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
14
- }>;
8
+ is<T extends abstract new (...args: any[]) => any>(this: T, target: unknown): target is InstanceType<T>;
15
9
  };
16
10
  export type InferValueObjectSchema<T> = T extends {
17
11
  schema: StandardSchemaV1;
package/package.json CHANGED
@@ -2,7 +2,15 @@
2
2
  "name": "@domain-first/types",
3
3
  "description": "Type-safe Entities and Value Objects with Standard Schema validation",
4
4
  "license": "MIT",
5
- "version": "0.0.2",
5
+ "keywords": [
6
+ "entity",
7
+ "value-object",
8
+ "value object",
9
+ "vo",
10
+ "domain model",
11
+ "domain-first"
12
+ ],
13
+ "version": "1.0.0",
6
14
  "type": "module",
7
15
  "repository": {
8
16
  "type": "git",
@@ -36,7 +44,11 @@
36
44
  "@rslib/core": "^0.23.2",
37
45
  "@rstest/adapter-rslib": "^0.10.6",
38
46
  "@rstest/core": "^0.10.6",
47
+ "@semantic-release/changelog": "^6.0.3",
48
+ "@semantic-release/git": "^10.0.1",
49
+ "@semantic-release/npm": "^13.1.5",
39
50
  "@types/node": "^24.13.2",
51
+ "semantic-release": "^25.0.8",
40
52
  "typescript": "^6.0.3",
41
53
  "valibot": "^1.4.2",
42
54
  "zod": "^4.4.3"
package/dist/parse.d.ts DELETED
File without changes
@@ -1,2 +0,0 @@
1
- import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- export declare function parseAsync<S extends StandardSchemaV1>(schema: S, value: StandardSchemaV1.InferInput<S>): Promise<StandardSchemaV1.InferOutput<S>>;
File without changes