1
1
import fs from 'fs' ;
2
2
import path from 'path' ;
3
3
import { sync as mkdirp } from 'mkdirp' ;
4
- import * as zod from 'zod' ;
4
+ import { z } from 'zod' ;
5
5
6
- const LegacyConfigModel = zod . object ( {
7
- registry : zod . string ( ) . optional ( ) ,
8
- token : zod . string ( ) . optional ( ) ,
6
+ const LegacyConfigModel = z . object ( {
7
+ registry : z . string ( ) . optional ( ) ,
8
+ token : z . string ( ) . optional ( ) ,
9
9
} ) ;
10
10
11
- const ConfigModel = zod . object ( {
12
- registry : zod
11
+ const ConfigModel = z . object ( {
12
+ registry : z
13
13
. object ( {
14
- endpoint : zod . string ( ) . url ( ) . optional ( ) ,
15
- accessToken : zod . string ( ) . optional ( ) ,
14
+ endpoint : z . string ( ) . url ( ) . optional ( ) ,
15
+ accessToken : z . string ( ) . optional ( ) ,
16
16
} )
17
17
. optional ( ) ,
18
- cdn : zod
18
+ cdn : z
19
19
. object ( {
20
- endpoint : zod . string ( ) . url ( ) . optional ( ) ,
21
- accessToken : zod . string ( ) . optional ( ) ,
20
+ endpoint : z . string ( ) . url ( ) . optional ( ) ,
21
+ accessToken : z . string ( ) . optional ( ) ,
22
22
} )
23
23
. optional ( ) ,
24
24
} ) ;
25
25
26
- const getAllowedConfigKeys = < TConfig extends zod . ZodObject < any > > (
26
+ const getAllowedConfigKeys = < TConfig extends z . ZodObject < any > > (
27
27
config : TConfig ,
28
28
) : Set < GetConfigurationKeys < TConfig > > => {
29
29
const keys = new Set < GetConfigurationKeys < TConfig > > ( ) ;
30
30
31
- const traverse = ( obj : zod . ZodObject < Record < string , any > > , path : Array < string > = [ ] ) : string => {
32
- if ( obj instanceof zod . ZodObject ) {
31
+ const traverse = ( obj : z . ZodObject < Record < string , any > > , path : Array < string > = [ ] ) : string => {
32
+ if ( obj instanceof z . ZodObject ) {
33
33
const shape = obj . shape ;
34
34
for ( const [ key , value ] of Object . entries ( shape ) ) {
35
35
traverse ( value , [ ...path , key ] ) ;
@@ -46,22 +46,22 @@ const getAllowedConfigKeys = <TConfig extends zod.ZodObject<any>>(
46
46
return keys ;
47
47
} ;
48
48
49
- export type ConfigModelType = zod . TypeOf < typeof ConfigModel > ;
49
+ export type ConfigModelType = z . TypeOf < typeof ConfigModel > ;
50
50
51
- type BuildPropertyPath < TObject extends zod . ZodObject < any > > = `.${GetConfigurationKeys < TObject > } `;
51
+ type BuildPropertyPath < TObject extends z . ZodObject < any > > = `.${GetConfigurationKeys < TObject > } `;
52
52
53
53
type GetConfigurationKeys <
54
- T extends zod . ZodObject < {
55
- [ key : string ] : zod . ZodType < any , any > ;
54
+ T extends z . ZodObject < {
55
+ [ key : string ] : z . ZodType < any , any > ;
56
56
} > ,
57
57
> =
58
- T extends zod . ZodObject < infer TObjectShape >
58
+ T extends z . ZodObject < infer TObjectShape >
59
59
? TObjectShape extends Record < infer TKey , infer TObjectPropertyType >
60
60
? TKey extends string
61
- ? `${TKey } ${TObjectPropertyType extends zod . ZodObject < any >
61
+ ? `${TKey } ${TObjectPropertyType extends z . ZodObject < any >
62
62
? BuildPropertyPath < TObjectPropertyType >
63
- : TObjectPropertyType extends zod . ZodOptional < infer TOptionalInnerObjectPropertyType >
64
- ? TOptionalInnerObjectPropertyType extends zod . ZodObject < any >
63
+ : TObjectPropertyType extends z . ZodOptional < infer TOptionalInnerObjectPropertyType >
64
+ ? TOptionalInnerObjectPropertyType extends z . ZodObject < any >
65
65
? BuildPropertyPath < TOptionalInnerObjectPropertyType >
66
66
: ''
67
67
: '' } `
@@ -71,19 +71,19 @@ type GetConfigurationKeys<
71
71
72
72
type GetZodValueType <
73
73
TString extends string ,
74
- ConfigurationModelType extends zod . ZodObject < any > ,
74
+ ConfigurationModelType extends z . ZodObject < any > ,
75
75
> = TString extends `${infer TKey } .${infer TNextKey } `
76
- ? ConfigurationModelType extends zod . ZodObject < infer InnerType >
77
- ? InnerType [ TKey ] extends zod . ZodObject < any >
76
+ ? ConfigurationModelType extends z . ZodObject < infer InnerType >
77
+ ? InnerType [ TKey ] extends z . ZodObject < any >
78
78
? GetZodValueType < TNextKey , InnerType [ TKey ] >
79
- : InnerType [ TKey ] extends zod . ZodOptional < infer OptionalInner >
80
- ? OptionalInner extends zod . ZodObject < any >
79
+ : InnerType [ TKey ] extends z . ZodOptional < infer OptionalInner >
80
+ ? OptionalInner extends z . ZodObject < any >
81
81
? GetZodValueType < TNextKey , OptionalInner >
82
82
: never
83
83
: never
84
84
: never
85
- : ConfigurationModelType extends zod . ZodObject < infer InnerType >
86
- ? zod . TypeOf < InnerType [ TString ] >
85
+ : ConfigurationModelType extends z . ZodObject < infer InnerType >
86
+ ? z . TypeOf < InnerType [ TString ] >
87
87
: never ;
88
88
89
89
export type GetConfigurationValueType < TString extends string > = GetZodValueType <
0 commit comments