Skip to content

Commit

Permalink
fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jasong689 committed Aug 16, 2023
1 parent 398f307 commit abf926d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 67 deletions.
62 changes: 25 additions & 37 deletions packages/react/src/auth/useSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, any>>;

export interface GadgetUser {
id: string;
Expand All @@ -27,43 +22,36 @@ export type ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT
currentSession: { get: GetFunction<SessionGivenOptions, any, SessionSchemaT, any> };
user: { findMany: FindManyFunction<UserGivenOptions, any, UserSchemaT, any> };
};

/**
* 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<any>;
export function useSession<
SessionGivenOptions extends OptionsType,
SessionSchemaT,
UserGivenOptions extends OptionsType,
UserSchemaT,
Client extends ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>,
Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions
Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions,
ClientType extends Client | undefined
>(
client: Client,
client?: ClientType,
options?: LimitToKnownKeys<Options, Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions>
): GadgetRecord<
Select<
Exclude<Client["currentSession"]["get"]["schemaType"], null | undefined>,
DefaultSelection<Client["currentSession"]["get"]["selectionType"], Options, Client["currentSession"]["get"]["defaultSelection"] & { user: Client["user"]["findMany"]["defaultSelection"]}>
>
>;
export function useSession<
SessionGivenOptions extends OptionsType,
SessionSchemaT,
UserGivenOptions extends OptionsType,
UserSchemaT,
Client extends ClientWithSessionAndUserManagers<SessionGivenOptions, SessionSchemaT, UserGivenOptions, UserSchemaT>,
Options extends Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions
>(
client?: Client,
options?: LimitToKnownKeys<Options, Client["currentSession"]["get"]["optionsType"] & ReadOperationOptions>
): GadgetRecord<
Select<
Exclude<Client["currentSession"]["get"]["schemaType"], null | undefined>,
DefaultSelection<Client["currentSession"]["get"]["selectionType"], Options, Client["currentSession"]["get"]["defaultSelection"] & { user: Client["user"]["findMany"]["defaultSelection"]}>
>
> {
): undefined extends ClientType
? GadgetSession
: GadgetRecord<
Select<
Exclude<Exclude<ClientType, undefined>["currentSession"]["get"]["schemaType"], null | undefined>,
DefaultSelection<
Exclude<ClientType, undefined>["currentSession"]["get"]["selectionType"],
Options,
Exclude<ClientType, undefined>["currentSession"]["get"]["defaultSelection"] & {
user: Exclude<ClientType, undefined>["user"]["findMany"]["defaultSelection"];
}
>
>
> {
const fallbackApi = useApi() as any;

const api = client ?? fallbackApi;
Expand All @@ -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;
}
46 changes: 16 additions & 30 deletions packages/react/src/auth/useUser.ts
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");
}

0 comments on commit abf926d

Please sign in to comment.