Skip to content

Commit

Permalink
feat(hooks): mutation error typing
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh committed May 16, 2022
1 parent 1ae60dd commit 5aeff10
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 63 deletions.
14 changes: 14 additions & 0 deletions docs/useCreate.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,17 @@ const LikeButton = ({ record }) => {
return <button disabled={isLoading} onClick={handleClick}>Like</button>;
};
```

**Tip**: If you use TypeScript, you can specify the record and error types for more type safety:

```tsx
useCreate<Product, Error>(undefined, undefined, {
onError: (error) => {
// error is an instance of Error.
},
onSettled: (data, error) => {
// data is an instance of Product.
// error is an instance of Error.
},
})
```
14 changes: 14 additions & 0 deletions docs/useDelete.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ const DeleteButton = ({ record }) => {
return <button disabled={isLoading} onClick={handleClick}>Delete</button>;
};
```

**Tip**: If you use TypeScript, you can specify the record and error types for more type safety:

```tsx
useDelete<Product, Error>(undefined, undefined, {
onError: (error) => {
// error is an instance of Error.
},
onSettled: (data, error) => {
// data is an instance of Product.
// error is an instance of Error.
},
})
```
14 changes: 14 additions & 0 deletions docs/useDeleteMany.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,17 @@ const BulkDeletePostsButton = ({ selectedIds }) => {
return <button disabled={isLoading} onClick={handleClick}>Delete selected posts</button>;
};
```

**Tip**: If you use TypeScript, you can specify the record and error types for more type safety:

```tsx
useDeleteMany<Product, Error>(undefined, undefined, {
onError: (error) => {
// error is an instance of Error.
},
onSettled: (data, error) => {
// data is an instance of Product.
// error is an instance of Error.
},
})
```
14 changes: 14 additions & 0 deletions docs/useUpdate.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ const IncreaseLikeButton = ({ record }) => {
return <button disabled={isLoading} onClick={handleClick}>Like</button>;
};
```

**Tip**: If you use TypeScript, you can specify the record and error types for more type safety:

```tsx
useUpdate<Product, Error>(undefined, undefined, {
onError: (error) => {
// error is an instance of Error.
},
onSettled: (data, error) => {
// data is an instance of Product.
// error is an instance of Error.
},
})
```
14 changes: 14 additions & 0 deletions docs/useUpdateMany.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ const BulkResetViewsButton = ({ selectedIds }) => {
return <button disabled={isLoading} onClick={handleClick}>Reset views</button>;
};
```

**Tip**: If you use TypeScript, you can specify the record and error types for more type safety:

```tsx
useUpdate<Product, Error>(undefined, undefined, {
onError: (error) => {
// error is an instance of Error.
},
onSettled: (data, error) => {
// data is an instance of Product.
// error is an instance of Error.
},
})
```
7 changes: 5 additions & 2 deletions packages/ra-core/src/controller/create/useCreateController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,17 @@ export const useCreateController = <RecordType extends RaRecord = RaRecord>(
};
};

export interface CreateControllerProps<RecordType extends RaRecord = RaRecord> {
export interface CreateControllerProps<
RecordType extends RaRecord = RaRecord,
MutationOptionsError = unknown
> {
disableAuthentication?: boolean;
record?: Partial<RecordType>;
redirect?: RedirectionSideEffect;
resource?: string;
mutationOptions?: UseMutationOptions<
RecordType,
unknown,
MutationOptionsError,
CreateParams<RecordType>
>;
transform?: TransformData;
Expand Down
7 changes: 5 additions & 2 deletions packages/ra-core/src/controller/edit/useEditController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,16 @@ export const useEditController = <RecordType extends RaRecord = any>(
};
};

export interface EditControllerProps<RecordType extends RaRecord = any> {
export interface EditControllerProps<
RecordType extends RaRecord = any,
MutationOptionsError = unknown
> {
disableAuthentication?: boolean;
id?: RecordType['id'];
mutationMode?: MutationMode;
mutationOptions?: UseMutationOptions<
RecordType,
unknown,
MutationOptionsError,
UpdateParams<RecordType>
>;
queryOptions?: UseQueryOptions<RecordType>;
Expand Down
25 changes: 15 additions & 10 deletions packages/ra-core/src/dataProvider/useCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,14 @@ import { RaRecord, CreateParams } from '../types';
* const [create, { data }] = useCreate<Product>('products', { data: product });
* \-- data is Product
*/
export const useCreate = <RecordType extends RaRecord = any>(
export const useCreate = <
RecordType extends RaRecord = any,
MutationError = unknown
>(
resource?: string,
params: Partial<CreateParams<Partial<RecordType>>> = {},
options: UseCreateOptions<RecordType> = {}
): UseCreateResult<RecordType> => {
options: UseCreateOptions<RecordType, MutationError> = {}
): UseCreateResult<RecordType, boolean, MutationError> => {
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
const paramsRef = useRef<Partial<CreateParams<Partial<RecordType>>>>(
Expand All @@ -79,7 +82,7 @@ export const useCreate = <RecordType extends RaRecord = any>(

const mutation = useMutation<
RecordType,
unknown,
MutationError,
Partial<UseCreateMutateParams<RecordType>>
>(
({
Expand Down Expand Up @@ -119,7 +122,7 @@ export const useCreate = <RecordType extends RaRecord = any>(
callTimeParams: Partial<CreateParams<RecordType>> = {},
createOptions: MutateOptions<
RecordType,
unknown,
MutationError,
Partial<UseCreateMutateParams<RecordType>>,
unknown
> & { returnPromise?: boolean } = {}
Expand Down Expand Up @@ -147,30 +150,32 @@ export interface UseCreateMutateParams<RecordType extends RaRecord = any> {
}

export type UseCreateOptions<
RecordType extends RaRecord = any
RecordType extends RaRecord = any,
MutationError = unknown
> = UseMutationOptions<
RecordType,
unknown,
MutationError,
Partial<UseCreateMutateParams<RecordType>>
>;

export type UseCreateResult<
RecordType extends RaRecord = any,
TReturnPromise extends boolean = boolean
TReturnPromise extends boolean = boolean,
MutationError = unknown
> = [
(
resource?: string,
params?: Partial<CreateParams<Partial<RecordType>>>,
options?: MutateOptions<
RecordType,
unknown,
MutationError,
Partial<UseCreateMutateParams<RecordType>>,
unknown
> & { returnPromise?: TReturnPromise }
) => Promise<TReturnPromise extends true ? RecordType : void>,
UseMutationResult<
RecordType,
unknown,
MutationError,
Partial<UseCreateMutateParams<RecordType>>,
unknown
>
Expand Down
31 changes: 19 additions & 12 deletions packages/ra-core/src/dataProvider/useDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,14 @@ import { RaRecord, DeleteParams, MutationMode } from '../types';
* const [delete, { data }] = useDelete<Product>('products', { id, previousData: product });
* \-- data is Product
*/
export const useDelete = <RecordType extends RaRecord = any>(
export const useDelete = <
RecordType extends RaRecord = any,
MutationError = unknown
>(
resource?: string,
params: Partial<DeleteParams<RecordType>> = {},
options: UseDeleteOptions<RecordType> = {}
): UseDeleteResult<RecordType> => {
options: UseDeleteOptions<RecordType, MutationError> = {}
): UseDeleteResult<RecordType, MutationError> => {
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
const { id, previousData } = params;
Expand Down Expand Up @@ -141,7 +144,7 @@ export const useDelete = <RecordType extends RaRecord = any>(

const mutation = useMutation<
RecordType,
unknown,
MutationError,
Partial<UseDeleteMutateParams<RecordType>>
>(
({
Expand Down Expand Up @@ -176,7 +179,7 @@ export const useDelete = <RecordType extends RaRecord = any>(
}
},
onError: (
error: unknown,
error: MutationError,
variables: Partial<UseDeleteMutateParams<RecordType>> = {},
context: { snapshot: Snapshot }
) => {
Expand Down Expand Up @@ -227,7 +230,7 @@ export const useDelete = <RecordType extends RaRecord = any>(
},
onSettled: (
data: RecordType,
error: unknown,
error: MutationError,
variables: Partial<UseDeleteMutateParams<RecordType>> = {},
context: { snapshot: Snapshot }
) => {
Expand Down Expand Up @@ -258,7 +261,7 @@ export const useDelete = <RecordType extends RaRecord = any>(
callTimeParams: Partial<DeleteParams<RecordType>> = {},
updateOptions: MutateOptions<
RecordType,
unknown,
MutationError,
Partial<UseDeleteMutateParams<RecordType>>,
unknown
> & { mutationMode?: MutationMode } = {}
Expand Down Expand Up @@ -388,27 +391,31 @@ export interface UseDeleteMutateParams<RecordType extends RaRecord = any> {
}

export type UseDeleteOptions<
RecordType extends RaRecord = any
RecordType extends RaRecord = any,
MutationError = unknown
> = UseMutationOptions<
RecordType,
unknown,
MutationError,
Partial<UseDeleteMutateParams<RecordType>>
> & { mutationMode?: MutationMode };

export type UseDeleteResult<RecordType extends RaRecord = any> = [
export type UseDeleteResult<
RecordType extends RaRecord = any,
MutationError = unknown
> = [
(
resource?: string,
params?: Partial<DeleteParams<RecordType>>,
options?: MutateOptions<
RecordType,
unknown,
MutationError,
Partial<UseDeleteMutateParams<RecordType>>,
unknown
> & { mutationMode?: MutationMode }
) => Promise<void>,
UseMutationResult<
RecordType,
unknown,
MutationError,
Partial<DeleteParams<RecordType> & { resource?: string }>,
unknown
>
Expand Down
29 changes: 18 additions & 11 deletions packages/ra-core/src/dataProvider/useDeleteMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ import { RaRecord, DeleteManyParams, MutationMode } from '../types';
* const [deleteMany, { data }] = useDeleteMany<Product>('products', { ids });
* \-- data is Product
*/
export const useDeleteMany = <RecordType extends RaRecord = any>(
export const useDeleteMany = <
RecordType extends RaRecord = any,
MutationError = unknown
>(
resource?: string,
params: Partial<DeleteManyParams<RecordType>> = {},
options: UseDeleteManyOptions<RecordType> = {}
): UseDeleteManyResult<RecordType> => {
options: UseDeleteManyOptions<RecordType, MutationError> = {}
): UseDeleteManyResult<RecordType, MutationError> => {
const dataProvider = useDataProvider();
const queryClient = useQueryClient();
const { ids } = params;
Expand Down Expand Up @@ -154,7 +157,7 @@ export const useDeleteMany = <RecordType extends RaRecord = any>(

const mutation = useMutation<
RecordType['id'][],
unknown,
MutationError,
Partial<UseDeleteManyMutateParams<RecordType>>
>(
({
Expand Down Expand Up @@ -187,7 +190,7 @@ export const useDeleteMany = <RecordType extends RaRecord = any>(
}
},
onError: (
error: unknown,
error: MutationError,
variables: Partial<UseDeleteManyMutateParams<RecordType>> = {},
context: { snapshot: Snapshot }
) => {
Expand Down Expand Up @@ -238,7 +241,7 @@ export const useDeleteMany = <RecordType extends RaRecord = any>(
},
onSettled: (
data: RecordType['id'][],
error: unknown,
error: MutationError,
variables: Partial<UseDeleteManyMutateParams<RecordType>> = {},
context: { snapshot: Snapshot }
) => {
Expand Down Expand Up @@ -394,27 +397,31 @@ export interface UseDeleteManyMutateParams<RecordType extends RaRecord = any> {
}

export type UseDeleteManyOptions<
RecordType extends RaRecord = any
RecordType extends RaRecord = any,
MutationError = unknown
> = UseMutationOptions<
RecordType['id'][],
unknown,
MutationError,
Partial<UseDeleteManyMutateParams<RecordType>>
> & { mutationMode?: MutationMode };

export type UseDeleteManyResult<RecordType extends RaRecord = any> = [
export type UseDeleteManyResult<
RecordType extends RaRecord = any,
MutationError = unknown
> = [
(
resource?: string,
params?: Partial<DeleteManyParams<RecordType>>,
options?: MutateOptions<
RecordType['id'][],
unknown,
MutationError,
Partial<UseDeleteManyMutateParams<RecordType>>,
unknown
> & { mutationMode?: MutationMode }
) => Promise<void>,
UseMutationResult<
RecordType['id'][],
unknown,
MutationError,
Partial<DeleteManyParams<RecordType> & { resource?: string }>,
unknown
>
Expand Down
Loading

0 comments on commit 5aeff10

Please sign in to comment.