@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.
- package/CHANGELOG.md +51 -10
- package/README.md +405 -273
- package/dist/src/client/declare-client.d.mts +13 -4
- package/dist/src/client/declare-client.d.mts.map +1 -1
- package/dist/src/client/types.d.mts +337 -113
- package/dist/src/client/types.d.mts.map +1 -1
- package/dist/src/mutation/make-hook.d.mts +2 -2
- package/dist/src/mutation/make-hook.d.mts.map +1 -1
- package/dist/src/mutation/types.d.mts +12 -4
- package/dist/src/mutation/types.d.mts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/_tsup-dts-rollup.d.mts +362 -120
- package/lib/_tsup-dts-rollup.d.ts +362 -120
- package/lib/index.js +51 -27
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +52 -28
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/declare-client.spec.mts +24 -5
- package/src/__tests__/make-mutation.spec.mts +150 -8
- package/src/client/__type-tests__/client-instance.spec-d.mts +2 -2
- package/src/client/declare-client.mts +33 -8
- package/src/client/types.mts +617 -141
- package/src/mutation/make-hook.mts +96 -42
- package/src/mutation/types.mts +28 -6
|
@@ -5,14 +5,13 @@ import type {
|
|
|
5
5
|
UrlHasParams,
|
|
6
6
|
UrlParams,
|
|
7
7
|
} from '@navios/builder'
|
|
8
|
-
import type {
|
|
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<
|
|
62
|
-
|
|
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
|
|
81
|
+
const ownContext = (useContext?.() as TContext) ?? {}
|
|
75
82
|
|
|
76
83
|
// @ts-expect-error The types match
|
|
77
|
-
return useMutation(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
mutationKey
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
package/src/mutation/types.mts
CHANGED
|
@@ -4,7 +4,11 @@ import type {
|
|
|
4
4
|
UrlHasParams,
|
|
5
5
|
UrlParams,
|
|
6
6
|
} from '@navios/builder'
|
|
7
|
-
import type {
|
|
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
|
-
|
|
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
|
/**
|