fix: session inference

This commit is contained in:
Bereket Engida
2024-09-12 14:29:54 +03:00
parent 910ce4066e
commit 2e7c82ff57
5 changed files with 35 additions and 10 deletions

View File

@@ -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;
}>();
});
});

View File

@@ -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;
},
};
}

View File

@@ -59,7 +59,7 @@ export const testClientPlugin = () => {
return testValue++;
});
return {
id: "test",
id: "test" as const,
getActions($fetch) {
return {
setTestAtom(value: boolean) {

View File

@@ -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]>;

View File

@@ -12,6 +12,7 @@
[ ] allow enabling two factor automatically for users
[ ] change the pg driver to https://www.npmjs.com/package/postgres (maybe)
[ ] mention how users can get user and session types
[ ] add a section about updating user and changing password
## Docs