@domain-first/types 0.0.1 → 0.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
@@ -13,3 +13,44 @@
13
13
  ```
14
14
  npm i @domain-first/types
15
15
  ```
16
+
17
+ # Quick Start
18
+
19
+ ## Value Object
20
+
21
+ ```ts
22
+ import { defineValueObjectClass } from "@domain-first/types";
23
+ import { z } from "zod";
24
+
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 }
56
+ ```
@@ -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
@@ -30,6 +30,7 @@ __webpack_require__.r(__webpack_exports__);
30
30
  __webpack_require__.d(__webpack_exports__, {
31
31
  AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
32
32
  InvalidDataParsingError: ()=>InvalidDataParsingError,
33
+ S: ()=>S,
33
34
  StructuredCloneNotFoundError: ()=>StructuredCloneNotFoundError,
34
35
  defineValueObjectClass: ()=>defineValueObjectClass
35
36
  });
@@ -63,38 +64,44 @@ function parseSync(schema, value) {
63
64
  });
64
65
  return result.value;
65
66
  }
67
+ class S {
68
+ }
66
69
  const defineValueObjectClass = (schema)=>{
67
70
  const modelSymbol = Symbol();
71
+ const classSymbol = Symbol();
72
+ const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
68
73
  class ValueObject {
69
- static schema = schema;
74
+ static schema = 'function' == typeof schema ? schema(isThisClass) : schema;
70
75
  constructor(data){
76
+ const parsedData = parseSync(ValueObject.schema, data);
71
77
  Object.defineProperty(this, modelSymbol, {
72
- value: data
78
+ value: parsedData
79
+ });
80
+ Object.defineProperty(this, classSymbol, {
81
+ value: true
73
82
  });
74
83
  }
84
+ static is = (target)=>isThisClass(target);
85
+ static async createWithAsyncSchema(data) {
86
+ const parsedData = await parseAsync(ValueObject.schema, data);
87
+ return new this(parsedData);
88
+ }
75
89
  get model() {
76
90
  const thisWithSymbol = this;
77
- if (!('structuredClone' in globalThis)) throw new StructuredCloneNotFoundError({});
78
- return structuredClone(thisWithSymbol[modelSymbol]);
91
+ return thisWithSymbol[modelSymbol];
79
92
  }
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
93
  }
89
94
  return ValueObject;
90
95
  };
91
96
  exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
92
97
  exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
98
+ exports.S = __webpack_exports__.S;
93
99
  exports.StructuredCloneNotFoundError = __webpack_exports__.StructuredCloneNotFoundError;
94
100
  exports.defineValueObjectClass = __webpack_exports__.defineValueObjectClass;
95
101
  for(var __rspack_i in __webpack_exports__)if (-1 === [
96
102
  "AsyncSchemaInSyncParsingError",
97
103
  "InvalidDataParsingError",
104
+ "S",
98
105
  "StructuredCloneNotFoundError",
99
106
  "defineValueObjectClass"
100
107
  ].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
package/dist/index.js CHANGED
@@ -28,29 +28,33 @@ function parseSync(schema, value) {
28
28
  });
29
29
  return result.value;
30
30
  }
31
+ class S {
32
+ }
31
33
  const defineValueObjectClass = (schema)=>{
32
34
  const modelSymbol = Symbol();
35
+ const classSymbol = Symbol();
36
+ const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
33
37
  class ValueObject {
34
- static schema = schema;
38
+ static schema = 'function' == typeof schema ? schema(isThisClass) : schema;
35
39
  constructor(data){
40
+ const parsedData = parseSync(ValueObject.schema, data);
36
41
  Object.defineProperty(this, modelSymbol, {
37
- value: data
42
+ value: parsedData
43
+ });
44
+ Object.defineProperty(this, classSymbol, {
45
+ value: true
38
46
  });
39
47
  }
48
+ static is = (target)=>isThisClass(target);
49
+ static async createWithAsyncSchema(data) {
50
+ const parsedData = await parseAsync(ValueObject.schema, data);
51
+ return new this(parsedData);
52
+ }
40
53
  get model() {
41
54
  const thisWithSymbol = this;
42
- if (!('structuredClone' in globalThis)) throw new StructuredCloneNotFoundError({});
43
- return structuredClone(thisWithSymbol[modelSymbol]);
55
+ return thisWithSymbol[modelSymbol];
44
56
  }
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
57
  }
54
58
  return ValueObject;
55
59
  };
56
- export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, StructuredCloneNotFoundError, defineValueObjectClass };
60
+ export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, S, StructuredCloneNotFoundError, defineValueObjectClass };
@@ -1,14 +1,16 @@
1
1
  import type { StandardSchemaV1 } from '@standard-schema/spec';
2
- export declare const defineValueObjectClass: <Schema extends StandardSchemaV1>(schema: Schema) => {
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)) => {
3
7
  new (data: StandardSchemaV1.InferOutput<Schema>): {
4
- get model(): StandardSchemaV1.InferOutput<Schema>;
8
+ get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
5
9
  };
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>;
10
+ readonly schema: Schema;
11
+ is: (target: unknown) => boolean;
12
+ createWithAsyncSchema(data: StandardSchemaV1.InferInput<Schema>): Promise<{
13
+ get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
12
14
  }>;
13
15
  };
14
16
  export type InferValueObjectSchema<T> = T extends {
package/package.json CHANGED
@@ -2,7 +2,7 @@
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
+ "version": "0.0.2",
6
6
  "type": "module",
7
7
  "repository": {
8
8
  "type": "git",