mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-24 08:01:56 -05:00
fix(stripe): add generic return type to getSchema for proper field inference (#7353)
This commit is contained in:
@@ -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>;
|
||||
};
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
Reference in New Issue
Block a user