@avandar/models 0.1.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/LICENSE +21 -0
- package/README.md +118 -0
- package/dist/index.d.ts +107 -0
- package/dist/index.js +46 -0
- package/dist/index.js.map +1 -0
- package/dist/zod.d.ts +24 -0
- package/dist/zod.js +14 -0
- package/dist/zod.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Avandar Labs, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
# @avandar/models
|
|
2
|
+
|
|
3
|
+
A lightweight library for representing typed, discriminated data models.
|
|
4
|
+
The only export is `Model`, a small bundle of helpers built around the
|
|
5
|
+
`__type` discriminator convention.
|
|
6
|
+
|
|
7
|
+
A model is just an object with a `__type` string and arbitrary additional
|
|
8
|
+
properties. The value of this library isn't in enforcing any clever
|
|
9
|
+
representation — it's the shared convention that lets the rest of the
|
|
10
|
+
Avandar packages (`@avandar/clients`, `@avandar/query-hooks`) auto-generate
|
|
11
|
+
parsers, CRUD clients, and React Query hooks against any model.
|
|
12
|
+
|
|
13
|
+
ESM only. Requires Node 22+.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
pnpm add @avandar/models
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`zod` is an optional peer dependency, needed only if you import
|
|
22
|
+
`@avandar/models/zod`.
|
|
23
|
+
|
|
24
|
+
## Entry points
|
|
25
|
+
|
|
26
|
+
| Entry | Contents |
|
|
27
|
+
| --------------------- | -------------------------------------------------- |
|
|
28
|
+
| `@avandar/models` | `Model` — the value helpers and the type namespace |
|
|
29
|
+
| `@avandar/models/zod` | Zod schema builders for models |
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
import { Model } from "@avandar/models";
|
|
35
|
+
|
|
36
|
+
// Create a model instance
|
|
37
|
+
const user = Model.make("User", { id: "u1", name: "Alice" });
|
|
38
|
+
|
|
39
|
+
// Pattern-match on a discriminated union of models
|
|
40
|
+
const label = Model.match(model, {
|
|
41
|
+
User: (m) => m.name,
|
|
42
|
+
Admin: (m) => `admin:${m.level}`,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// Extract the id coupled with the model type
|
|
46
|
+
const typedId = Model.getTypedId(user); // { id: 'u1', __type: 'User' }
|
|
47
|
+
|
|
48
|
+
// Runtime type guards
|
|
49
|
+
Model.isModel(value); // true if value has a `__type` string
|
|
50
|
+
Model.isOfModelType(value, "User"); // true if `__type === "User"`
|
|
51
|
+
|
|
52
|
+
// Higher-order guard for use with `.filter`, `.find`, etc.
|
|
53
|
+
users.filter(Model.valIsOfModelType("Admin"));
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### Constructors
|
|
59
|
+
|
|
60
|
+
#### `Model.make(modelType, modelProps)`
|
|
61
|
+
|
|
62
|
+
Returns `{ __type: modelType, ...modelProps }`. The return type carries the
|
|
63
|
+
literal `__type` so unions of models remain discriminated.
|
|
64
|
+
|
|
65
|
+
### Pattern matching
|
|
66
|
+
|
|
67
|
+
#### `Model.match(model, fns)`
|
|
68
|
+
|
|
69
|
+
Pattern-matches a model union by its `__type`, calling the corresponding
|
|
70
|
+
function. Throws if no branch matches.
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
type Shape = Model.Base<"Circle", { r: number }> | Model.Base<"Square", { s: number }>;
|
|
74
|
+
const area = Model.match(shape, {
|
|
75
|
+
Circle: ({ r }) => Math.PI * r * r,
|
|
76
|
+
Square: ({ s }) => s * s,
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Identity helpers
|
|
81
|
+
|
|
82
|
+
#### `Model.getTypedId(model)`
|
|
83
|
+
|
|
84
|
+
Returns `{ __type, id }` from a model with an `id` property, discarding
|
|
85
|
+
every other property. Useful for keying lookups or passing references.
|
|
86
|
+
|
|
87
|
+
### Type guards
|
|
88
|
+
|
|
89
|
+
| Function | Returns |
|
|
90
|
+
| ------------------------------------- | ------------------------------------------- |
|
|
91
|
+
| `Model.isModel(val)` | `val is Model.Base` |
|
|
92
|
+
| `Model.isOfModelType(val, modelType)` | `val is Model.Base<modelType>` |
|
|
93
|
+
| `Model.valIsOfModelType(modelType)` | Curried guard for `.filter` / `.find` calls |
|
|
94
|
+
|
|
95
|
+
## Types
|
|
96
|
+
|
|
97
|
+
| Type | Description |
|
|
98
|
+
| ----------------- | ---------------------------------------------------------------------------- |
|
|
99
|
+
| `Model.Base<T,P>` | Base model shape: `{ __type: T } & P` (defaults: `string`, `EmptyObject`) |
|
|
100
|
+
| `Model.Versioned` | Base model extended with a numeric `version` field |
|
|
101
|
+
| `Model.Type<M>` | Utility type: extracts the `__type` string literal from a model |
|
|
102
|
+
| `Model.TypedId<M>`| Utility type: `{ __type, id }` picked from a model with an `id` property |
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
## Dependencies
|
|
106
|
+
|
|
107
|
+
### Runtime
|
|
108
|
+
|
|
109
|
+
- **type-fest** — utility types (types only, zero runtime cost)
|
|
110
|
+
|
|
111
|
+
### Development
|
|
112
|
+
|
|
113
|
+
- **vitest** — test runner
|
|
114
|
+
- **typescript** — type checking
|
|
115
|
+
|
|
116
|
+
## License
|
|
117
|
+
|
|
118
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { EmptyObject, Simplify, DistributedPick, UnionToIntersection } from 'type-fest';
|
|
2
|
+
|
|
3
|
+
/** The internal key stored in a model object which holds its string type. */
|
|
4
|
+
type ModelTypeKey = "__type";
|
|
5
|
+
type ModelBase<MType extends string = string, MProps extends Record<string, unknown> = EmptyObject> = MProps extends EmptyObject ? {
|
|
6
|
+
__type: MType;
|
|
7
|
+
} : Simplify<{
|
|
8
|
+
__type: MType;
|
|
9
|
+
} & MProps>;
|
|
10
|
+
type ModelVersioned<MType extends string = string, Version extends number = number, MProps extends Record<string, unknown> = EmptyObject> = Simplify<ModelBase<MType, MProps> & {
|
|
11
|
+
version: Version;
|
|
12
|
+
}>;
|
|
13
|
+
type ModelType<M extends ModelBase<string>> = M[ModelTypeKey];
|
|
14
|
+
type ModelTypedId<M extends ModelBase<string> & {
|
|
15
|
+
id: unknown;
|
|
16
|
+
}> = DistributedPick<M, ModelTypeKey | "id">;
|
|
17
|
+
|
|
18
|
+
type ValidMatchedValues<M extends ModelBase> = ((model: M) => unknown) | string | number | boolean | Record<PropertyKey, unknown>;
|
|
19
|
+
type ValidValueRecord<M extends ModelBase> = UnionToIntersection<M extends any ? {
|
|
20
|
+
[Mod in M as ModelType<Mod>]: ValidMatchedValues<Mod>;
|
|
21
|
+
} : never>;
|
|
22
|
+
type MappedValue<M extends ModelBase, FunctionRecord extends ValidValueRecord<M>> = M extends any ? ModelType<M> extends keyof FunctionRecord ? FunctionRecord[ModelType<M>] extends (model: M) => infer R ? R : FunctionRecord[ModelType<M>] : never : never;
|
|
23
|
+
type MatchModelFn = <M extends ModelBase, FunctionRecord extends ValidValueRecord<M>>(model: M, fns: FunctionRecord) => MappedValue<M, FunctionRecord>;
|
|
24
|
+
|
|
25
|
+
type IModelModule = {
|
|
26
|
+
/**
|
|
27
|
+
* Make a new model instance.
|
|
28
|
+
* @param modelType - The type of the model to make.
|
|
29
|
+
* @param modelProps - The properties of the model to make.
|
|
30
|
+
* @returns The model.
|
|
31
|
+
*/
|
|
32
|
+
make: <MType extends string, const MProps extends Record<string, unknown> = EmptyObject>(modelType: MType, modelProps: MProps) => ModelBase<MType, MProps>;
|
|
33
|
+
/**
|
|
34
|
+
* Match a model instance to a function based on its type.
|
|
35
|
+
* This is useful for when you have a union of model types and want to do
|
|
36
|
+
* something different depending on the type of the model.
|
|
37
|
+
*
|
|
38
|
+
* Example:
|
|
39
|
+
* ```ts
|
|
40
|
+
* // assume we have received a model which could be either User or Admin
|
|
41
|
+
* // which are each of type Model<"User"> and Model<"Admin"> respectively
|
|
42
|
+
* const model: User | Admin;
|
|
43
|
+
*
|
|
44
|
+
* const result = Models.match(model, {
|
|
45
|
+
* User: (model) => doSomething();
|
|
46
|
+
* Admin: (model) => doSomethingElse();
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
* @param model - The model instance to pattern match.
|
|
50
|
+
* @param fns - A record of functions, mapping a model type to a function.
|
|
51
|
+
* @returns The result of the function that matched the model instance.
|
|
52
|
+
*/
|
|
53
|
+
match: MatchModelFn;
|
|
54
|
+
/**
|
|
55
|
+
* Get the typed id of a model (i.e. an object that couples the model id
|
|
56
|
+
* with its model type).
|
|
57
|
+
*
|
|
58
|
+
* @param model - The model to get the typed id of.
|
|
59
|
+
* @returns The typed id of the model.
|
|
60
|
+
*/
|
|
61
|
+
getTypedId: <M extends ModelBase<string> & {
|
|
62
|
+
id: unknown;
|
|
63
|
+
}>(model: M) => Simplify<ModelTypedId<M>>;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if a value is a model object of any type.
|
|
66
|
+
* @param val - The value to check.
|
|
67
|
+
* @returns `true` if the value is a model, `false` otherwise.
|
|
68
|
+
*/
|
|
69
|
+
isModel: (val: unknown) => val is ModelBase;
|
|
70
|
+
/**
|
|
71
|
+
* Checks if a value is a model of the given type.
|
|
72
|
+
* @param val - The value to check.
|
|
73
|
+
* @param modelType - The model type to check.
|
|
74
|
+
*/
|
|
75
|
+
isOfModelType: <T extends string>(val: unknown, modelType: T) => val is ModelBase<T>;
|
|
76
|
+
/**
|
|
77
|
+
* Returns a function that checks if a value is a model of the given type.
|
|
78
|
+
* @param modelType - The model type to check.
|
|
79
|
+
* @returns A function that checks if a value is a model of the given type.
|
|
80
|
+
*/
|
|
81
|
+
valIsOfModelType: <MType extends string>(modelType: MType) => <MaybeModel extends ModelBase<string>>(v: MaybeModel | null | undefined) => v is MaybeModel & ModelBase<MType>;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
declare const Model: IModelModule;
|
|
85
|
+
declare namespace Model {
|
|
86
|
+
type Base<MType extends string = string, MProps extends Record<string, unknown> = EmptyObject> = ModelBase<MType, MProps>;
|
|
87
|
+
/**
|
|
88
|
+
* A model that has a version field.
|
|
89
|
+
*/
|
|
90
|
+
type Versioned<MType extends string = string, Version extends number = number, MProps extends Record<string, unknown> = EmptyObject> = ModelVersioned<MType, Version, MProps>;
|
|
91
|
+
/** Utility type: gets the string type of a model. */
|
|
92
|
+
type Type<M extends Base<string>> = ModelType<M>;
|
|
93
|
+
/**
|
|
94
|
+
* Get the `id` type of a model coupled with the model type key.
|
|
95
|
+
*
|
|
96
|
+
* Example:
|
|
97
|
+
* ```ts
|
|
98
|
+
* type MyType = TypedId<Model<"User">>;
|
|
99
|
+
* // TypedId = { __type: "User", id: UserId }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
type TypedId<M extends Base<string> & {
|
|
103
|
+
id: unknown;
|
|
104
|
+
}> = ModelTypedId<M>;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export { Model };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// src/Model/matchModel/matchModel.ts
|
|
2
|
+
function matchModel(model, fns) {
|
|
3
|
+
const mType = model.__type;
|
|
4
|
+
if (mType in fns && fns[mType] !== void 0) {
|
|
5
|
+
if (typeof fns[mType] === "function") {
|
|
6
|
+
return fns[mType](model);
|
|
7
|
+
}
|
|
8
|
+
return fns[mType];
|
|
9
|
+
}
|
|
10
|
+
throw new Error(`No match found for model type: ${mType}`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// src/Model/ModelModule/ModelModule.ts
|
|
14
|
+
var ModelModule = {
|
|
15
|
+
make: (modelType, modelProps) => {
|
|
16
|
+
return {
|
|
17
|
+
__type: modelType,
|
|
18
|
+
...modelProps
|
|
19
|
+
};
|
|
20
|
+
},
|
|
21
|
+
match: matchModel,
|
|
22
|
+
getTypedId: (model) => {
|
|
23
|
+
return {
|
|
24
|
+
__type: model.__type,
|
|
25
|
+
id: model.id
|
|
26
|
+
};
|
|
27
|
+
},
|
|
28
|
+
isModel: (val) => {
|
|
29
|
+
return typeof val === "object" && val !== null && "__type" in val && typeof val.__type === "string";
|
|
30
|
+
},
|
|
31
|
+
isOfModelType: (val, modelType) => {
|
|
32
|
+
return typeof val === "object" && val !== null && "__type" in val && typeof val.__type === "string" && (modelType === void 0 || val.__type === modelType);
|
|
33
|
+
},
|
|
34
|
+
valIsOfModelType: (modelType) => {
|
|
35
|
+
return (v) => {
|
|
36
|
+
return ModelModule.isOfModelType(v, modelType);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/Model/Model.ts
|
|
42
|
+
var Model = ModelModule;
|
|
43
|
+
|
|
44
|
+
export { Model };
|
|
45
|
+
//# sourceMappingURL=index.js.map
|
|
46
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/Model/matchModel/matchModel.ts","../src/Model/ModelModule/ModelModule.ts","../src/Model/Model.ts"],"names":[],"mappings":";AA4CO,SAAS,UAAA,CAGd,OAAU,GAAA,EAAqD;AAC/D,EAAA,MAAM,QAAQ,KAAA,CAAM,MAAA;AACpB,EAAA,IAAI,KAAA,IAAS,GAAA,IAAO,GAAA,CAAI,KAAK,MAAM,MAAA,EAAW;AAC5C,IAAA,IAAI,OAAO,GAAA,CAAI,KAAK,CAAA,KAAM,UAAA,EAAY;AACpC,MAAA,OAAO,GAAA,CAAI,KAAK,CAAA,CAAE,KAAK,CAAA;AAAA,IACzB;AACA,IAAA,OAAO,IAAI,KAAK,CAAA;AAAA,EAClB;AACA,EAAA,MAAM,IAAI,KAAA,CAAM,CAAA,+BAAA,EAAkC,KAAK,CAAA,CAAE,CAAA;AAC3D;;;AC0BO,IAAM,WAAA,GAA4B;AAAA,EACvC,IAAA,EAAM,CAIJ,SAAA,EACA,UAAA,KAC6B;AAC7B,IAAA,OAAO;AAAA,MACL,MAAA,EAAQ,SAAA;AAAA,MACR,GAAG;AAAA,KACL;AAAA,EACF,CAAA;AAAA,EAEA,KAAA,EAAO,UAAA;AAAA,EAEP,UAAA,EAAY,CACV,KAAA,KACoB;AACpB,IAAA,OAAO;AAAA,MACL,QAAQ,KAAA,CAAM,MAAA;AAAA,MACd,IAAI,KAAA,CAAM;AAAA,KACZ;AAAA,EACF,CAAA;AAAA,EAEA,OAAA,EAAS,CAAC,GAAA,KAAmC;AAC3C,IAAA,OACE,OAAO,QAAQ,QAAA,IACf,GAAA,KAAQ,QACR,QAAA,IAAY,GAAA,IACZ,OAAO,GAAA,CAAI,MAAA,KAAW,QAAA;AAAA,EAE1B,CAAA;AAAA,EAEA,aAAA,EAAe,CACb,GAAA,EACA,SAAA,KACyC;AACzC,IAAA,OACE,OAAO,GAAA,KAAQ,QAAA,IACf,GAAA,KAAQ,QACR,QAAA,IAAY,GAAA,IACZ,OAAO,GAAA,CAAI,MAAA,KAAW,QAAA,KACrB,SAAA,KAAc,MAAA,IAAa,IAAI,MAAA,KAAW,SAAA,CAAA;AAAA,EAE/C,CAAA;AAAA,EAEA,gBAAA,EAAkB,CAChB,SAAA,KAG0C;AAC1C,IAAA,OAAO,CAAC,CAAA,KAAM;AACZ,MAAA,OAAO,WAAA,CAAY,aAAA,CAAc,CAAA,EAAG,SAAS,CAAA;AAAA,IAC/C,CAAA;AAAA,EACF;AACF,CAAA;;;AChIO,IAAM,KAAA,GAAQ","file":"index.js","sourcesContent":["/* eslint-disable @typescript-eslint/no-explicit-any */\nimport type { ModelBase, ModelType } from \"@models/Model/Model.types.ts\";\nimport type { UnionToIntersection } from \"type-fest\";\n\ntype ValidMatchedValues<M extends ModelBase> =\n | ((model: M) => unknown)\n | string\n | number\n | boolean\n // Spelled out rather than imported as `UnknownObject` from `@avandar/utils`.\n // Pulling that one alias in from the utils barrel degraded inference here:\n // the handler parameters in `matchModel(x, { User: (model) => ... })` fell\n // back to `any`. This is the exact definition of `UnknownObject`, and the\n // type is used once, so the import bought nothing.\n | Record<PropertyKey, unknown>;\n\ntype ValidValueRecord<M extends ModelBase> = UnionToIntersection<\n M extends any ?\n {\n [Mod in M as ModelType<Mod>]: ValidMatchedValues<Mod>;\n }\n : never\n>;\n\ntype MappedValue<\n M extends ModelBase,\n FunctionRecord extends ValidValueRecord<M>,\n> =\n M extends any ?\n ModelType<M> extends keyof FunctionRecord ?\n FunctionRecord[ModelType<M>] extends (model: M) => infer R ?\n R\n : FunctionRecord[ModelType<M>]\n : never\n : never;\n\nexport type MatchModelFn = <\n M extends ModelBase,\n FunctionRecord extends ValidValueRecord<M>,\n>(\n model: M,\n fns: FunctionRecord,\n) => MappedValue<M, FunctionRecord>;\n\nexport function matchModel<\n M extends ModelBase,\n FunctionRecord extends ValidValueRecord<M>,\n>(model: M, fns: FunctionRecord): MappedValue<M, FunctionRecord> {\n const mType = model.__type;\n if (mType in fns && fns[mType] !== undefined) {\n if (typeof fns[mType] === \"function\") {\n return fns[mType](model) as MappedValue<M, FunctionRecord>;\n }\n return fns[mType] as MappedValue<M, FunctionRecord>;\n }\n throw new Error(`No match found for model type: ${mType}`);\n}\n","import { matchModel } from \"@models/Model/matchModel/matchModel.ts\";\nimport type { MatchModelFn } from \"@models/Model/matchModel/matchModel.ts\";\nimport type { ModelBase, ModelTypedId } from \"@models/Model/Model.types.ts\";\nimport type { EmptyObject, Simplify } from \"type-fest\";\n\nexport type IModelModule = {\n /**\n * Make a new model instance.\n * @param modelType - The type of the model to make.\n * @param modelProps - The properties of the model to make.\n * @returns The model.\n */\n make: <\n MType extends string,\n const MProps extends Record<string, unknown> = EmptyObject,\n >(\n modelType: MType,\n modelProps: MProps,\n ) => ModelBase<MType, MProps>;\n\n /**\n * Match a model instance to a function based on its type.\n * This is useful for when you have a union of model types and want to do\n * something different depending on the type of the model.\n *\n * Example:\n * ```ts\n * // assume we have received a model which could be either User or Admin\n * // which are each of type Model<\"User\"> and Model<\"Admin\"> respectively\n * const model: User | Admin;\n *\n * const result = Models.match(model, {\n * User: (model) => doSomething();\n * Admin: (model) => doSomethingElse();\n * });\n * ```\n * @param model - The model instance to pattern match.\n * @param fns - A record of functions, mapping a model type to a function.\n * @returns The result of the function that matched the model instance.\n */\n match: MatchModelFn;\n\n /**\n * Get the typed id of a model (i.e. an object that couples the model id\n * with its model type).\n *\n * @param model - The model to get the typed id of.\n * @returns The typed id of the model.\n */\n getTypedId: <M extends ModelBase<string> & { id: unknown }>(\n model: M,\n ) => Simplify<ModelTypedId<M>>;\n\n /**\n * Checks if a value is a model object of any type.\n * @param val - The value to check.\n * @returns `true` if the value is a model, `false` otherwise.\n */\n isModel: (val: unknown) => val is ModelBase;\n\n /**\n * Checks if a value is a model of the given type.\n * @param val - The value to check.\n * @param modelType - The model type to check.\n */\n isOfModelType: <T extends string>(\n val: unknown,\n modelType: T,\n ) => val is ModelBase<T>;\n\n /**\n * Returns a function that checks if a value is a model of the given type.\n * @param modelType - The model type to check.\n * @returns A function that checks if a value is a model of the given type.\n */\n valIsOfModelType: <MType extends string>(\n modelType: MType,\n ) => <MaybeModel extends ModelBase<string>>(\n v: MaybeModel | null | undefined,\n ) => v is MaybeModel & ModelBase<MType>;\n};\n\nexport const ModelModule: IModelModule = {\n make: <\n MType extends string,\n MProps extends Record<string, unknown> = EmptyObject,\n >(\n modelType: MType,\n modelProps: MProps,\n ): ModelBase<MType, MProps> => {\n return {\n __type: modelType,\n ...modelProps,\n } as ModelBase<MType, MProps>;\n },\n\n match: matchModel,\n\n getTypedId: <M extends ModelBase<string> & { id: unknown }>(\n model: M,\n ): ModelTypedId<M> => {\n return {\n __type: model.__type,\n id: model.id,\n } as ModelTypedId<M>;\n },\n\n isModel: (val: unknown): val is ModelBase => {\n return (\n typeof val === \"object\" &&\n val !== null &&\n \"__type\" in val &&\n typeof val.__type === \"string\"\n );\n },\n\n isOfModelType: <MType extends string, MaybeModel>(\n val: MaybeModel,\n modelType: MType,\n ): val is MaybeModel & ModelBase<MType> => {\n return (\n typeof val === \"object\" &&\n val !== null &&\n \"__type\" in val &&\n typeof val.__type === \"string\" &&\n (modelType === undefined || val.__type === modelType)\n );\n },\n\n valIsOfModelType: <MType extends string>(\n modelType: MType,\n ): (<MaybeModel extends ModelBase<string>>(\n v: MaybeModel | null | undefined,\n ) => v is MaybeModel & ModelBase<MType>) => {\n return (v) => {\n return ModelModule.isOfModelType(v, modelType);\n };\n },\n};\n","/* eslint-disable @typescript-eslint/no-namespace,import-x/export */\nimport { ModelModule } from \"@models/Model/ModelModule/ModelModule.ts\";\nimport type {\n ModelBase,\n ModelType,\n ModelTypedId,\n ModelVersioned,\n} from \"@models/Model/Model.types.ts\";\nimport type { EmptyObject } from \"type-fest\";\n\nexport const Model = ModelModule;\n\nexport namespace Model {\n export type Base<\n MType extends string = string,\n MProps extends Record<string, unknown> = EmptyObject,\n > = ModelBase<MType, MProps>;\n\n /**\n * A model that has a version field.\n */\n export type Versioned<\n MType extends string = string,\n Version extends number = number,\n MProps extends Record<string, unknown> = EmptyObject,\n > = ModelVersioned<MType, Version, MProps>;\n\n /** Utility type: gets the string type of a model. */\n export type Type<M extends Base<string>> = ModelType<M>;\n\n /**\n * Get the `id` type of a model coupled with the model type key.\n *\n * Example:\n * ```ts\n * type MyType = TypedId<Model<\"User\">>;\n * // TypedId = { __type: \"User\", id: UserId }\n * ```\n */\n export type TypedId<M extends Base<string> & { id: unknown }> =\n ModelTypedId<M>;\n}\n"]}
|
package/dist/zod.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns a Zod schema for a model.
|
|
5
|
+
*
|
|
6
|
+
* This lives in the `@models/zod` subpath rather than the core `@models`
|
|
7
|
+
* export so the core entry stays free of any `zod` dependency. `zod` is a peer
|
|
8
|
+
* dependency of `@avandar/models`, pulled in only by consumers that import
|
|
9
|
+
* from `@models/zod`.
|
|
10
|
+
*
|
|
11
|
+
* @param options - The model definition.
|
|
12
|
+
* @param options.type - The model's discriminant type.
|
|
13
|
+
* @param options.props - Zod schema for the model's properties. Optional;
|
|
14
|
+
* defaults to an empty object.
|
|
15
|
+
* @returns A Zod schema for the model.
|
|
16
|
+
*/
|
|
17
|
+
declare function AvaModelSchema<MType extends string, MPropsSchema extends Record<string, z.ZodType<unknown, unknown>> = Record<string, never>>(options: {
|
|
18
|
+
type: MType;
|
|
19
|
+
props?: MPropsSchema;
|
|
20
|
+
}): z.ZodObject<{
|
|
21
|
+
__type: z.ZodLiteral<MType>;
|
|
22
|
+
} & MPropsSchema>;
|
|
23
|
+
|
|
24
|
+
export { AvaModelSchema };
|
package/dist/zod.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
|
|
3
|
+
// src/zod/index.ts
|
|
4
|
+
function AvaModelSchema(options) {
|
|
5
|
+
const { type, props } = options;
|
|
6
|
+
return z.object({
|
|
7
|
+
__type: z.literal(type),
|
|
8
|
+
...props
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { AvaModelSchema };
|
|
13
|
+
//# sourceMappingURL=zod.js.map
|
|
14
|
+
//# sourceMappingURL=zod.js.map
|
package/dist/zod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/zod/index.ts"],"names":[],"mappings":";;;AAgBO,SAAS,eAMd,OAAA,EAOA;AACA,EAAA,MAAM,EAAE,IAAA,EAAM,KAAA,EAAM,GAAI,OAAA;AAIxB,EAAA,OAAO,EAAE,MAAA,CAAO;AAAA,IACd,MAAA,EAAQ,CAAA,CAAE,OAAA,CAAQ,IAAI,CAAA;AAAA,IACtB,GAAG;AAAA,GACJ,CAAA;AAKH","file":"zod.js","sourcesContent":["import { z } from \"zod\";\n\n/**\n * Returns a Zod schema for a model.\n *\n * This lives in the `@models/zod` subpath rather than the core `@models`\n * export so the core entry stays free of any `zod` dependency. `zod` is a peer\n * dependency of `@avandar/models`, pulled in only by consumers that import\n * from `@models/zod`.\n *\n * @param options - The model definition.\n * @param options.type - The model's discriminant type.\n * @param options.props - Zod schema for the model's properties. Optional;\n * defaults to an empty object.\n * @returns A Zod schema for the model.\n */\nexport function AvaModelSchema<\n MType extends string,\n MPropsSchema extends Record<string, z.ZodType<unknown, unknown>> = Record<\n string,\n never\n >,\n>(options: {\n type: MType;\n props?: MPropsSchema;\n}): z.ZodObject<\n {\n __type: z.ZodLiteral<MType>;\n } & MPropsSchema\n> {\n const { type, props } = options;\n\n // Spreading an optional generic loses the `& MPropsSchema` part of the\n // inferred shape, so assert the fully-typed return explicitly.\n return z.object({\n __type: z.literal(type),\n ...props,\n }) as z.ZodObject<\n {\n __type: z.ZodLiteral<MType>;\n } & MPropsSchema\n >;\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@avandar/models",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Core model primitives for typed, discriminated data objects",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/AvandarLabs/avandar.git",
|
|
9
|
+
"directory": "packages/shared/models"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/AvandarLabs/avandar/tree/main/packages/shared/models#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/AvandarLabs/avandar/issues"
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
},
|
|
21
|
+
"./zod": {
|
|
22
|
+
"types": "./dist/zod.d.ts",
|
|
23
|
+
"default": "./dist/zod.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"type-fest": "^5.4.4",
|
|
32
|
+
"@avandar/utils": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"zod": "^4.1.13"
|
|
36
|
+
},
|
|
37
|
+
"peerDependenciesMeta": {
|
|
38
|
+
"zod": {
|
|
39
|
+
"optional": true
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"tsup": "^8.5.0",
|
|
44
|
+
"typescript": "~5.9.3",
|
|
45
|
+
"vitest": "^3.2.4",
|
|
46
|
+
"zod": "^4.1.13"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=22.0.0"
|
|
50
|
+
},
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsup",
|
|
56
|
+
"test": "vitest run",
|
|
57
|
+
"test:watch": "vitest",
|
|
58
|
+
"type-check": "tsc --noEmit"
|
|
59
|
+
}
|
|
60
|
+
}
|