fix(stripe): add generic return type to getSchema for proper field inference (#7353)

This commit is contained in:
Taesu
2026-01-15 04:13:46 +09:00
committed by GitHub
parent d90218f901
commit e0230a8c58
2 changed files with 49 additions and 4 deletions

View File

@@ -88,8 +88,14 @@ export const organization = {
},
} satisfies BetterAuthPluginDBSchema;
export const getSchema = (options: StripeOptions) => {
let baseSchema = {};
type GetSchemaResult<O extends StripeOptions> = typeof user &
(O["subscription"] extends { enabled: true } ? typeof subscriptions : {}) &
(O["organization"] extends { enabled: true } ? typeof organization : {});
export const getSchema = <O extends StripeOptions>(
options: O,
): GetSchemaResult<O> => {
let baseSchema: BetterAuthPluginDBSchema = {};
if (options.subscription?.enabled) {
baseSchema = {
@@ -115,8 +121,8 @@ export const getSchema = (options: StripeOptions) => {
"subscription" in options.schema
) {
const { subscription: _subscription, ...restSchema } = options.schema;
return mergeSchema(baseSchema, restSchema);
return mergeSchema(baseSchema, restSchema) as GetSchemaResult<O>;
}
return mergeSchema(baseSchema, options.schema);
return mergeSchema(baseSchema, options.schema) as GetSchemaResult<O>;
};

View File

@@ -58,6 +58,45 @@ describe("stripe type", () => {
expectTypeOf<MyAuth["api"]["upgradeSubscription"]>().toBeFunction();
expectTypeOf<MyAuth["api"]["createBillingPortal"]>().toBeFunction();
});
it("should infer plugin schema fields on user type", async () => {
const { auth } = await getTestInstance({
plugins: [
stripe({
stripeClient: {} as Stripe,
stripeWebhookSecret: "test",
}),
],
});
expectTypeOf<
(typeof auth)["$Infer"]["Session"]["user"]["stripeCustomerId"]
>().toEqualTypeOf<string | null | undefined>();
});
it("should infer plugin schema fields alongside additional user fields", async () => {
const { auth } = await getTestInstance({
plugins: [
stripe({
stripeClient: {} as Stripe,
stripeWebhookSecret: "test",
}),
],
user: {
additionalFields: {
customField: {
type: "string",
required: false,
},
},
},
});
expectTypeOf<
(typeof auth)["$Infer"]["Session"]["user"]["stripeCustomerId"]
>().toEqualTypeOf<string | null | undefined>();
expectTypeOf<
(typeof auth)["$Infer"]["Session"]["user"]["customField"]
>().toEqualTypeOf<string | null | undefined>();
});
});
describe("stripe", () => {