@domain-first/types 3.0.6 → 4.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 +78 -24
- package/dist/domain-type.d.ts +12 -0
- package/dist/errors/async-schema-in-sync-parsing-error.d.ts +14 -28
- package/dist/errors/invalid-data-parsing-error.d.ts +11 -31
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/entity.d.ts +0 -26
- package/dist/value-object.d.ts +0 -17
- /package/dist/{value-object.spec.d.ts → domain-type.spec.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
</p>
|
|
6
6
|
|
|
7
7
|
<p align="center">
|
|
8
|
-
Type-
|
|
8
|
+
Type-safe domain models powered by Standard Schema validation.
|
|
9
9
|
</p>
|
|
10
10
|
|
|
11
11
|
<p align="center">
|
|
@@ -22,41 +22,95 @@ npm i @domain-first/types
|
|
|
22
22
|
|
|
23
23
|
# Motivation
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
Domain models often need both runtime validation and domain-specific behavior. Keeping validation, type inference, and business logic together can lead to repetitive boilerplate.
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
Domain-First Types creates type-safe domain models from Standard Schema validators while leaving domain-specific behavior to your classes.
|
|
28
|
+
|
|
29
|
+
# Features
|
|
30
|
+
|
|
31
|
+
- Runtime validation through any Standard Schema compatible library
|
|
32
|
+
- Full TypeScript inference
|
|
33
|
+
- Domain behavior through class methods
|
|
34
|
+
- Recursive domain types
|
|
35
|
+
|
|
36
|
+
# Examples
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
28
39
|
|
|
29
40
|
```ts
|
|
30
|
-
import {
|
|
31
|
-
defineValueObject,
|
|
32
|
-
defineEntity,
|
|
33
|
-
InvalidDataParsingError,
|
|
34
|
-
} from "@domain-first/types";
|
|
41
|
+
import { domainType } from "@domain-first/types";
|
|
35
42
|
// any schema library supporting Standard Schema can be used
|
|
36
43
|
import { z } from "zod";
|
|
37
44
|
|
|
38
|
-
class UserId extends
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
class UserId extends domainType(z.string().nonempty()) {
|
|
46
|
+
static get randomId() {
|
|
47
|
+
return new UserId(globalThis.crypto.randomUUID());
|
|
48
|
+
}
|
|
49
|
+
}
|
|
42
50
|
|
|
43
|
-
class User extends
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
) {
|
|
51
|
+
class User extends domainType(
|
|
52
|
+
z.object({
|
|
53
|
+
id: z.instanceof(UserId),
|
|
54
|
+
name: z.string().nonempty(),
|
|
55
|
+
}),
|
|
56
|
+
) {
|
|
57
|
+
withNewName = (name: string) => {
|
|
58
|
+
return new User({
|
|
59
|
+
id: this.id,
|
|
60
|
+
name,
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const user = new User({
|
|
66
|
+
id: UserId.randomId,
|
|
67
|
+
name: "First User",
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
console.log(user.name); // 'First User'
|
|
71
|
+
// user.name = 'New Name' -> TS Error
|
|
49
72
|
|
|
50
|
-
|
|
73
|
+
/**
|
|
74
|
+
* Domain types created from primitive schemas
|
|
75
|
+
* expose their value through the `value` property.
|
|
76
|
+
*/
|
|
77
|
+
console.log(user.id.value); // 'user-1'
|
|
78
|
+
// user.id.value = 'new value' -> TS Error
|
|
79
|
+
|
|
80
|
+
console.log(user.withNewName("Test Name").name); // 'Test Name'
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Recursive types
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import z from "zod";
|
|
87
|
+
import { recursiveDomainType } from "@domain-first/types";
|
|
88
|
+
|
|
89
|
+
class Node extends recursiveDomainType((isNode) => {
|
|
90
|
+
return z.object({
|
|
91
|
+
id: z.string().nonempty(),
|
|
92
|
+
linkedNode: z.custom<Node>(isNode).optional(),
|
|
93
|
+
});
|
|
94
|
+
}) {}
|
|
95
|
+
|
|
96
|
+
const node = new Node({ id: "1" });
|
|
97
|
+
const nodeWithLink = new Node({ id: "2", linkedNode: node });
|
|
98
|
+
|
|
99
|
+
console.log(nodeWithLink.linkedNode?.id); // 1
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Error handling
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
import { domainType, InvalidDataParsingError } from "@domain-first/types";
|
|
106
|
+
import z from "zod";
|
|
51
107
|
|
|
52
|
-
|
|
53
|
-
console.log(user.model); // { name: "First User" }, deep readonly
|
|
108
|
+
class NonEmptyString extends domainType(z.string().nonempty()) {}
|
|
54
109
|
|
|
55
110
|
try {
|
|
56
|
-
const
|
|
111
|
+
const _string = new NonEmptyString("");
|
|
57
112
|
} catch (e: unknown) {
|
|
58
|
-
|
|
59
|
-
if (InvalidDataParsingError.is(e)) {
|
|
113
|
+
if (e instanceof InvalidDataParsingError) {
|
|
60
114
|
console.error(e.details.parsingIssues);
|
|
61
115
|
console.error(e.details.value);
|
|
62
116
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import { type DeepReadonly } from './utils';
|
|
3
|
+
export declare const domainType: <ModelSchema extends StandardSchemaV1>(modelSchema: ModelSchema) => (abstract new (model: StandardSchemaV1.InferInput<ModelSchema>) => DeepReadonly<StandardSchemaV1.InferOutput<ModelSchema> extends object ? StandardSchemaV1.InferOutput<ModelSchema> : {
|
|
4
|
+
value: StandardSchemaV1.InferOutput<ModelSchema>;
|
|
5
|
+
}>) & {
|
|
6
|
+
schema: ModelSchema;
|
|
7
|
+
};
|
|
8
|
+
export declare const recursiveDomainType: <ModelSchema extends StandardSchemaV1>(modelSchema: (isCurrentType: (target: unknown) => boolean) => ModelSchema) => (abstract new (model: StandardSchemaV1.InferInput<ModelSchema>) => DeepReadonly<StandardSchemaV1.InferOutput<ModelSchema> extends object ? StandardSchemaV1.InferOutput<ModelSchema> : {
|
|
9
|
+
value: StandardSchemaV1.InferOutput<ModelSchema>;
|
|
10
|
+
}>) & {
|
|
11
|
+
schema: ModelSchema;
|
|
12
|
+
};
|
|
@@ -1,19 +1,12 @@
|
|
|
1
1
|
export declare const AsyncSchemaInSyncParsingError: {
|
|
2
|
-
new (details: Record<string,
|
|
3
|
-
readonly
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
readonly details: Record<string, any>;
|
|
2
|
+
new (details: Record<string, unknown>, options?: ErrorOptions): {
|
|
3
|
+
readonly code: string;
|
|
4
|
+
readonly metadata: {};
|
|
5
|
+
readonly details: Record<string, unknown>;
|
|
7
6
|
readonly formattedDetails: import("@domain-first/errors").PlainPrimitivesObject;
|
|
8
|
-
readonly serialized: import("@domain-first/errors").TransportedError<
|
|
9
|
-
|
|
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
|
-
}>;
|
|
7
|
+
readonly serialized: import("@domain-first/errors").TransportedError<{}>;
|
|
8
|
+
readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<{}>;
|
|
9
|
+
readonly serializedFull: import("@domain-first/errors").FullTransportedError<Record<string, unknown>, {}>;
|
|
17
10
|
name: string;
|
|
18
11
|
message: string;
|
|
19
12
|
stack?: string;
|
|
@@ -21,20 +14,13 @@ export declare const AsyncSchemaInSyncParsingError: {
|
|
|
21
14
|
};
|
|
22
15
|
matches: (target: unknown) => boolean | null;
|
|
23
16
|
is: (target: unknown) => target is {
|
|
24
|
-
readonly
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}>;
|
|
32
|
-
readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
|
|
33
|
-
code: string;
|
|
34
|
-
}>;
|
|
35
|
-
readonly serializedFull: import("@domain-first/errors").FullTransportedError<Record<string, any>, import("@domain-first/errors").PlainPrimitivesObject & {
|
|
36
|
-
code: string;
|
|
37
|
-
}>;
|
|
17
|
+
readonly code: string;
|
|
18
|
+
readonly metadata: Metadata;
|
|
19
|
+
readonly details: Details_1;
|
|
20
|
+
get formattedDetails(): import("@domain-first/errors").PlainPrimitivesObject;
|
|
21
|
+
get serialized(): import("@domain-first/errors").TransportedError<Metadata>;
|
|
22
|
+
get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<Metadata>;
|
|
23
|
+
get serializedFull(): import("@domain-first/errors").FullTransportedError<Details_1, Metadata>;
|
|
38
24
|
name: string;
|
|
39
25
|
message: string;
|
|
40
26
|
stack?: string;
|
|
@@ -4,26 +4,19 @@ export declare const InvalidDataParsingError: {
|
|
|
4
4
|
parsingIssues: readonly StandardSchemaV1.Issue[];
|
|
5
5
|
value: unknown;
|
|
6
6
|
}, options?: ErrorOptions): {
|
|
7
|
-
readonly
|
|
8
|
-
|
|
9
|
-
};
|
|
7
|
+
readonly code: string;
|
|
8
|
+
readonly metadata: {};
|
|
10
9
|
readonly details: {
|
|
11
10
|
parsingIssues: readonly StandardSchemaV1.Issue[];
|
|
12
11
|
value: unknown;
|
|
13
12
|
};
|
|
14
13
|
readonly formattedDetails: import("@domain-first/errors").PlainPrimitivesObject;
|
|
15
|
-
readonly serialized: import("@domain-first/errors").TransportedError<
|
|
16
|
-
|
|
17
|
-
}>;
|
|
18
|
-
readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
|
|
19
|
-
code: string;
|
|
20
|
-
}>;
|
|
14
|
+
readonly serialized: import("@domain-first/errors").TransportedError<{}>;
|
|
15
|
+
readonly serializedWithNativeData: import("@domain-first/errors").TransportedErrorWithNativeData<{}>;
|
|
21
16
|
readonly serializedFull: import("@domain-first/errors").FullTransportedError<{
|
|
22
17
|
parsingIssues: readonly StandardSchemaV1.Issue[];
|
|
23
18
|
value: unknown;
|
|
24
|
-
},
|
|
25
|
-
code: string;
|
|
26
|
-
}>;
|
|
19
|
+
}, {}>;
|
|
27
20
|
name: string;
|
|
28
21
|
message: string;
|
|
29
22
|
stack?: string;
|
|
@@ -31,26 +24,13 @@ export declare const InvalidDataParsingError: {
|
|
|
31
24
|
};
|
|
32
25
|
matches: (target: unknown) => boolean | null;
|
|
33
26
|
is: (target: unknown) => target is {
|
|
34
|
-
readonly
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
readonly details: {
|
|
38
|
-
parsingIssues: readonly StandardSchemaV1.Issue[];
|
|
39
|
-
value: unknown;
|
|
40
|
-
};
|
|
27
|
+
readonly code: string;
|
|
28
|
+
readonly metadata: Metadata;
|
|
29
|
+
readonly details: Details_1;
|
|
41
30
|
get formattedDetails(): import("@domain-first/errors").PlainPrimitivesObject;
|
|
42
|
-
get serialized(): import("@domain-first/errors").TransportedError<
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<import("@domain-first/errors").PlainPrimitivesObject & {
|
|
46
|
-
code: string;
|
|
47
|
-
}>;
|
|
48
|
-
get serializedFull(): import("@domain-first/errors").FullTransportedError<{
|
|
49
|
-
parsingIssues: readonly StandardSchemaV1.Issue[];
|
|
50
|
-
value: unknown;
|
|
51
|
-
}, import("@domain-first/errors").PlainPrimitivesObject & {
|
|
52
|
-
code: string;
|
|
53
|
-
}>;
|
|
31
|
+
get serialized(): import("@domain-first/errors").TransportedError<Metadata>;
|
|
32
|
+
get serializedWithNativeData(): import("@domain-first/errors").TransportedErrorWithNativeData<Metadata>;
|
|
33
|
+
get serializedFull(): import("@domain-first/errors").FullTransportedError<Details_1, Metadata>;
|
|
54
34
|
name: string;
|
|
55
35
|
message: string;
|
|
56
36
|
stack?: string;
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";const __rslib_import_meta_url__="u"<typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_require__={};__webpack_require__.d=(e,r,
|
|
1
|
+
"use strict";const __rslib_import_meta_url__="u"<typeof document?new(require("url".replace("",""))).URL("file:"+__filename).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href;var __webpack_require__={};__webpack_require__.d=(e,r,a)=>{var _=(r,a)=>{for(var _ in r)__webpack_require__.o(r,_)&&!__webpack_require__.o(e,_)&&Object.defineProperty(e,_,{enumerable:!0,[a]:r[_]})};_(r,"get"),_(a,"value")},__webpack_require__.o=(e,r)=>Object.prototype.hasOwnProperty.call(e,r),__webpack_require__.r=e=>{"u">typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var __webpack_exports__={};__webpack_require__.r(__webpack_exports__),__webpack_require__.d(__webpack_exports__,{AsyncSchemaInSyncParsingError:()=>AsyncSchemaInSyncParsingError,InvalidDataParsingError:()=>InvalidDataParsingError,domainType:()=>domainType,recursiveDomainType:()=>recursiveDomainType});const errors_namespaceObject=require("@domain-first/errors"),SchemaErrors=(0,errors_namespaceObject.errorNamespace)("SCHEMA"),AsyncSchemaInSyncParsingError=SchemaErrors.error("ASYNC_LOGIC"),ParsingErrors=(0,errors_namespaceObject.errorNamespace)("PARSING"),InvalidDataParsingError=ParsingErrors.error("INVALID_DATA");function parseSync(e,r){let a=e["~standard"].validate(r);if(a instanceof Promise)throw new AsyncSchemaInSyncParsingError({schema:e,value:r});if("issues"in a&&a.issues)throw new InvalidDataParsingError({parsingIssues:a.issues,value:r});return a.value}const createDomainType=e=>{let r=Symbol(),a=e=>!!e&&"object"==typeof e&&r in e;class _{static schema=e(a);constructor(e){const a=parseSync(this.constructor.schema,e);if(a&&"object"==typeof a)for(const[e,r]of Object.entries(a))Object.defineProperty(this,e,{value:r,writable:!1,configurable:!1,enumerable:!0});else Object.defineProperty(this,"value",{value:a,writable:!1,configurable:!1,enumerable:!0});Object.defineProperty(this,r,{value:!0,writable:!1,configurable:!1})}}return _},domainType=e=>createDomainType(()=>e),recursiveDomainType=e=>createDomainType(e);for(var __rspack_i in exports.AsyncSchemaInSyncParsingError=__webpack_exports__.AsyncSchemaInSyncParsingError,exports.InvalidDataParsingError=__webpack_exports__.InvalidDataParsingError,exports.domainType=__webpack_exports__.domainType,exports.recursiveDomainType=__webpack_exports__.recursiveDomainType,__webpack_exports__)-1===["AsyncSchemaInSyncParsingError","InvalidDataParsingError","domainType","recursiveDomainType"].indexOf(__rspack_i)&&(exports[__rspack_i]=__webpack_exports__[__rspack_i]);Object.defineProperty(exports,"__esModule",{value:!0});
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["webpack://webpack/runtime/define_property_getters","webpack://webpack/runtime/has_own_property","webpack://webpack/runtime/make_namespace_object","../src/errors/async-schema-in-sync-parsing-error.ts","../src/errors/invalid-data-parsing-error.ts","../src/utils/parse-sync.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["webpack://webpack/runtime/define_property_getters","webpack://webpack/runtime/has_own_property","webpack://webpack/runtime/make_namespace_object","../src/errors/async-schema-in-sync-parsing-error.ts","../src/errors/invalid-data-parsing-error.ts","../src/utils/parse-sync.ts","../src/domain-type.ts"],"sourcesContent":["__webpack_require__.d = (exports, getters, values) => {\n\tvar define = (defs, kind) => {\n\t\tfor(var key in defs) {\n\t\t\tif(__webpack_require__.o(defs, key) && !__webpack_require__.o(exports, key)) {\n\t\t\t\tObject.defineProperty(exports, key, { enumerable: true, [kind]: defs[key] });\n\t\t\t}\n\t\t}\n\t};\n\tdefine(getters, \"get\");\n\tdefine(values, \"value\");\n};","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { errorNamespace } from '@domain-first/errors';\n\nconst SchemaErrors = errorNamespace('SCHEMA')\n\nexport const AsyncSchemaInSyncParsingError = SchemaErrors.error('ASYNC_LOGIC');\n","import { errorNamespace } from '@domain-first/errors';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\n\nconst ParsingErrors = errorNamespace(\"PARSING\")\n\nexport const InvalidDataParsingError = ParsingErrors.error<{\n parsingIssues: readonly StandardSchemaV1.Issue[];\n value: unknown;\n}>('INVALID_DATA');\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport {\n AsyncSchemaInSyncParsingError,\n InvalidDataParsingError\n} from '../errors';\n\nexport function parseSync<S extends StandardSchemaV1>(\n schema: S,\n value: StandardSchemaV1.InferInput<S>\n): StandardSchemaV1.InferOutput<S> {\n const result = schema['~standard'].validate(value);\n\n if (result instanceof Promise) {\n throw new AsyncSchemaInSyncParsingError({ schema, value });\n }\n\n if ('issues' in result && result.issues) {\n throw new InvalidDataParsingError({ parsingIssues: result.issues, value });\n }\n\n return result.value;\n}\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type DeepReadonly, parseSync } from './utils';\n\nconst createDomainType = <ModelSchema extends StandardSchemaV1>(\n getModelSchema: (\n isCurrentEntity: (target: unknown) => boolean\n ) => ModelSchema\n) => {\n const classSymbol = Symbol();\n\n const isInstanceOfThisType = (target: unknown): boolean => {\n return !!target && typeof target === 'object' && classSymbol in target;\n };\n\n class DomainType {\n public static readonly schema = getModelSchema(isInstanceOfThisType);\n\n constructor(model: StandardSchemaV1.InferInput<ModelSchema>) {\n const schema = (this.constructor as typeof DomainType).schema;\n\n const parsedModelData = parseSync(schema, model);\n\n if (parsedModelData && typeof parsedModelData === 'object') {\n for (const [key, value] of Object.entries(parsedModelData)) {\n Object.defineProperty(this, key, {\n value,\n writable: false,\n configurable: false,\n enumerable: true\n });\n }\n } else {\n Object.defineProperty(this, 'value', {\n value: parsedModelData,\n writable: false,\n configurable: false,\n enumerable: true\n });\n }\n\n Object.defineProperty(this, classSymbol, {\n value: true,\n writable: false,\n configurable: false\n });\n }\n }\n\n return DomainType as unknown as (abstract new (\n model: StandardSchemaV1.InferInput<ModelSchema>\n ) => DeepReadonly<\n StandardSchemaV1.InferOutput<ModelSchema> extends object\n ? StandardSchemaV1.InferOutput<ModelSchema>\n : {\n value: StandardSchemaV1.InferOutput<ModelSchema>;\n }\n >) & { schema: ModelSchema };\n};\n\nexport const domainType = <ModelSchema extends StandardSchemaV1>(\n modelSchema: ModelSchema\n) => {\n return createDomainType(() => modelSchema);\n};\n\nexport const recursiveDomainType = <ModelSchema extends StandardSchemaV1>(\n modelSchema: (isCurrentType: (target: unknown) => boolean) => ModelSchema\n) => {\n return createDomainType(modelSchema);\n};\n"],"names":["__webpack_require__","e","Object","Symbol","SchemaErrors","errorNamespace","AsyncSchemaInSyncParsingError","ParsingErrors","InvalidDataParsingError","parseSync","schema","value","result","Promise","createDomainType","getModelSchema","classSymbol","isInstanceOfThisType","target","DomainType","model","parsedModelData","key","domainType","modelSchema","recursiveDomainType"],"mappings":"wPAAAA,CAAAA,oBAAoB,CAAC,CAAG,CAACC,EAAS,EAAS,KAC1C,IAAI,EAAS,CAAC,EAAM,KACnB,IAAI,IAAI,KAAO,EACXD,oBAAoB,CAAC,CAAC,EAAM,IAAQ,CAACA,oBAAoB,CAAC,CAACC,EAAS,IACtEC,OAAO,cAAc,CAACD,EAAS,EAAK,CAAE,WAAY,GAAM,CAAC,EAAK,CAAE,CAAI,CAAC,EAAI,AAAC,EAG7E,EACA,EAAO,EAAS,OAChB,EAAO,EAAQ,QAChB,ECVAD,oBAAoB,CAAC,CAAG,CAAC,EAAK,IAAUE,OAAO,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,EAAK,GCClFF,oBAAoB,CAAC,CAAG,AAACC,IACrB,AAAkB,IAAlB,OAAOE,QAA0BA,OAAO,WAAW,EACrDD,OAAO,cAAc,CAACD,EAASE,OAAO,WAAW,CAAE,CAAE,MAAO,QAAS,GAEtED,OAAO,cAAc,CAACD,EAAS,aAAc,CAAE,MAAO,EAAK,EAC5D,E,0WCJMG,aAAeC,AAAAA,GAAAA,uBAAAA,cAAAA,AAAAA,EAAe,UAEvBC,8BAAgCF,aAAa,KAAK,CAAC,eCD1DG,cAAgBF,AAAAA,GAAAA,uBAAAA,cAAAA,AAAAA,EAAe,WAExBG,wBAA0BD,cAAc,KAAK,CAGvD,gBCFI,SAASE,UACZC,CAAS,CACTC,CAAqC,EAErC,IAAMC,EAASF,CAAM,CAAC,YAAY,CAAC,QAAQ,CAACC,GAE5C,GAAIC,aAAkBC,QAClB,MAAM,IAAIP,8BAA8B,CAAEI,OAAAA,EAAQC,MAAAA,CAAM,GAG5D,GAAI,WAAYC,GAAUA,EAAO,MAAM,CACnC,MAAM,IAAIJ,wBAAwB,CAAE,cAAeI,EAAO,MAAM,CAAED,MAAAA,CAAM,GAG5E,OAAOC,EAAO,KAAK,AACvB,CClBA,MAAME,iBAAmB,AACrBC,IAIA,IAAMC,EAAcb,SAEdc,EAAuB,AAACC,GACnB,CAAC,CAACA,GAAU,AAAkB,UAAlB,OAAOA,GAAuBF,KAAeE,CAGpE,OAAMC,EACF,OAAuB,OAASJ,EAAeE,EAAsB,AAErE,aAAYG,CAA+C,CAAE,CAGzD,MAAMC,EAAkBZ,UAFR,IAAI,CAAC,WAAW,CAAuB,MAAM,CAEnBW,GAE1C,GAAIC,GAAmB,AAA2B,UAA3B,OAAOA,EAC1B,IAAK,KAAM,CAACC,EAAKX,EAAM,GAAIT,OAAO,OAAO,CAACmB,GACtCnB,OAAO,cAAc,CAAC,IAAI,CAAEoB,EAAK,CAC7BX,MAAAA,EACA,SAAU,GACV,aAAc,GACd,WAAY,EAChB,QAGJT,OAAO,cAAc,CAAC,IAAI,CAAE,QAAS,CACjC,MAAOmB,EACP,SAAU,GACV,aAAc,GACd,WAAY,EAChB,GAGJnB,OAAO,cAAc,CAAC,IAAI,CAAEc,EAAa,CACrC,MAAO,GACP,SAAU,GACV,aAAc,EAClB,EACJ,CACJ,CAEA,OAAOG,CASX,EAEaI,WAAa,AACtBC,GAEOV,iBAAiB,IAAMU,GAGrBC,oBAAsB,AAC/BD,GAEOV,iBAAiBU,G"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{errorNamespace as e}from"@domain-first/errors";let r=e("SCHEMA").error("ASYNC_LOGIC"),t=e("PARSING").error("INVALID_DATA"),i=e=>{let i=Symbol(),s=e=>!!e&&"object"==typeof e&&i in e;class o{static schema=e(s);constructor(e){let s=function(e,i){let s=e["~standard"].validate(i);if(s instanceof Promise)throw new r({schema:e,value:i});if("issues"in s&&s.issues)throw new t({parsingIssues:s.issues,value:i});return s.value}(this.constructor.schema,e);if(s&&"object"==typeof s)for(let[e,r]of Object.entries(s))Object.defineProperty(this,e,{value:r,writable:!1,configurable:!1,enumerable:!0});else Object.defineProperty(this,"value",{value:s,writable:!1,configurable:!1,enumerable:!0});Object.defineProperty(this,i,{value:!0,writable:!1,configurable:!1})}}return o},s=e=>i(()=>e),o=e=>i(e);export{r as AsyncSchemaInSyncParsingError,t as InvalidDataParsingError,s as domainType,o as recursiveDomainType};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/errors/async-schema-in-sync-parsing-error.ts","../src/errors/invalid-data-parsing-error.ts","../src/
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/errors/async-schema-in-sync-parsing-error.ts","../src/errors/invalid-data-parsing-error.ts","../src/domain-type.ts","../src/utils/parse-sync.ts"],"sourcesContent":["import { errorNamespace } from '@domain-first/errors';\n\nconst SchemaErrors = errorNamespace('SCHEMA')\n\nexport const AsyncSchemaInSyncParsingError = SchemaErrors.error('ASYNC_LOGIC');\n","import { errorNamespace } from '@domain-first/errors';\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\n\nconst ParsingErrors = errorNamespace(\"PARSING\")\n\nexport const InvalidDataParsingError = ParsingErrors.error<{\n parsingIssues: readonly StandardSchemaV1.Issue[];\n value: unknown;\n}>('INVALID_DATA');\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport { type DeepReadonly, parseSync } from './utils';\n\nconst createDomainType = <ModelSchema extends StandardSchemaV1>(\n getModelSchema: (\n isCurrentEntity: (target: unknown) => boolean\n ) => ModelSchema\n) => {\n const classSymbol = Symbol();\n\n const isInstanceOfThisType = (target: unknown): boolean => {\n return !!target && typeof target === 'object' && classSymbol in target;\n };\n\n class DomainType {\n public static readonly schema = getModelSchema(isInstanceOfThisType);\n\n constructor(model: StandardSchemaV1.InferInput<ModelSchema>) {\n const schema = (this.constructor as typeof DomainType).schema;\n\n const parsedModelData = parseSync(schema, model);\n\n if (parsedModelData && typeof parsedModelData === 'object') {\n for (const [key, value] of Object.entries(parsedModelData)) {\n Object.defineProperty(this, key, {\n value,\n writable: false,\n configurable: false,\n enumerable: true\n });\n }\n } else {\n Object.defineProperty(this, 'value', {\n value: parsedModelData,\n writable: false,\n configurable: false,\n enumerable: true\n });\n }\n\n Object.defineProperty(this, classSymbol, {\n value: true,\n writable: false,\n configurable: false\n });\n }\n }\n\n return DomainType as unknown as (abstract new (\n model: StandardSchemaV1.InferInput<ModelSchema>\n ) => DeepReadonly<\n StandardSchemaV1.InferOutput<ModelSchema> extends object\n ? StandardSchemaV1.InferOutput<ModelSchema>\n : {\n value: StandardSchemaV1.InferOutput<ModelSchema>;\n }\n >) & { schema: ModelSchema };\n};\n\nexport const domainType = <ModelSchema extends StandardSchemaV1>(\n modelSchema: ModelSchema\n) => {\n return createDomainType(() => modelSchema);\n};\n\nexport const recursiveDomainType = <ModelSchema extends StandardSchemaV1>(\n modelSchema: (isCurrentType: (target: unknown) => boolean) => ModelSchema\n) => {\n return createDomainType(modelSchema);\n};\n","import type { StandardSchemaV1 } from '@standard-schema/spec';\nimport {\n AsyncSchemaInSyncParsingError,\n InvalidDataParsingError\n} from '../errors';\n\nexport function parseSync<S extends StandardSchemaV1>(\n schema: S,\n value: StandardSchemaV1.InferInput<S>\n): StandardSchemaV1.InferOutput<S> {\n const result = schema['~standard'].validate(value);\n\n if (result instanceof Promise) {\n throw new AsyncSchemaInSyncParsingError({ schema, value });\n }\n\n if ('issues' in result && result.issues) {\n throw new InvalidDataParsingError({ parsingIssues: result.issues, value });\n }\n\n return result.value;\n}\n"],"names":["AsyncSchemaInSyncParsingError","SchemaErrors","errorNamespace","InvalidDataParsingError","ParsingErrors","createDomainType","getModelSchema","classSymbol","Symbol","isInstanceOfThisType","target","DomainType","model","parsedModelData","parseSync","schema","value","result","Promise","key","Object","domainType","modelSchema","recursiveDomainType"],"mappings":"sDAIO,IAAMA,EAAgCC,AAFxBC,EAAe,UAEsB,KAAK,CAAC,eCCnDC,EAA0BC,AAFjBF,EAAe,WAEgB,KAAK,CAGvD,gBCLGG,EAAmB,AACrBC,IAIA,IAAMC,EAAcC,SAEdC,EAAuB,AAACC,GACnB,CAAC,CAACA,GAAU,AAAkB,UAAlB,OAAOA,GAAuBH,KAAeG,CAGpE,OAAMC,EACF,OAAuB,OAASL,EAAeG,EAAsB,AAErE,aAAYG,CAA+C,CAAE,CAGzD,IAAMC,EAAkBC,ACd7B,SACHC,CAAS,CACTC,CAAqC,EAErC,IAAMC,EAASF,CAAM,CAAC,YAAY,CAAC,QAAQ,CAACC,GAE5C,GAAIC,aAAkBC,QAClB,MAAM,IAAIlB,EAA8B,CAAEe,OAAAA,EAAQC,MAAAA,CAAM,GAG5D,GAAI,WAAYC,GAAUA,EAAO,MAAM,CACnC,MAAM,IAAId,EAAwB,CAAE,cAAec,EAAO,MAAM,CAAED,MAAAA,CAAM,GAG5E,OAAOC,EAAO,KAAK,AACvB,EDH4B,IAAI,CAAC,WAAW,CAAuB,MAAM,CAEnBL,GAE1C,GAAIC,GAAmB,AAA2B,UAA3B,OAAOA,EAC1B,IAAK,GAAM,CAACM,EAAKH,EAAM,GAAII,OAAO,OAAO,CAACP,GACtCO,OAAO,cAAc,CAAC,IAAI,CAAED,EAAK,CAC7BH,MAAAA,EACA,SAAU,GACV,aAAc,GACd,WAAY,EAChB,QAGJI,OAAO,cAAc,CAAC,IAAI,CAAE,QAAS,CACjC,MAAOP,EACP,SAAU,GACV,aAAc,GACd,WAAY,EAChB,GAGJO,OAAO,cAAc,CAAC,IAAI,CAAEb,EAAa,CACrC,MAAO,GACP,SAAU,GACV,aAAc,EAClB,EACJ,CACJ,CAEA,OAAOI,CASX,EAEaU,EAAa,AACtBC,GAEOjB,EAAiB,IAAMiB,GAGrBC,EAAsB,AAC/BD,GAEOjB,EAAiBiB,U"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@domain-first/types",
|
|
3
|
-
"description": "Type-
|
|
3
|
+
"description": "Type-safe domain models powered by Standard Schema validation",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"entity",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"valibot"
|
|
17
17
|
],
|
|
18
18
|
"homepage": "https://codesandbox.io/p/devbox/async-cache-d8crnx?file=%2Findex.js%3A8%2C65",
|
|
19
|
-
"version": "
|
|
19
|
+
"version": "4.0.0",
|
|
20
20
|
"type": "module",
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"zod": "^4.4.3"
|
|
62
62
|
},
|
|
63
63
|
"dependencies": {
|
|
64
|
-
"@domain-first/errors": "^
|
|
64
|
+
"@domain-first/errors": "^3.0.0",
|
|
65
65
|
"@standard-schema/spec": "^1.1.0"
|
|
66
66
|
}
|
|
67
67
|
}
|
package/dist/entity.d.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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) => (abstract new (id: StandardSchemaV1.InferInput<IdSchema>, model: StandardSchemaV1.InferInput<ModelSchema>) => {
|
|
4
|
-
get model(): DeepReadonly<StandardSchemaV1.InferOutput<ModelSchema>>;
|
|
5
|
-
get id(): DeepReadonly<StandardSchemaV1.InferOutput<IdSchema>>;
|
|
6
|
-
}) & {
|
|
7
|
-
readonly idSchema: IdSchema;
|
|
8
|
-
readonly modelSchema: ModelSchema;
|
|
9
|
-
};
|
|
10
|
-
export declare const defineRecursiveEntity: <IdSchema extends StandardSchemaV1, ModelSchema extends StandardSchemaV1>(idSchema: IdSchema, modelSchema: (isCurrentEntity: (target: unknown) => boolean) => ModelSchema) => (abstract new (id: StandardSchemaV1.InferInput<IdSchema>, model: StandardSchemaV1.InferInput<ModelSchema>) => {
|
|
11
|
-
get model(): DeepReadonly<StandardSchemaV1.InferOutput<ModelSchema>>;
|
|
12
|
-
get id(): DeepReadonly<StandardSchemaV1.InferOutput<IdSchema>>;
|
|
13
|
-
}) & {
|
|
14
|
-
readonly idSchema: IdSchema;
|
|
15
|
-
readonly modelSchema: ModelSchema;
|
|
16
|
-
};
|
|
17
|
-
export type InferEntityModel<T> = T extends {
|
|
18
|
-
modelSchema: StandardSchemaV1;
|
|
19
|
-
} ? StandardSchemaV1.InferOutput<T['modelSchema']> : T extends {
|
|
20
|
-
model: infer M;
|
|
21
|
-
} ? M : never;
|
|
22
|
-
export type InferEntityId<T> = T extends {
|
|
23
|
-
idSchema: StandardSchemaV1;
|
|
24
|
-
} ? StandardSchemaV1.InferOutput<T['idSchema']> : T extends {
|
|
25
|
-
id: infer M;
|
|
26
|
-
} ? M : never;
|
package/dist/value-object.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import { type DeepReadonly } from './utils';
|
|
3
|
-
export declare const defineValueObject: <Schema extends StandardSchemaV1>(schema: Schema) => (abstract new (data: StandardSchemaV1.InferInput<Schema>) => {
|
|
4
|
-
get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
|
|
5
|
-
}) & {
|
|
6
|
-
readonly schema: Schema;
|
|
7
|
-
};
|
|
8
|
-
export declare const defineRecursiveValueObject: <Schema extends StandardSchemaV1>(schema: (isCurrentValueObject: (target: unknown) => boolean) => Schema) => (abstract new (data: StandardSchemaV1.InferInput<Schema>) => {
|
|
9
|
-
get model(): DeepReadonly<StandardSchemaV1.InferOutput<Schema>>;
|
|
10
|
-
}) & {
|
|
11
|
-
readonly schema: Schema;
|
|
12
|
-
};
|
|
13
|
-
export type InferValueObjectSchema<T> = T extends {
|
|
14
|
-
schema: infer S extends StandardSchemaV1;
|
|
15
|
-
} ? StandardSchemaV1.InferOutput<S> : T extends {
|
|
16
|
-
model: infer M;
|
|
17
|
-
} ? M : never;
|
|
File without changes
|