Skip to content

Commit de6542f

Browse files
committed
pass in full ctx, args, and result
1 parent f453401 commit de6542f

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

packages/convex-helpers/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ const myQueryBuilder = customQuery(query, {
6565
return {
6666
ctx: { db, apiUser },
6767
args: {},
68-
onSuccess: (result) => {
68+
onSuccess: ({ args, result }) => {
6969
// Optional callback that runs after the function executes
7070
// Has access to resources created during input processing
71-
console.log(`Query for ${apiUser.name} completed:`, result);
71+
console.log(apiUser.name, args, result);
7272
},
7373
};
7474
},

packages/convex-helpers/server/customFunctions.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ describe("finally callback", () => {
633633
input: async () => ({
634634
ctx: { foo: "bar" },
635635
args: {},
636-
onSuccess: (params) => {
637-
finallyMock(params);
636+
onSuccess: ({ result }) => {
637+
finallyMock(result);
638638
},
639639
}),
640640
});
@@ -684,8 +684,8 @@ describe("finally callback", () => {
684684
input: async () => ({
685685
ctx: { foo: "bar" },
686686
args: {},
687-
onSuccess: (params) => {
688-
finallyMock(params);
687+
onSuccess: ({ result }) => {
688+
finallyMock(result);
689689
},
690690
}),
691691
});
@@ -714,8 +714,8 @@ describe("finally callback", () => {
714714
input: async () => ({
715715
ctx: { foo: "bar" },
716716
args: {},
717-
onSuccess: (params) => {
718-
finallyMock(params);
717+
onSuccess: ({ result }) => {
718+
finallyMock(result);
719719
},
720720
}),
721721
});

packages/convex-helpers/server/customFunctions.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type {
1515
ObjectType,
1616
PropertyValidators,
1717
Validator,
18-
Value,
1918
} from "convex/values";
2019
import { asObjectValidator, v } from "convex/values";
2120
import type {
@@ -92,12 +91,20 @@ export type Customization<
9291
| Promise<{
9392
ctx: CustomCtx;
9493
args: CustomMadeArgs;
95-
onSuccess?: (result: unknown) => void | Promise<void>;
94+
onSuccess?: (obj: {
95+
ctx: Ctx;
96+
args: Record<string, unknown>;
97+
result: unknown;
98+
}) => void | Promise<void>;
9699
}>
97100
| {
98101
ctx: CustomCtx;
99102
args: CustomMadeArgs;
100-
onSuccess?: (result: unknown) => void | Promise<void>;
103+
onSuccess?: (obj: {
104+
ctx: Ctx;
105+
args: Record<string, unknown>;
106+
result: unknown;
107+
}) => void | Promise<void>;
101108
};
102109
};
103110

@@ -201,7 +208,7 @@ export const NoOp = {
201208
* return {
202209
* ctx: { db, user, session },
203210
* args: {},
204-
* onSuccess: (result) => {
211+
* onSuccess: ({ result }) => {
205212
* // Optional callback that runs after the function executes
206213
* // Has access to resources created during input processing
207214
* console.log(`Query for ${user.name} returned:`, result);
@@ -289,7 +296,7 @@ export function customQuery<
289296
* return {
290297
* ctx: { db, user, session },
291298
* args: {},
292-
* onSuccess: (result) => {
299+
* onSuccess: ({ result }) => {
293300
* // Optional callback that runs after the function executes
294301
* // Has access to resources created during input processing
295302
* console.log(`User ${user.name} returned:`, result);
@@ -381,7 +388,7 @@ export function customMutation<
381388
* return {
382389
* ctx: { user },
383390
* args: {},
384-
* onSuccess: (result) => {
391+
* onSuccess: ({ result }) => {
385392
* // Optional callback that runs after the function executes
386393
* // Has access to resources created during input processing
387394
* logger.info(`Action for user ${user.name} returned:`, result);
@@ -484,9 +491,10 @@ function customFnBuilder(
484491
);
485492
const args = omit(allArgs, Object.keys(inputArgs));
486493
const finalCtx = { ...ctx, ...added.ctx };
487-
const result = await handler(finalCtx, { ...args, ...added.args });
494+
const finalArgs = { ...args, ...added.args };
495+
const result = await handler(finalCtx, finalArgs);
488496
if (added.onSuccess) {
489-
await added.onSuccess(result ?? null);
497+
await added.onSuccess({ ctx, args, result });
490498
}
491499
return result;
492500
},
@@ -503,9 +511,10 @@ function customFnBuilder(
503511
handler: async (ctx: any, args: any) => {
504512
const added = await customInput(ctx, args, extra);
505513
const finalCtx = { ...ctx, ...added.ctx };
506-
const result = await handler(finalCtx, { ...args, ...added.args });
514+
const finalArgs = { ...args, ...added.args };
515+
const result = await handler(finalCtx, finalArgs);
507516
if (added.onSuccess) {
508-
await added.onSuccess(result ?? null);
517+
await added.onSuccess({ ctx, args, result });
509518
}
510519
return result;
511520
},

0 commit comments

Comments
 (0)