@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 +29 -0
- package/dist/entity.d.ts +21 -0
- package/dist/errors/async-schema-in-sync-parsing-error.d.ts +4 -4
- package/dist/errors/invalid-data-parsing-error.d.ts +4 -4
- package/dist/errors/structured-clone-not-found-error.d.ts +4 -4
- package/dist/index.cjs +16 -24
- package/dist/index.d.ts +1 -0
- package/dist/index.js +14 -22
- package/dist/utils/index.d.ts +1 -1
- package/dist/value-object.d.ts +5 -9
- package/package.json +13 -1
- package/dist/parse.d.ts +0 -0
- package/dist/utils/parse-async.d.ts +0 -2
- /package/dist/{utils/parse-async.spec.d.ts → entity.spec.d.ts} +0 -0
- /package/dist/{types.d.ts → utils/types.d.ts} +0 -0
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
|
+
```
|
package/dist/entity.d.ts
ADDED
|
@@ -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
|
-
|
|
29
|
-
|
|
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
|
-
|
|
32
|
+
get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
|
|
33
33
|
code: string;
|
|
34
34
|
}>;
|
|
35
|
-
|
|
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
|
-
|
|
42
|
-
|
|
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
|
-
|
|
45
|
+
readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
|
|
46
46
|
code: string;
|
|
47
47
|
}>;
|
|
48
|
-
|
|
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
|
-
|
|
29
|
-
|
|
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
|
-
|
|
32
|
+
get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
|
|
33
33
|
code: string;
|
|
34
34
|
}>;
|
|
35
|
-
|
|
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
|
-
|
|
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
|
|
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:
|
|
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
|
-
|
|
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.
|
|
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
|
-
"
|
|
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
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
|
|
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:
|
|
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
|
-
|
|
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,
|
|
48
|
+
export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, StructuredCloneNotFoundError, defineValueObject };
|
package/dist/utils/index.d.ts
CHANGED
package/dist/value-object.d.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
|
|
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
|
-
|
|
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
|
-
"
|
|
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
|
|
File without changes
|
|
File without changes
|