Skip to content

Commit 4920d81

Browse files
Merge pull request #865 from gadget-inc/mill/updateUseSessionToMakeUserModelOptional
Updated `useSession` so that there is no type error when apps do not have `user` models
2 parents eda6d0b + 6936a1d commit 4920d81

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

packages/react/src/auth/useSession.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ export type ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT
2020
user: { findMany: FindManyFunction<UserGivenOptions, any, UserSchemaT, any> };
2121
};
2222

23+
export type ClientWithSessionAndMaybeUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT> = Omit<
24+
ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>,
25+
"user"
26+
> & {
27+
user?: { findMany: FindManyFunction<UserGivenOptions, any, UserSchemaT, any> };
28+
};
29+
2330
/**
2431
* Used for fetching the current `Session` record from Gadget. Will suspend while the user is being fetched.
2532
* @returns The current session
@@ -29,7 +36,7 @@ export function useSession<
2936
SessionSchemaT,
3037
UserGivenOptions extends OptionsType,
3138
UserSchemaT,
32-
Client extends ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>,
39+
Client extends ClientWithSessionAndMaybeUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>,
3340
Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions,
3441
ClientType extends Client | undefined
3542
>(
@@ -44,7 +51,9 @@ export function useSession<
4451
Exclude<ClientType, undefined>["currentSession"]["get"]["selectionType"],
4552
Options,
4653
Exclude<ClientType, undefined>["currentSession"]["get"]["defaultSelection"] & {
47-
user: Exclude<ClientType, undefined>["user"]["findMany"]["defaultSelection"];
54+
user: Exclude<ClientType, undefined>["user"] extends { findMany: FindManyFunction<UserGivenOptions, any, UserSchemaT, any> }
55+
? Exclude<ClientType, undefined>["user"]["findMany"]["defaultSelection"]
56+
: never;
4857
}
4958
>
5059
>

0 commit comments

Comments
 (0)