@navios/react-query 0.5.0 → 0.5.2

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,6 +1,8 @@
1
1
  import type {
2
2
  AbstractEndpoint,
3
+ AbstractStream,
3
4
  AnyEndpointConfig,
5
+ AnyStreamConfig,
4
6
  HttpMethod,
5
7
  } from '@navios/builder'
6
8
  import type { InfiniteData, QueryClient } from '@tanstack/react-query'
@@ -244,9 +246,11 @@ export function declareClient<Options extends ClientOptions>({
244
246
  }
245
247
 
246
248
  function mutationFromEndpoint(
247
- endpoint: AbstractEndpoint<AnyEndpointConfig>,
248
- options: {
249
- processResponse: ProcessResponseFunction
249
+ endpoint:
250
+ | AbstractEndpoint<AnyEndpointConfig>
251
+ | AbstractStream<AnyStreamConfig>,
252
+ options?: {
253
+ processResponse?: ProcessResponseFunction
250
254
  useContext?: () => unknown
251
255
  onSuccess?: (
252
256
  queryClient: QueryClient,
@@ -262,12 +266,12 @@ export function declareClient<Options extends ClientOptions>({
262
266
  ) => void | Promise<void>
263
267
  },
264
268
  ) {
269
+ // @ts-expect-error endpoint types are compatible at runtime
265
270
  return makeMutation(endpoint, {
266
- processResponse: options.processResponse,
267
- useContext: options.useContext,
268
- onSuccess: options.onSuccess,
269
- // @ts-expect-error simplify types here
270
- onError: options.onError,
271
+ processResponse: options?.processResponse,
272
+ useContext: options?.useContext,
273
+ onSuccess: options?.onSuccess,
274
+ onError: options?.onError,
271
275
  ...defaults,
272
276
  })
273
277
  }
@@ -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.
@@ -393,7 +412,7 @@ export interface ClientInstance {
393
412
  context: Context,
394
413
  ) => void | Promise<void>
395
414
  }): ((
396
- params: UrlHasParams<Url> extends true ? UrlParams<Url> : {},
415
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
397
416
  ) => UseMutationResult<
398
417
  Result,
399
418
  Error,
@@ -437,7 +456,7 @@ export interface ClientInstance {
437
456
  context: Context,
438
457
  ) => void | Promise<void>
439
458
  }): ((
440
- params: UrlHasParams<Url> extends true ? UrlParams<Url> : {},
459
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
441
460
  ) => UseMutationResult<
442
461
  Result,
443
462
  Error,
@@ -511,7 +530,7 @@ export interface ClientInstance {
511
530
  context: Context,
512
531
  ) => void | Promise<void>
513
532
  }): ((
514
- params: UrlHasParams<Url> extends true ? UrlParams<Url> : {},
533
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
515
534
  ) => UseMutationResult<
516
535
  Result,
517
536
  Error,
@@ -686,7 +705,7 @@ export interface ClientInstance {
686
705
  ) => void | Promise<void>
687
706
  },
688
707
  ): ((
689
- params: Util_FlatObject<MutationArgs<Url, RequestSchema, QuerySchema>>,
708
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
690
709
  ) => UseMutationResult<
691
710
  Result,
692
711
  Error,
@@ -732,7 +751,7 @@ export interface ClientInstance {
732
751
  ) => void | Promise<void>
733
752
  },
734
753
  ): ((
735
- params: Util_FlatObject<MutationArgs<Url, RequestSchema, undefined>>,
754
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
736
755
  ) => UseMutationResult<
737
756
  Result,
738
757
  Error,
@@ -780,9 +799,7 @@ export interface ClientInstance {
780
799
  context: Context,
781
800
  ) => void | Promise<void>
782
801
  },
783
- ): ((
784
- params: Util_FlatObject<MutationArgs<Url, RequestSchema, QuerySchema>>,
785
- ) => UseMutationResult<
802
+ ): (() => UseMutationResult<
786
803
  Result,
787
804
  Error,
788
805
  MutationArgs<Url, RequestSchema, QuerySchema>
@@ -823,9 +840,7 @@ export interface ClientInstance {
823
840
  context: Context,
824
841
  ) => void | Promise<void>
825
842
  },
826
- ): ((
827
- params: Util_FlatObject<MutationArgs<Url, RequestSchema, undefined>>,
828
- ) => UseMutationResult<
843
+ ): (() => UseMutationResult<
829
844
  Result,
830
845
  Error,
831
846
  MutationArgs<Url, RequestSchema, undefined>
@@ -866,9 +881,7 @@ export interface ClientInstance {
866
881
  context: Context,
867
882
  ) => void | Promise<void>
868
883
  },
869
- ): ((
870
- params: Util_FlatObject<MutationArgs<Url, undefined, QuerySchema>>,
871
- ) => UseMutationResult<
884
+ ): (() => UseMutationResult<
872
885
  Result,
873
886
  Error,
874
887
  MutationArgs<Url, undefined, QuerySchema>
@@ -906,7 +919,7 @@ export interface ClientInstance {
906
919
  ) => void | Promise<void>
907
920
  },
908
921
  ): ((
909
- params: Util_FlatObject<MutationArgs<Url, undefined, undefined>>,
922
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
910
923
  ) => UseMutationResult<
911
924
  Result,
912
925
  Error,
@@ -943,9 +956,7 @@ export interface ClientInstance {
943
956
  context: Context,
944
957
  ) => void | Promise<void>
945
958
  },
946
- ): ((
947
- params: Util_FlatObject<MutationArgs<Url, undefined, QuerySchema>>,
948
- ) => UseMutationResult<
959
+ ): (() => UseMutationResult<
949
960
  Result,
950
961
  Error,
951
962
  MutationArgs<Url, undefined, QuerySchema>
@@ -979,9 +990,7 @@ export interface ClientInstance {
979
990
  context: Context,
980
991
  ) => void | Promise<void>
981
992
  },
982
- ): ((
983
- params: Util_FlatObject<MutationArgs<Url, undefined, undefined>>,
984
- ) => UseMutationResult<
993
+ ): (() => UseMutationResult<
985
994
  Result,
986
995
  Error,
987
996
  MutationArgs<Url, undefined, undefined>
@@ -1135,7 +1144,7 @@ export interface ClientInstance {
1135
1144
  context: Context,
1136
1145
  ) => void | Promise<void>
1137
1146
  }): ((
1138
- params: UrlHasParams<Url> extends true ? UrlParams<Url> : {},
1147
+ params: UrlHasParams<Url> extends true ? { urlParams: UrlParams<Url> } : {},
1139
1148
  ) => UseMutationResult<
1140
1149
  Result,
1141
1150
  Error,
@@ -1143,4 +1152,308 @@ export interface ClientInstance {
1143
1152
  >) &
1144
1153
  MutationHelpers<Url, Result> &
1145
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>
1146
1459
  }
@@ -48,7 +48,10 @@ export function makeMutation<
48
48
  ) {
49
49
  const config = endpoint.config
50
50
 
51
- const mutationKey = createMutationKey(config, options)
51
+ const mutationKey = createMutationKey(config, {
52
+ ...options,
53
+ processResponse: options.processResponse ?? ((data) => data),
54
+ })
52
55
  const result = (
53
56
  keyParams: UseKey extends true
54
57
  ? UrlHasParams<Config['url']> extends true
@@ -91,7 +94,7 @@ export function makeMutation<
91
94
  async mutationFn(params: TVariables) {
92
95
  const response = await endpoint(params)
93
96
 
94
- return processResponse(response) as TData
97
+ return (processResponse ? processResponse(response) : response) as TData
95
98
  },
96
99
  onSuccess: onSuccess
97
100
  ? (data: TData, variables: TVariables) => {
@@ -48,7 +48,7 @@ export interface MutationParams<
48
48
  UseMutationOptions<TData, Error, TVariables>,
49
49
  'mutationKey' | 'mutationFn' | 'onSuccess' | 'onError' | 'scope'
50
50
  > {
51
- processResponse: ProcessResponseFunction<TData, TResponse>
51
+ processResponse?: ProcessResponseFunction<TData, TResponse>
52
52
  /**
53
53
  * React hooks that will prepare the context for the mutation onSuccess and onError
54
54
  * callbacks. This is useful for when you want to use the context in the callbacks