@nmtjs/type 0.4.7 → 0.5.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/dist/compiler.js +84 -38
- package/dist/compiler.js.map +1 -1
- package/dist/formats.js +1 -1
- package/dist/formats.js.map +1 -1
- package/dist/index.js +53 -22
- package/dist/index.js.map +1 -1
- package/dist/schemas/discriminated-union.js +9 -0
- package/dist/schemas/discriminated-union.js.map +1 -0
- package/dist/schemas/nullable.js +1 -6
- package/dist/schemas/nullable.js.map +1 -1
- package/dist/temporal.js +7 -7
- package/dist/temporal.js.map +1 -1
- package/dist/types/any.js +3 -43
- package/dist/types/any.js.map +1 -1
- package/dist/types/array.js +17 -63
- package/dist/types/array.js.map +1 -1
- package/dist/types/base.js +78 -41
- package/dist/types/base.js.map +1 -1
- package/dist/types/boolean.js +3 -43
- package/dist/types/boolean.js.map +1 -1
- package/dist/types/custom.js +8 -48
- package/dist/types/custom.js.map +1 -1
- package/dist/types/date.js +8 -0
- package/dist/types/date.js.map +1 -0
- package/dist/types/enum.js +10 -94
- package/dist/types/enum.js.map +1 -1
- package/dist/types/literal.js +3 -43
- package/dist/types/literal.js.map +1 -1
- package/dist/types/never.js +3 -26
- package/dist/types/never.js.map +1 -1
- package/dist/types/number.js +52 -186
- package/dist/types/number.js.map +1 -1
- package/dist/types/object.js +10 -131
- package/dist/types/object.js.map +1 -1
- package/dist/types/string.js +25 -65
- package/dist/types/string.js.map +1 -1
- package/dist/types/temporal.js +23 -328
- package/dist/types/temporal.js.map +1 -1
- package/dist/types/union.js +16 -90
- package/dist/types/union.js.map +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +124 -41
- package/src/formats.ts +1 -1
- package/src/index.ts +145 -63
- package/src/schemas/discriminated-union.ts +49 -0
- package/src/schemas/nullable.ts +7 -13
- package/src/temporal.ts +8 -7
- package/src/types/any.ts +6 -46
- package/src/types/array.ts +38 -86
- package/src/types/base.ts +205 -81
- package/src/types/boolean.ts +13 -47
- package/src/types/custom.ts +21 -79
- package/src/types/date.ts +10 -0
- package/src/types/enum.ts +18 -107
- package/src/types/literal.ts +7 -63
- package/src/types/never.ts +6 -36
- package/src/types/number.ts +52 -188
- package/src/types/object.ts +61 -202
- package/src/types/string.ts +25 -61
- package/src/types/temporal.ts +53 -410
- package/src/types/union.ts +98 -138
- package/src/utils.ts +8 -0
- package/dist/constants.js +0 -2
- package/dist/constants.js.map +0 -1
- package/dist/types/datetime.js +0 -53
- package/dist/types/datetime.js.map +0 -1
- package/src/constants.ts +0 -5
- package/src/types/datetime.ts +0 -65
package/dist/types/array.js
CHANGED
|
@@ -1,73 +1,27 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
|
-
import { BaseType
|
|
2
|
+
import { BaseType } from "./base.js";
|
|
3
3
|
export class ArrayType extends BaseType {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.element = element;
|
|
8
|
-
}
|
|
9
|
-
_constructSchema(options, element) {
|
|
10
|
-
return Type.Array(getTypeSchema(element), options);
|
|
11
|
-
}
|
|
12
|
-
nullable() {
|
|
13
|
-
return new ArrayType(this.element, ...this._with({
|
|
14
|
-
isNullable: true
|
|
15
|
-
}));
|
|
16
|
-
}
|
|
17
|
-
optional() {
|
|
18
|
-
return new ArrayType(this.element, ...this._with({
|
|
19
|
-
isOptional: true
|
|
20
|
-
}));
|
|
21
|
-
}
|
|
22
|
-
nullish() {
|
|
23
|
-
return new ArrayType(this.element, ...this._with({
|
|
24
|
-
isNullable: true,
|
|
25
|
-
isOptional: true
|
|
26
|
-
}));
|
|
27
|
-
}
|
|
28
|
-
default(value) {
|
|
29
|
-
return new ArrayType(this.element, ...this._with({
|
|
30
|
-
options: {
|
|
31
|
-
default: value
|
|
32
|
-
},
|
|
33
|
-
hasDefault: true
|
|
34
|
-
}));
|
|
35
|
-
}
|
|
36
|
-
description(description) {
|
|
37
|
-
return new ArrayType(this.element, ...this._with({
|
|
38
|
-
options: {
|
|
39
|
-
description
|
|
40
|
-
}
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
examples(...examples) {
|
|
44
|
-
return new ArrayType(this.element, ...this._with({
|
|
45
|
-
options: {
|
|
46
|
-
example: examples[0],
|
|
47
|
-
examples
|
|
48
|
-
}
|
|
49
|
-
}));
|
|
4
|
+
_;
|
|
5
|
+
static factory(element, options = {}) {
|
|
6
|
+
return new ArrayType(Type.Array(element.schema, options));
|
|
50
7
|
}
|
|
51
8
|
min(value) {
|
|
52
|
-
return
|
|
53
|
-
options
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}));
|
|
9
|
+
return ArrayType.factory(this.props.element, {
|
|
10
|
+
...this.props.options,
|
|
11
|
+
minItems: value
|
|
12
|
+
});
|
|
57
13
|
}
|
|
58
14
|
max(value) {
|
|
59
|
-
return
|
|
60
|
-
options
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}));
|
|
15
|
+
return ArrayType.factory(this.props.element, {
|
|
16
|
+
...this.props.options,
|
|
17
|
+
maxItems: value
|
|
18
|
+
});
|
|
64
19
|
}
|
|
65
20
|
length(value) {
|
|
66
|
-
return
|
|
67
|
-
options
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}));
|
|
21
|
+
return ArrayType.factory(this.props.element, {
|
|
22
|
+
...this.props.options,
|
|
23
|
+
minItems: value,
|
|
24
|
+
maxItems: value
|
|
25
|
+
});
|
|
72
26
|
}
|
|
73
27
|
}
|
package/dist/types/array.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/array.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/array.ts"],"sourcesContent":["import {\n type ArrayOptions,\n type StaticDecode,\n type TArray,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nexport class ArrayType<T extends BaseType = BaseType> extends BaseType<\n TArray<T['schema']>,\n { element: T; options: ArrayOptions }\n> {\n _!: {\n encoded: {\n input: TArray<T['_']['encoded']['input']>\n output: TArray<T['_']['encoded']['output']>\n }\n decoded: {\n input: TArray<T['_']['decoded']['input']>\n output: TArray<T['_']['decoded']['output']>\n }\n }\n\n static factory<T extends BaseType>(element: T, options: ArrayOptions = {}) {\n return new ArrayType<T>(Type.Array(element.schema, options))\n }\n\n min(value: number) {\n return ArrayType.factory(this.props.element, {\n ...this.props.options,\n minItems: value,\n })\n }\n\n max(value: number) {\n return ArrayType.factory(this.props.element, {\n ...this.props.options,\n maxItems: value,\n })\n }\n\n length(value: number) {\n return ArrayType.factory(this.props.element, {\n ...this.props.options,\n minItems: value,\n maxItems: value,\n })\n }\n}\n"],"names":["Type","BaseType","ArrayType","_","factory","element","options","Array","schema","min","value","props","minItems","max","maxItems","length"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAEpC,OAAO,MAAMC,kBAAiDD;IAI5DE,EASC;IAED,OAAOC,QAA4BC,OAAU,EAAEC,UAAwB,CAAC,CAAC,EAAE;QACzE,OAAO,IAAIJ,UAAaF,KAAKO,KAAK,CAACF,QAAQG,MAAM,EAAEF;IACrD;IAEAG,IAAIC,KAAa,EAAE;QACjB,OAAOR,UAAUE,OAAO,CAAC,IAAI,CAACO,KAAK,CAACN,OAAO,EAAE;YAC3C,GAAG,IAAI,CAACM,KAAK,CAACL,OAAO;YACrBM,UAAUF;QACZ;IACF;IAEAG,IAAIH,KAAa,EAAE;QACjB,OAAOR,UAAUE,OAAO,CAAC,IAAI,CAACO,KAAK,CAACN,OAAO,EAAE;YAC3C,GAAG,IAAI,CAACM,KAAK,CAACL,OAAO;YACrBQ,UAAUJ;QACZ;IACF;IAEAK,OAAOL,KAAa,EAAE;QACpB,OAAOR,UAAUE,OAAO,CAAC,IAAI,CAACO,KAAK,CAACN,OAAO,EAAE;YAC3C,GAAG,IAAI,CAACM,KAAK,CAACL,OAAO;YACrBM,UAAUF;YACVI,UAAUJ;QACZ;IACF;AACF"}
|
package/dist/types/base.js
CHANGED
|
@@ -1,46 +1,83 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { typeSchema, typeStatic } from "../constants.js";
|
|
1
|
+
import { Optional } from '@sinclair/typebox';
|
|
3
2
|
import { Nullable } from "../schemas/nullable.js";
|
|
4
3
|
export class BaseType {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
this.
|
|
12
|
-
this.
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
4
|
+
schema;
|
|
5
|
+
final;
|
|
6
|
+
props;
|
|
7
|
+
params;
|
|
8
|
+
constructor(schema, props = {}, params = {}){
|
|
9
|
+
const { hasDefault = false, nullable = false, optional = false } = params;
|
|
10
|
+
this.schema = schema;
|
|
11
|
+
this.final = schema;
|
|
12
|
+
if (nullable) this.final = Nullable(this.final);
|
|
13
|
+
if (optional || hasDefault) this.final = Optional(this.final);
|
|
14
|
+
this.props = props;
|
|
15
|
+
this.params = {
|
|
16
|
+
hasDefault,
|
|
17
|
+
nullable,
|
|
18
|
+
optional
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
optional() {
|
|
22
|
+
return OptionalType.factory(this);
|
|
23
|
+
}
|
|
24
|
+
nullable() {
|
|
25
|
+
return NullableType.factory(this);
|
|
26
|
+
}
|
|
27
|
+
nullish() {
|
|
28
|
+
return this.nullable().optional();
|
|
29
|
+
}
|
|
30
|
+
default(value) {
|
|
31
|
+
return DefaultType.factory(this, this.params.encode?.(value) ?? value);
|
|
32
|
+
}
|
|
33
|
+
description(description) {
|
|
34
|
+
const ThisConstructor = this.constructor;
|
|
35
|
+
return new ThisConstructor({
|
|
36
|
+
...this.schema,
|
|
37
|
+
description
|
|
38
|
+
}, this.props, this.params);
|
|
39
|
+
}
|
|
40
|
+
examples(...examples) {
|
|
41
|
+
const ThisConstructor = this.constructor;
|
|
42
|
+
return new ThisConstructor({
|
|
43
|
+
...this.schema,
|
|
44
|
+
examples
|
|
45
|
+
}, this.props, this.params);
|
|
42
46
|
}
|
|
43
47
|
}
|
|
44
|
-
export
|
|
45
|
-
|
|
48
|
+
export class OptionalType extends BaseType {
|
|
49
|
+
_;
|
|
50
|
+
static factory(type) {
|
|
51
|
+
return new OptionalType(type.schema, {
|
|
52
|
+
inner: type
|
|
53
|
+
}, {
|
|
54
|
+
...type.params,
|
|
55
|
+
optional: true
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class NullableType extends BaseType {
|
|
60
|
+
_;
|
|
61
|
+
static factory(type) {
|
|
62
|
+
return new NullableType(type.schema, {
|
|
63
|
+
inner: type
|
|
64
|
+
}, {
|
|
65
|
+
...type.params,
|
|
66
|
+
nullable: true
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
export class DefaultType extends BaseType {
|
|
71
|
+
_;
|
|
72
|
+
static factory(type, defaultValue) {
|
|
73
|
+
return new DefaultType({
|
|
74
|
+
...type.schema,
|
|
75
|
+
default: defaultValue
|
|
76
|
+
}, {
|
|
77
|
+
inner: type
|
|
78
|
+
}, {
|
|
79
|
+
...type.params,
|
|
80
|
+
hasDefault: true
|
|
81
|
+
});
|
|
82
|
+
}
|
|
46
83
|
}
|
package/dist/types/base.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/base.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type StaticDecode,\n type StaticEncode,\n type
|
|
1
|
+
{"version":3,"sources":["../../../src/types/base.ts"],"sourcesContent":["import {\n Optional,\n type SchemaOptions,\n type StaticDecode,\n type StaticEncode,\n type TSchema,\n} from '@sinclair/typebox'\nimport {\n Nullable,\n type TNullable,\n type TOptionalUndefined,\n} from '../schemas/nullable.ts'\nimport type { Merge } from '../utils.ts'\n\nexport type TypeProps = Record<string, any>\n\nexport type TypeParams = {\n optional?: boolean\n nullable?: boolean\n hasDefault?: boolean\n encode?: (value: any) => any\n}\n\nexport type DefaultTypeParams = {\n optional: false\n nullable: false\n hasDefault: false\n encode?: TypeParams['encode']\n}\n\nexport type BaseTypeAny<T extends TSchema = TSchema> = BaseType<T, any, any>\n\ntype ResolveNullable<\n T extends TSchema,\n P extends TypeParams,\n> = P['nullable'] extends true\n ? T extends TNullable<infer S>\n ? TNullable<S>\n : TNullable<T>\n : T\n\ntype ResolveOptional<\n T extends TSchema,\n P extends TypeParams,\n> = P['optional'] extends true\n ? T extends TOptionalUndefined<infer S>\n ? TOptionalUndefined<S>\n : TOptionalUndefined<T>\n : T\n\ntype ResolveDefault<\n T extends TSchema,\n P extends TypeParams,\n> = P['hasDefault'] extends true\n ? T extends TOptionalUndefined<infer U>\n ? U\n : T\n : T\n\nexport abstract class BaseType<\n Schema extends TSchema = TSchema,\n Props extends TypeProps = TypeProps,\n Params extends TypeParams = DefaultTypeParams,\n> {\n abstract _: {\n encoded: {\n input: TSchema\n output: TSchema\n }\n decoded: {\n input: TSchema\n output: TSchema\n }\n }\n\n readonly schema: Schema\n readonly final: TSchema\n readonly props: Props\n readonly params: Params\n\n constructor(\n schema: Schema,\n props: Props = {} as Props,\n params: Params = {} as Params,\n ) {\n const { hasDefault = false, nullable = false, optional = false } = params\n this.schema = schema\n this.final = schema\n if (nullable) this.final = Nullable(this.final) as any\n if (optional || hasDefault) this.final = Optional(this.final) as any\n\n this.props = props\n this.params = {\n hasDefault,\n nullable,\n optional,\n } as Params\n }\n\n optional(): OptionalType<this> {\n return OptionalType.factory(this) as any\n }\n\n nullable(): NullableType<this> {\n return NullableType.factory(this) as any\n }\n\n nullish() {\n return this.nullable().optional()\n }\n\n default(\n value: StaticDecode<this['_']['decoded']['output']>,\n ): DefaultType<this> {\n return DefaultType.factory(\n this,\n this.params.encode?.(value) ?? value,\n ) as any\n }\n\n description(description: string): this {\n const ThisConstructor = this.constructor as any\n return new ThisConstructor(\n {\n ...this.schema,\n description,\n },\n this.props,\n this.params,\n ) as any\n }\n\n examples(...examples: any[]): this {\n const ThisConstructor = this.constructor as any\n return new ThisConstructor(\n {\n ...this.schema,\n examples,\n },\n this.props,\n this.params,\n ) as any\n }\n}\n\nexport type ConstantType<T extends TSchema> = {\n encoded: {\n input: T\n output: T\n }\n decoded: {\n input: T\n output: T\n }\n}\n\nexport type Static<\n T extends BaseTypeAny,\n P extends TypeProps,\n Params extends Merge<T['params'], P> = Merge<T['params'], P>,\n> = {\n encoded: {\n input: ResolveOptional<\n ResolveNullable<T['_']['encoded']['input'], Params>,\n Params\n >\n output: ResolveDefault<\n ResolveOptional<\n ResolveNullable<T['_']['encoded']['output'], Params>,\n Params\n >,\n Params\n >\n }\n decoded: {\n input: ResolveOptional<\n ResolveNullable<T['_']['decoded']['input'], Params>,\n Params\n >\n output: ResolveDefault<\n ResolveOptional<\n ResolveNullable<T['_']['decoded']['output'], Params>,\n Params\n >,\n Params\n >\n }\n}\n\nexport class OptionalType<\n Type extends BaseTypeAny<any>,\n Params extends TypeParams = DefaultTypeParams,\n> extends BaseType<Type['schema'], { inner: Type }, Params> {\n _!: Static<Type, Params>\n\n static factory<T extends BaseTypeAny<any>>(type: T) {\n return new OptionalType<T, Merge<T['params'], { optional: true }>>(\n type.schema,\n { inner: type },\n { ...type.params, optional: true } as any,\n )\n }\n}\n\nexport class NullableType<\n Type extends BaseTypeAny<any>,\n Params extends TypeParams = DefaultTypeParams,\n> extends BaseType<Type['schema'], { inner: Type }, Params> {\n _!: Static<Type, Params>\n\n static factory<T extends BaseTypeAny<any>>(type: T) {\n return new NullableType<T, Merge<T['params'], { nullable: true }>>(\n type.schema,\n { inner: type },\n { ...type.params, nullable: true } as any,\n )\n }\n}\n\nexport class DefaultType<\n Type extends BaseTypeAny<any>,\n Params extends TypeParams = DefaultTypeParams,\n> extends BaseType<Type['schema'], { inner: Type }, Params> {\n _!: Static<Type, Params>\n\n static factory<T extends BaseTypeAny<any>>(type: T, defaultValue: any) {\n return new DefaultType<T, Merge<T['params'], { hasDefault: true }>>(\n { ...type.schema, default: defaultValue },\n { inner: type },\n { ...type.params, hasDefault: true } as any,\n )\n }\n}\n"],"names":["Optional","Nullable","BaseType","schema","final","props","params","constructor","hasDefault","nullable","optional","OptionalType","factory","NullableType","nullish","default","value","DefaultType","encode","description","ThisConstructor","examples","_","type","inner","defaultValue"],"mappings":"AAAA,SACEA,QAAQ,QAKH,oBAAmB;AAC1B,SACEC,QAAQ,QAGH,yBAAwB;AAgD/B,OAAO,MAAeC;IAgBXC,OAAc;IACdC,MAAc;IACdC,MAAY;IACZC,OAAc;IAEvBC,YACEJ,MAAc,EACdE,QAAe,CAAC,CAAU,EAC1BC,SAAiB,CAAC,CAAW,CAC7B;QACA,MAAM,EAAEE,aAAa,KAAK,EAAEC,WAAW,KAAK,EAAEC,WAAW,KAAK,EAAE,GAAGJ;QACnE,IAAI,CAACH,MAAM,GAAGA;QACd,IAAI,CAACC,KAAK,GAAGD;QACb,IAAIM,UAAU,IAAI,CAACL,KAAK,GAAGH,SAAS,IAAI,CAACG,KAAK;QAC9C,IAAIM,YAAYF,YAAY,IAAI,CAACJ,KAAK,GAAGJ,SAAS,IAAI,CAACI,KAAK;QAE5D,IAAI,CAACC,KAAK,GAAGA;QACb,IAAI,CAACC,MAAM,GAAG;YACZE;YACAC;YACAC;QACF;IACF;IAEAA,WAA+B;QAC7B,OAAOC,aAAaC,OAAO,CAAC,IAAI;IAClC;IAEAH,WAA+B;QAC7B,OAAOI,aAAaD,OAAO,CAAC,IAAI;IAClC;IAEAE,UAAU;QACR,OAAO,IAAI,CAACL,QAAQ,GAAGC,QAAQ;IACjC;IAEAK,QACEC,KAAmD,EAChC;QACnB,OAAOC,YAAYL,OAAO,CACxB,IAAI,EACJ,IAAI,CAACN,MAAM,CAACY,MAAM,GAAGF,UAAUA;IAEnC;IAEAG,YAAYA,WAAmB,EAAQ;QACrC,MAAMC,kBAAkB,IAAI,CAACb,WAAW;QACxC,OAAO,IAAIa,gBACT;YACE,GAAG,IAAI,CAACjB,MAAM;YACdgB;QACF,GACA,IAAI,CAACd,KAAK,EACV,IAAI,CAACC,MAAM;IAEf;IAEAe,SAAS,GAAGA,QAAe,EAAQ;QACjC,MAAMD,kBAAkB,IAAI,CAACb,WAAW;QACxC,OAAO,IAAIa,gBACT;YACE,GAAG,IAAI,CAACjB,MAAM;YACdkB;QACF,GACA,IAAI,CAAChB,KAAK,EACV,IAAI,CAACC,MAAM;IAEf;AACF;AA8CA,OAAO,MAAMK,qBAGHT;IACRoB,EAAwB;IAExB,OAAOV,QAAoCW,IAAO,EAAE;QAClD,OAAO,IAAIZ,aACTY,KAAKpB,MAAM,EACX;YAAEqB,OAAOD;QAAK,GACd;YAAE,GAAGA,KAAKjB,MAAM;YAAEI,UAAU;QAAK;IAErC;AACF;AAEA,OAAO,MAAMG,qBAGHX;IACRoB,EAAwB;IAExB,OAAOV,QAAoCW,IAAO,EAAE;QAClD,OAAO,IAAIV,aACTU,KAAKpB,MAAM,EACX;YAAEqB,OAAOD;QAAK,GACd;YAAE,GAAGA,KAAKjB,MAAM;YAAEG,UAAU;QAAK;IAErC;AACF;AAEA,OAAO,MAAMQ,oBAGHf;IACRoB,EAAwB;IAExB,OAAOV,QAAoCW,IAAO,EAAEE,YAAiB,EAAE;QACrE,OAAO,IAAIR,YACT;YAAE,GAAGM,KAAKpB,MAAM;YAAEY,SAASU;QAAa,GACxC;YAAED,OAAOD;QAAK,GACd;YAAE,GAAGA,KAAKjB,MAAM;YAAEE,YAAY;QAAK;IAEvC;AACF"}
|
package/dist/types/boolean.js
CHANGED
|
@@ -1,48 +1,8 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class BooleanType extends BaseType {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
_constructSchema(options) {
|
|
8
|
-
return Type.Boolean(options);
|
|
9
|
-
}
|
|
10
|
-
nullable() {
|
|
11
|
-
return new BooleanType(...this._with({
|
|
12
|
-
isNullable: true
|
|
13
|
-
}));
|
|
14
|
-
}
|
|
15
|
-
optional() {
|
|
16
|
-
return new BooleanType(...this._with({
|
|
17
|
-
isOptional: true
|
|
18
|
-
}));
|
|
19
|
-
}
|
|
20
|
-
nullish() {
|
|
21
|
-
return new BooleanType(...this._with({
|
|
22
|
-
isNullable: true,
|
|
23
|
-
isOptional: true
|
|
24
|
-
}));
|
|
25
|
-
}
|
|
26
|
-
default(value) {
|
|
27
|
-
return new BooleanType(...this._with({
|
|
28
|
-
options: {
|
|
29
|
-
default: value
|
|
30
|
-
},
|
|
31
|
-
hasDefault: true
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
34
|
-
description(description) {
|
|
35
|
-
return new BooleanType(...this._with({
|
|
36
|
-
options: {
|
|
37
|
-
description
|
|
38
|
-
}
|
|
39
|
-
}));
|
|
40
|
-
}
|
|
41
|
-
examples(...examples) {
|
|
42
|
-
return new BooleanType(...this._with({
|
|
43
|
-
options: {
|
|
44
|
-
examples
|
|
45
|
-
}
|
|
46
|
-
}));
|
|
4
|
+
_;
|
|
5
|
+
static factory() {
|
|
6
|
+
return new BooleanType(Type.Boolean());
|
|
47
7
|
}
|
|
48
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/boolean.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/boolean.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type StaticDecode,\n type TBoolean,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class BooleanType extends BaseType<TBoolean> {\n _!: ConstantType<this['schema']>\n\n static factory() {\n return new BooleanType(Type.Boolean())\n }\n}\n"],"names":["Type","BaseType","BooleanType","_","factory","Boolean"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,oBAAoBD;IAC/BE,EAAgC;IAEhC,OAAOC,UAAU;QACf,OAAO,IAAIF,YAAYF,KAAKK,OAAO;IACrC;AACF"}
|
package/dist/types/custom.js
CHANGED
|
@@ -1,52 +1,12 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
|
-
export class
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
_constructSchema(options, decode, encode) {
|
|
12
|
-
return Type.Transform(Type.Any(options)).Decode(decode).Encode(encode);
|
|
13
|
-
}
|
|
14
|
-
nullable() {
|
|
15
|
-
return new CustomType(this.decode, this.encode, ...this._with({
|
|
16
|
-
isNullable: true
|
|
17
|
-
}));
|
|
18
|
-
}
|
|
19
|
-
optional() {
|
|
20
|
-
return new CustomType(this.decode, this.encode, ...this._with({
|
|
21
|
-
isOptional: true
|
|
22
|
-
}));
|
|
23
|
-
}
|
|
24
|
-
nullish() {
|
|
25
|
-
return new CustomType(this.decode, this.encode, ...this._with({
|
|
26
|
-
isNullable: true,
|
|
27
|
-
isOptional: true
|
|
28
|
-
}));
|
|
29
|
-
}
|
|
30
|
-
default(value) {
|
|
31
|
-
return new CustomType(this.decode, this.encode, ...this._with({
|
|
32
|
-
options: {
|
|
33
|
-
default: this.encode(value)
|
|
34
|
-
},
|
|
35
|
-
hasDefault: true
|
|
36
|
-
}));
|
|
37
|
-
}
|
|
38
|
-
description(description) {
|
|
39
|
-
return new CustomType(this.decode, this.encode, ...this._with({
|
|
40
|
-
options: {
|
|
41
|
-
description
|
|
42
|
-
}
|
|
43
|
-
}));
|
|
44
|
-
}
|
|
45
|
-
examples(...examples) {
|
|
46
|
-
return new CustomType(this.decode, this.encode, ...this._with({
|
|
47
|
-
options: {
|
|
48
|
-
examples: examples.map(this.encode)
|
|
49
|
-
}
|
|
50
|
-
}));
|
|
3
|
+
export class TransformType extends BaseType {
|
|
4
|
+
_;
|
|
5
|
+
}
|
|
6
|
+
export class CustomType extends TransformType {
|
|
7
|
+
static factory(decode, encode, schema = Type.Any()) {
|
|
8
|
+
return new CustomType(Type.Transform(schema).Decode(decode).Encode(encode), {}, {
|
|
9
|
+
encode
|
|
10
|
+
});
|
|
51
11
|
}
|
|
52
12
|
}
|
package/dist/types/custom.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/custom.ts"],"sourcesContent":["import {\n type
|
|
1
|
+
{"version":3,"sources":["../../../src/types/custom.ts"],"sourcesContent":["import {\n type TAny,\n type TTransform,\n type TUnsafe,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport type CustomTypeDecode<T> = (value: any) => T\nexport type CustomTypeEncode<T> = (value: T) => any\n\nexport abstract class TransformType<T, S = TAny> extends BaseType<\n TTransform<TUnsafe<S>, T>\n> {\n _!: ConstantType<this['schema']>\n}\n\nexport class CustomType<T, S = TAny> extends TransformType<T, S> {\n static factory<T, S = TAny>(\n decode: CustomTypeDecode<T>,\n encode: CustomTypeEncode<T>,\n schema = Type.Any() as unknown as TUnsafe<S>,\n ) {\n return new CustomType<T, S>(\n Type.Transform(schema as unknown as TUnsafe<S>)\n .Decode(decode)\n .Encode(encode),\n {},\n { encode } as any,\n )\n }\n}\n"],"names":["Type","BaseType","TransformType","_","CustomType","factory","decode","encode","schema","Any","Transform","Decode","Encode"],"mappings":"AAAA,SAIEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAKvD,OAAO,MAAeC,sBAAmCD;IAGvDE,EAAgC;AAClC;AAEA,OAAO,MAAMC,mBAAgCF;IAC3C,OAAOG,QACLC,MAA2B,EAC3BC,MAA2B,EAC3BC,SAASR,KAAKS,GAAG,EAA2B,EAC5C;QACA,OAAO,IAAIL,WACTJ,KAAKU,SAAS,CAACF,QACZG,MAAM,CAACL,QACPM,MAAM,CAACL,SACV,CAAC,GACD;YAAEA;QAAO;IAEb;AACF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { CustomType, TransformType } from "./custom.js";
|
|
2
|
+
const decode = (value)=>new Date(value);
|
|
3
|
+
const encode = (value)=>value.toISOString();
|
|
4
|
+
export class DateType extends TransformType {
|
|
5
|
+
static factory() {
|
|
6
|
+
return CustomType.factory(decode, encode);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/types/date.ts"],"sourcesContent":["import { CustomType, TransformType } from './custom.ts'\n\nconst decode = (value: any): Date => new Date(value)\nconst encode = (value: Date): any => value.toISOString()\n\nexport class DateType extends TransformType<Date> {\n static factory() {\n return CustomType.factory<Date>(decode, encode)\n }\n}\n"],"names":["CustomType","TransformType","decode","value","Date","encode","toISOString","DateType","factory"],"mappings":"AAAA,SAASA,UAAU,EAAEC,aAAa,QAAQ,cAAa;AAEvD,MAAMC,SAAS,CAACC,QAAqB,IAAIC,KAAKD;AAC9C,MAAME,SAAS,CAACF,QAAqBA,MAAMG,WAAW;AAEtD,OAAO,MAAMC,iBAAiBN;IAC5B,OAAOO,UAAU;QACf,OAAOR,WAAWQ,OAAO,CAAON,QAAQG;IAC1C;AACF"}
|
package/dist/types/enum.js
CHANGED
|
@@ -1,101 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class ObjectEnumType extends BaseType {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.values = values;
|
|
8
|
-
}
|
|
9
|
-
_constructSchema(options, values) {
|
|
10
|
-
return Enum(values, options);
|
|
11
|
-
}
|
|
12
|
-
nullable() {
|
|
13
|
-
return new ObjectEnumType(this.values, ...this._with({
|
|
14
|
-
isNullable: true
|
|
15
|
-
}));
|
|
16
|
-
}
|
|
17
|
-
optional() {
|
|
18
|
-
return new ObjectEnumType(this.values, ...this._with({
|
|
19
|
-
isOptional: true
|
|
20
|
-
}));
|
|
21
|
-
}
|
|
22
|
-
nullish() {
|
|
23
|
-
return new ObjectEnumType(this.values, ...this._with({
|
|
24
|
-
isNullable: true,
|
|
25
|
-
isOptional: true
|
|
26
|
-
}));
|
|
27
|
-
}
|
|
28
|
-
default(value) {
|
|
29
|
-
return new ObjectEnumType(this.values, ...this._with({
|
|
30
|
-
options: {
|
|
31
|
-
default: value
|
|
32
|
-
},
|
|
33
|
-
hasDefault: true
|
|
34
|
-
}));
|
|
35
|
-
}
|
|
36
|
-
description(description) {
|
|
37
|
-
return new ObjectEnumType(this.values, ...this._with({
|
|
38
|
-
options: {
|
|
39
|
-
description
|
|
40
|
-
}
|
|
41
|
-
}));
|
|
42
|
-
}
|
|
43
|
-
examples(...examples) {
|
|
44
|
-
return new ObjectEnumType(this.values, ...this._with({
|
|
45
|
-
options: {
|
|
46
|
-
examples
|
|
47
|
-
}
|
|
48
|
-
}));
|
|
4
|
+
_;
|
|
5
|
+
static factory(values) {
|
|
6
|
+
return new ObjectEnumType(Type.Enum(values));
|
|
49
7
|
}
|
|
50
8
|
}
|
|
51
9
|
export class EnumType extends BaseType {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return Enum(Object.fromEntries(values.map((k)=>[
|
|
59
|
-
k,
|
|
60
|
-
k
|
|
61
|
-
])), options);
|
|
62
|
-
}
|
|
63
|
-
nullable() {
|
|
64
|
-
return new EnumType(this.values, ...this._with({
|
|
65
|
-
isNullable: true
|
|
66
|
-
}));
|
|
67
|
-
}
|
|
68
|
-
optional() {
|
|
69
|
-
return new EnumType(this.values, ...this._with({
|
|
70
|
-
isOptional: true
|
|
71
|
-
}));
|
|
72
|
-
}
|
|
73
|
-
nullish() {
|
|
74
|
-
return new EnumType(this.values, ...this._with({
|
|
75
|
-
isNullable: true,
|
|
76
|
-
isOptional: true
|
|
77
|
-
}));
|
|
78
|
-
}
|
|
79
|
-
default(value) {
|
|
80
|
-
return new EnumType(this.values, ...this._with({
|
|
81
|
-
options: {
|
|
82
|
-
default: value
|
|
83
|
-
},
|
|
84
|
-
hasDefault: true
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
description(description) {
|
|
88
|
-
return new EnumType(this.values, ...this._with({
|
|
89
|
-
options: {
|
|
90
|
-
description
|
|
91
|
-
}
|
|
92
|
-
}));
|
|
93
|
-
}
|
|
94
|
-
examples(...examples) {
|
|
95
|
-
return new EnumType(this.values, ...this._with({
|
|
96
|
-
options: {
|
|
97
|
-
examples
|
|
98
|
-
}
|
|
99
|
-
}));
|
|
10
|
+
_;
|
|
11
|
+
static factory(values) {
|
|
12
|
+
return new EnumType(Type.Enum(Object.fromEntries(values.map((v)=>[
|
|
13
|
+
v,
|
|
14
|
+
v
|
|
15
|
+
]))));
|
|
100
16
|
}
|
|
101
17
|
}
|
package/dist/types/enum.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../../../src/types/enum.ts"],"sourcesContent":["import {\n Enum,\n type SchemaOptions,\n type StaticDecode,\n type TEnum,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType, type ConstantType } from './base.ts'\n\nexport class ObjectEnumType<\n T extends { [K in string]: K } = { [K in string]: K },\n> extends BaseType<TEnum<T>> {\n _!: ConstantType<this['schema']>\n\n static factory<T extends { [K in string]: K }>(values: T) {\n return new ObjectEnumType<T>(Type.Enum(values as any))\n }\n}\n\nexport class EnumType<\n T extends (string | number)[] = (string | number)[],\n> extends BaseType<TEnum<Record<string, T[number]>>> {\n _!: ConstantType<this['schema']>\n\n static factory<T extends (string | number)[]>(values: [...T]) {\n return new EnumType<T>(\n Type.Enum(Object.fromEntries(values.map((v) => [v, v])) as any),\n )\n }\n}\n"],"names":["Type","BaseType","ObjectEnumType","_","factory","values","Enum","EnumType","Object","fromEntries","map","v"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAA2B,YAAW;AAEvD,OAAO,MAAMC,uBAEHD;IACRE,EAAgC;IAEhC,OAAOC,QAAwCC,MAAS,EAAE;QACxD,OAAO,IAAIH,eAAkBF,KAAKM,IAAI,CAACD;IACzC;AACF;AAEA,OAAO,MAAME,iBAEHN;IACRE,EAAgC;IAEhC,OAAOC,QAAuCC,MAAc,EAAE;QAC5D,OAAO,IAAIE,SACTP,KAAKM,IAAI,CAACE,OAAOC,WAAW,CAACJ,OAAOK,GAAG,CAAC,CAACC,IAAM;gBAACA;gBAAGA;aAAE;IAEzD;AACF"}
|
package/dist/types/literal.js
CHANGED
|
@@ -1,48 +1,8 @@
|
|
|
1
1
|
import { Type } from '@sinclair/typebox';
|
|
2
2
|
import { BaseType } from "./base.js";
|
|
3
3
|
export class LiteralType extends BaseType {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
this.value = value;
|
|
8
|
-
}
|
|
9
|
-
_constructSchema(options, value) {
|
|
10
|
-
return Type.Literal(value, options);
|
|
11
|
-
}
|
|
12
|
-
nullable() {
|
|
13
|
-
return new LiteralType(this.value, this.options, true, this.isOptional);
|
|
14
|
-
}
|
|
15
|
-
optional() {
|
|
16
|
-
return new LiteralType(this.value, ...this._with({
|
|
17
|
-
isOptional: true
|
|
18
|
-
}));
|
|
19
|
-
}
|
|
20
|
-
nullish() {
|
|
21
|
-
return new LiteralType(this.value, ...this._with({
|
|
22
|
-
isNullable: true,
|
|
23
|
-
isOptional: true
|
|
24
|
-
}));
|
|
25
|
-
}
|
|
26
|
-
default(value = this.value) {
|
|
27
|
-
return new LiteralType(this.value, ...this._with({
|
|
28
|
-
options: {
|
|
29
|
-
default: value
|
|
30
|
-
},
|
|
31
|
-
hasDefault: true
|
|
32
|
-
}));
|
|
33
|
-
}
|
|
34
|
-
description(description) {
|
|
35
|
-
return new LiteralType(this.value, ...this._with({
|
|
36
|
-
options: {
|
|
37
|
-
description
|
|
38
|
-
}
|
|
39
|
-
}));
|
|
40
|
-
}
|
|
41
|
-
examples(...examples) {
|
|
42
|
-
return new LiteralType(this.value, ...this._with({
|
|
43
|
-
options: {
|
|
44
|
-
examples
|
|
45
|
-
}
|
|
46
|
-
}));
|
|
4
|
+
_;
|
|
5
|
+
static factory(value) {
|
|
6
|
+
return new LiteralType(Type.Literal(value));
|
|
47
7
|
}
|
|
48
8
|
}
|