@navios/react-query 0.5.1 → 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,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
  }
@@ -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