@domain-first/types 0.0.2 → 1.0.1
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 +22 -34
- package/dist/entity.d.ts +21 -0
- package/dist/errors/index.d.ts +0 -1
- package/dist/index.cjs +6 -27
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -19
- package/dist/utils/index.d.ts +1 -1
- package/dist/value-object.d.ts +3 -9
- package/package.json +13 -1
- package/dist/errors/structured-clone-not-found-error.d.ts +0 -46
- 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
|
@@ -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 {
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
```
|
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;
|
package/dist/errors/index.d.ts
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -30,9 +30,7 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
30
30
|
__webpack_require__.d(__webpack_exports__, {
|
|
31
31
|
AsyncSchemaInSyncParsingError: ()=>AsyncSchemaInSyncParsingError,
|
|
32
32
|
InvalidDataParsingError: ()=>InvalidDataParsingError,
|
|
33
|
-
|
|
34
|
-
StructuredCloneNotFoundError: ()=>StructuredCloneNotFoundError,
|
|
35
|
-
defineValueObjectClass: ()=>defineValueObjectClass
|
|
33
|
+
defineValueObject: ()=>defineValueObject
|
|
36
34
|
});
|
|
37
35
|
const errors_namespaceObject = require("@domain-first/errors");
|
|
38
36
|
const AsyncSchemaInSyncParsingError = (0, errors_namespaceObject.defineErrorClass)({
|
|
@@ -41,17 +39,6 @@ const AsyncSchemaInSyncParsingError = (0, errors_namespaceObject.defineErrorClas
|
|
|
41
39
|
const InvalidDataParsingError = (0, errors_namespaceObject.defineErrorClass)({
|
|
42
40
|
code: 'INVALID_DATA_PARSING'
|
|
43
41
|
});
|
|
44
|
-
const StructuredCloneNotFoundError = (0, errors_namespaceObject.defineErrorClass)({
|
|
45
|
-
code: 'STRUCTURED_CLONE_NOT_FOUND'
|
|
46
|
-
});
|
|
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
42
|
function parseSync(schema, value) {
|
|
56
43
|
const result = schema['~standard'].validate(value);
|
|
57
44
|
if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
|
|
@@ -64,9 +51,7 @@ function parseSync(schema, value) {
|
|
|
64
51
|
});
|
|
65
52
|
return result.value;
|
|
66
53
|
}
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
const defineValueObjectClass = (schema)=>{
|
|
54
|
+
const defineValueObject = (schema)=>{
|
|
70
55
|
const modelSymbol = Symbol();
|
|
71
56
|
const classSymbol = Symbol();
|
|
72
57
|
const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
|
|
@@ -81,10 +66,8 @@ const defineValueObjectClass = (schema)=>{
|
|
|
81
66
|
value: true
|
|
82
67
|
});
|
|
83
68
|
}
|
|
84
|
-
static is
|
|
85
|
-
|
|
86
|
-
const parsedData = await parseAsync(ValueObject.schema, data);
|
|
87
|
-
return new this(parsedData);
|
|
69
|
+
static is(target) {
|
|
70
|
+
return isThisClass(target);
|
|
88
71
|
}
|
|
89
72
|
get model() {
|
|
90
73
|
const thisWithSymbol = this;
|
|
@@ -95,15 +78,11 @@ const defineValueObjectClass = (schema)=>{
|
|
|
95
78
|
};
|
|
96
79
|
exports.AsyncSchemaInSyncParsingError = __webpack_exports__.AsyncSchemaInSyncParsingError;
|
|
97
80
|
exports.InvalidDataParsingError = __webpack_exports__.InvalidDataParsingError;
|
|
98
|
-
exports.
|
|
99
|
-
exports.StructuredCloneNotFoundError = __webpack_exports__.StructuredCloneNotFoundError;
|
|
100
|
-
exports.defineValueObjectClass = __webpack_exports__.defineValueObjectClass;
|
|
81
|
+
exports.defineValueObject = __webpack_exports__.defineValueObject;
|
|
101
82
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
102
83
|
"AsyncSchemaInSyncParsingError",
|
|
103
84
|
"InvalidDataParsingError",
|
|
104
|
-
"
|
|
105
|
-
"StructuredCloneNotFoundError",
|
|
106
|
-
"defineValueObjectClass"
|
|
85
|
+
"defineValueObject"
|
|
107
86
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
108
87
|
Object.defineProperty(exports, '__esModule', {
|
|
109
88
|
value: true
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -5,17 +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
|
-
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
8
|
function parseSync(schema, value) {
|
|
20
9
|
const result = schema['~standard'].validate(value);
|
|
21
10
|
if (result instanceof Promise) throw new AsyncSchemaInSyncParsingError({
|
|
@@ -28,9 +17,7 @@ function parseSync(schema, value) {
|
|
|
28
17
|
});
|
|
29
18
|
return result.value;
|
|
30
19
|
}
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
const defineValueObjectClass = (schema)=>{
|
|
20
|
+
const defineValueObject = (schema)=>{
|
|
34
21
|
const modelSymbol = Symbol();
|
|
35
22
|
const classSymbol = Symbol();
|
|
36
23
|
const isThisClass = (target)=>!!target && 'object' == typeof target && classSymbol in target;
|
|
@@ -45,10 +32,8 @@ const defineValueObjectClass = (schema)=>{
|
|
|
45
32
|
value: true
|
|
46
33
|
});
|
|
47
34
|
}
|
|
48
|
-
static is
|
|
49
|
-
|
|
50
|
-
const parsedData = await parseAsync(ValueObject.schema, data);
|
|
51
|
-
return new this(parsedData);
|
|
35
|
+
static is(target) {
|
|
36
|
+
return isThisClass(target);
|
|
52
37
|
}
|
|
53
38
|
get model() {
|
|
54
39
|
const thisWithSymbol = this;
|
|
@@ -57,4 +42,4 @@ const defineValueObjectClass = (schema)=>{
|
|
|
57
42
|
}
|
|
58
43
|
return ValueObject;
|
|
59
44
|
};
|
|
60
|
-
export { AsyncSchemaInSyncParsingError, InvalidDataParsingError,
|
|
45
|
+
export { AsyncSchemaInSyncParsingError, InvalidDataParsingError, defineValueObject };
|
package/dist/utils/index.d.ts
CHANGED
package/dist/value-object.d.ts
CHANGED
|
@@ -1,17 +1,11 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import type
|
|
3
|
-
export declare
|
|
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)
|
|
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
|
-
"
|
|
5
|
+
"keywords": [
|
|
6
|
+
"entity",
|
|
7
|
+
"value-object",
|
|
8
|
+
"value object",
|
|
9
|
+
"vo",
|
|
10
|
+
"domain model",
|
|
11
|
+
"domain-first"
|
|
12
|
+
],
|
|
13
|
+
"version": "1.0.1",
|
|
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"
|
|
@@ -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
|
-
};
|
package/dist/parse.d.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|