Skip to content

Commit 849401f

Browse files
committed
fix customCtx args type
1 parent 79501fe commit 849401f

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

packages/convex-helpers/server/customFunctions.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,15 @@ import { omit, pick } from "../index.js";
7676
* has access to resources created during input processing via closure.
7777
*/
7878
export type Customization<
79+
// The ctx object from the original function.
7980
Ctx extends Record<string, any>,
81+
// The validators for the args the customization function consumes.
8082
CustomArgsValidator extends PropertyValidators,
83+
// The ctx object produced: a patch applied to the original ctx.
8184
CustomCtx extends Record<string, any>,
85+
// The args produced by the customization function.
8286
CustomMadeArgs extends Record<string, any>,
87+
// Extra args that are passed to the input function.
8388
ExtraArgs extends Record<string, any> = Record<string, any>,
8489
> = {
8590
args: CustomArgsValidator;
@@ -155,31 +160,30 @@ export function customCtxAndArgs<
155160
CustomMadeArgs,
156161
ExtraArgs
157162
> {
163+
// This is already the right type. This function just helps you define it.
158164
return objectWithArgsAndInput;
159165
}
160166

161167
/**
162168
* A helper for defining a Customization when your mod doesn't need to add or remove
163169
* anything from args.
164-
* @param mod A function that defines how to modify the ctx.
170+
* @param modifyCtx A function that defines how to modify the ctx.
165171
* @returns A ctx delta to be applied to the original ctx.
166172
*/
167173
export function customCtx<
168174
InCtx extends Record<string, any>,
169175
OutCtx extends Record<string, any>,
170176
ExtraArgs extends Record<string, any> = Record<string, any>,
171177
>(
172-
mod: (original: InCtx, extra: ExtraArgs) => Promise<OutCtx> | OutCtx,
173-
): Customization<
174-
InCtx,
175-
Record<string, never>,
176-
OutCtx,
177-
Record<string, never>,
178-
ExtraArgs
179-
> {
178+
modifyCtx: (original: InCtx, extra: ExtraArgs) => Promise<OutCtx> | OutCtx,
179+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
180+
): Customization<InCtx, {}, OutCtx, object, ExtraArgs> {
180181
return {
181182
args: {},
182-
input: async (ctx, _, extra) => ({ ctx: await mod(ctx, extra), args: {} }),
183+
input: async (ctx, _, extra) => ({
184+
ctx: await modifyCtx(ctx, extra),
185+
args: {},
186+
}),
183187
};
184188
}
185189

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ApiFromModules,
55
RegisteredQuery,
66
DefaultFunctionArgs,
7+
FunctionReference,
78
} from "convex/server";
89
import { defineTable, defineSchema, queryGeneric, anyApi } from "convex/server";
910
import type { Equals } from "../index.js";
@@ -361,6 +362,7 @@ const testApi: ApiFromModules<{
361362
addC: typeof addC;
362363
addCU: typeof addCU;
363364
addCU2: typeof addCU2;
365+
addCtxWithExistingArg: typeof addCtxWithExistingArg;
364366
add: typeof add;
365367
addUnverified: typeof addUnverified;
366368
addUnverified2: typeof addUnverified2;
@@ -644,6 +646,32 @@ describe("zod functions", () => {
644646
});
645647
});
646648

649+
test("add ctx with existing arg", async () => {
650+
const t = convexTest(schema, modules);
651+
expect(
652+
await t.query(testApi.addCtxWithExistingArg, { b: "foo" }),
653+
).toMatchObject({
654+
ctxA: "hi",
655+
argB: "foo",
656+
});
657+
expectTypeOf(testApi.addCtxWithExistingArg).toExtend<
658+
FunctionReference<
659+
"query",
660+
"public",
661+
{ b: string },
662+
{ ctxA: string; argB: string }
663+
>
664+
>();
665+
expectTypeOf<
666+
FunctionReference<
667+
"query",
668+
"public",
669+
{ b: string },
670+
{ ctxA: string; argB: string }
671+
>
672+
>().toExtend<typeof testApi.addCtxWithExistingArg>();
673+
});
674+
647675
test("add args", async () => {
648676
const t = convexTest(schema, modules);
649677
expect(await t.query(testApi.add, {})).toMatchObject({

0 commit comments

Comments
 (0)