diff --git a/.changeset/cyan-chefs-sniff.md b/.changeset/cyan-chefs-sniff.md new file mode 100644 index 0000000..0e43c6f --- /dev/null +++ b/.changeset/cyan-chefs-sniff.md @@ -0,0 +1,5 @@ +--- +"@epicenterhq/result": patch +--- + +Update first parameter of createMutation for improved clarity diff --git a/src/query.ts b/src/query.ts index a7a169d..4036fdc 100644 --- a/src/query.ts +++ b/src/query.ts @@ -12,43 +12,42 @@ export function createMutation({ onError = () => undefined, onSettled = () => undefined, }: { - mutationFn: (args: { input: I; context: TContext }) => - | Promise> - | Result; + mutationFn: ( + input: I, + args: { context: TContext }, + ) => Promise> | Result; onMutate?: ( input: I, ) => Promise> | Result; - onSuccess?: (args: { output: O; input: I; context: TContext }) => void; - onError?: (args: { - error: ServiceError; - input: I; - contextResult: Result; - }) => void; - onSettled?: (args: { - result: Result; - input: I; - contextResult: Result; - }) => void; + onSuccess?: (output: O, args: { input: I; context: TContext }) => void; + onError?: ( + error: ServiceError, + args: { input: I; contextResult: Result }, + ) => void; + onSettled?: ( + result: Result, + args: { input: I; contextResult: Result }, + ) => void; }) { const mutate = async (input: I): Promise => { const contextResult = await onMutate(input); if (!contextResult.ok) { const error = contextResult.error; - onError({ error, input, contextResult }); - onSettled({ result: contextResult, input, contextResult }); + onError(error, { input, contextResult }); + onSettled(contextResult, { input, contextResult }); return; } const context = contextResult.data; - const result = await mutationFn({ input, context }); + const result = await mutationFn(input, { context }); if (!result.ok) { const error = result.error; - onError({ error, input, contextResult }); - onSettled({ result, input, contextResult }); + onError(error, { input, contextResult }); + onSettled(result, { input, contextResult }); return; } const output = result.data; - onSuccess({ output, input, context }); - onSettled({ result, input, contextResult }); + onSuccess(output, { input, context }); + onSettled(result, { input, contextResult }); }; return { mutate }; }