diff --git a/src/mutations.ts b/src/mutations.ts index bc4e55c..29e9f53 100644 --- a/src/mutations.ts +++ b/src/mutations.ts @@ -58,56 +58,3 @@ export function createMutation< }; return mutate; } - -export function runMutation< - O, - MutationFnError = never, - OnMutateError = never, - TContext = undefined, ->({ - mutationFn, - onMutate = () => Ok(undefined as TContext), - onSuccess = () => undefined, - onError = () => undefined, - onSettled = () => undefined, -}: { - mutationFn: (args: { context: TContext }) => - | Promise> - | Result; - onMutate?: () => - | Promise> - | Result; - onSuccess?: (output: O, args: { context: TContext }) => void; - onError?: ( - error: MutationFnError | OnMutateError, - args: { - contextResult: Result; - }, - ) => void; - onSettled?: ( - result: Result, - args: { contextResult: Result }, - ) => void; -}) { - const mutate = async (): Promise => { - const contextResult = await onMutate(); - if (!contextResult.ok) { - const error = contextResult.error; - onError(error, { contextResult }); - onSettled(contextResult, { contextResult }); - return; - } - const context = contextResult.data; - const result = await mutationFn({ context }); - if (!result.ok) { - const error = result.error; - onError(error, { contextResult }); - onSettled(result, { contextResult }); - return; - } - const output = result.data; - onSuccess(output, { context }); - onSettled(result, { contextResult }); - }; - return mutate(); -}