@@ -931,11 +931,16 @@ const generateQueryHook = async (
931
931
932
932
const doc = jsDoc ( { summary, deprecated } ) ;
933
933
934
- if (
935
- verb === Verbs . GET ||
934
+ let implementation = '' ;
935
+ let mutators = undefined ;
936
+
937
+ const isQuery =
938
+ ( Verbs . GET === verb &&
939
+ ( override . query . useQuery || override . query . useInfinite ) ) ||
936
940
operationQueryOptions ?. useInfinite ||
937
- operationQueryOptions ?. useQuery
938
- ) {
941
+ operationQueryOptions ?. useQuery ;
942
+
943
+ if ( isQuery ) {
939
944
const queryKeyMutator = query . queryKey
940
945
? await generateMutator ( {
941
946
output,
@@ -1009,7 +1014,7 @@ const generateQueryHook = async (
1009
1014
queryParams ? ', ...(params ? [params]: [])' : ''
1010
1015
} ${ body . implementation ? `, ${ body . implementation } ` : '' } ] as const;`;
1011
1016
1012
- const implementation = `${ ! queryKeyMutator ? queryKeyFn : '' }
1017
+ implementation + = `${ ! queryKeyMutator ? queryKeyFn : '' }
1013
1018
1014
1019
1015
1020
${ queries . reduce (
@@ -1040,82 +1045,84 @@ const generateQueryHook = async (
1040
1045
) }
1041
1046
` ;
1042
1047
1043
- return {
1044
- implementation,
1045
- mutators :
1046
- queryOptionsMutator || queryKeyMutator
1047
- ? [
1048
- ...( queryOptionsMutator ? [ queryOptionsMutator ] : [ ] ) ,
1049
- ...( queryKeyMutator ? [ queryKeyMutator ] : [ ] ) ,
1050
- ]
1051
- : undefined ,
1052
- } ;
1048
+ mutators =
1049
+ queryOptionsMutator || queryKeyMutator
1050
+ ? [
1051
+ ...( queryOptionsMutator ? [ queryOptionsMutator ] : [ ] ) ,
1052
+ ...( queryKeyMutator ? [ queryKeyMutator ] : [ ] ) ,
1053
+ ]
1054
+ : undefined ;
1053
1055
}
1054
1056
1055
- const mutationOptionsMutator = query . mutationOptions
1056
- ? await generateMutator ( {
1057
- output,
1058
- mutator : query . mutationOptions ,
1059
- name : `${ operationName } MutationOptions` ,
1060
- workspace : context . workspace ,
1061
- tsconfig : context . tsconfig ,
1062
- } )
1063
- : undefined ;
1064
-
1065
- const definitions = props
1066
- . map ( ( { definition, type } ) =>
1067
- type === GetterPropType . BODY
1068
- ? mutator ?. bodyTypeName
1069
- ? `data: ${ mutator . bodyTypeName } <${ body . definition } >`
1070
- : `data: ${ body . definition } `
1071
- : definition ,
1072
- )
1073
- . join ( ';' ) ;
1074
-
1075
- const properties = props
1076
- . map ( ( { name, type } ) => ( type === GetterPropType . BODY ? 'data' : name ) )
1077
- . join ( ',' ) ;
1057
+ const isMutation =
1058
+ ( verb !== Verbs . GET && override . query . useMutation ) ||
1059
+ operationQueryOptions ?. useMutation ;
1078
1060
1079
- let errorType = `AxiosError<${ response . definition . errors || 'unknown' } >` ;
1061
+ if ( isMutation ) {
1062
+ const mutationOptionsMutator = query . mutationOptions
1063
+ ? await generateMutator ( {
1064
+ output,
1065
+ mutator : query . mutationOptions ,
1066
+ name : `${ operationName } MutationOptions` ,
1067
+ workspace : context . workspace ,
1068
+ tsconfig : context . tsconfig ,
1069
+ } )
1070
+ : undefined ;
1080
1071
1081
- if ( mutator ) {
1082
- errorType = mutator . hasErrorType
1083
- ? `${ mutator . default ? pascal ( operationName ) : '' } ErrorType<${
1084
- response . definition . errors || 'unknown'
1085
- } >`
1086
- : response . definition . errors || 'unknown' ;
1087
- }
1072
+ const definitions = props
1073
+ . map ( ( { definition, type } ) =>
1074
+ type === GetterPropType . BODY
1075
+ ? mutator ?. bodyTypeName
1076
+ ? `data: ${ mutator . bodyTypeName } <${ body . definition } >`
1077
+ : `data: ${ body . definition } `
1078
+ : definition ,
1079
+ )
1080
+ . join ( ';' ) ;
1088
1081
1089
- const dataType = mutator ?. isHook
1090
- ? `ReturnType<typeof use ${ pascal ( operationName ) } Hook>`
1091
- : `typeof ${ operationName } ` ;
1082
+ const properties = props
1083
+ . map ( ( { name , type } ) => ( type === GetterPropType . BODY ? 'data' : name ) )
1084
+ . join ( ',' ) ;
1092
1085
1093
- const mutationOptionFnReturnType = getQueryOptionsDefinition ( {
1094
- operationName,
1095
- definitions,
1096
- mutator,
1097
- hasSvelteQueryV4,
1098
- } ) ;
1086
+ let errorType = `AxiosError<${ response . definition . errors || 'unknown' } >` ;
1099
1087
1100
- const mutationArguments = generateQueryArguments ( {
1101
- operationName ,
1102
- definitions ,
1103
- mutator ,
1104
- isRequestOptions ,
1105
- hasSvelteQueryV4 ,
1106
- } ) ;
1088
+ if ( mutator ) {
1089
+ errorType = mutator . hasErrorType
1090
+ ? ` ${ mutator . default ? pascal ( operationName ) : '' } ErrorType< ${
1091
+ response . definition . errors || 'unknown'
1092
+ } >`
1093
+ : response . definition . errors || 'unknown' ;
1094
+ }
1107
1095
1108
- const mutationOptionsFnName = camel (
1109
- mutationOptionsMutator || mutator ?. isHook
1110
- ? `use-${ operationName } -mutationOptions`
1111
- : `get-${ operationName } -mutationOptions` ,
1112
- ) ;
1096
+ const dataType = mutator ?. isHook
1097
+ ? `ReturnType<typeof use${ pascal ( operationName ) } Hook>`
1098
+ : `typeof ${ operationName } ` ;
1113
1099
1114
- const mutationOptionsVarName = isRequestOptions
1115
- ? 'mutationOptions'
1116
- : 'options' ;
1100
+ const mutationOptionFnReturnType = getQueryOptionsDefinition ( {
1101
+ operationName,
1102
+ definitions,
1103
+ mutator,
1104
+ hasSvelteQueryV4,
1105
+ } ) ;
1117
1106
1118
- const mutationOptionsFn = `export const ${ mutationOptionsFnName } = <TError = ${ errorType } ,
1107
+ const mutationArguments = generateQueryArguments ( {
1108
+ operationName,
1109
+ definitions,
1110
+ mutator,
1111
+ isRequestOptions,
1112
+ hasSvelteQueryV4,
1113
+ } ) ;
1114
+
1115
+ const mutationOptionsFnName = camel (
1116
+ mutationOptionsMutator || mutator ?. isHook
1117
+ ? `use-${ operationName } -mutationOptions`
1118
+ : `get-${ operationName } -mutationOptions` ,
1119
+ ) ;
1120
+
1121
+ const mutationOptionsVarName = isRequestOptions
1122
+ ? 'mutationOptions'
1123
+ : 'options' ;
1124
+
1125
+ const mutationOptionsFn = `export const ${ mutationOptionsFnName } = <TError = ${ errorType } ,
1119
1126
${ ! definitions ? `TVariables = void,` : '' }
1120
1127
TContext = unknown>(${ mutationArguments } ): ${ mutationOptionFnReturnType } => {
1121
1128
${
@@ -1138,19 +1145,19 @@ const generateQueryHook = async (
1138
1145
1139
1146
1140
1147
const mutationFn: MutationFunction<Awaited<ReturnType<${ dataType } >>, ${
1141
- definitions ? `{${ definitions } }` : 'TVariables'
1142
- } > = (${ properties ? 'props' : '' } ) => {
1148
+ definitions ? `{${ definitions } }` : 'TVariables'
1149
+ } > = (${ properties ? 'props' : '' } ) => {
1143
1150
${ properties ? `const {${ properties } } = props ?? {};` : '' }
1144
1151
1145
1152
return ${ operationName } (${ properties } ${ properties ? ',' : '' } ${
1146
- isRequestOptions
1147
- ? ! mutator
1148
- ? `axiosOptions`
1149
- : mutator ?. hasSecondArg
1150
- ? 'requestOptions'
1153
+ isRequestOptions
1154
+ ? ! mutator
1155
+ ? `axiosOptions`
1156
+ : mutator ?. hasSecondArg
1157
+ ? 'requestOptions'
1158
+ : ''
1151
1159
: ''
1152
- : ''
1153
- } )
1160
+ } )
1154
1161
}
1155
1162
1156
1163
${
@@ -1172,9 +1179,9 @@ const generateQueryHook = async (
1172
1179
: 'customOptions'
1173
1180
} }`;
1174
1181
1175
- const operationPrefix = hasSvelteQueryV4 ? 'create' : 'use' ;
1182
+ const operationPrefix = hasSvelteQueryV4 ? 'create' : 'use' ;
1176
1183
1177
- const implementation = `
1184
+ implementation + = `
1178
1185
${ mutationOptionsFn }
1179
1186
1180
1187
export type ${ pascal (
@@ -1192,22 +1199,27 @@ ${mutationOptionsFn}
1192
1199
export type ${ pascal ( operationName ) } MutationError = ${ errorType }
1193
1200
1194
1201
${ doc } export const ${ camel (
1195
- `${ operationPrefix } -${ operationName } ` ,
1196
- ) } = <TError = ${ errorType } ,
1202
+ `${ operationPrefix } -${ operationName } ` ,
1203
+ ) } = <TError = ${ errorType } ,
1197
1204
${ ! definitions ? `TVariables = void,` : '' }
1198
1205
TContext = unknown>(${ mutationArguments } ) => {
1199
1206
1200
1207
const ${ mutationOptionsVarName } = ${ mutationOptionsFnName } (${
1201
- isRequestOptions ? 'options' : 'mutationOptions'
1202
- } );
1208
+ isRequestOptions ? 'options' : 'mutationOptions'
1209
+ } );
1203
1210
1204
1211
return ${ operationPrefix } Mutation(${ mutationOptionsVarName } );
1205
1212
}
1206
1213
` ;
1207
1214
1215
+ mutators = mutationOptionsMutator
1216
+ ? [ ...( mutators ?? [ ] ) , mutationOptionsMutator ]
1217
+ : mutators ;
1218
+ }
1219
+
1208
1220
return {
1209
1221
implementation,
1210
- mutators : mutationOptionsMutator ? [ mutationOptionsMutator ] : undefined ,
1222
+ mutators,
1211
1223
} ;
1212
1224
} ;
1213
1225
1 commit comments
vercel[bot] commentedon May 23, 2023
Successfully deployed to the following URLs:
orval – ./
orval.vercel.app
www.orval.dev
orval.dev
orval-git-master-anymaniax.vercel.app
orval-anymaniax.vercel.app