From 7246015278df33e6f1c257950bb3632ae0317523 Mon Sep 17 00:00:00 2001 From: Richard Solomou Date: Thu, 23 Oct 2025 14:45:00 +0300 Subject: [PATCH 1/2] fix: preserve BetterAuth options type in CreateAuth and getStaticAuth for proper type inference --- src/client/index.ts | 123 +++++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/src/client/index.ts b/src/client/index.ts index b461fa4..823df0b 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -19,7 +19,7 @@ import { } from "convex/server"; import { type GenericId, Infer, v } from "convex/values"; import { convexAdapter } from "./adapter"; -import { AdapterInstance, betterAuth } from "better-auth"; +import { AdapterInstance, betterAuth, type Auth, type BetterAuthOptions } from "better-auth"; import { asyncMap } from "convex-helpers"; import { partial } from "convex-helpers/validators"; import { @@ -44,16 +44,19 @@ export type CreateAdapter = >( ctx: Ctx ) => AdapterInstance; -export type CreateAuth = - | ((ctx: GenericCtx) => ReturnType) - | (( - ctx: GenericCtx, - opts?: { optionsOnly?: boolean } - ) => ReturnType); +export type CreateAuth< + DataModel extends GenericDataModel, + Options extends BetterAuthOptions = any +> = + | ((ctx: GenericCtx) => Auth) + | ((ctx: GenericCtx, opts?: { optionsOnly?: boolean }) => Auth); -export const getStaticAuth = ( - createAuth: CreateAuth -) => { +export const getStaticAuth = < + DataModel extends GenericDataModel, + Options extends BetterAuthOptions = any +>( + createAuth: CreateAuth +): Auth => { return createAuth({} as any, { optionsOnly: true }); }; @@ -407,34 +410,34 @@ export type Triggers< DataModel extends GenericDataModel, Schema extends SchemaDefinition, > = { - [K in keyof Schema["tables"]]?: { - onCreate?: >( - ctx: Ctx, - doc: Infer & { - _id: string; - _creationTime: number; - } - ) => Promise; - onUpdate?: >( - ctx: Ctx, - newDoc: Infer & { - _id: string; - _creationTime: number; - }, - oldDoc: Infer & { - _id: string; - _creationTime: number; - } - ) => Promise; - onDelete?: >( - ctx: Ctx, - doc: Infer & { - _id: string; - _creationTime: number; - } - ) => Promise; + [K in keyof Schema["tables"]]?: { + onCreate?: >( + ctx: Ctx, + doc: Infer & { + _id: string; + _creationTime: number; + } + ) => Promise; + onUpdate?: >( + ctx: Ctx, + newDoc: Infer & { + _id: string; + _creationTime: number; + }, + oldDoc: Infer & { + _id: string; + _creationTime: number; + } + ) => Promise; + onDelete?: >( + ctx: Ctx, + doc: Infer & { + _id: string; + _creationTime: number; + } + ) => Promise; + }; }; -}; export const createClient = < DataModel extends GenericDataModel, @@ -650,13 +653,13 @@ export const createClient = < createAuth: CreateAuth, opts: { cors?: - | boolean - | { - // These values are appended to the default values - allowedOrigins?: string[]; - allowedHeaders?: string[]; - exposedHeaders?: string[]; - }; + | boolean + | { + // These values are appended to the default values + allowedOrigins?: string[]; + allowedHeaders?: string[]; + exposedHeaders?: string[]; + }; } = {} ) => { const staticAuth = getStaticAuth(createAuth); @@ -761,14 +764,14 @@ export const createClient = < export type OpaqueIds = T extends GenericId - ? string - : T extends (infer U)[] - ? OpaqueIds[] - : T extends ArrayBuffer - ? ArrayBuffer - : T extends object - ? { [K in keyof T]: OpaqueIds } - : T; + ? string + : T extends (infer U)[] + ? OpaqueIds[] + : T extends ArrayBuffer + ? ArrayBuffer + : T extends object + ? { [K in keyof T]: OpaqueIds } + : T; export type UseApi = Expand<{ [mod in keyof API]: API[mod] extends FunctionReference< @@ -778,12 +781,12 @@ export type UseApi = Expand<{ infer FReturnType, infer FComponentPath > - ? FunctionReference< - FType, - "internal", - OpaqueIds, - OpaqueIds, - FComponentPath - > - : UseApi; + ? FunctionReference< + FType, + "internal", + OpaqueIds, + OpaqueIds, + FComponentPath + > + : UseApi; }>; From 6911663baa65aad20edec068cc5381820ba5f227 Mon Sep 17 00:00:00 2001 From: Richard Solomou Date: Thu, 23 Oct 2025 14:49:30 +0300 Subject: [PATCH 2/2] fix whitespace --- src/client/index.ts | 100 ++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/src/client/index.ts b/src/client/index.ts index 823df0b..1ee5333 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -410,34 +410,34 @@ export type Triggers< DataModel extends GenericDataModel, Schema extends SchemaDefinition, > = { - [K in keyof Schema["tables"]]?: { - onCreate?: >( - ctx: Ctx, - doc: Infer & { - _id: string; - _creationTime: number; - } - ) => Promise; - onUpdate?: >( - ctx: Ctx, - newDoc: Infer & { - _id: string; - _creationTime: number; - }, - oldDoc: Infer & { - _id: string; - _creationTime: number; - } - ) => Promise; - onDelete?: >( - ctx: Ctx, - doc: Infer & { - _id: string; - _creationTime: number; - } - ) => Promise; - }; + [K in keyof Schema["tables"]]?: { + onCreate?: >( + ctx: Ctx, + doc: Infer & { + _id: string; + _creationTime: number; + } + ) => Promise; + onUpdate?: >( + ctx: Ctx, + newDoc: Infer & { + _id: string; + _creationTime: number; + }, + oldDoc: Infer & { + _id: string; + _creationTime: number; + } + ) => Promise; + onDelete?: >( + ctx: Ctx, + doc: Infer & { + _id: string; + _creationTime: number; + } + ) => Promise; }; +}; export const createClient = < DataModel extends GenericDataModel, @@ -653,13 +653,13 @@ export const createClient = < createAuth: CreateAuth, opts: { cors?: - | boolean - | { - // These values are appended to the default values - allowedOrigins?: string[]; - allowedHeaders?: string[]; - exposedHeaders?: string[]; - }; + | boolean + | { + // These values are appended to the default values + allowedOrigins?: string[]; + allowedHeaders?: string[]; + exposedHeaders?: string[]; + }; } = {} ) => { const staticAuth = getStaticAuth(createAuth); @@ -764,14 +764,14 @@ export const createClient = < export type OpaqueIds = T extends GenericId - ? string - : T extends (infer U)[] - ? OpaqueIds[] - : T extends ArrayBuffer - ? ArrayBuffer - : T extends object - ? { [K in keyof T]: OpaqueIds } - : T; + ? string + : T extends (infer U)[] + ? OpaqueIds[] + : T extends ArrayBuffer + ? ArrayBuffer + : T extends object + ? { [K in keyof T]: OpaqueIds } + : T; export type UseApi = Expand<{ [mod in keyof API]: API[mod] extends FunctionReference< @@ -781,12 +781,12 @@ export type UseApi = Expand<{ infer FReturnType, infer FComponentPath > - ? FunctionReference< - FType, - "internal", - OpaqueIds, - OpaqueIds, - FComponentPath - > - : UseApi; + ? FunctionReference< + FType, + "internal", + OpaqueIds, + OpaqueIds, + FComponentPath + > + : UseApi; }>;