@domain-first/types 0.0.1 → 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
@@ -13,3 +13,32 @@
13
13
  ```
14
14
  npm i @domain-first/types
15
15
  ```
16
+
17
+ # Quick Start
18
+
19
+ ```ts
20
+ import { defineValueObject, defineEntity } from "@domain-first/types";
21
+ // any schema library supporting Standard Schema can be used
22
+ import { z } from "zod";
23
+
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
+ );
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;
@@ -25,14 +25,14 @@ export declare const AsyncSchemaInSyncParsingError: {
25
25
  code: string;
26
26
  };
27
27
  readonly details: Record<string, any>;
28
- readonly formattedDetails: import("@domain-first/errors").PlainPrimitivesObject;
29
- readonly serialized: import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
28
+ get formattedDetails(): import("@domain-first/errors").PlainPrimitivesObject;
29
+ get serialized(): import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
30
30
  code: string;
31
31
  }>;
32
- readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
32
+ get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
33
33
  code: string;
34
34
  }>;
35
- readonly serializedFull: import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
35
+ get serializedFull(): import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
36
36
  code: string;
37
37
  }>;
38
38
  name: string;
@@ -38,14 +38,14 @@ export declare const InvalidDataParsingError: {
38
38
  parsingIssues: readonly StandardSchemaV1.Issue[];
39
39
  value: unknown;
40
40
  };
41
- get formattedDetails(): import("@domain-first/errors").PlainPrimitivesObject;
42
- get serialized(): import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
41
+ readonly formattedDetails: import("@domain-first/errors").PlainPrimitivesObject;
42
+ readonly serialized: import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
43
43
  code: string;
44
44
  }>;
45
- get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
45
+ readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
46
46
  code: string;
47
47
  }>;
48
- get serializedFull(): import("@domain-first/errors").FullTransportedError<{
48
+ readonly serializedFull: import("@domain-first/errors").FullTransportedError<{
49
49
  parsingIssues: readonly StandardSchemaV1.Issue[];
50
50
  value: unknown;
51
51
  }, import("@domain-first/errors").PlainPrimitivesObject & {
@@ -25,14 +25,14 @@ export declare const StructuredCloneNotFoundError: {
25
25
  code: string;
26
26
  };
27
27
  readonly details: Record<string, any>;
28
- readonly formattedDetails: import("@domain-first/errors").PlainPrimitivesObject;
29
- readonly serialized: import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
28
+ get formattedDetails(): import("@domain-first/errors").PlainPrimitivesObject;
29
+ get serialized(): import("@domain-first/errors").TransportedError<import("@domain-first/errors").PlainPrimitivesObject & {
30
30
  code: string;
31
31
  }>;
32
- readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
32
+ get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
33
33
  code: string;
34
34
  }>;
35
- readonly serializedFull: import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
35
+ get serializedFull(): import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
36
36
  code: string;
37
37
  }>;
38
38
  name: string;
package/dist/index.cjs CHANGED
@@ -31,7 +31,7 @@ __webpack_require__.d(__webpack_exports__, {
31
31
  AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
32
32
  InvalidDataParsingError: ()=>InvalidDataParsingError,
33
33
  StructuredCloneNotFoundError: ()=>StructuredCloneNotFoundError,
34
- defineValueObjectClass: ()=>defineValueObjectClass
34
+ defineValueObject: ()=>defineValueObject
35
35
  });
36
36
  const errors_namespaceObject = require("@domain-first/errors");
37
37
  const AsyncSchemaInSyncParsingError = (0, errors_namespaceObject.defineErrorClass)({
@@ -43,14 +43,6 @@ const InvalidDataParsingError = (0, errors_namespaceObject.defineErrorClass)({
43
43
  const StructuredCloneNotFoundError = (0, errors_namespaceObject.defineErrorClass)({
44
44
  code: 'STRUCTURED_CLONE_NOT_FOUND'
45
45
  });
46
- async function parseAsync(schema, value) {
47
- const result = await schema['~standard'].validate(value);
48
- if ('issues' in result && result.issues) throw new InvalidDataParsingError({
49
- parsingIssues: result.issues,
50
- value
51
- });
52
- return result.value;
53
- }
54
46
  function parseSync(schema, value) {
55
47
  const result = schema['~standard'].validate(value);
56
48
  if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
@@ -63,40 +55,40 @@ function parseSync(schema, value) {
63
55
  });
64
56
  return result.value;
65
57
  }
66
- const defineValueObjectClass = (schema)=>{
58
+ const defineValueObject = (schema)=>{
67
59
  const modelSymbol = Symbol();
60
+ const classSymbol = Symbol();
61
+ const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
68
62
  class ValueObject {
69
- static schema = schema;
63
+ static schema = 'function' == typeof schema ? schema(isThisClass) : schema;
70
64
  constructor(data){
65
+ const parsedData = parseSync(ValueObject.schema, data);
71
66
  Object.defineProperty(this, modelSymbol, {
72
- value: data
67
+ value: parsedData
68
+ });
69
+ Object.defineProperty(this, classSymbol, {
70
+ value: true
73
71
  });
74
72
  }
73
+ static is(target) {
74
+ return isThisClass(target);
75
+ }
75
76
  get model() {
76
77
  const thisWithSymbol = this;
77
- if (!('structuredClone' in globalThis)) throw new StructuredCloneNotFoundError({});
78
- return structuredClone(thisWithSymbol[modelSymbol]);
78
+ return thisWithSymbol[modelSymbol];
79
79
  }
80
- static create = (data)=>{
81
- const parsedData = parseSync(schema, data);
82
- return new ValueObject(parsedData);
83
- };
84
- static createWithAsyncSchema = async (data)=>{
85
- const parsedData = await parseAsync(schema, data);
86
- return new ValueObject(parsedData);
87
- };
88
80
  }
89
81
  return ValueObject;
90
82
  };
91
83
  exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
92
84
  exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
93
85
  exports.StructuredCloneNotFoundError = __webpack_exports__.StructuredCloneNotFoundError;
94
- exports.defineValueObjectClass = __webpack_exports__.defineValueObjectClass;
86
+ exports.defineValueObject = __webpack_exports__.defineValueObject;
95
87
  for(var __rspack_i in __webpack_exports__)if (-1 === [
96
88
  "AsyncSchemaInSyncParsingError",
97
89
  "InvalidDataParsingError",
98
90
  "StructuredCloneNotFoundError",
99
- "defineValueObjectClass"
91
+ "defineValueObject"
100
92
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
101
93
  Object.defineProperty(exports, '__esModule', {
102
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,29 +20,29 @@ function parseSync(schema, value) {
28
20
  });
29
21
  return result.value;
30
22
  }
31
- const defineValueObjectClass = (schema)=>{
23
+ const defineValueObject = (schema)=>{
32
24
  const modelSymbol = Symbol();
25
+ const classSymbol = Symbol();
26
+ const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
33
27
  class ValueObject {
34
- static schema = schema;
28
+ static schema = 'function' == typeof schema ? schema(isThisClass) : schema;
35
29
  constructor(data){
30
+ const parsedData = parseSync(ValueObject.schema, data);
36
31
  Object.defineProperty(this, modelSymbol, {
37
- value: data
32
+ value: parsedData
38
33
  });
34
+ Object.defineProperty(this, classSymbol, {
35
+ value: true
36
+ });
37
+ }
38
+ static is(target) {
39
+ return isThisClass(target);
39
40
  }
40
41
  get model() {
41
42
  const thisWithSymbol = this;
42
- if (!('structuredClone' in globalThis)) throw new StructuredCloneNotFoundError({});
43
- return structuredClone(thisWithSymbol[modelSymbol]);
43
+ return thisWithSymbol[modelSymbol];
44
44
  }
45
- static create = (data)=>{
46
- const parsedData = parseSync(schema, data);
47
- return new ValueObject(parsedData);
48
- };
49
- static createWithAsyncSchema = async (data)=>{
50
- const parsedData = await parseAsync(schema, data);
51
- return new ValueObject(parsedData);
52
- };
53
45
  }
54
46
  return ValueObject;
55
47
  };
56
- export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, 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,15 +1,11 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- export declare const defineValueObjectClass: <Schema extends StandardSchemaV1>(schema: Schema) => {
2
+ import { type DeepReadonly } from './utils';
3
+ export declare const defineValueObject: <Schema extends StandardSchemaV1>(schema: Schema | ((isCurrentValueObject: (target: unknown) => boolean) => Schema)) => {
3
4
  new (data: StandardSchemaV1.InferOutput<Schema>): {
4
- get model(): StandardSchemaV1.InferOutput<Schema>;
5
+ get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
5
6
  };
6
- schema: Schema;
7
- create: (data: StandardSchemaV1.InferInput<Schema>) => {
8
- get model(): StandardSchemaV1.InferOutput<Schema>;
9
- };
10
- createWithAsyncSchema: (data: StandardSchemaV1.InferInput<Schema>) => Promise<{
11
- get model(): StandardSchemaV1.InferOutput<Schema>;
12
- }>;
7
+ readonly schema: Schema;
8
+ is<T extends abstract new (...args: any[]) => any>(this: T, target: unknown): target is InstanceType<T>;
13
9
  };
14
10
  export type InferValueObjectSchema<T> = T extends {
15
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.1",
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