@navios/react-query 0.5.1 → 0.6.0

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.
@@ -1,5 +1,6 @@
1
1
  import type {
2
2
  BaseEndpointConfig,
3
+ BaseStreamConfig,
3
4
  EndpointFunctionArgs,
4
5
  HttpMethod,
5
6
  UrlHasParams,
@@ -55,6 +56,24 @@ export type ClientEndpointHelper<
55
56
  QuerySchema = unknown,
56
57
  > = EndpointHelper<Method, Url, RequestSchema, ResponseSchema, QuerySchema>
57
58
 
59
+ /**
60
+ * Helper type that attaches a stream endpoint to mutation results.
61
+ */
62
+ export type StreamHelper<
63
+ Method extends HttpMethod = HttpMethod,
64
+ Url extends string = string,
65
+ RequestSchema = unknown,
66
+ QuerySchema = unknown,
67
+ > = {
68
+ endpoint: ((
69
+ params: Util_FlatObject<
70
+ EndpointFunctionArgs<Url, QuerySchema, RequestSchema>
71
+ >,
72
+ ) => Promise<Blob>) & {
73
+ config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
74
+ }
75
+ }
76
+
58
77
  /**
59
78
  * The main client instance interface.
60
79
  * Provides methods for creating queries, infinite queries, and mutations.
@@ -1133,4 +1152,308 @@ export interface ClientInstance {
1133
1152
  >) &
1134
1153
  MutationHelpers<Url, Result> &
1135
1154
  EndpointHelper<Method, Url, RequestSchema, Response>
1155
+
1156
+ // ============================================================================
1157
+ // STREAM MUTATION FROM ENDPOINT METHODS
1158
+ // ============================================================================
1159
+
1160
+ // Stream mutation with useKey, requestSchema, and querySchema
1161
+ mutationFromEndpoint<
1162
+ Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
1163
+ Url extends string = string,
1164
+ RequestSchema extends ZodType = ZodType,
1165
+ QuerySchema extends ZodObject = ZodObject,
1166
+ Result = Blob,
1167
+ Context = unknown,
1168
+ UseKey extends true = true,
1169
+ >(
1170
+ endpoint: {
1171
+ config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
1172
+ },
1173
+ mutationOptions: {
1174
+ processResponse?: ProcessResponseFunction<Result, Blob>
1175
+ useKey: UseKey
1176
+ useContext?: () => Context
1177
+ onSuccess?: (
1178
+ queryClient: QueryClient,
1179
+ data: NoInfer<Result>,
1180
+ variables: Util_FlatObject<
1181
+ MutationArgs<Url, RequestSchema, QuerySchema>
1182
+ >,
1183
+ context: Context,
1184
+ ) => void | Promise<void>
1185
+ onError?: (
1186
+ queryClient: QueryClient,
1187
+ error: Error,
1188
+ variables: Util_FlatObject<
1189
+ MutationArgs<Url, RequestSchema, QuerySchema>
1190
+ >,
1191
+ context: Context,
1192
+ ) => void | Promise<void>
1193
+ },
1194
+ ): ((
1195
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
1196
+ ) => UseMutationResult<
1197
+ Result,
1198
+ Error,
1199
+ MutationArgs<Url, RequestSchema, QuerySchema>
1200
+ >) &
1201
+ MutationHelpers<Url, Result> &
1202
+ StreamHelper<Method, Url, RequestSchema, QuerySchema>
1203
+
1204
+ // Stream mutation without useKey, with requestSchema and querySchema
1205
+ mutationFromEndpoint<
1206
+ Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
1207
+ Url extends string = string,
1208
+ RequestSchema extends ZodType = ZodType,
1209
+ QuerySchema extends ZodObject = ZodObject,
1210
+ Result = Blob,
1211
+ Context = unknown,
1212
+ >(
1213
+ endpoint: {
1214
+ config: BaseStreamConfig<Method, Url, QuerySchema, RequestSchema>
1215
+ },
1216
+ mutationOptions?: {
1217
+ processResponse?: ProcessResponseFunction<Result, Blob>
1218
+ useContext?: () => Context
1219
+ onSuccess?: (
1220
+ queryClient: QueryClient,
1221
+ data: NoInfer<Result>,
1222
+ variables: Util_FlatObject<
1223
+ MutationArgs<Url, RequestSchema, QuerySchema>
1224
+ >,
1225
+ context: Context,
1226
+ ) => void | Promise<void>
1227
+ onError?: (
1228
+ queryClient: QueryClient,
1229
+ error: Error,
1230
+ variables: Util_FlatObject<
1231
+ MutationArgs<Url, RequestSchema, QuerySchema>
1232
+ >,
1233
+ context: Context,
1234
+ ) => void | Promise<void>
1235
+ },
1236
+ ): (() => UseMutationResult<
1237
+ Result,
1238
+ Error,
1239
+ MutationArgs<Url, RequestSchema, QuerySchema>
1240
+ >) &
1241
+ StreamHelper<Method, Url, RequestSchema, QuerySchema>
1242
+
1243
+ // Stream mutation with useKey, requestSchema only
1244
+ mutationFromEndpoint<
1245
+ Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
1246
+ Url extends string = string,
1247
+ RequestSchema extends ZodType = ZodType,
1248
+ Result = Blob,
1249
+ Context = unknown,
1250
+ UseKey extends true = true,
1251
+ >(
1252
+ endpoint: {
1253
+ config: BaseStreamConfig<Method, Url, undefined, RequestSchema>
1254
+ },
1255
+ mutationOptions: {
1256
+ processResponse?: ProcessResponseFunction<Result, Blob>
1257
+ useKey: UseKey
1258
+ useContext?: () => Context
1259
+ onSuccess?: (
1260
+ queryClient: QueryClient,
1261
+ data: NoInfer<Result>,
1262
+ variables: Util_FlatObject<MutationArgs<Url, RequestSchema, undefined>>,
1263
+ context: Context,
1264
+ ) => void | Promise<void>
1265
+ onError?: (
1266
+ queryClient: QueryClient,
1267
+ error: Error,
1268
+ variables: Util_FlatObject<MutationArgs<Url, RequestSchema, undefined>>,
1269
+ context: Context,
1270
+ ) => void | Promise<void>
1271
+ },
1272
+ ): ((
1273
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
1274
+ ) => UseMutationResult<
1275
+ Result,
1276
+ Error,
1277
+ MutationArgs<Url, RequestSchema, undefined>
1278
+ >) &
1279
+ MutationHelpers<Url, Result> &
1280
+ StreamHelper<Method, Url, RequestSchema, undefined>
1281
+
1282
+ // Stream mutation without useKey, with requestSchema only
1283
+ mutationFromEndpoint<
1284
+ Method extends 'POST' | 'PUT' | 'PATCH' = 'POST' | 'PUT' | 'PATCH',
1285
+ Url extends string = string,
1286
+ RequestSchema extends ZodType = ZodType,
1287
+ Result = Blob,
1288
+ Context = unknown,
1289
+ >(
1290
+ endpoint: {
1291
+ config: BaseStreamConfig<Method, Url, undefined, RequestSchema>
1292
+ },
1293
+ mutationOptions?: {
1294
+ processResponse?: ProcessResponseFunction<Result, Blob>
1295
+ useContext?: () => Context
1296
+ onSuccess?: (
1297
+ queryClient: QueryClient,
1298
+ data: NoInfer<Result>,
1299
+ variables: Util_FlatObject<MutationArgs<Url, RequestSchema, undefined>>,
1300
+ context: Context,
1301
+ ) => void | Promise<void>
1302
+ onError?: (
1303
+ queryClient: QueryClient,
1304
+ error: Error,
1305
+ variables: Util_FlatObject<MutationArgs<Url, RequestSchema, undefined>>,
1306
+ context: Context,
1307
+ ) => void | Promise<void>
1308
+ },
1309
+ ): (() => UseMutationResult<
1310
+ Result,
1311
+ Error,
1312
+ MutationArgs<Url, RequestSchema, undefined>
1313
+ >) &
1314
+ StreamHelper<Method, Url, RequestSchema, undefined>
1315
+
1316
+ // Stream mutation GET methods with useKey and querySchema
1317
+ mutationFromEndpoint<
1318
+ Method extends 'GET' | 'DELETE' | 'OPTIONS' | 'HEAD' = 'GET',
1319
+ Url extends string = string,
1320
+ QuerySchema extends ZodObject = ZodObject,
1321
+ Result = Blob,
1322
+ Context = unknown,
1323
+ UseKey extends true = true,
1324
+ >(
1325
+ endpoint: {
1326
+ config: BaseStreamConfig<Method, Url, QuerySchema, undefined>
1327
+ },
1328
+ mutationOptions: {
1329
+ processResponse?: ProcessResponseFunction<Result, Blob>
1330
+ useKey: UseKey
1331
+ useContext?: () => Context
1332
+ onSuccess?: (
1333
+ queryClient: QueryClient,
1334
+ data: NoInfer<Result>,
1335
+ variables: Util_FlatObject<MutationArgs<Url, undefined, QuerySchema>>,
1336
+ context: Context,
1337
+ ) => void | Promise<void>
1338
+ onError?: (
1339
+ queryClient: QueryClient,
1340
+ error: Error,
1341
+ variables: Util_FlatObject<MutationArgs<Url, undefined, QuerySchema>>,
1342
+ context: Context,
1343
+ ) => void | Promise<void>
1344
+ },
1345
+ ): ((
1346
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
1347
+ ) => UseMutationResult<
1348
+ Result,
1349
+ Error,
1350
+ MutationArgs<Url, undefined, QuerySchema>
1351
+ >) &
1352
+ MutationHelpers<Url, Result> &
1353
+ StreamHelper<Method, Url, undefined, QuerySchema>
1354
+
1355
+ // Stream mutation GET methods without useKey, with querySchema
1356
+ mutationFromEndpoint<
1357
+ Method extends 'GET' | 'DELETE' | 'OPTIONS' | 'HEAD' = 'GET',
1358
+ Url extends string = string,
1359
+ QuerySchema extends ZodObject = ZodObject,
1360
+ Result = Blob,
1361
+ Context = unknown,
1362
+ >(
1363
+ endpoint: {
1364
+ config: BaseStreamConfig<Method, Url, QuerySchema, undefined>
1365
+ },
1366
+ mutationOptions?: {
1367
+ processResponse?: ProcessResponseFunction<Result, Blob>
1368
+ useContext?: () => Context
1369
+ onSuccess?: (
1370
+ queryClient: QueryClient,
1371
+ data: NoInfer<Result>,
1372
+ variables: Util_FlatObject<MutationArgs<Url, undefined, QuerySchema>>,
1373
+ context: Context,
1374
+ ) => void | Promise<void>
1375
+ onError?: (
1376
+ queryClient: QueryClient,
1377
+ error: Error,
1378
+ variables: Util_FlatObject<MutationArgs<Url, undefined, QuerySchema>>,
1379
+ context: Context,
1380
+ ) => void | Promise<void>
1381
+ },
1382
+ ): (() => UseMutationResult<
1383
+ Result,
1384
+ Error,
1385
+ MutationArgs<Url, undefined, QuerySchema>
1386
+ >) &
1387
+ StreamHelper<Method, Url, undefined, QuerySchema>
1388
+
1389
+ // Stream mutation GET methods with useKey only (no schemas)
1390
+ mutationFromEndpoint<
1391
+ Method extends 'GET' | 'DELETE' | 'OPTIONS' | 'HEAD' = 'GET',
1392
+ Url extends string = string,
1393
+ Result = Blob,
1394
+ Context = unknown,
1395
+ UseKey extends true = true,
1396
+ >(
1397
+ endpoint: {
1398
+ config: BaseStreamConfig<Method, Url, undefined, undefined>
1399
+ },
1400
+ mutationOptions: {
1401
+ processResponse?: ProcessResponseFunction<Result, Blob>
1402
+ useKey: UseKey
1403
+ useContext?: () => Context
1404
+ onSuccess?: (
1405
+ queryClient: QueryClient,
1406
+ data: NoInfer<Result>,
1407
+ variables: Util_FlatObject<MutationArgs<Url, undefined, undefined>>,
1408
+ context: Context,
1409
+ ) => void | Promise<void>
1410
+ onError?: (
1411
+ queryClient: QueryClient,
1412
+ error: Error,
1413
+ variables: Util_FlatObject<MutationArgs<Url, undefined, undefined>>,
1414
+ context: Context,
1415
+ ) => void | Promise<void>
1416
+ },
1417
+ ): ((
1418
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
1419
+ ) => UseMutationResult<
1420
+ Result,
1421
+ Error,
1422
+ MutationArgs<Url, undefined, undefined>
1423
+ >) &
1424
+ MutationHelpers<Url, Result> &
1425
+ StreamHelper<Method, Url, undefined, undefined>
1426
+
1427
+ // Stream mutation GET methods without useKey (no schemas)
1428
+ mutationFromEndpoint<
1429
+ Method extends 'GET' | 'DELETE' | 'OPTIONS' | 'HEAD' = 'GET',
1430
+ Url extends string = string,
1431
+ Result = Blob,
1432
+ Context = unknown,
1433
+ >(
1434
+ endpoint: {
1435
+ config: BaseStreamConfig<Method, Url, undefined, undefined>
1436
+ },
1437
+ mutationOptions?: {
1438
+ processResponse?: ProcessResponseFunction<Result, Blob>
1439
+ useContext?: () => Context
1440
+ onSuccess?: (
1441
+ queryClient: QueryClient,
1442
+ data: NoInfer<Result>,
1443
+ variables: Util_FlatObject<MutationArgs<Url, undefined, undefined>>,
1444
+ context: Context,
1445
+ ) => void | Promise<void>
1446
+ onError?: (
1447
+ queryClient: QueryClient,
1448
+ error: Error,
1449
+ variables: Util_FlatObject<MutationArgs<Url, undefined, undefined>>,
1450
+ context: Context,
1451
+ ) => void | Promise<void>
1452
+ },
1453
+ ): (() => UseMutationResult<
1454
+ Result,
1455
+ Error,
1456
+ MutationArgs<Url, undefined, undefined>
1457
+ >) &
1458
+ StreamHelper<Method, Url, undefined, undefined>
1136
1459
  }
@@ -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,70 +42,127 @@ export function makeMutation<
42
42
  TData,
43
43
  TVariables,
44
44
  TResponse,
45
+ TOnMutateResult,
45
46
  TContext,
46
47
  UseKey
47
48
  >,
48
49
  ) {
49
50
  const config = endpoint.config
50
51
 
51
- const mutationKey = createMutationKey(config, options)
52
+ const mutationKey = createMutationKey(config, {
53
+ ...options,
54
+ processResponse: options.processResponse ?? ((data) => data),
55
+ })
52
56
  const result = (
53
57
  keyParams: UseKey extends true
54
58
  ? UrlHasParams<Config['url']> extends true
55
59
  ? UrlParams<Config['url']>
56
60
  : never
57
61
  : never,
58
- ): UseMutationResult<TData, Error, NaviosZodRequest<Config>> => {
59
- const queryClient = useQueryClient()
62
+ ): UseMutationResult<
63
+ TData,
64
+ Error,
65
+ NaviosZodRequest<Config>,
66
+ TOnMutateResult
67
+ > => {
60
68
  const {
61
69
  useKey,
62
70
  useContext,
71
+ onMutate,
63
72
  onError,
64
73
  onSuccess,
74
+ onSettled,
65
75
  keyPrefix: _keyPrefix,
66
76
  keySuffix: _keySuffix,
67
77
  processResponse,
68
78
  ...rest
69
79
  } = options
70
80
 
71
- const context = useContext?.() as TContext
81
+ const ownContext = (useContext?.() as TContext) ?? {}
72
82
 
73
83
  // @ts-expect-error The types match
74
- return useMutation(
75
- {
76
- ...rest,
77
- mutationKey: useKey
78
- ? mutationKey({
79
- urlParams: keyParams,
80
- })
81
- : undefined,
82
- scope: useKey
83
- ? {
84
- id: JSON.stringify(
85
- mutationKey({
86
- urlParams: keyParams,
87
- }),
88
- ),
89
- }
90
- : undefined,
91
- async mutationFn(params: TVariables) {
92
- 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)
93
102
 
94
- return processResponse(response) as TData
95
- },
96
- onSuccess: onSuccess
97
- ? (data: TData, variables: TVariables) => {
98
- return onSuccess?.(queryClient, data, variables, context)
99
- }
100
- : undefined,
101
- onError: onError
102
- ? (err: Error, variables: TVariables) => {
103
- return onError?.(queryClient, err, variables, context)
104
- }
105
- : undefined,
103
+ return (processResponse ? processResponse(response) : response) as TData
106
104
  },
107
- queryClient,
108
- )
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
+ })
109
166
  }
110
167
  result.useIsMutating = (
111
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,29 +46,47 @@ 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
- processResponse: ProcessResponseFunction<TData, TResponse>
62
+ processResponse?: ProcessResponseFunction<TData, TResponse>
52
63
  /**
53
64
  * React hooks that will prepare the context for the mutation onSuccess and onError
54
65
  * callbacks. This is useful for when you want to use the context in the callbacks
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
  /**