@nmtjs/type 0.14.5 → 0.15.0-beta.10
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.md +1 -1
- package/README.md +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -0
- package/dist/temporal/global.d.ts +8 -7
- package/dist/temporal/global.js +1 -0
- package/dist/temporal/global.js.map +1 -0
- package/dist/temporal/polyfill.d.ts +8 -8
- package/dist/temporal/polyfill.js +1 -0
- package/dist/temporal/polyfill.js.map +1 -0
- package/dist/types/_convert.d.ts +5 -0
- package/dist/types/_convert.js +56 -0
- package/dist/types/_convert.js.map +1 -0
- package/dist/types/_type.d.ts +1 -0
- package/dist/types/_type.js +2 -0
- package/dist/types/_type.js.map +1 -0
- package/dist/types/{_plain.d.ts → _utils.d.ts} +2 -0
- package/dist/types/{_plain.js → _utils.js} +1 -0
- package/dist/types/_utils.js.map +1 -0
- package/dist/types/any.js +1 -0
- package/dist/types/any.js.map +1 -0
- package/dist/types/array.d.ts +1 -1
- package/dist/types/array.js +1 -0
- package/dist/types/array.js.map +1 -0
- package/dist/types/base.js +1 -0
- package/dist/types/base.js.map +1 -0
- package/dist/types/boolean.js +1 -0
- package/dist/types/boolean.js.map +1 -0
- package/dist/types/custom.d.ts +7 -1
- package/dist/types/custom.js +22 -5
- package/dist/types/custom.js.map +1 -0
- package/dist/types/date.d.ts +2 -2
- package/dist/types/date.js +2 -0
- package/dist/types/date.js.map +1 -0
- package/dist/types/enum.js +1 -0
- package/dist/types/enum.js.map +1 -0
- package/dist/types/literal.js +1 -0
- package/dist/types/literal.js.map +1 -0
- package/dist/types/never.js +1 -0
- package/dist/types/never.js.map +1 -0
- package/dist/types/null.d.ts +7 -0
- package/dist/types/null.js +10 -0
- package/dist/types/null.js.map +1 -0
- package/dist/types/number.js +1 -0
- package/dist/types/number.js.map +1 -0
- package/dist/types/object.d.ts +42 -15
- package/dist/types/object.js +19 -2
- package/dist/types/object.js.map +1 -0
- package/dist/types/string.js +1 -0
- package/dist/types/string.js.map +1 -0
- package/dist/types/temporal.d.ts +8 -8
- package/dist/types/temporal.js +1 -0
- package/dist/types/temporal.js.map +1 -0
- package/dist/types/tuple.d.ts +1 -1
- package/dist/types/tuple.js +1 -0
- package/dist/types/tuple.js.map +1 -0
- package/dist/types/union.d.ts +1 -1
- package/dist/types/union.js +1 -0
- package/dist/types/union.js.map +1 -0
- package/package.json +13 -26
- package/src/index.ts +14 -0
- package/src/temporal/global.ts +32 -0
- package/src/temporal/polyfill.ts +31 -0
- package/src/types/_convert.ts +68 -0
- package/src/types/_type.ts +46 -0
- package/src/types/_utils.ts +24 -0
- package/src/types/any.ts +12 -0
- package/src/types/array.ts +53 -0
- package/src/types/base.ts +209 -0
- package/src/types/boolean.ts +12 -0
- package/src/types/custom.ts +109 -0
- package/src/types/date.ts +23 -0
- package/src/types/enum.ts +23 -0
- package/src/types/literal.ts +18 -0
- package/src/types/never.ts +12 -0
- package/src/types/null.ts +14 -0
- package/src/types/number.ts +73 -0
- package/src/types/object.ts +225 -0
- package/src/types/string.ts +95 -0
- package/src/types/temporal.ts +155 -0
- package/src/types/tuple.ts +49 -0
- package/src/types/union.ts +120 -0
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import type { Temporal } from 'temporal-spec'
|
|
2
|
+
import type { ZodMiniString } from 'zod/mini'
|
|
3
|
+
import { iso, regex, string } from 'zod/mini'
|
|
4
|
+
|
|
5
|
+
import { CustomType, TransformType } from './custom.ts'
|
|
6
|
+
|
|
7
|
+
type Types = Exclude<
|
|
8
|
+
keyof typeof Temporal,
|
|
9
|
+
'Now' | 'Instant' | 'Calendar' | 'TimeZone'
|
|
10
|
+
>
|
|
11
|
+
|
|
12
|
+
type TemporalTransformer<T extends Types> = {
|
|
13
|
+
decode: (value: string) => ReturnType<(typeof Temporal)[T]['from']>
|
|
14
|
+
encode: (value: ReturnType<(typeof Temporal)[T]['from']>) => string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const createTemporalTransformer = <T extends Types>(
|
|
18
|
+
implementation: typeof Temporal,
|
|
19
|
+
type: T,
|
|
20
|
+
decode = (value: string) => implementation[type].from(value),
|
|
21
|
+
encode = (value: ReturnType<(typeof Temporal)[T]['from']>) =>
|
|
22
|
+
value.toString({
|
|
23
|
+
calendarName: 'never',
|
|
24
|
+
smallestUnit: 'microsecond',
|
|
25
|
+
timeZoneName: 'never',
|
|
26
|
+
}),
|
|
27
|
+
) => {
|
|
28
|
+
return { decode, encode } as TemporalTransformer<T>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
type EncodeType = ZodMiniString<string>
|
|
32
|
+
|
|
33
|
+
export class PlainDateType extends TransformType<
|
|
34
|
+
Temporal.PlainDate,
|
|
35
|
+
EncodeType
|
|
36
|
+
> {
|
|
37
|
+
static factory(implementation: typeof Temporal): PlainDateType {
|
|
38
|
+
const transformer = createTemporalTransformer(implementation, 'PlainDate')
|
|
39
|
+
return CustomType.factory<Temporal.PlainDate, EncodeType>({
|
|
40
|
+
decode: transformer.decode,
|
|
41
|
+
encode: transformer.encode,
|
|
42
|
+
type: iso.date(),
|
|
43
|
+
error: 'Invalid date format',
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class PlainDateTimeType extends TransformType<
|
|
49
|
+
Temporal.PlainDateTime,
|
|
50
|
+
EncodeType
|
|
51
|
+
> {
|
|
52
|
+
static factory(implementation: typeof Temporal): PlainDateTimeType {
|
|
53
|
+
const transformer = createTemporalTransformer(
|
|
54
|
+
implementation,
|
|
55
|
+
'PlainDateTime',
|
|
56
|
+
)
|
|
57
|
+
return CustomType.factory<Temporal.PlainDateTime, EncodeType>({
|
|
58
|
+
decode: transformer.decode,
|
|
59
|
+
encode: transformer.encode,
|
|
60
|
+
type: iso.datetime({ local: true }),
|
|
61
|
+
error: 'Invalid datetime format',
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export class ZonedDateTimeType extends TransformType<
|
|
67
|
+
Temporal.ZonedDateTime,
|
|
68
|
+
EncodeType
|
|
69
|
+
> {
|
|
70
|
+
static factory(implementation: typeof Temporal): ZonedDateTimeType {
|
|
71
|
+
const transformer = createTemporalTransformer(
|
|
72
|
+
implementation,
|
|
73
|
+
'ZonedDateTime',
|
|
74
|
+
(value) => implementation.Instant.from(value).toZonedDateTimeISO('UTC'),
|
|
75
|
+
(value) =>
|
|
76
|
+
value
|
|
77
|
+
.withTimeZone('UTC')
|
|
78
|
+
.toString({
|
|
79
|
+
smallestUnit: 'milliseconds',
|
|
80
|
+
timeZoneName: 'never',
|
|
81
|
+
calendarName: 'never',
|
|
82
|
+
offset: 'never',
|
|
83
|
+
}) + 'Z',
|
|
84
|
+
)
|
|
85
|
+
return CustomType.factory<Temporal.ZonedDateTime, EncodeType>({
|
|
86
|
+
decode: transformer.decode,
|
|
87
|
+
encode: transformer.encode,
|
|
88
|
+
type: iso.datetime(),
|
|
89
|
+
error: 'Invalid datetime format',
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export class PlainTimeType extends TransformType<
|
|
95
|
+
Temporal.PlainTime,
|
|
96
|
+
EncodeType
|
|
97
|
+
> {
|
|
98
|
+
static factory(implementation: typeof Temporal): PlainTimeType {
|
|
99
|
+
const transformer = createTemporalTransformer(implementation, 'PlainTime')
|
|
100
|
+
return CustomType.factory<Temporal.PlainTime, EncodeType>({
|
|
101
|
+
decode: transformer.decode,
|
|
102
|
+
encode: transformer.encode,
|
|
103
|
+
type: iso.time(),
|
|
104
|
+
error: 'Invalid time format',
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export class DurationType extends TransformType<Temporal.Duration, EncodeType> {
|
|
110
|
+
static factory(implementation: typeof Temporal): DurationType {
|
|
111
|
+
const transformer = createTemporalTransformer(implementation, 'Duration')
|
|
112
|
+
return CustomType.factory<Temporal.Duration, EncodeType>({
|
|
113
|
+
decode: transformer.decode,
|
|
114
|
+
encode: transformer.encode,
|
|
115
|
+
type: iso.duration(),
|
|
116
|
+
error: 'Invalid duration format',
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export class PlainYearMonthType extends TransformType<
|
|
122
|
+
Temporal.PlainYearMonth,
|
|
123
|
+
EncodeType
|
|
124
|
+
> {
|
|
125
|
+
static factory(implementation: typeof Temporal): PlainYearMonthType {
|
|
126
|
+
const transformer = createTemporalTransformer(
|
|
127
|
+
implementation,
|
|
128
|
+
'PlainYearMonth',
|
|
129
|
+
)
|
|
130
|
+
return CustomType.factory<Temporal.PlainYearMonth, EncodeType>({
|
|
131
|
+
decode: transformer.decode,
|
|
132
|
+
encode: transformer.encode,
|
|
133
|
+
type: string().check(regex(/^\d{4}-\d{2}$/)),
|
|
134
|
+
error: 'Invalid year-month format',
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export class PlainMonthDayType extends TransformType<
|
|
140
|
+
Temporal.PlainMonthDay,
|
|
141
|
+
EncodeType
|
|
142
|
+
> {
|
|
143
|
+
static factory(implementation: typeof Temporal): PlainMonthDayType {
|
|
144
|
+
const transformer = createTemporalTransformer(
|
|
145
|
+
implementation,
|
|
146
|
+
'PlainMonthDay',
|
|
147
|
+
)
|
|
148
|
+
return CustomType.factory<Temporal.PlainMonthDay, EncodeType>({
|
|
149
|
+
decode: transformer.decode,
|
|
150
|
+
encode: transformer.encode,
|
|
151
|
+
type: string().check(regex(/^\d{2}-\d{2}$/)),
|
|
152
|
+
error: 'Invalid month-day format',
|
|
153
|
+
})
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ArrayMap } from '@nmtjs/common'
|
|
2
|
+
import type { ZodMiniTuple } from 'zod/mini'
|
|
3
|
+
import { tuple as zodTuple } from 'zod/mini'
|
|
4
|
+
|
|
5
|
+
import type { ZodPlainType } from './_utils.ts'
|
|
6
|
+
import { BaseType } from './base.ts'
|
|
7
|
+
|
|
8
|
+
export class TupleType<
|
|
9
|
+
T extends readonly [BaseType, ...BaseType[]] = readonly [
|
|
10
|
+
BaseType,
|
|
11
|
+
...BaseType[],
|
|
12
|
+
],
|
|
13
|
+
R extends BaseType | null = BaseType | null,
|
|
14
|
+
> extends BaseType<
|
|
15
|
+
R extends BaseType
|
|
16
|
+
? ZodMiniTuple<ArrayMap<T, 'encodeZodType'>, R['encodeZodType']>
|
|
17
|
+
: ZodMiniTuple<ArrayMap<T, 'encodeZodType'>, null>,
|
|
18
|
+
R extends BaseType
|
|
19
|
+
? ZodMiniTuple<ArrayMap<T, 'decodeZodType'>, R['decodeZodType']>
|
|
20
|
+
: ZodMiniTuple<ArrayMap<T, 'decodeZodType'>, null>,
|
|
21
|
+
{ elements: T; rest?: R },
|
|
22
|
+
R extends BaseType
|
|
23
|
+
? ZodPlainType<
|
|
24
|
+
ZodMiniTuple<ArrayMap<T, 'encodeZodType'>, R['encodeZodType']>
|
|
25
|
+
>
|
|
26
|
+
: ZodPlainType<ZodMiniTuple<ArrayMap<T, 'encodeZodType'>, null>>,
|
|
27
|
+
R extends BaseType
|
|
28
|
+
? ZodPlainType<
|
|
29
|
+
ZodMiniTuple<ArrayMap<T, 'decodeZodType'>, R['decodeZodType']>
|
|
30
|
+
>
|
|
31
|
+
: ZodPlainType<ZodMiniTuple<ArrayMap<T, 'decodeZodType'>, null>>
|
|
32
|
+
> {
|
|
33
|
+
static factory<
|
|
34
|
+
T extends readonly [BaseType, ...BaseType[]],
|
|
35
|
+
R extends BaseType | null = null,
|
|
36
|
+
>(elements: T, rest: R = null as R) {
|
|
37
|
+
const encode = elements.map((el) => el.encodeZodType)
|
|
38
|
+
const decode = elements.map((el) => el.decodeZodType)
|
|
39
|
+
return new TupleType<T, R>({
|
|
40
|
+
// @ts-expect-error
|
|
41
|
+
encodeZodType: zodTuple(encode, rest?.encodeZodType),
|
|
42
|
+
// @ts-expect-error
|
|
43
|
+
decodeZodType: zodTuple(decode, rest?.decodeZodType),
|
|
44
|
+
props: { elements, rest },
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const tuple = TupleType.factory
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { ArrayMap } from '@nmtjs/common'
|
|
2
|
+
import type {
|
|
3
|
+
ZodMiniDiscriminatedUnion,
|
|
4
|
+
ZodMiniIntersection,
|
|
5
|
+
ZodMiniUnion,
|
|
6
|
+
} from 'zod/mini'
|
|
7
|
+
import {
|
|
8
|
+
discriminatedUnion as zodDiscriminatedUnion,
|
|
9
|
+
intersection as zodIntersection,
|
|
10
|
+
union as zodUnion,
|
|
11
|
+
} from 'zod/mini'
|
|
12
|
+
|
|
13
|
+
import type { ZodPlainType } from './_utils.ts'
|
|
14
|
+
import type { BaseTypeAny } from './base.ts'
|
|
15
|
+
import type { LiteralType } from './literal.ts'
|
|
16
|
+
import type { ObjectType, ObjectTypeProps } from './object.ts'
|
|
17
|
+
import { BaseType } from './base.ts'
|
|
18
|
+
|
|
19
|
+
export class UnionType<
|
|
20
|
+
T extends readonly [BaseType, ...BaseType[]] = readonly [
|
|
21
|
+
BaseType,
|
|
22
|
+
...BaseType[],
|
|
23
|
+
],
|
|
24
|
+
> extends BaseType<
|
|
25
|
+
ZodMiniUnion<ArrayMap<T, 'encodeZodType'>>,
|
|
26
|
+
ZodMiniUnion<ArrayMap<T, 'decodeZodType'>>,
|
|
27
|
+
{ options: T },
|
|
28
|
+
ZodPlainType<ZodMiniUnion<ArrayMap<T, 'encodeZodType'>>>,
|
|
29
|
+
ZodPlainType<ZodMiniUnion<ArrayMap<T, 'decodeZodType'>>>
|
|
30
|
+
> {
|
|
31
|
+
static factory<
|
|
32
|
+
T extends readonly [BaseType, ...BaseType[]] = readonly [
|
|
33
|
+
BaseType,
|
|
34
|
+
...BaseType[],
|
|
35
|
+
],
|
|
36
|
+
>(...options: T) {
|
|
37
|
+
const encode = options.map((t) => t.encodeZodType) as ArrayMap<
|
|
38
|
+
T,
|
|
39
|
+
'encodeZodType'
|
|
40
|
+
>
|
|
41
|
+
const decode = options.map((t) => t.decodeZodType) as ArrayMap<
|
|
42
|
+
T,
|
|
43
|
+
'decodeZodType'
|
|
44
|
+
>
|
|
45
|
+
return new UnionType<T>({
|
|
46
|
+
encodeZodType: zodUnion(encode),
|
|
47
|
+
decodeZodType: zodUnion(decode),
|
|
48
|
+
props: { options },
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class IntersactionType<
|
|
54
|
+
T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],
|
|
55
|
+
> extends BaseType<
|
|
56
|
+
ZodMiniIntersection<T[0]['encodeZodType'], T[1]['encodeZodType']>,
|
|
57
|
+
ZodMiniIntersection<T[0]['decodeZodType'], T[1]['decodeZodType']>,
|
|
58
|
+
{ options: T }
|
|
59
|
+
> {
|
|
60
|
+
static factory<
|
|
61
|
+
T extends readonly [BaseType, BaseType] = readonly [BaseType, BaseType],
|
|
62
|
+
>(...options: T) {
|
|
63
|
+
const [first, second] = options
|
|
64
|
+
return new IntersactionType<T>({
|
|
65
|
+
encodeZodType: zodIntersection(first.encodeZodType, second.encodeZodType),
|
|
66
|
+
decodeZodType: zodIntersection(first.decodeZodType, second.decodeZodType),
|
|
67
|
+
props: { options },
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type DiscriminatedUnionProperties<K extends string = string> = {
|
|
73
|
+
[OK in K]: LiteralType<string>
|
|
74
|
+
} & {
|
|
75
|
+
[OK in string]: any
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type DiscriminatedUnionOptionType<K extends string> = ObjectType<
|
|
79
|
+
ObjectTypeProps & { [_ in K]: BaseTypeAny }
|
|
80
|
+
>
|
|
81
|
+
|
|
82
|
+
export class DiscriminatedUnionType<
|
|
83
|
+
K extends string = string,
|
|
84
|
+
T extends
|
|
85
|
+
readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],
|
|
86
|
+
> extends BaseType<
|
|
87
|
+
ZodMiniDiscriminatedUnion<ArrayMap<T, 'encodeZodType'>>,
|
|
88
|
+
ZodMiniDiscriminatedUnion<ArrayMap<T, 'decodeZodType'>>,
|
|
89
|
+
{ key: K; options: T },
|
|
90
|
+
ZodPlainType<ZodMiniDiscriminatedUnion<ArrayMap<T, 'encodeZodType'>>>,
|
|
91
|
+
ZodPlainType<ZodMiniDiscriminatedUnion<ArrayMap<T, 'decodeZodType'>>>
|
|
92
|
+
> {
|
|
93
|
+
static factory<
|
|
94
|
+
K extends string = string,
|
|
95
|
+
T extends
|
|
96
|
+
readonly DiscriminatedUnionOptionType<K>[] = DiscriminatedUnionOptionType<K>[],
|
|
97
|
+
>(key: K, ...options: T) {
|
|
98
|
+
const encode = options.map((t) => t.encodeZodType) as ArrayMap<
|
|
99
|
+
T,
|
|
100
|
+
'encodeZodType'
|
|
101
|
+
>
|
|
102
|
+
const decode = options.map((t) => t.decodeZodType) as ArrayMap<
|
|
103
|
+
T,
|
|
104
|
+
'decodeZodType'
|
|
105
|
+
>
|
|
106
|
+
return new DiscriminatedUnionType<K, T>({
|
|
107
|
+
// @ts-expect-error
|
|
108
|
+
encodeZodType: zodDiscriminatedUnion(key, encode),
|
|
109
|
+
// @ts-expect-error
|
|
110
|
+
decodeZodType: zodDiscriminatedUnion(key, decode),
|
|
111
|
+
props: { key, options },
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const union = UnionType.factory
|
|
117
|
+
export const or = UnionType.factory
|
|
118
|
+
export const intersection = IntersactionType.factory
|
|
119
|
+
export const and = IntersactionType.factory
|
|
120
|
+
export const discriminatedUnion = DiscriminatedUnionType.factory
|