From abf926df8bb7998848c6512c25c35e60ff0f043c Mon Sep 17 00:00:00 2001 From: Jason Gao Date: Wed, 16 Aug 2023 14:05:32 -0400 Subject: [PATCH] fix type errors --- packages/react/src/auth/useSession.ts | 62 +++++++++++---------------- packages/react/src/auth/useUser.ts | 46 +++++++------------- 2 files changed, 41 insertions(+), 67 deletions(-) diff --git a/packages/react/src/auth/useSession.ts b/packages/react/src/auth/useSession.ts index c4c557361..c356d2d59 100644 --- a/packages/react/src/auth/useSession.ts +++ b/packages/react/src/auth/useSession.ts @@ -7,16 +7,11 @@ import type { LimitToKnownKeys, Select, } from "@gadgetinc/api-client-core"; -import type { OptionsType, ReadOperationOptions } from "src/utils"; +import { useApi } from "../../src/GadgetProvider"; import { useGet } from "../../src/useGet"; -import { useApi } from "src/GadgetProvider"; +import type { OptionsType, ReadOperationOptions } from "../../src/utils"; -export interface GadgetSession { - id: string; - userId: string | null; - user: GadgetUser | null; - [key: string]: any; -} +export type GadgetSession = GadgetRecord>; export interface GadgetUser { id: string; @@ -27,43 +22,36 @@ export type ClientWithSessionAndUserManagers }; user: { findMany: FindManyFunction }; }; + /** * Used for fetching the current `Session` record from Gadget. Will suspend while the user is being fetched. * @returns The current session */ -export function useSession(): GadgetRecord; export function useSession< SessionGivenOptions extends OptionsType, SessionSchemaT, UserGivenOptions extends OptionsType, UserSchemaT, Client extends ClientWithSessionAndUserManagers, - Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions + Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions, + ClientType extends Client | undefined >( - client: Client, + client?: ClientType, options?: LimitToKnownKeys -): GadgetRecord< - Select< - Exclude, - DefaultSelection - > ->; -export function useSession< - SessionGivenOptions extends OptionsType, - SessionSchemaT, - UserGivenOptions extends OptionsType, - UserSchemaT, - Client extends ClientWithSessionAndUserManagers, - Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions ->( - client?: Client, - options?: LimitToKnownKeys -): GadgetRecord< - Select< - Exclude, - DefaultSelection - > -> { +): undefined extends ClientType + ? GadgetSession + : GadgetRecord< + Select< + Exclude["currentSession"]["get"]["schemaType"], null | undefined>, + DefaultSelection< + Exclude["currentSession"]["get"]["selectionType"], + Options, + Exclude["currentSession"]["get"]["defaultSelection"] & { + user: Exclude["user"]["findMany"]["defaultSelection"]; + } + > + > + > { const fallbackApi = useApi() as any; const api = client ?? fallbackApi; @@ -76,10 +64,10 @@ export function useSession< ...(options ?? {}), } as any; -const [{ data: session, error }] = useGet(api.currentSession, opts); + const [{ data: session, error }] = useGet(api.currentSession, opts); if (error) throw error; if (!session) throw new Error("currentSession not found but should be present"); - return typeof client == "undefined" ? session : session as any; - // return client ? session : session as any; -}; + + return session as any; +} diff --git a/packages/react/src/auth/useUser.ts b/packages/react/src/auth/useUser.ts index 2013ecadd..b20352de5 100644 --- a/packages/react/src/auth/useUser.ts +++ b/packages/react/src/auth/useUser.ts @@ -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; export function useUser< SessionGivenOptions extends OptionsType, SessionSchemaT, UserGivenOptions extends OptionsType, UserSchemaT, Client extends ClientWithSessionAndUserManagers, - Options extends Client["user"]["findMany"]["optionsType"] & ReadOperationOptions + Options extends Client["user"]["findMany"]["optionsType"] & ReadOperationOptions, + ClientType extends Client | undefined >( - client: Client, + client?: ClientType, options?: LimitToKnownKeys -): - | GadgetRecord - | GadgetRecord< +): undefined extends ClientType + ? GadgetRecord> + : GadgetRecord< Select< - Exclude, - DefaultSelection - > - >; -export function useUser< - SessionGivenOptions extends OptionsType, - SessionSchemaT, - UserGivenOptions extends OptionsType, - UserSchemaT, - Client extends ClientWithSessionAndUserManagers, - Options extends Client["user"]["findMany"]["optionsType"] & ReadOperationOptions ->( - client?: Client, - options?: LimitToKnownKeys -): - | GadgetRecord - | GadgetRecord< - Select< - Exclude, - DefaultSelection + Exclude["user"]["findMany"]["schemaType"], null | undefined>, + DefaultSelection< + Exclude["user"]["findMany"]["selectionType"], + Options, + Exclude["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"); }