mirror of
https://github.com/Sonarr/Sonarr.git
synced 2026-04-28 23:16:32 -04:00
35 lines
965 B
TypeScript
35 lines
965 B
TypeScript
import { useMutation, UseMutationOptions } from '@tanstack/react-query';
|
|
import { useMemo } from 'react';
|
|
import { Error } from 'App/State/AppSectionState';
|
|
import fetchJson, {
|
|
apiRoot,
|
|
FetchJsonOptions,
|
|
} from 'Utilities/Fetch/fetchJson';
|
|
|
|
interface MutationOptions<T, TData>
|
|
extends Omit<FetchJsonOptions<TData>, 'method'> {
|
|
method: 'POST' | 'PUT' | 'DELETE';
|
|
mutationOptions?: Omit<UseMutationOptions<T, Error, TData>, 'mutationFn'>;
|
|
}
|
|
|
|
function useApiMutation<T, TData>(options: MutationOptions<T, TData>) {
|
|
const requestOptions = useMemo(() => {
|
|
return {
|
|
...options,
|
|
path: apiRoot + options.path,
|
|
headers: {
|
|
...options.headers,
|
|
'X-Api-Key': window.Sonarr.apiKey,
|
|
},
|
|
};
|
|
}, [options]);
|
|
|
|
return useMutation<T, Error, TData>({
|
|
...options.mutationOptions,
|
|
mutationFn: async (data: TData) =>
|
|
fetchJson<T, TData>({ ...requestOptions, body: data }),
|
|
});
|
|
}
|
|
|
|
export default useApiMutation;
|