mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-23 15:42:09 -05:00
fix: session inference
This commit is contained in:
@@ -9,6 +9,7 @@ import type { Ref } from "vue";
|
||||
import type { ReadableAtom } from "nanostores";
|
||||
import type { Session } from "../adapters/schema";
|
||||
import { BetterFetchError } from "@better-fetch/fetch";
|
||||
import { twoFactorClient } from "../plugins";
|
||||
|
||||
describe("run time proxy", async () => {
|
||||
it("proxy api should be called", async () => {
|
||||
@@ -160,7 +161,7 @@ describe("type", () => {
|
||||
|
||||
it("should infer session", () => {
|
||||
const client = createSolidClient({
|
||||
plugins: [testClientPlugin(), testClientPlugin2()],
|
||||
plugins: [testClientPlugin(), testClientPlugin2(), twoFactorClient()],
|
||||
baseURL: "http://localhost:3000",
|
||||
});
|
||||
const $infer = client.$infer;
|
||||
@@ -182,6 +183,8 @@ describe("type", () => {
|
||||
testField?: string | undefined;
|
||||
testField2?: number | undefined;
|
||||
testField4: string;
|
||||
twoFactorEnabled?: boolean | undefined;
|
||||
twoFactorSecret?: string | undefined;
|
||||
}>();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,15 +12,15 @@ export function getSessionAtom<Option extends ClientOptions>(
|
||||
) {
|
||||
type Plugins = Option["plugins"] extends Array<AuthClientPlugin>
|
||||
? Array<
|
||||
UnionToIntersection<
|
||||
Option["plugins"] extends Array<infer Pl>
|
||||
? Pl extends AuthClientPlugin
|
||||
? Pl["$InferServerPlugin"] extends BetterAuthPlugin
|
||||
? Pl["$InferServerPlugin"]
|
||||
Option["plugins"][number] extends infer T
|
||||
? T extends AuthClientPlugin
|
||||
? T["$InferServerPlugin"] extends infer U
|
||||
? U extends BetterAuthPlugin
|
||||
? U
|
||||
: never
|
||||
: never
|
||||
: never
|
||||
>
|
||||
: never
|
||||
>
|
||||
: never;
|
||||
|
||||
@@ -33,9 +33,7 @@ export function getSessionAtom<Option extends ClientOptions>(
|
||||
};
|
||||
};
|
||||
|
||||
//@ts-expect-error
|
||||
type UserWithAdditionalFields = InferUser<Auth["options"]>;
|
||||
//@ts-expect-error
|
||||
type SessionWithAdditionalFields = InferSession<Auth["options"]>;
|
||||
const $signal = atom<boolean>(false);
|
||||
const session = useAuthQuery<{
|
||||
@@ -50,6 +48,7 @@ export function getSessionAtom<Option extends ClientOptions>(
|
||||
$infer: {} as {
|
||||
session: Prettify<SessionWithAdditionalFields>;
|
||||
user: Prettify<UserWithAdditionalFields>;
|
||||
pl: Plugins;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ export const testClientPlugin = () => {
|
||||
return testValue++;
|
||||
});
|
||||
return {
|
||||
id: "test",
|
||||
id: "test" as const,
|
||||
getActions($fetch) {
|
||||
return {
|
||||
setTestAtom(value: boolean) {
|
||||
|
||||
@@ -30,3 +30,25 @@ export type RequiredKeysOf<BaseType extends object> = Exclude<
|
||||
export type HasRequiredKeys<BaseType extends object> =
|
||||
RequiredKeysOf<BaseType> extends never ? false : true;
|
||||
export type WithoutEmpty<T> = T extends T ? ({} extends T ? never : T) : never;
|
||||
|
||||
type LastOf<T> = UnionToIntersection<
|
||||
T extends any ? () => T : never
|
||||
> extends () => infer R
|
||||
? R
|
||||
: never;
|
||||
|
||||
type Push<T extends any[], V> = [...T, V];
|
||||
|
||||
type TuplifyUnion<
|
||||
T,
|
||||
L = LastOf<T>,
|
||||
N = [T] extends [never] ? true : false,
|
||||
> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>;
|
||||
|
||||
// The magic happens here!
|
||||
export type Tuple<
|
||||
T,
|
||||
A extends T[] = [],
|
||||
> = TuplifyUnion<T>["length"] extends A["length"]
|
||||
? [...A]
|
||||
: Tuple<T, [T, ...A]>;
|
||||
|
||||
Reference in New Issue
Block a user