diff --git a/.changeset/soft-tips-mix.md b/.changeset/soft-tips-mix.md new file mode 100644 index 0000000..867f370 --- /dev/null +++ b/.changeset/soft-tips-mix.md @@ -0,0 +1,5 @@ +--- +"@kubiks/otel-better-auth": patch +--- + +Fix return type of instrumentBetterAuth function. diff --git a/packages/otel-better-auth/src/index.ts b/packages/otel-better-auth/src/index.ts index e54eff1..c8bb610 100644 --- a/packages/otel-better-auth/src/index.ts +++ b/packages/otel-better-auth/src/index.ts @@ -6,7 +6,7 @@ import { type Span, type Tracer, } from "@opentelemetry/api"; -import type { Auth, BetterAuthPlugin, betterAuth } from "better-auth"; +import type { Auth, BetterAuthPlugin } from "better-auth"; import { createAuthMiddleware } from "better-auth/api"; const DEFAULT_TRACER_NAME = "@kubiks/otel-better-auth"; @@ -44,16 +44,21 @@ interface InstrumentedAuth { [INSTRUMENTED_FLAG]?: true; } -type BetterAuthInstance = ReturnType; -type BetterAuthInstanceOptions = BetterAuthInstance["options"]; +type AnyAuth = Auth; -type AuthWithOptions< - Options extends BetterAuthInstanceOptions = BetterAuthInstanceOptions, -> = Omit, "options"> & { - options: Options & { - plugins?: BetterAuthPlugin[] | Iterable; - }; -}; +type AuthWithPlugins = TAuth extends { + options: infer Options; +} + ? TAuth & { + options: Options & { + plugins?: BetterAuthPlugin[] | Iterable; + }; + } + : TAuth & { + options: { + plugins?: BetterAuthPlugin[] | Iterable; + }; + }; /** * Ends a span with an error status and exception recording. @@ -187,11 +192,11 @@ const API_METHOD_METADATA: Record< /** * Instruments a Better Auth instance with OpenTelemetry tracing. */ -function instrumentServer = any>( - server: Auth, +function instrumentServer( + server: TAuth, tracer: Tracer, -): Auth { - const api = server.api as any; // Cast to any to allow property assignment +): TAuth { + const api = (server as AnyAuth).api as Record; const instrumentedMethods = new Set(); // First, instrument all known API methods with specific metadata @@ -242,13 +247,11 @@ function instrumentServer = any>( return server; } -function ensureOtelPlugin< - Options extends BetterAuthInstanceOptions = BetterAuthInstanceOptions, ->( - auth: Auth, +function ensureOtelPlugin( + auth: TAuth, config: InstrumentBetterAuthConfig & { tracer: Tracer }, ): void { - const authWithOptions = auth as AuthWithOptions; + const authWithOptions = auth as AuthWithPlugins; const options = authWithOptions.options; const existingPlugins = options.plugins; const pluginsArray = Array.isArray(existingPlugins) @@ -303,14 +306,15 @@ function ensureOtelPlugin< * await auth.api.getSession({ headers }); * ``` */ -export function instrumentBetterAuth< - Options extends BetterAuthInstanceOptions = BetterAuthInstanceOptions, ->(auth: Auth, config?: InstrumentBetterAuthConfig): Auth { +export function instrumentBetterAuth( + auth: TAuth, + config?: InstrumentBetterAuthConfig, +): TAuth { if (!auth || typeof auth !== "object") { return auth; } - if ((auth as Auth & InstrumentedAuth)[INSTRUMENTED_FLAG]) { + if ((auth as TAuth & InstrumentedAuth)[INSTRUMENTED_FLAG]) { return auth; } @@ -322,7 +326,7 @@ export function instrumentBetterAuth< ensureOtelPlugin(auth, { tracerName, tracer }); instrumentServer(auth, tracer); - (auth as Auth & InstrumentedAuth)[INSTRUMENTED_FLAG] = true; + (auth as TAuth & InstrumentedAuth)[INSTRUMENTED_FLAG] = true; return auth; }