@@ -13,39 +13,21 @@ const resourceReference = (resourcePath: ReadonlyArray<string>) => ['resources',
13
13
* Get the reference to the type representing the resource function this resource
14
14
*/
15
15
export function getResourceTypeReference ( resourceConfig : ResourceConfig , resourcePath : ReadonlyArray < string > ) {
16
- function toPropertyTypePath ( path : ReadonlyArray < string > ) : string {
17
- assert ( path . length >= 1 , 'expected resource path to be a not empty array' ) ;
18
-
19
- if ( path . length === 1 ) {
20
- return path [ 0 ] ;
21
- }
22
-
23
- return `$PropertyType<${ toPropertyTypePath ( path . slice ( 0 , - 1 ) ) } , '${ path . slice ( - 1 ) } '>` ;
24
- }
25
-
26
- return toPropertyTypePath ( [ 'ResourcesType' , ...resourcePath ] ) ;
16
+ return `ResourcesType${ resourcePath . map ( segment => `['${ segment } ']` ) . join ( ) } ` ;
27
17
}
28
18
29
19
function getResourceArg ( resourceConfig : ResourceConfig , resourcePath : ReadonlyArray < string > ) {
30
20
// TODO: We assume that the resource accepts a single dict argument. Let's
31
21
// make thie configurable to handle resources that use seperate arguments.
32
- return `\
33
- $Call<
34
- ExtractArg,
35
- [${ getResourceTypeReference ( resourceConfig , resourcePath ) } ]
36
- >` ;
22
+ return `Parameters<${ getResourceTypeReference ( resourceConfig , resourcePath ) } >[0]` ;
37
23
}
38
24
39
25
/**
40
26
* Extract the type T from a Set<T> resource (in this case a batchKey's resource)
41
27
* using its `.has(T)`'s function paremeter type
42
28
*/
43
29
export function getNewKeyTypeFromBatchKeySetType ( batchKey : string , resourceArgs : string ) {
44
- return `\
45
- $Call<
46
- ExtractArg,
47
- [$PropertyType<$PropertyType<${ resourceArgs } , '${ batchKey } '>, 'has'>]
48
- >` ;
30
+ return `Parameters<${ resourceArgs } ['${ batchKey } ']['has']]>[0]` ;
49
31
}
50
32
51
33
export function getLoaderTypeKey ( resourceConfig : ResourceConfig , resourcePath : ReadonlyArray < string > ) {
@@ -55,8 +37,8 @@ export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: R
55
37
56
38
if ( resourceConfig . isBatchResource ) {
57
39
// Extract newKeyType from the batch key's Array's type
58
- // We add NonMaybeType before batch key element type to force the batch key to be required, regardless if the OpenAPI spec specifies it as being optional
59
- let newKeyType = `${ resourceConfig . newKey } : $ElementType<$NonMaybeType<$PropertyType< $ {resourceArgs } , '${ resourceConfig . batchKey } '>>, 0> ` ;
40
+ // We add NonNullable before batch key element type to force the batch key to be required, regardless if the OpenAPI spec specifies it as being optional
41
+ let newKeyType = `${ resourceConfig . newKey } : NonNullable< $ {resourceArgs } [ '${ resourceConfig . batchKey } ']>[0] ` ;
60
42
61
43
if ( resourceConfig . isBatchKeyASet ) {
62
44
/**
@@ -69,28 +51,14 @@ export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: R
69
51
) } `;
70
52
}
71
53
72
- return `{|
73
- ...$Diff<${ resourceArgs } , {
74
- ${ resourceConfig . batchKey } : $PropertyType<${ resourceArgs } , '${ resourceConfig . batchKey } '>
75
- }>,
76
- ...{| ${ newKeyType } |}
77
- |}` ;
54
+ return `Exclude<${ resourceArgs } , '${ resourceConfig . batchKey } '> & ${ newKeyType } ` ;
78
55
}
79
56
80
57
return resourceArgs ;
81
58
}
82
59
83
60
export function getLoaderTypeVal ( resourceConfig : ResourceConfig , resourcePath : ReadonlyArray < string > ) {
84
- // TODO: We assume that the resource accepts a single dict argument. Let's
85
- // make this configurable to handle resources that use seperate arguments.
86
- const resourceArgs = getResourceArg ( resourceConfig , resourcePath ) ;
87
-
88
- // TODO: DRY up in codegen to something like RetVal<resource>
89
- let retVal = `\
90
- $Call<
91
- ExtractPromisedReturnValue<[${ resourceArgs } ]>,
92
- ${ getResourceTypeReference ( resourceConfig , resourcePath ) }
93
- >` ;
61
+ let retVal = `PromisedReturnType<${ getResourceTypeReference ( resourceConfig , resourcePath ) } >` ;
94
62
95
63
if ( resourceConfig . isBatchResource ) {
96
64
/**
@@ -115,9 +83,9 @@ export function getLoaderTypeVal(resourceConfig: ResourceConfig, resourcePath: R
115
83
* ```
116
84
*/
117
85
if ( resourceConfig . nestedPath ) {
118
- retVal = `$PropertyType< $ {retVal } , '${ resourceConfig . nestedPath } '> ` ;
86
+ retVal = `${ retVal } [ '${ resourceConfig . nestedPath } '] ` ;
119
87
}
120
- retVal = resourceConfig . isResponseDictionary ? `$ Values< ${ retVal } > ` : `$ElementType< $ {retVal } , 0> ` ;
88
+ retVal = resourceConfig . isResponseDictionary ? `Values[ ${ retVal } ] ` : `${ retVal } [0] ` ;
121
89
}
122
90
123
91
return retVal ;
@@ -147,17 +115,17 @@ export function getLoadersTypeMap(
147
115
const nextValues = _ . uniq ( paths . map ( ( p ) => p [ 0 ] ) ) ;
148
116
149
117
const objectProperties : ReadonlyArray < string > = nextValues . map (
150
- ( nextVal ) =>
118
+ ( nextVal : any ) =>
151
119
`${ nextVal } : ${ getLoadersTypeMap (
152
120
config ,
153
121
paths . filter ( ( p ) => p [ 0 ] === nextVal ) . map ( ( p ) => p . slice ( 1 ) ) ,
154
122
[ ...current , nextVal ] ,
155
123
) } ,`,
156
124
) ;
157
125
158
- return `$ReadOnly<{|
126
+ return `Readonly<{
159
127
${ objectProperties . join ( '\n' ) }
160
- | }>` ;
128
+ }>` ;
161
129
}
162
130
163
131
export function getResourceTypings (
0 commit comments