@navios/react-query 0.5.2 → 0.6.1

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.
@@ -5,14 +5,13 @@ import type {
5
5
  UrlHasParams,
6
6
  UrlParams,
7
7
  } from '@navios/builder'
8
- import type { UseMutationResult } from '@tanstack/react-query'
8
+ import type {
9
+ MutationFunctionContext,
10
+ UseMutationResult,
11
+ } from '@tanstack/react-query'
9
12
  import type { z } from 'zod/v4'
10
13
 
11
- import {
12
- useIsMutating,
13
- useMutation,
14
- useQueryClient,
15
- } from '@tanstack/react-query'
14
+ import { useIsMutating, useMutation } from '@tanstack/react-query'
16
15
 
17
16
  import type { MutationParams } from './types.mjs'
18
17
 
@@ -33,6 +32,7 @@ export function makeMutation<
33
32
  TData = unknown,
34
33
  TVariables extends NaviosZodRequest<Config> = NaviosZodRequest<Config>,
35
34
  TResponse = z.output<Config['responseSchema']>,
35
+ TOnMutateResult = unknown,
36
36
  TContext = unknown,
37
37
  UseKey extends boolean = false,
38
38
  >(
@@ -42,6 +42,7 @@ export function makeMutation<
42
42
  TData,
43
43
  TVariables,
44
44
  TResponse,
45
+ TOnMutateResult,
45
46
  TContext,
46
47
  UseKey
47
48
  >,
@@ -58,57 +59,110 @@ export function makeMutation<
58
59
  ? UrlParams<Config['url']>
59
60
  : never
60
61
  : never,
61
- ): UseMutationResult<TData, Error, NaviosZodRequest<Config>> => {
62
- const queryClient = useQueryClient()
62
+ ): UseMutationResult<
63
+ TData,
64
+ Error,
65
+ NaviosZodRequest<Config>,
66
+ TOnMutateResult
67
+ > => {
63
68
  const {
64
69
  useKey,
65
70
  useContext,
71
+ onMutate,
66
72
  onError,
67
73
  onSuccess,
74
+ onSettled,
68
75
  keyPrefix: _keyPrefix,
69
76
  keySuffix: _keySuffix,
70
77
  processResponse,
71
78
  ...rest
72
79
  } = options
73
80
 
74
- const context = useContext?.() as TContext
81
+ const ownContext = (useContext?.() as TContext) ?? {}
75
82
 
76
83
  // @ts-expect-error The types match
77
- return useMutation(
78
- {
79
- ...rest,
80
- mutationKey: useKey
81
- ? mutationKey({
82
- urlParams: keyParams,
83
- })
84
- : undefined,
85
- scope: useKey
86
- ? {
87
- id: JSON.stringify(
88
- mutationKey({
89
- urlParams: keyParams,
90
- }),
91
- ),
92
- }
93
- : undefined,
94
- async mutationFn(params: TVariables) {
95
- const response = await endpoint(params)
84
+ return useMutation({
85
+ ...rest,
86
+ mutationKey: useKey
87
+ ? mutationKey({
88
+ urlParams: keyParams,
89
+ })
90
+ : undefined,
91
+ scope: useKey
92
+ ? {
93
+ id: JSON.stringify(
94
+ mutationKey({
95
+ urlParams: keyParams,
96
+ }),
97
+ ),
98
+ }
99
+ : undefined,
100
+ async mutationFn(params: TVariables) {
101
+ const response = await endpoint(params)
96
102
 
97
- return (processResponse ? processResponse(response) : response) as TData
98
- },
99
- onSuccess: onSuccess
100
- ? (data: TData, variables: TVariables) => {
101
- return onSuccess?.(queryClient, data, variables, context)
102
- }
103
- : undefined,
104
- onError: onError
105
- ? (err: Error, variables: TVariables) => {
106
- return onError?.(queryClient, err, variables, context)
107
- }
108
- : undefined,
103
+ return (processResponse ? processResponse(response) : response) as TData
109
104
  },
110
- queryClient,
111
- )
105
+ onSuccess: onSuccess
106
+ ? (
107
+ data: TData,
108
+ variables: TVariables,
109
+ onMutateResult: TOnMutateResult | undefined,
110
+ context: MutationFunctionContext,
111
+ ) => {
112
+ return onSuccess?.(data, variables, {
113
+ ...ownContext,
114
+ ...context,
115
+ onMutateResult,
116
+ } as TContext &
117
+ MutationFunctionContext & {
118
+ onMutateResult: TOnMutateResult | undefined
119
+ })
120
+ }
121
+ : undefined,
122
+ onError: onError
123
+ ? (
124
+ err: Error,
125
+ variables: TVariables,
126
+ onMutateResult: TOnMutateResult | undefined,
127
+ context: MutationFunctionContext,
128
+ ) => {
129
+ return onError?.(err, variables, {
130
+ onMutateResult,
131
+ ...ownContext,
132
+ ...context,
133
+ } as TContext &
134
+ MutationFunctionContext & {
135
+ onMutateResult: TOnMutateResult | undefined
136
+ })
137
+ }
138
+ : undefined,
139
+ onMutate: onMutate
140
+ ? (variables: TVariables, context: MutationFunctionContext) => {
141
+ return onMutate(variables, {
142
+ ...ownContext,
143
+ ...context,
144
+ } as TContext & MutationFunctionContext)
145
+ }
146
+ : undefined,
147
+ onSettled: onSettled
148
+ ? (
149
+ data: TData | undefined,
150
+ error: Error | null,
151
+ variables: TVariables,
152
+ onMutateResult: TOnMutateResult | undefined,
153
+ context: MutationFunctionContext,
154
+ ) => {
155
+ return onSettled(data, error, variables, {
156
+ ...ownContext,
157
+ ...context,
158
+ onMutateResult,
159
+ } as TContext &
160
+ MutationFunctionContext & {
161
+ onMutateResult: TOnMutateResult | undefined
162
+ })
163
+ }
164
+ : undefined,
165
+ })
112
166
  }
113
167
  result.useIsMutating = (
114
168
  keyParams: UseKey extends true
@@ -4,7 +4,11 @@ import type {
4
4
  UrlHasParams,
5
5
  UrlParams,
6
6
  } from '@navios/builder'
7
- import type { DataTag, QueryClient, UseMutationOptions } from '@tanstack/react-query'
7
+ import type {
8
+ DataTag,
9
+ MutationFunctionContext,
10
+ UseMutationOptions,
11
+ } from '@tanstack/react-query'
8
12
  import type { z, ZodObject } from 'zod/v4'
9
13
 
10
14
  import type { ProcessResponseFunction } from '../common/types.mjs'
@@ -42,11 +46,18 @@ export interface MutationParams<
42
46
  TData = unknown,
43
47
  TVariables = NaviosZodRequest<Config>,
44
48
  TResponse = z.output<Config['responseSchema']>,
49
+ TOnMutateResult = unknown,
45
50
  TContext = unknown,
46
51
  UseKey extends boolean = false,
47
52
  > extends Omit<
48
53
  UseMutationOptions<TData, Error, TVariables>,
49
- 'mutationKey' | 'mutationFn' | 'onSuccess' | 'onError' | 'scope'
54
+ | 'mutationKey'
55
+ | 'mutationFn'
56
+ | 'onMutate'
57
+ | 'onSuccess'
58
+ | 'onError'
59
+ | 'onSettled'
60
+ | 'scope'
50
61
  > {
51
62
  processResponse?: ProcessResponseFunction<TData, TResponse>
52
63
  /**
@@ -55,16 +66,27 @@ export interface MutationParams<
55
66
  */
56
67
  useContext?: () => TContext
57
68
  onSuccess?: (
58
- queryClient: QueryClient,
59
69
  data: TData,
60
70
  variables: TVariables,
61
- context: TContext,
71
+ context: TContext &
72
+ MutationFunctionContext & { onMutateResult: TOnMutateResult | undefined },
62
73
  ) => void | Promise<void>
63
74
  onError?: (
64
- queryClient: QueryClient,
65
75
  err: unknown,
66
76
  variables: TVariables,
67
- context: TContext,
77
+ context: TContext &
78
+ MutationFunctionContext & { onMutateResult: TOnMutateResult | undefined },
79
+ ) => void | Promise<void>
80
+ onMutate?: (
81
+ variables: TVariables,
82
+ context: TContext & MutationFunctionContext,
83
+ ) => TOnMutateResult | Promise<TOnMutateResult>
84
+ onSettled?: (
85
+ data: TData | undefined,
86
+ error: Error | null,
87
+ variables: TVariables,
88
+ context: TContext &
89
+ MutationFunctionContext & { onMutateResult: TOnMutateResult | undefined },
68
90
  ) => void | Promise<void>
69
91
 
70
92
  /**