Skip to content

fix: make all partial args result in optional arg #731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/convex-helpers/react/sessions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ expectTypeOf<
>
>().toEqualTypeOf<[{ arg: string } | "skip"]>();

expectTypeOf<
SessionQueryArgsArray<
FunctionReference<
"query",
"public",
{ arg?: string; sessionId: SessionId | null },
any
>
>
>().toEqualTypeOf<[args?: { arg?: string } | "skip"]>();

expectTypeOf<
SessionQueryArgsArray<
FunctionReference<"query", "public", { sessionId: SessionId | null }, any>
Expand All @@ -43,6 +54,17 @@ expectTypeOf<
>
>().toEqualTypeOf<[{ arg: string }]>();

expectTypeOf<
SessionArgsArray<
FunctionReference<
"mutation",
"public",
{ arg?: string; sessionId: SessionId },
any
>
>
>().toEqualTypeOf<[args?: { arg?: string }]>();

expectTypeOf<
SessionArgsArray<
FunctionReference<"mutation", "public", { sessionId: SessionId }, any>
Expand Down
16 changes: 13 additions & 3 deletions packages/convex-helpers/react/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,33 @@ type SessionFunction<
Args = any,
> = FunctionReference<T, "public", { sessionId: SessionId } & Args>;

type ArgsWithoutSession<
Fn extends SessionFunction<"query" | "mutation" | "action">,
> = BetterOmit<FunctionArgs<Fn>, "sessionId">;

export type SessionQueryArgsArray<Fn extends SessionFunction<"query">> =
keyof FunctionArgs<Fn> extends "sessionId"
? [args?: EmptyObject | "skip"]
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId"> | "skip"];
: Partial<ArgsWithoutSession<Fn>> extends ArgsWithoutSession<Fn>
? [args?: ArgsWithoutSession<Fn> | "skip"]
: [args: ArgsWithoutSession<Fn> | "skip"];

export type SessionArgsArray<
Fn extends SessionFunction<"query" | "mutation" | "action">,
> = keyof FunctionArgs<Fn> extends "sessionId"
? [args?: EmptyObject]
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId">];
: Partial<ArgsWithoutSession<Fn>> extends ArgsWithoutSession<Fn>
? [args?: ArgsWithoutSession<Fn>]
: [args: ArgsWithoutSession<Fn>];

export type SessionArgsAndOptions<
Fn extends SessionFunction<"mutation">,
Options,
> = keyof FunctionArgs<Fn> extends "sessionId"
? [args?: EmptyObject, options?: Options]
: [args: BetterOmit<FunctionArgs<Fn>, "sessionId">, options?: Options];
: Partial<ArgsWithoutSession<Fn>> extends ArgsWithoutSession<Fn>
? [args?: ArgsWithoutSession<Fn>, options?: Options]
: [args: ArgsWithoutSession<Fn>, options?: Options];

type SessionPaginatedQueryFunction<
Args extends { paginationOpts: PaginationOptions } = {
Expand Down