@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.
Files changed (69) hide show
  1. package/dist/compiler.js +84 -38
  2. package/dist/compiler.js.map +1 -1
  3. package/dist/formats.js +1 -1
  4. package/dist/formats.js.map +1 -1
  5. package/dist/index.js +53 -22
  6. package/dist/index.js.map +1 -1
  7. package/dist/schemas/discriminated-union.js +9 -0
  8. package/dist/schemas/discriminated-union.js.map +1 -0
  9. package/dist/schemas/nullable.js +1 -6
  10. package/dist/schemas/nullable.js.map +1 -1
  11. package/dist/temporal.js +7 -7
  12. package/dist/temporal.js.map +1 -1
  13. package/dist/types/any.js +3 -43
  14. package/dist/types/any.js.map +1 -1
  15. package/dist/types/array.js +17 -63
  16. package/dist/types/array.js.map +1 -1
  17. package/dist/types/base.js +78 -41
  18. package/dist/types/base.js.map +1 -1
  19. package/dist/types/boolean.js +3 -43
  20. package/dist/types/boolean.js.map +1 -1
  21. package/dist/types/custom.js +8 -48
  22. package/dist/types/custom.js.map +1 -1
  23. package/dist/types/date.js +8 -0
  24. package/dist/types/date.js.map +1 -0
  25. package/dist/types/enum.js +10 -94
  26. package/dist/types/enum.js.map +1 -1
  27. package/dist/types/literal.js +3 -43
  28. package/dist/types/literal.js.map +1 -1
  29. package/dist/types/never.js +3 -26
  30. package/dist/types/never.js.map +1 -1
  31. package/dist/types/number.js +52 -186
  32. package/dist/types/number.js.map +1 -1
  33. package/dist/types/object.js +10 -131
  34. package/dist/types/object.js.map +1 -1
  35. package/dist/types/string.js +25 -65
  36. package/dist/types/string.js.map +1 -1
  37. package/dist/types/temporal.js +23 -328
  38. package/dist/types/temporal.js.map +1 -1
  39. package/dist/types/union.js +16 -90
  40. package/dist/types/union.js.map +1 -1
  41. package/dist/utils.js.map +1 -1
  42. package/package.json +3 -3
  43. package/src/compiler.ts +124 -41
  44. package/src/formats.ts +1 -1
  45. package/src/index.ts +145 -63
  46. package/src/schemas/discriminated-union.ts +49 -0
  47. package/src/schemas/nullable.ts +7 -13
  48. package/src/temporal.ts +8 -7
  49. package/src/types/any.ts +6 -46
  50. package/src/types/array.ts +38 -86
  51. package/src/types/base.ts +205 -81
  52. package/src/types/boolean.ts +13 -47
  53. package/src/types/custom.ts +21 -79
  54. package/src/types/date.ts +10 -0
  55. package/src/types/enum.ts +18 -107
  56. package/src/types/literal.ts +7 -63
  57. package/src/types/never.ts +6 -36
  58. package/src/types/number.ts +52 -188
  59. package/src/types/object.ts +61 -202
  60. package/src/types/string.ts +25 -61
  61. package/src/types/temporal.ts +53 -410
  62. package/src/types/union.ts +98 -138
  63. package/src/utils.ts +8 -0
  64. package/dist/constants.js +0 -2
  65. package/dist/constants.js.map +0 -1
  66. package/dist/types/datetime.js +0 -53
  67. package/dist/types/datetime.js.map +0 -1
  68. package/src/constants.ts +0 -5
  69. package/src/types/datetime.ts +0 -65
@@ -1,159 +1,119 @@
1
1
  import {
2
- type SchemaOptions,
3
2
  type TIntersect,
4
3
  type TUnion,
5
4
  Type,
5
+ type UnionToTuple,
6
6
  } from '@sinclair/typebox'
7
- import type { typeStatic } from '../constants.ts'
8
- import type { UnionToTuple } from '../utils.ts'
9
- import { BaseType, getTypeSchema } from './base.ts'
7
+ import {
8
+ DiscriminatedUnion,
9
+ type TDiscriminatedUnion,
10
+ } from '../schemas/discriminated-union.ts'
11
+ import { BaseType, type BaseTypeAny } from './base.ts'
12
+ import type { LiteralType } from './literal.ts'
13
+ import type { ObjectType, ObjectTypeProps } from './object.ts'
10
14
 
11
- export type AnyUnionType<
12
- T extends [BaseType, BaseType, ...BaseType[]] = [
13
- BaseType,
14
- BaseType,
15
- ...BaseType[],
16
- ],
17
- > = UnionType<T, boolean, boolean, boolean>
18
15
  export class UnionType<
19
- T extends [BaseType, BaseType, ...BaseType[]] = [
20
- BaseType,
21
- BaseType,
22
- ...BaseType[],
23
- ],
24
- N extends boolean = false,
25
- O extends boolean = false,
26
- D extends boolean = false,
27
- > extends BaseType<
28
- //@ts-expect-error
29
- TUnion<UnionToTuple<T[number][typeStatic]['schema']>>,
30
- N,
31
- O,
32
- D
33
- > {
34
- constructor(
35
- protected readonly types: T,
36
- options: SchemaOptions = {},
37
- isNullable: N = false as N,
38
- isOptional: O = false as O,
39
- hasDefault: D = false as D,
16
+ T extends readonly BaseType[] = readonly BaseType[],
17
+ > extends BaseType<TUnion<UnionToTuple<T[number]['schema']>>> {
18
+ _!: {
19
+ encoded: {
20
+ input: TUnion<UnionToTuple<T[number]['_']['encoded']['input']>>
21
+ output: TUnion<UnionToTuple<T[number]['_']['encoded']['output']>>
22
+ }
23
+ decoded: {
24
+ input: TUnion<UnionToTuple<T[number]['_']['decoded']['input']>>
25
+ output: TUnion<UnionToTuple<T[number]['_']['decoded']['output']>>
26
+ }
27
+ }
28
+
29
+ static factory<T extends readonly BaseType[] = readonly BaseType[]>(
30
+ ...types: T
40
31
  ) {
41
- super(options, isNullable, isOptional, hasDefault, types)
42
- }
43
-
44
- protected _constructSchema(
45
- options: SchemaOptions,
46
- types: T,
47
- //@ts-expect-error
48
- ): TUnion<UnionToTuple<T[number][typeStatic]['schema']>> {
49
- return Type.Union(types.map(getTypeSchema), options) as any
50
- }
51
-
52
- nullable() {
53
- return new UnionType(this.types, ...this._with({ isNullable: true }))
54
- }
55
-
56
- optional() {
57
- return new UnionType(this.types, ...this._with({ isOptional: true }))
58
- }
59
-
60
- nullish() {
61
- return new UnionType(
62
- this.types,
63
- ...this._with({ isNullable: true, isOptional: true }),
64
- )
65
- }
66
-
67
- default(value: this[typeStatic]['encoded']) {
68
- return new UnionType(
69
- this.types,
70
- ...this._with({ options: { default: value }, hasDefault: true }),
71
- )
72
- }
73
-
74
- description(description: string) {
75
- return new UnionType(
76
- this.types,
77
- ...this._with({ options: { description } }),
78
- )
79
- }
80
-
81
- examples(
82
- ...examples: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
83
- ) {
84
- return new UnionType(this.types, ...this._with({ options: { examples } }))
32
+ return new UnionType<T>(Type.Union(types.map((t) => t.schema)) as any)
85
33
  }
86
34
  }
87
35
 
88
36
  export class IntersactionType<
89
- T extends [BaseType, BaseType, ...BaseType[]] = [
90
- BaseType,
91
- BaseType,
92
- ...BaseType[],
93
- ],
94
- N extends boolean = false,
95
- O extends boolean = false,
96
- D extends boolean = false,
97
- > extends BaseType<
98
- // @ts-expect-error
99
- TIntersect<UnionToTuple<T[number][typeStatic]['schema']>>,
100
- N,
101
- O,
102
- D
103
- > {
104
- constructor(
105
- protected readonly types: T,
106
- options: SchemaOptions = {},
107
- isNullable: N = false as N,
108
- isOptional: O = false as O,
109
- hasDefault: D = false as D,
37
+ T extends readonly BaseType[] = readonly BaseType[],
38
+ > extends BaseType<TIntersect<UnionToTuple<T[number]['schema']>>> {
39
+ _!: {
40
+ encoded: {
41
+ input: TIntersect<UnionToTuple<T[number]['_']['encoded']['input']>>
42
+ output: TIntersect<UnionToTuple<T[number]['_']['encoded']['output']>>
43
+ }
44
+ decoded: {
45
+ input: TIntersect<UnionToTuple<T[number]['_']['decoded']['input']>>
46
+ output: TIntersect<UnionToTuple<T[number]['_']['decoded']['output']>>
47
+ }
48
+ }
49
+
50
+ static factory<T extends readonly BaseType[] = readonly BaseType[]>(
51
+ ...types: T
110
52
  ) {
111
- super(options, isNullable, isOptional, hasDefault, types)
112
- }
113
-
114
- protected _constructSchema(
115
- options: SchemaOptions,
116
- types: T,
117
- // @ts-expect-error
118
- ): TIntersect<UnionToTuple<T[number][typeStatic]['schema']>> {
119
- return Type.Intersect(types.map(getTypeSchema), options) as any
120
- }
121
-
122
- nullable() {
123
- return new IntersactionType(this.types, ...this._with({ isNullable: true }))
124
- }
125
-
126
- optional() {
127
- return new IntersactionType(this.types, ...this._with({ isOptional: true }))
128
- }
129
-
130
- nullish() {
131
- return new IntersactionType(
132
- this.types,
133
- ...this._with({ isNullable: true, isOptional: true }),
53
+ return new IntersactionType<T>(
54
+ Type.Intersect(types.map((t) => t.schema)) as any,
134
55
  )
135
56
  }
57
+ }
136
58
 
137
- default(value: this[typeStatic]['encoded']) {
138
- return new IntersactionType(
139
- this.types,
140
- ...this._with({ options: { default: value }, hasDefault: true }),
141
- )
142
- }
59
+ export type DiscriminatedUnionOptionType<K extends string> = ObjectType<
60
+ ObjectTypeProps & { [_ in K]: BaseTypeAny }
61
+ >
143
62
 
144
- description(description: string) {
145
- return new IntersactionType(
146
- this.types,
147
- ...this._with({ options: { description } }),
148
- )
63
+ export class DiscriminatedUnionType<
64
+ K extends string,
65
+ T extends readonly [
66
+ DiscriminatedUnionOptionType<K>,
67
+ ...DiscriminatedUnionOptionType<K>[],
68
+ ],
69
+ > extends BaseType<
70
+ TDiscriminatedUnion<
71
+ K,
72
+ //@ts-expect-error
73
+ UnionToTuple<T[number]['schema']>
74
+ >,
75
+ {
76
+ key: K
77
+ options: T
149
78
  }
150
-
151
- examples(
152
- ...values: [this[typeStatic]['encoded'], ...this[typeStatic]['encoded'][]]
153
- ) {
154
- return new IntersactionType(
155
- this.types,
156
- ...this._with({ options: { examples: values } }),
79
+ > {
80
+ _!: {
81
+ encoded: {
82
+ input: TDiscriminatedUnion<
83
+ K,
84
+ //@ts-expect-error
85
+ UnionToTuple<T[number]['_']['encoded']['input']>
86
+ >
87
+ output: TDiscriminatedUnion<
88
+ K,
89
+ //@ts-expect-error
90
+ UnionToTuple<T[number]['_']['encoded']['output']>
91
+ >
92
+ }
93
+ decoded: {
94
+ input: TDiscriminatedUnion<
95
+ K,
96
+ //@ts-expect-error
97
+ UnionToTuple<T[number]['_']['decoded']['input']>
98
+ >
99
+ output: TDiscriminatedUnion<
100
+ K,
101
+ //@ts-expect-error
102
+ UnionToTuple<T[number]['_']['decoded']['output']>
103
+ >
104
+ }
105
+ }
106
+
107
+ static factory<
108
+ K extends string,
109
+ T extends readonly [
110
+ DiscriminatedUnionOptionType<K>,
111
+ ...DiscriminatedUnionOptionType<K>[],
112
+ ],
113
+ >(key: K, ...options: T) {
114
+ return new DiscriminatedUnionType<K, T>(
115
+ DiscriminatedUnion(key, options.map((t) => t.schema) as any),
116
+ { key, options },
157
117
  )
158
118
  }
159
119
  }
package/src/utils.ts CHANGED
@@ -14,3 +14,11 @@ export type UnionToTuple<T, Tuple extends unknown[] = []> = [T] extends [never]
14
14
  export type CastToStringTuple<T> = T extends [string, ...string[]] ? T : never
15
15
 
16
16
  export type UnionToTupleString<T> = CastToStringTuple<UnionToTuple<T>>
17
+
18
+ export type Merge<T, U> = {
19
+ [K in keyof T | keyof U]: K extends keyof U
20
+ ? U[K]
21
+ : K extends keyof T
22
+ ? T[K]
23
+ : never
24
+ }
package/dist/constants.js DELETED
@@ -1,2 +0,0 @@
1
- export const typeStatic = Symbol.for('neemataType:typeStatic');
2
- export const typeSchema = Symbol.for('neemataType:typeSchema');
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/constants.ts"],"sourcesContent":["export const typeStatic: unique symbol = Symbol.for('neemataType:typeStatic')\nexport type typeStatic = typeof typeStatic\n\nexport const typeSchema: unique symbol = Symbol.for('neemataType:typeSchema')\nexport type typeSchema = typeof typeSchema\n"],"names":["typeStatic","Symbol","for","typeSchema"],"mappings":"AAAA,OAAO,MAAMA,aAA4BC,OAAOC,GAAG,CAAC,0BAAyB;AAG7E,OAAO,MAAMC,aAA4BF,OAAOC,GAAG,CAAC,0BAAyB"}
@@ -1,53 +0,0 @@
1
- import { Type } from '@sinclair/typebox';
2
- import { BaseType } from "./base.js";
3
- const decode = (value)=>new Date(value);
4
- const encode = (value)=>value.toISOString();
5
- export class DateType extends BaseType {
6
- constructor(options = {}, isNullable = false, isOptional = false, hasDefault = false){
7
- super(options, isNullable, isOptional, hasDefault);
8
- }
9
- _constructSchema(options) {
10
- return Type.Transform(Type.String({
11
- ...options,
12
- format: 'date-time'
13
- })).Decode(decode).Encode(encode);
14
- }
15
- nullable() {
16
- return new DateType(...this._with({
17
- isNullable: true
18
- }));
19
- }
20
- optional() {
21
- return new DateType(...this._with({
22
- isOptional: true
23
- }));
24
- }
25
- nullish() {
26
- return new DateType(...this._with({
27
- isNullable: true,
28
- isOptional: true
29
- }));
30
- }
31
- default(value) {
32
- return new DateType(...this._with({
33
- options: {
34
- default: encode(value)
35
- },
36
- hasDefault: true
37
- }));
38
- }
39
- description(value) {
40
- return new DateType(...this._with({
41
- options: {
42
- description: value
43
- }
44
- }));
45
- }
46
- examples(...values) {
47
- return new DateType(...this._with({
48
- options: {
49
- examples: values.map(encode)
50
- }
51
- }));
52
- }
53
- }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../../src/types/datetime.ts"],"sourcesContent":["import {\n type SchemaOptions,\n type StringOptions,\n type TString,\n type TTransform,\n Type,\n} from '@sinclair/typebox'\nimport { BaseType } from './base.ts'\n\nconst decode = (value: any): Date => new Date(value)\nconst encode = (value: Date): any => value.toISOString()\n\nexport class DateType<\n N extends boolean = false,\n O extends boolean = false,\n D extends boolean = false,\n> extends BaseType<TTransform<TString, Date>, N, O, D, StringOptions> {\n constructor(\n options: SchemaOptions = {},\n isNullable: N = false as N,\n isOptional: O = false as O,\n hasDefault: D = false as D,\n ) {\n super(options, isNullable, isOptional, hasDefault)\n }\n\n protected _constructSchema(\n options: StringOptions,\n ): TTransform<TString, Date> {\n return Type.Transform(Type.String({ ...options, format: 'date-time' }))\n .Decode(decode)\n .Encode(encode)\n }\n\n nullable() {\n return new DateType(...this._with({ isNullable: true }))\n }\n\n optional() {\n return new DateType(...this._with({ isOptional: true }))\n }\n\n nullish() {\n return new DateType(...this._with({ isNullable: true, isOptional: true }))\n }\n\n default(value: Date) {\n return new DateType(\n ...this._with({\n options: { default: encode(value) },\n hasDefault: true,\n }),\n )\n }\n\n description(value: string) {\n return new DateType(...this._with({ options: { description: value } }))\n }\n\n examples(...values: [Date, ...Date[]]) {\n return new DateType(\n ...this._with({ options: { examples: values.map(encode) } }),\n )\n }\n}\n"],"names":["Type","BaseType","decode","value","Date","encode","toISOString","DateType","constructor","options","isNullable","isOptional","hasDefault","_constructSchema","Transform","String","format","Decode","Encode","nullable","_with","optional","nullish","default","description","examples","values","map"],"mappings":"AAAA,SAKEA,IAAI,QACC,oBAAmB;AAC1B,SAASC,QAAQ,QAAQ,YAAW;AAEpC,MAAMC,SAAS,CAACC,QAAqB,IAAIC,KAAKD;AAC9C,MAAME,SAAS,CAACF,QAAqBA,MAAMG,WAAW;AAEtD,OAAO,MAAMC,iBAIHN;IACRO,YACEC,UAAyB,CAAC,CAAC,EAC3BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,EAC1BC,aAAgB,KAAU,CAC1B;QACA,KAAK,CAACH,SAASC,YAAYC,YAAYC;IACzC;IAEUC,iBACRJ,OAAsB,EACK;QAC3B,OAAOT,KAAKc,SAAS,CAACd,KAAKe,MAAM,CAAC;YAAE,GAAGN,OAAO;YAAEO,QAAQ;QAAY,IACjEC,MAAM,CAACf,QACPgB,MAAM,CAACb;IACZ;IAEAc,WAAW;QACT,OAAO,IAAIZ,YAAY,IAAI,CAACa,KAAK,CAAC;YAAEV,YAAY;QAAK;IACvD;IAEAW,WAAW;QACT,OAAO,IAAId,YAAY,IAAI,CAACa,KAAK,CAAC;YAAET,YAAY;QAAK;IACvD;IAEAW,UAAU;QACR,OAAO,IAAIf,YAAY,IAAI,CAACa,KAAK,CAAC;YAAEV,YAAY;YAAMC,YAAY;QAAK;IACzE;IAEAY,QAAQpB,KAAW,EAAE;QACnB,OAAO,IAAII,YACN,IAAI,CAACa,KAAK,CAAC;YACZX,SAAS;gBAAEc,SAASlB,OAAOF;YAAO;YAClCS,YAAY;QACd;IAEJ;IAEAY,YAAYrB,KAAa,EAAE;QACzB,OAAO,IAAII,YAAY,IAAI,CAACa,KAAK,CAAC;YAAEX,SAAS;gBAAEe,aAAarB;YAAM;QAAE;IACtE;IAEAsB,SAAS,GAAGC,MAAyB,EAAE;QACrC,OAAO,IAAInB,YACN,IAAI,CAACa,KAAK,CAAC;YAAEX,SAAS;gBAAEgB,UAAUC,OAAOC,GAAG,CAACtB;YAAQ;QAAE;IAE9D;AACF"}
package/src/constants.ts DELETED
@@ -1,5 +0,0 @@
1
- export const typeStatic: unique symbol = Symbol.for('neemataType:typeStatic')
2
- export type typeStatic = typeof typeStatic
3
-
4
- export const typeSchema: unique symbol = Symbol.for('neemataType:typeSchema')
5
- export type typeSchema = typeof typeSchema
@@ -1,65 +0,0 @@
1
- import {
2
- type SchemaOptions,
3
- type StringOptions,
4
- type TString,
5
- type TTransform,
6
- Type,
7
- } from '@sinclair/typebox'
8
- import { BaseType } from './base.ts'
9
-
10
- const decode = (value: any): Date => new Date(value)
11
- const encode = (value: Date): any => value.toISOString()
12
-
13
- export class DateType<
14
- N extends boolean = false,
15
- O extends boolean = false,
16
- D extends boolean = false,
17
- > extends BaseType<TTransform<TString, Date>, N, O, D, StringOptions> {
18
- constructor(
19
- options: SchemaOptions = {},
20
- isNullable: N = false as N,
21
- isOptional: O = false as O,
22
- hasDefault: D = false as D,
23
- ) {
24
- super(options, isNullable, isOptional, hasDefault)
25
- }
26
-
27
- protected _constructSchema(
28
- options: StringOptions,
29
- ): TTransform<TString, Date> {
30
- return Type.Transform(Type.String({ ...options, format: 'date-time' }))
31
- .Decode(decode)
32
- .Encode(encode)
33
- }
34
-
35
- nullable() {
36
- return new DateType(...this._with({ isNullable: true }))
37
- }
38
-
39
- optional() {
40
- return new DateType(...this._with({ isOptional: true }))
41
- }
42
-
43
- nullish() {
44
- return new DateType(...this._with({ isNullable: true, isOptional: true }))
45
- }
46
-
47
- default(value: Date) {
48
- return new DateType(
49
- ...this._with({
50
- options: { default: encode(value) },
51
- hasDefault: true,
52
- }),
53
- )
54
- }
55
-
56
- description(value: string) {
57
- return new DateType(...this._with({ options: { description: value } }))
58
- }
59
-
60
- examples(...values: [Date, ...Date[]]) {
61
- return new DateType(
62
- ...this._with({ options: { examples: values.map(encode) } }),
63
- )
64
- }
65
- }