@@ -13,39 +13,21 @@ const resourceReference = (resourcePath: ReadonlyArray<string>) => ['resources',
1313 * Get the reference to the type representing the resource function this resource
1414 */
1515export 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 ( ) } ` ;
2717}
2818
2919function getResourceArg ( resourceConfig : ResourceConfig , resourcePath : ReadonlyArray < string > ) {
3020 // TODO: We assume that the resource accepts a single dict argument. Let's
3121 // 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]` ;
3723}
3824
3925/**
4026 * Extract the type T from a Set<T> resource (in this case a batchKey's resource)
4127 * using its `.has(T)`'s function paremeter type
4228 */
4329export 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]` ;
4931}
5032
5133export function getLoaderTypeKey ( resourceConfig : ResourceConfig , resourcePath : ReadonlyArray < string > ) {
@@ -55,8 +37,8 @@ export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: R
5537
5638 if ( resourceConfig . isBatchResource ) {
5739 // 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] ` ;
6042
6143 if ( resourceConfig . isBatchKeyASet ) {
6244 /**
@@ -69,28 +51,14 @@ export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: R
6951 ) } `;
7052 }
7153
72- return `{|
73- ...$Diff<${ resourceArgs } , {
74- ${ resourceConfig . batchKey } : $PropertyType<${ resourceArgs } , '${ resourceConfig . batchKey } '>
75- }>,
76- ...{| ${ newKeyType } |}
77- |}` ;
54+ return `Exclude<${ resourceArgs } , '${ resourceConfig . batchKey } '> & ${ newKeyType } ` ;
7855 }
7956
8057 return resourceArgs ;
8158}
8259
8360export 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 ) } >` ;
9462
9563 if ( resourceConfig . isBatchResource ) {
9664 /**
@@ -115,9 +83,9 @@ export function getLoaderTypeVal(resourceConfig: ResourceConfig, resourcePath: R
11583 * ```
11684 */
11785 if ( resourceConfig . nestedPath ) {
118- retVal = `$PropertyType< $ {retVal } , '${ resourceConfig . nestedPath } '> ` ;
86+ retVal = `${ retVal } [ '${ resourceConfig . nestedPath } '] ` ;
11987 }
120- retVal = resourceConfig . isResponseDictionary ? `$ Values< ${ retVal } > ` : `$ElementType< $ {retVal } , 0> ` ;
88+ retVal = resourceConfig . isResponseDictionary ? `Values[ ${ retVal } ] ` : `${ retVal } [0] ` ;
12189 }
12290
12391 return retVal ;
@@ -147,17 +115,17 @@ export function getLoadersTypeMap(
147115 const nextValues = _ . uniq ( paths . map ( ( p ) => p [ 0 ] ) ) ;
148116
149117 const objectProperties : ReadonlyArray < string > = nextValues . map (
150- ( nextVal ) =>
118+ ( nextVal : any ) =>
151119 `${ nextVal } : ${ getLoadersTypeMap (
152120 config ,
153121 paths . filter ( ( p ) => p [ 0 ] === nextVal ) . map ( ( p ) => p . slice ( 1 ) ) ,
154122 [ ...current , nextVal ] ,
155123 ) } ,`,
156124 ) ;
157125
158- return `$ReadOnly<{|
126+ return `Readonly<{
159127 ${ objectProperties . join ( '\n' ) }
160- | }>` ;
128+ }>` ;
161129}
162130
163131export function getResourceTypings (
0 commit comments