-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
41 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,47 @@ | ||
import type { DefaultSelection, GadgetRecord, LimitToKnownKeys, Select } from "@gadgetinc/api-client-core"; | ||
import type { OptionsType, ReadOperationOptions } from "src/utils"; | ||
import { useApi } from "../../src/GadgetProvider"; | ||
import type { ClientWithSessionAndUserManagers} from "./useSession"; | ||
import type { OptionsType, ReadOperationOptions } from "../../src/utils"; | ||
import type { ClientWithSessionAndUserManagers } from "./useSession"; | ||
import { useSession } from "./useSession"; | ||
|
||
/** | ||
* Used for fetching the current `User` record from Gadget. Will return `null` if the session is unauthenticated. Will suspend while the user is being fetched. | ||
* @returns The current user associated with the session or `null`. | ||
*/ | ||
export function useUser(): GadgetRecord<any>; | ||
export function useUser< | ||
SessionGivenOptions extends OptionsType, | ||
SessionSchemaT, | ||
UserGivenOptions extends OptionsType, | ||
UserSchemaT, | ||
Client extends ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>, | ||
Options extends Client["user"]["findMany"]["optionsType"] & ReadOperationOptions | ||
Options extends Client["user"]["findMany"]["optionsType"] & ReadOperationOptions, | ||
ClientType extends Client | undefined | ||
>( | ||
client: Client, | ||
client?: ClientType, | ||
options?: LimitToKnownKeys<Options, Client["user"]["findMany"]["optionsType"] & ReadOperationOptions> | ||
): | ||
| GadgetRecord<any> | ||
| GadgetRecord< | ||
): undefined extends ClientType | ||
? GadgetRecord<Record<string, any>> | ||
: GadgetRecord< | ||
Select< | ||
Exclude<Client["user"]["findMany"]["schemaType"], null | undefined>, | ||
DefaultSelection<Client["user"]["findMany"]["selectionType"], Options, Client["user"]["findMany"]["defaultSelection"]> | ||
> | ||
>; | ||
export function useUser< | ||
SessionGivenOptions extends OptionsType, | ||
SessionSchemaT, | ||
UserGivenOptions extends OptionsType, | ||
UserSchemaT, | ||
Client extends ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>, | ||
Options extends Client["user"]["findMany"]["optionsType"] & ReadOperationOptions | ||
>( | ||
client?: Client, | ||
options?: LimitToKnownKeys<Options, Client["user"]["findMany"]["optionsType"] & ReadOperationOptions> | ||
): | ||
| GadgetRecord<any> | ||
| GadgetRecord< | ||
Select< | ||
Exclude<Client["user"]["findMany"]["schemaType"], null | undefined>, | ||
DefaultSelection<Client["user"]["findMany"]["selectionType"], Options, Client["user"]["findMany"]["defaultSelection"]> | ||
Exclude<Exclude<ClientType, undefined>["user"]["findMany"]["schemaType"], null | undefined>, | ||
DefaultSelection< | ||
Exclude<ClientType, undefined>["user"]["findMany"]["selectionType"], | ||
Options, | ||
Exclude<ClientType, undefined>["user"]["findMany"]["defaultSelection"] | ||
> | ||
> | ||
> { | ||
const fallbackApi = useApi() as any; | ||
const api = client ?? fallbackApi; | ||
|
||
const { select, ...opts } = options ?? {}; | ||
const defaultOpts: Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions = { | ||
const defaultOpts = { | ||
suspense: true, | ||
...opts, | ||
select: { | ||
user: select ?? api.user.findMany.defaultSelection, | ||
}, | ||
} as any; | ||
}; | ||
const session = useSession(api, defaultOpts); | ||
return session && session.getField("user"); | ||
} |