mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
fix: should infer types correctly when empty list of plugins is provided (#4612)
This commit is contained in:
@@ -274,19 +274,21 @@ export type InferFieldsFromPlugins<
|
|||||||
Options extends BetterAuthOptions,
|
Options extends BetterAuthOptions,
|
||||||
Key extends string,
|
Key extends string,
|
||||||
Format extends "output" | "input" = "output",
|
Format extends "output" | "input" = "output",
|
||||||
> = Options["plugins"] extends Array<infer T>
|
> = Options["plugins"] extends []
|
||||||
? T extends {
|
? {}
|
||||||
schema: {
|
: Options["plugins"] extends Array<infer T>
|
||||||
[key in Key]: {
|
? T extends {
|
||||||
fields: infer Field;
|
schema: {
|
||||||
|
[key in Key]: {
|
||||||
|
fields: infer Field;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
}
|
? Format extends "output"
|
||||||
? Format extends "output"
|
? InferFieldsOutput<Field>
|
||||||
? InferFieldsOutput<Field>
|
: InferFieldsInput<Field>
|
||||||
: InferFieldsInput<Field>
|
: {}
|
||||||
: {}
|
: {};
|
||||||
: {};
|
|
||||||
|
|
||||||
export type InferFieldsFromOptions<
|
export type InferFieldsFromOptions<
|
||||||
Options extends BetterAuthOptions,
|
Options extends BetterAuthOptions,
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ export async function getTestInstanceMemory<
|
|||||||
...options?.advanced,
|
...options?.advanced,
|
||||||
},
|
},
|
||||||
plugins: [bearer(), ...(options?.plugins || [])],
|
plugins: [bearer(), ...(options?.plugins || [])],
|
||||||
} as O extends undefined ? typeof opts : O & typeof opts);
|
} as unknown as O extends undefined ? typeof opts : O & typeof opts);
|
||||||
|
|
||||||
const testUser = {
|
const testUser = {
|
||||||
email: "test@test.com",
|
email: "test@test.com",
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ export async function getTestInstance<
|
|||||||
...options?.advanced,
|
...options?.advanced,
|
||||||
},
|
},
|
||||||
plugins: [bearer(), ...(options?.plugins || [])],
|
plugins: [bearer(), ...(options?.plugins || [])],
|
||||||
} as O extends undefined ? typeof opts : O & typeof opts);
|
} as unknown as O extends undefined ? typeof opts : O & typeof opts);
|
||||||
|
|
||||||
const testUser = {
|
const testUser = {
|
||||||
email: "test@test.com",
|
email: "test@test.com",
|
||||||
|
|||||||
@@ -291,7 +291,7 @@ export type BetterAuthOptions = {
|
|||||||
/**
|
/**
|
||||||
* List of Better Auth plugins
|
* List of Better Auth plugins
|
||||||
*/
|
*/
|
||||||
plugins?: BetterAuthPlugin[];
|
plugins?: [] | BetterAuthPlugin[];
|
||||||
/**
|
/**
|
||||||
* User configuration
|
* User configuration
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -55,4 +55,26 @@ describe("general types", async (it) => {
|
|||||||
activeOrganizationId?: string | undefined | null;
|
activeOrganizationId?: string | undefined | null;
|
||||||
}>();
|
}>();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should infer the same types for empty plugins and no plugins", async () => {
|
||||||
|
const { auth: authWithEmptyPlugins } = await getTestInstance({
|
||||||
|
plugins: [],
|
||||||
|
secret: "test-secret",
|
||||||
|
emailAndPassword: {
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const { auth: authWithoutPlugins } = await getTestInstance({
|
||||||
|
secret: "test-secret",
|
||||||
|
emailAndPassword: {
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
type SessionWithEmptyPlugins = typeof authWithEmptyPlugins.$Infer;
|
||||||
|
type SessionWithoutPlugins = typeof authWithoutPlugins.$Infer;
|
||||||
|
|
||||||
|
expectTypeOf<SessionWithEmptyPlugins>().toEqualTypeOf<SessionWithoutPlugins>();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user