mulink 1.0.7 → 1.0.9

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.
@@ -3769,12 +3769,36 @@ const searchParamsParser = {
3769
3769
  filter: parseAsString.withDefault(''),
3770
3770
  }` : ""}
3771
3771
 
3772
- ${hasMutations ? `// Error handling utility
3772
+ // Error handling utility
3773
3773
  function handleActionError(error: unknown): never {
3774
3774
  const message = error instanceof Error ? error.message : 'An unexpected error occurred'
3775
3775
  toast.error(message)
3776
3776
  throw new Error(message)
3777
- }` : ""}
3777
+ }
3778
+
3779
+ type ActionResultLike<T> = {
3780
+ data?: T
3781
+ serverError?: unknown
3782
+ validationErrors?: unknown
3783
+ }
3784
+
3785
+ async function resolveActionResult<T>(actionPromise: Promise<any>): Promise<T> {
3786
+ const result = await actionPromise
3787
+ if (result && typeof result === 'object') {
3788
+ const actionResult = result as ActionResultLike<T>
3789
+ if (actionResult.serverError) {
3790
+ const message = typeof actionResult.serverError === 'string' ? actionResult.serverError : 'Server action failed'
3791
+ throw new Error(message)
3792
+ }
3793
+ if (actionResult.validationErrors) {
3794
+ throw new Error('Validation failed, please check your input')
3795
+ }
3796
+ if (typeof actionResult.data !== 'undefined') {
3797
+ return actionResult.data as T
3798
+ }
3799
+ }
3800
+ return result as T
3801
+ }
3778
3802
 
3779
3803
  ${queryHooks}
3780
3804
 
@@ -3857,7 +3881,7 @@ export function ${hookName}(${parameterTypes.length > 0 ? `${parameterTypes.join
3857
3881
  queryKey: [...${queryKey}, searchParams],
3858
3882
  queryFn: async ({ signal }: { signal?: AbortSignal }) => {
3859
3883
  try {
3860
- const result = await ${actionName}(${actionCallParams.replace(queryParamObject, "{ ...searchParams }")})
3884
+ const result = await resolveActionResult<${returnType}>(${actionName}(${actionCallParams.replace(queryParamObject, "{ ...searchParams }")}))
3861
3885
  return result
3862
3886
  } catch (error) {
3863
3887
  handleActionError(error)
@@ -3895,7 +3919,7 @@ export function ${hookName.replace("use", "useInfinite")}(${parameterTypes.lengt
3895
3919
  queryKey: [...${queryKey}, 'infinite', searchParams],
3896
3920
  queryFn: async ({ pageParam = 1, signal }: { pageParam?: number; signal?: AbortSignal }) => {
3897
3921
  try {
3898
- const result = await ${actionName}(${actionCallParams.replace(queryParamObject, "{ ...searchParams, page: pageParam, limit: searchParams.limit }")})
3922
+ const result = await resolveActionResult<${returnType}>(${actionName}(${actionCallParams.replace(queryParamObject, "{ ...searchParams, page: pageParam, limit: searchParams.limit }")}))
3899
3923
  return result
3900
3924
  } catch (error) {
3901
3925
  handleActionError(error)
@@ -3930,7 +3954,7 @@ export function ${hookName.replace("use", "useSuspense")}(${parameterTypes.lengt
3930
3954
  return useSuspenseQuery({
3931
3955
  queryKey: ${queryKey},
3932
3956
  queryFn: async () => {
3933
- const result = await ${actionName}(${actionCallParams})
3957
+ const result = await resolveActionResult<${returnType}>(${actionName}(${actionCallParams}))
3934
3958
  return result
3935
3959
  },
3936
3960
  staleTime: ${staleTime},
@@ -3951,7 +3975,7 @@ export function ${hookName}(${parameterTypes.length > 0 ? `${parameterTypes.join
3951
3975
  queryKey: ${queryKey},
3952
3976
  queryFn: async ({ signal }: { signal?: AbortSignal }) => {
3953
3977
  try {
3954
- const result = await ${actionName}(${actionCallParams})
3978
+ const result = await resolveActionResult<${returnType}>(${actionCallParams === "{}" ? `${actionName}()` : `${actionName}(${actionCallParams})`})
3955
3979
  return result
3956
3980
  } catch (error) {
3957
3981
  handleActionError(error)
@@ -3987,7 +4011,7 @@ export function ${hookName.replace("use", "useSuspense")}(${parameterTypes.lengt
3987
4011
  return useSuspenseQuery({
3988
4012
  queryKey: ${queryKey},
3989
4013
  queryFn: async () => {
3990
- const result = await ${actionName}(${actionCallParams})
4014
+ const result = await resolveActionResult<${returnType}>(${actionCallParams === "{}" ? `${actionName}()` : `${actionName}(${actionCallParams})`})
3991
4015
  return result
3992
4016
  },
3993
4017
  staleTime: ${staleTime},
@@ -4036,12 +4060,12 @@ export function ${hookName}(options?: {
4036
4060
  }) {
4037
4061
  const queryClient = useQueryClient()
4038
4062
  const [isPending, startTransition] = useTransition()
4039
- const [optimisticData, setOptimisticData] = useOptimistic(null)
4063
+ const [optimisticData, setOptimisticData] = useOptimistic<unknown, ${inputType}>(null, (_, newData) => newData)
4040
4064
 
4041
4065
  const mutation = useMutation({
4042
4066
  mutationFn: async (variables: ${inputType}): Promise<${outputType}> => {
4043
4067
  try {
4044
- const result = await ${actionName}(${variablesParam === "undefined" ? "" : "variables"})
4068
+ const result = await resolveActionResult<${outputType}>(${actionName}(${variablesParam === "undefined" ? "" : "variables"}))
4045
4069
  return (result ?? ({} as ${outputType}))
4046
4070
  } catch (error) {
4047
4071
  handleActionError(error)
@@ -9610,5 +9634,5 @@ ${errorMessages}`,
9610
9634
  };
9611
9635
 
9612
9636
  export { BridgeCore, BridgeError, BridgeLogger, ConfigurationLoader, FileSystemManager, GenerationError, LogLevel, NextJsCodeGenerator, OpenApiSchemaParser, SchemaParseError, ValidationError, VersionChecker, __name, checkAndNotifyUpdates, createBridgeVersionChecker };
9613
- //# sourceMappingURL=chunk-FHGHNZLL.js.map
9614
- //# sourceMappingURL=chunk-FHGHNZLL.js.map
9637
+ //# sourceMappingURL=chunk-EED6FUEW.js.map
9638
+ //# sourceMappingURL=chunk-EED6FUEW.js.map