@refinedev/core 4.46.0 → 4.46.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.
Files changed (59) hide show
  1. package/CHANGELOG.md +109 -0
  2. package/dist/definitions/helpers/flatten-object-keys/index.d.ts.map +1 -1
  3. package/dist/esm/index.js +6 -6
  4. package/dist/esm/index.js.map +1 -1
  5. package/dist/hooks/accessControl/useCan/index.d.ts.map +1 -1
  6. package/dist/hooks/auditLog/useLogList/index.d.ts.map +1 -1
  7. package/dist/hooks/auth/useOnError/index.d.ts.map +1 -1
  8. package/dist/hooks/data/useCreate.d.ts.map +1 -1
  9. package/dist/hooks/data/useCreateMany.d.ts.map +1 -1
  10. package/dist/hooks/data/useDelete.d.ts.map +1 -1
  11. package/dist/hooks/data/useDeleteMany.d.ts.map +1 -1
  12. package/dist/hooks/data/useInfiniteList.d.ts.map +1 -1
  13. package/dist/hooks/data/useList.d.ts.map +1 -1
  14. package/dist/hooks/data/useMany.d.ts.map +1 -1
  15. package/dist/hooks/data/useOne.d.ts.map +1 -1
  16. package/dist/hooks/data/useUpdate.d.ts.map +1 -1
  17. package/dist/hooks/data/useUpdateMany.d.ts.map +1 -1
  18. package/dist/hooks/router/use-go/index.d.ts +2 -0
  19. package/dist/hooks/router/use-go/index.d.ts.map +1 -1
  20. package/dist/hooks/useMeta/index.d.ts +2 -0
  21. package/dist/hooks/useMeta/index.d.ts.map +1 -1
  22. package/dist/iife/index.js +6 -6
  23. package/dist/iife/index.js.map +1 -1
  24. package/dist/index.js +6 -6
  25. package/dist/index.js.map +1 -1
  26. package/dist/interfaces/auth.d.ts +6 -6
  27. package/dist/interfaces/auth.d.ts.map +1 -1
  28. package/dist/interfaces/metaData/graphqlQueryOptions.d.ts +6 -0
  29. package/dist/interfaces/metaData/graphqlQueryOptions.d.ts.map +1 -0
  30. package/dist/interfaces/metaData/metaQuery.d.ts +2 -1
  31. package/dist/interfaces/metaData/metaQuery.d.ts.map +1 -1
  32. package/package.json +2 -1
  33. package/src/definitions/helpers/flatten-object-keys/index.ts +2 -0
  34. package/src/hooks/accessControl/useCan/index.ts +10 -12
  35. package/src/hooks/auditLog/useLogList/index.ts +10 -12
  36. package/src/hooks/auth/useForgotPassword/index.ts +51 -54
  37. package/src/hooks/auth/useGetIdentity/index.ts +23 -26
  38. package/src/hooks/auth/useIsAuthenticated/index.ts +20 -20
  39. package/src/hooks/auth/useLogin/index.ts +33 -35
  40. package/src/hooks/auth/useLogout/index.ts +32 -34
  41. package/src/hooks/auth/useOnError/index.ts +29 -33
  42. package/src/hooks/auth/usePermissions/index.ts +19 -22
  43. package/src/hooks/auth/useRegister/index.ts +60 -66
  44. package/src/hooks/auth/useUpdatePassword/index.ts +51 -54
  45. package/src/hooks/data/useCreate.ts +115 -117
  46. package/src/hooks/data/useCreateMany.ts +113 -118
  47. package/src/hooks/data/useDelete.ts +212 -223
  48. package/src/hooks/data/useDeleteMany.ts +230 -244
  49. package/src/hooks/data/useInfiniteList.ts +36 -42
  50. package/src/hooks/data/useList.ts +56 -62
  51. package/src/hooks/data/useMany.ts +36 -37
  52. package/src/hooks/data/useOne.ts +54 -55
  53. package/src/hooks/data/useUpdate.ts +288 -311
  54. package/src/hooks/data/useUpdateMany.ts +294 -320
  55. package/src/hooks/menu/useMenu.tsx +1 -1
  56. package/src/hooks/router/use-go/index.tsx +3 -0
  57. package/src/interfaces/auth.tsx +6 -6
  58. package/src/interfaces/metaData/graphqlQueryOptions.ts +6 -0
  59. package/src/interfaces/metaData/metaQuery.ts +3 -1
@@ -189,8 +189,8 @@ export const useUpdate = <
189
189
  TError,
190
190
  UpdateParams<TData, TError, TVariables>,
191
191
  UpdateContext<TData>
192
- >(
193
- ({
192
+ >({
193
+ mutationFn: ({
194
194
  id,
195
195
  values,
196
196
  resource: resourceName,
@@ -269,359 +269,336 @@ export const useUpdate = <
269
269
  );
270
270
  return updatePromise;
271
271
  },
272
- {
273
- onMutate: async ({
274
- resource: resourceName,
275
- id,
276
- mutationMode,
277
- values,
278
- dataProviderName,
279
- meta,
280
- metaData,
281
- optimisticUpdateMap = { list: true, many: true, detail: true },
282
- }) => {
283
- const { identifier } = select(resourceName);
272
+ onMutate: async ({
273
+ resource: resourceName,
274
+ id,
275
+ mutationMode,
276
+ values,
277
+ dataProviderName,
278
+ meta,
279
+ metaData,
280
+ optimisticUpdateMap = { list: true, many: true, detail: true },
281
+ }) => {
282
+ const { identifier } = select(resourceName);
283
+
284
+ const {
285
+ gqlMutation: _,
286
+ gqlQuery: __,
287
+ ...preferredMeta
288
+ } = pickNotDeprecated(meta, metaData) ?? {};
289
+
290
+ const queryKey = queryKeysReplacement(preferLegacyKeys)(
291
+ identifier,
292
+ pickDataProvider(identifier, dataProviderName, resources),
293
+ preferredMeta,
294
+ );
284
295
 
285
- const preferredMeta = pickNotDeprecated(meta, metaData);
296
+ const resourceKeys = keys()
297
+ .data(pickDataProvider(identifier, dataProviderName, resources))
298
+ .resource(identifier);
286
299
 
287
- const queryKey = queryKeysReplacement(preferLegacyKeys)(
288
- identifier,
289
- pickDataProvider(identifier, dataProviderName, resources),
290
- preferredMeta,
291
- );
300
+ const previousQueries: PreviousQuery<TData>[] =
301
+ queryClient.getQueriesData(resourceKeys.get(preferLegacyKeys));
292
302
 
293
- const resourceKeys = keys()
294
- .data(
295
- pickDataProvider(
296
- identifier,
297
- dataProviderName,
298
- resources,
299
- ),
300
- )
301
- .resource(identifier);
303
+ const mutationModePropOrContext =
304
+ mutationMode ?? mutationModeContext;
302
305
 
303
- const previousQueries: PreviousQuery<TData>[] =
304
- queryClient.getQueriesData(
305
- resourceKeys.get(preferLegacyKeys),
306
- );
306
+ await queryClient.cancelQueries(
307
+ resourceKeys.get(preferLegacyKeys),
308
+ undefined,
309
+ {
310
+ silent: true,
311
+ },
312
+ );
307
313
 
308
- const mutationModePropOrContext =
309
- mutationMode ?? mutationModeContext;
314
+ if (mutationModePropOrContext !== "pessimistic") {
315
+ if (optimisticUpdateMap.list) {
316
+ // Set the previous queries to the new ones:
317
+ queryClient.setQueriesData(
318
+ resourceKeys
319
+ .action("list")
320
+ .params(preferredMeta ?? {})
321
+ .get(preferLegacyKeys),
322
+ (previous?: GetListResponse<TData> | null) => {
323
+ if (
324
+ typeof optimisticUpdateMap.list === "function"
325
+ ) {
326
+ return optimisticUpdateMap.list(
327
+ previous,
328
+ values,
329
+ id,
330
+ );
331
+ }
310
332
 
311
- await queryClient.cancelQueries(
312
- resourceKeys.get(preferLegacyKeys),
313
- undefined,
314
- {
315
- silent: true,
316
- },
317
- );
333
+ if (!previous) {
334
+ return null;
335
+ }
318
336
 
319
- if (mutationModePropOrContext !== "pessimistic") {
320
- if (optimisticUpdateMap.list) {
321
- // Set the previous queries to the new ones:
322
- queryClient.setQueriesData(
323
- resourceKeys
324
- .action("list")
325
- .params(preferredMeta ?? {})
326
- .get(preferLegacyKeys),
327
- (previous?: GetListResponse<TData> | null) => {
328
- if (
329
- typeof optimisticUpdateMap.list ===
330
- "function"
331
- ) {
332
- return optimisticUpdateMap.list(
333
- previous,
334
- values,
337
+ const data = previous.data.map((record: TData) => {
338
+ if (record.id?.toString() === id?.toString()) {
339
+ return {
335
340
  id,
336
- );
341
+ ...record,
342
+ ...values,
343
+ } as unknown as TData;
337
344
  }
345
+ return record;
346
+ });
338
347
 
339
- if (!previous) {
340
- return null;
341
- }
348
+ return {
349
+ ...previous,
350
+ data,
351
+ };
352
+ },
353
+ );
354
+ }
342
355
 
343
- const data = previous.data.map(
344
- (record: TData) => {
345
- if (
346
- record.id?.toString() ===
347
- id?.toString()
348
- ) {
349
- return {
350
- id,
351
- ...record,
352
- ...values,
353
- } as unknown as TData;
354
- }
355
- return record;
356
- },
356
+ if (optimisticUpdateMap.many) {
357
+ queryClient.setQueriesData(
358
+ resourceKeys.action("many").get(preferLegacyKeys),
359
+ (previous?: GetManyResponse<TData> | null) => {
360
+ if (
361
+ typeof optimisticUpdateMap.many === "function"
362
+ ) {
363
+ return optimisticUpdateMap.many(
364
+ previous,
365
+ values,
366
+ id,
357
367
  );
368
+ }
358
369
 
359
- return {
360
- ...previous,
361
- data,
362
- };
363
- },
364
- );
365
- }
370
+ if (!previous) {
371
+ return null;
372
+ }
366
373
 
367
- if (optimisticUpdateMap.many) {
368
- queryClient.setQueriesData(
369
- resourceKeys.action("many").get(preferLegacyKeys),
370
- (previous?: GetManyResponse<TData> | null) => {
371
- if (
372
- typeof optimisticUpdateMap.many ===
373
- "function"
374
- ) {
375
- return optimisticUpdateMap.many(
376
- previous,
377
- values,
374
+ const data = previous.data.map((record: TData) => {
375
+ if (record.id?.toString() === id?.toString()) {
376
+ record = {
378
377
  id,
379
- );
380
- }
381
-
382
- if (!previous) {
383
- return null;
378
+ ...record,
379
+ ...values,
380
+ } as unknown as TData;
384
381
  }
382
+ return record;
383
+ });
384
+ return {
385
+ ...previous,
386
+ data,
387
+ };
388
+ },
389
+ );
390
+ }
385
391
 
386
- const data = previous.data.map(
387
- (record: TData) => {
388
- if (
389
- record.id?.toString() ===
390
- id?.toString()
391
- ) {
392
- record = {
393
- id,
394
- ...record,
395
- ...values,
396
- } as unknown as TData;
397
- }
398
- return record;
399
- },
392
+ if (optimisticUpdateMap.detail) {
393
+ queryClient.setQueriesData(
394
+ resourceKeys
395
+ .action("one")
396
+ .id(id)
397
+ .params(preferredMeta ?? {})
398
+ .get(preferLegacyKeys),
399
+ (previous?: GetOneResponse<TData> | null) => {
400
+ if (
401
+ typeof optimisticUpdateMap.detail === "function"
402
+ ) {
403
+ return optimisticUpdateMap.detail(
404
+ previous,
405
+ values,
406
+ id,
400
407
  );
401
- return {
402
- ...previous,
403
- data,
404
- };
405
- },
406
- );
407
- }
408
-
409
- if (optimisticUpdateMap.detail) {
410
- queryClient.setQueriesData(
411
- resourceKeys
412
- .action("one")
413
- .id(id)
414
- .params(preferredMeta ?? {})
415
- .get(preferLegacyKeys),
416
- (previous?: GetOneResponse<TData> | null) => {
417
- if (
418
- typeof optimisticUpdateMap.detail ===
419
- "function"
420
- ) {
421
- return optimisticUpdateMap.detail(
422
- previous,
423
- values,
424
- id,
425
- );
426
- }
408
+ }
409
+
410
+ if (!previous) {
411
+ return null;
412
+ }
413
+
414
+ return {
415
+ ...previous,
416
+ data: {
417
+ ...previous.data,
418
+ ...values,
419
+ },
420
+ };
421
+ },
422
+ );
423
+ }
424
+ }
427
425
 
428
- if (!previous) {
429
- return null;
430
- }
426
+ return {
427
+ previousQueries,
428
+ queryKey,
429
+ };
430
+ },
431
+ onSettled: (
432
+ _data,
433
+ _error,
434
+ {
435
+ id,
436
+ resource: resourceName,
437
+ dataProviderName,
438
+ invalidates = ["list", "many", "detail"],
439
+ },
440
+ ) => {
441
+ const { identifier } = select(resourceName);
431
442
 
432
- return {
433
- ...previous,
434
- data: {
435
- ...previous.data,
436
- ...values,
437
- },
438
- };
439
- },
440
- );
441
- }
442
- }
443
+ invalidateStore({
444
+ resource: identifier,
445
+ dataProviderName: pickDataProvider(
446
+ identifier,
447
+ dataProviderName,
448
+ resources,
449
+ ),
450
+ invalidates,
451
+ id,
452
+ });
443
453
 
444
- return {
445
- previousQueries,
446
- queryKey,
447
- };
454
+ notificationDispatch({
455
+ type: ActionTypes.REMOVE,
456
+ payload: { id, resource: identifier },
457
+ });
458
+ },
459
+ onSuccess: (
460
+ data,
461
+ {
462
+ id,
463
+ resource: resourceName,
464
+ successNotification,
465
+ dataProviderName: dataProviderNameFromProp,
466
+ values,
467
+ meta,
468
+ metaData,
448
469
  },
449
- onSettled: (
450
- _data,
451
- _error,
452
- {
453
- id,
454
- resource: resourceName,
470
+ context,
471
+ ) => {
472
+ const { resource, identifier } = select(resourceName);
473
+ const resourceSingular = textTransformers.singular(identifier);
474
+
475
+ const dataProviderName = pickDataProvider(
476
+ identifier,
477
+ dataProviderNameFromProp,
478
+ resources,
479
+ );
480
+
481
+ const combinedMeta = getMeta({
482
+ resource,
483
+ meta: pickNotDeprecated(meta, metaData),
484
+ });
485
+
486
+ const notificationConfig =
487
+ typeof successNotification === "function"
488
+ ? successNotification(data, { id, values }, identifier)
489
+ : successNotification;
490
+
491
+ handleNotification(notificationConfig, {
492
+ key: `${id}-${identifier}-notification`,
493
+ description: translate("notifications.success", "Successful"),
494
+ message: translate(
495
+ "notifications.editSuccess",
496
+ {
497
+ resource: translate(
498
+ `${identifier}.${identifier}`,
499
+ resourceSingular,
500
+ ),
501
+ },
502
+ `Successfully updated ${resourceSingular}`,
503
+ ),
504
+ type: "success",
505
+ });
506
+
507
+ publish?.({
508
+ channel: `resources/${resource.name}`,
509
+ type: "updated",
510
+ payload: {
511
+ ids: data.data?.id ? [data.data.id] : undefined,
512
+ },
513
+ date: new Date(),
514
+ meta: {
515
+ ...combinedMeta,
455
516
  dataProviderName,
456
- invalidates = ["list", "many", "detail"],
457
517
  },
458
- ) => {
459
- const { identifier } = select(resourceName);
460
-
461
- invalidateStore({
462
- resource: identifier,
463
- dataProviderName: pickDataProvider(
464
- identifier,
465
- dataProviderName,
466
- resources,
467
- ),
468
- invalidates,
469
- id,
470
- });
518
+ });
471
519
 
472
- notificationDispatch({
473
- type: ActionTypes.REMOVE,
474
- payload: { id, resource: identifier },
475
- });
476
- },
477
- onSuccess: (
478
- data,
479
- {
520
+ let previousData: any;
521
+ if (context) {
522
+ const queryData = queryClient.getQueryData<
523
+ UpdateResponse<TData>
524
+ >(context.queryKey.detail(id));
525
+
526
+ previousData = Object.keys(values || {}).reduce<any>(
527
+ (acc, item) => {
528
+ acc[item] = queryData?.data?.[item];
529
+ return acc;
530
+ },
531
+ {},
532
+ );
533
+ }
534
+
535
+ const {
536
+ fields: _fields,
537
+ operation: _operation,
538
+ variables: _variables,
539
+ ...rest
540
+ } = combinedMeta || {};
541
+ log?.mutate({
542
+ action: "update",
543
+ resource: resource.name,
544
+ data: values,
545
+ previousData,
546
+ meta: {
480
547
  id,
481
- resource: resourceName,
482
- successNotification,
483
- dataProviderName: dataProviderNameFromProp,
484
- values,
485
- meta,
486
- metaData,
548
+ dataProviderName,
549
+ ...rest,
487
550
  },
488
- context,
489
- ) => {
490
- const { resource, identifier } = select(resourceName);
491
- const resourceSingular = textTransformers.singular(identifier);
551
+ });
552
+ },
553
+ onError: (
554
+ err: TError,
555
+ { id, resource: resourceName, errorNotification, values },
556
+ context,
557
+ ) => {
558
+ const { identifier } = select(resourceName);
559
+
560
+ // set back the queries to the context:
561
+ if (context) {
562
+ for (const query of context.previousQueries) {
563
+ queryClient.setQueryData(query[0], query[1]);
564
+ }
565
+ }
492
566
 
493
- const dataProviderName = pickDataProvider(
494
- identifier,
495
- dataProviderNameFromProp,
496
- resources,
497
- );
567
+ if (err.message !== "mutationCancelled") {
568
+ checkError?.(err);
498
569
 
499
- const combinedMeta = getMeta({
500
- resource,
501
- meta: pickNotDeprecated(meta, metaData),
502
- });
570
+ const resourceSingular = textTransformers.singular(identifier);
503
571
 
504
572
  const notificationConfig =
505
- typeof successNotification === "function"
506
- ? successNotification(data, { id, values }, identifier)
507
- : successNotification;
573
+ typeof errorNotification === "function"
574
+ ? errorNotification(err, { id, values }, identifier)
575
+ : errorNotification;
508
576
 
509
577
  handleNotification(notificationConfig, {
510
578
  key: `${id}-${identifier}-notification`,
511
- description: translate(
512
- "notifications.success",
513
- "Successful",
514
- ),
515
579
  message: translate(
516
- "notifications.editSuccess",
580
+ "notifications.editError",
517
581
  {
518
582
  resource: translate(
519
583
  `${identifier}.${identifier}`,
520
584
  resourceSingular,
521
585
  ),
586
+ statusCode: err.statusCode,
522
587
  },
523
- `Successfully updated ${resourceSingular}`,
588
+ `Error when updating ${resourceSingular} (status code: ${err.statusCode})`,
524
589
  ),
525
- type: "success",
526
- });
527
-
528
- publish?.({
529
- channel: `resources/${resource.name}`,
530
- type: "updated",
531
- payload: {
532
- ids: data.data?.id ? [data.data.id] : undefined,
533
- },
534
- date: new Date(),
535
- meta: {
536
- ...combinedMeta,
537
- dataProviderName,
538
- },
539
- });
540
-
541
- let previousData: any;
542
- if (context) {
543
- const queryData = queryClient.getQueryData<
544
- UpdateResponse<TData>
545
- >(context.queryKey.detail(id));
546
-
547
- previousData = Object.keys(values || {}).reduce<any>(
548
- (acc, item) => {
549
- acc[item] = queryData?.data?.[item];
550
- return acc;
551
- },
552
- {},
553
- );
554
- }
555
-
556
- const {
557
- fields: _fields,
558
- operation: _operation,
559
- variables: _variables,
560
- ...rest
561
- } = combinedMeta || {};
562
- log?.mutate({
563
- action: "update",
564
- resource: resource.name,
565
- data: values,
566
- previousData,
567
- meta: {
568
- id,
569
- dataProviderName,
570
- ...rest,
571
- },
590
+ description: err.message,
591
+ type: "error",
572
592
  });
573
- },
574
- onError: (
575
- err: TError,
576
- { id, resource: resourceName, errorNotification, values },
577
- context,
578
- ) => {
579
- const { identifier } = select(resourceName);
580
-
581
- // set back the queries to the context:
582
- if (context) {
583
- for (const query of context.previousQueries) {
584
- queryClient.setQueryData(query[0], query[1]);
585
- }
586
- }
587
-
588
- if (err.message !== "mutationCancelled") {
589
- checkError?.(err);
590
-
591
- const resourceSingular =
592
- textTransformers.singular(identifier);
593
-
594
- const notificationConfig =
595
- typeof errorNotification === "function"
596
- ? errorNotification(err, { id, values }, identifier)
597
- : errorNotification;
598
-
599
- handleNotification(notificationConfig, {
600
- key: `${id}-${identifier}-notification`,
601
- message: translate(
602
- "notifications.editError",
603
- {
604
- resource: translate(
605
- `${identifier}.${identifier}`,
606
- resourceSingular,
607
- ),
608
- statusCode: err.statusCode,
609
- },
610
- `Error when updating ${resourceSingular} (status code: ${err.statusCode})`,
611
- ),
612
- description: err.message,
613
- type: "error",
614
- });
615
- }
616
- },
617
- mutationKey: keys().data().mutation("update").get(preferLegacyKeys),
618
- ...mutationOptions,
619
- meta: {
620
- ...mutationOptions?.meta,
621
- ...getXRay("useUpdate", preferLegacyKeys),
622
- },
593
+ }
623
594
  },
624
- );
595
+ mutationKey: keys().data().mutation("update").get(preferLegacyKeys),
596
+ ...mutationOptions,
597
+ meta: {
598
+ ...mutationOptions?.meta,
599
+ ...getXRay("useUpdate", preferLegacyKeys),
600
+ },
601
+ });
625
602
 
626
603
  const { elapsedTime } = useLoadingOvertime({
627
604
  isLoading: mutation.isLoading,