@better-auth/core 1.6.0-beta.0 → 1.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,15 +4,13 @@ import { createLogger } from "../env/logger.mjs";
4
4
  import { AuthContext } from "../types/context.mjs";
5
5
  import { OAuthProvider } from "../oauth2/oauth-provider.mjs";
6
6
  import * as better_call0 from "better-call";
7
- import { Endpoint, EndpointContext, EndpointMetadata, HTTPMethod, Middleware, ResolveBodyInput, ResolveErrorInput, ResolveMetaInput, ResolveQueryInput, StandardSchemaV1 } from "better-call";
7
+ import { EndpointContext, EndpointOptions, StrictEndpoint } from "better-call";
8
8
  import * as _better_auth_core0 from "@better-auth/core";
9
9
 
10
10
  //#region src/api/index.d.ts
11
- declare const optionsMiddleware: Middleware<(inputContext: Record<string, any>) => Promise<AuthContext>>;
11
+ declare const optionsMiddleware: <InputCtx extends better_call0.MiddlewareInputContext<better_call0.MiddlewareOptions>>(inputContext: InputCtx) => Promise<AuthContext>;
12
12
  declare const createAuthMiddleware: {
13
- <R>(options: {
14
- use?: Middleware[];
15
- }, handler: (ctx: better_call0.MiddlewareContext<{
13
+ <Options extends better_call0.MiddlewareOptions, R>(options: Options, handler: (ctx: better_call0.MiddlewareContext<Options, {
16
14
  returned?: unknown | undefined;
17
15
  responseHeaders?: Headers | undefined;
18
16
  } & _better_auth_core0.PluginContext<_better_auth_core0.BetterAuthOptions> & _better_auth_core0.InfoContext & {
@@ -140,8 +138,8 @@ declare const createAuthMiddleware: {
140
138
  skipCSRFCheck: boolean;
141
139
  runInBackground: (promise: Promise<unknown>) => void;
142
140
  runInBackgroundOrAwait: (promise: Promise<unknown> | void) => _better_auth_core0.Awaitable<unknown>;
143
- }>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
144
- <R>(handler: (ctx: better_call0.MiddlewareContext<{
141
+ }>) => Promise<R>): (inputContext: better_call0.MiddlewareInputContext<Options>) => Promise<R>;
142
+ <Options extends better_call0.MiddlewareOptions, R_1>(handler: (ctx: better_call0.MiddlewareContext<Options, {
145
143
  returned?: unknown | undefined;
146
144
  responseHeaders?: Headers | undefined;
147
145
  } & _better_auth_core0.PluginContext<_better_auth_core0.BetterAuthOptions> & _better_auth_core0.InfoContext & {
@@ -269,40 +267,12 @@ declare const createAuthMiddleware: {
269
267
  skipCSRFCheck: boolean;
270
268
  runInBackground: (promise: Promise<unknown>) => void;
271
269
  runInBackgroundOrAwait: (promise: Promise<unknown> | void) => _better_auth_core0.Awaitable<unknown>;
272
- }>) => Promise<R>): Middleware<(inputContext: Record<string, any>) => Promise<R>>;
270
+ }>) => Promise<R_1>): (inputContext: better_call0.MiddlewareInputContext<Options>) => Promise<R_1>;
273
271
  };
274
- type BodyOption<M, B extends object | undefined = undefined> = M extends "GET" | "HEAD" | ("GET" | "HEAD")[] ? {
275
- body?: never;
276
- } : {
277
- body?: B;
278
- };
279
- type AuthEndpointOptions<Method extends HTTPMethod | HTTPMethod[] | "*", BodySchema extends object | undefined, QuerySchema extends object | undefined, Use extends Middleware[], ReqHeaders extends boolean, ReqRequest extends boolean, Meta extends EndpointMetadata | undefined, ErrorSchema extends StandardSchemaV1 | undefined = undefined> = {
280
- method: Method;
281
- } & BodyOption<Method, BodySchema> & {
282
- query?: QuerySchema;
283
- use?: [...Use];
284
- requireHeaders?: ReqHeaders;
285
- requireRequest?: ReqRequest;
286
- error?: ErrorSchema;
287
- cloneRequest?: boolean;
288
- disableBody?: boolean;
289
- metadata?: Meta;
290
- [key: string]: any;
291
- };
292
- /**
293
- * Normalize readonly tuples produced by `const` type parameters
294
- * into mutable arrays so downstream `M extends Array<any>` checks work.
295
- */
296
- type NormalizeMethod<M> = M extends readonly (infer E)[] ? E[] : M;
297
- declare function createAuthEndpoint<Path extends string, const Method extends HTTPMethod | HTTPMethod[] | "*", BodySchema extends object | undefined = undefined, QuerySchema extends object | undefined = undefined, Use extends Middleware[] = [], ReqHeaders extends boolean = false, ReqRequest extends boolean = false, R = unknown, Meta extends EndpointMetadata | undefined = undefined, ErrorSchema extends StandardSchemaV1 | undefined = undefined>(path: Path, options: AuthEndpointOptions<Method, BodySchema, QuerySchema, Use, ReqHeaders, ReqRequest, Meta, ErrorSchema>, handler: (ctx: EndpointContext<Path, Method, BodySchema, QuerySchema, Use, ReqHeaders, ReqRequest, AuthContext, Meta>) => Promise<R>): Endpoint<Path, NormalizeMethod<Method>, ResolveBodyInput<BodySchema, Meta>, ResolveQueryInput<QuerySchema, Meta>, Use, R, ResolveMetaInput<Meta>, ResolveErrorInput<ErrorSchema, Meta>>;
298
- declare function createAuthEndpoint<const Method extends HTTPMethod | HTTPMethod[] | "*", BodySchema extends object | undefined = undefined, QuerySchema extends object | undefined = undefined, Use extends Middleware[] = [], ReqHeaders extends boolean = false, ReqRequest extends boolean = false, R = unknown, Meta extends EndpointMetadata | undefined = undefined, ErrorSchema extends StandardSchemaV1 | undefined = undefined>(options: AuthEndpointOptions<Method, BodySchema, QuerySchema, Use, ReqHeaders, ReqRequest, Meta, ErrorSchema>, handler: (ctx: EndpointContext<string, Method, BodySchema, QuerySchema, Use, ReqHeaders, ReqRequest, AuthContext, Meta>) => Promise<R>): Endpoint<string, NormalizeMethod<Method>, ResolveBodyInput<BodySchema, Meta>, ResolveQueryInput<QuerySchema, Meta>, Use, R, ResolveMetaInput<Meta>, ResolveErrorInput<ErrorSchema, Meta>>;
299
- type AuthEndpoint = ReturnType<typeof createAuthEndpoint>;
300
- /**
301
- * The handler type for plugin hooks.
302
- *
303
- * Accepts both `Middleware` instances (from `createAuthMiddleware`)
304
- * and plain async functions for better-call v1/v2 compatibility.
305
- */
306
- type AuthMiddleware = (inputContext: Record<string, any>) => Promise<unknown>;
272
+ type EndpointHandler<Path extends string, Options extends EndpointOptions, R> = (context: EndpointContext<Path, Options, AuthContext>) => Promise<R>;
273
+ declare function createAuthEndpoint<Path extends string, Options extends EndpointOptions, R>(path: Path, options: Options, handler: EndpointHandler<Path, Options, R>): StrictEndpoint<Path, Options, R>;
274
+ declare function createAuthEndpoint<Path extends string, Options extends EndpointOptions, R>(options: Options, handler: EndpointHandler<Path, Options, R>): StrictEndpoint<Path, Options, R>;
275
+ type AuthEndpoint<Path extends string, Opts extends EndpointOptions, R> = ReturnType<typeof createAuthEndpoint<Path, Opts, R>>;
276
+ type AuthMiddleware = ReturnType<typeof createAuthMiddleware>;
307
277
  //#endregion
308
278
  export { AuthEndpoint, AuthMiddleware, createAuthEndpoint, createAuthMiddleware, optionsMiddleware };
@@ -3,7 +3,7 @@ import { EndpointContext, InputContext } from "better-call";
3
3
  import { AsyncLocalStorage } from "@better-auth/core/async_hooks";
4
4
 
5
5
  //#region src/context/endpoint-context.d.ts
6
- type AuthEndpointContext = Partial<InputContext<string, any, any, any, any, any> & EndpointContext<string, any, any, any, any, any, any, AuthContext>> & {
6
+ type AuthEndpointContext = Partial<InputContext<string, any> & EndpointContext<string, any>> & {
7
7
  context: AuthContext;
8
8
  };
9
9
  /**
@@ -2,7 +2,7 @@
2
2
  const symbol = Symbol.for("better-auth:global");
3
3
  let bind = null;
4
4
  const __context = {};
5
- const __betterAuthVersion = "1.6.0-beta.0";
5
+ const __betterAuthVersion = "1.6.1";
6
6
  /**
7
7
  * We store context instance in the globalThis.
8
8
  *
@@ -1,7 +1,7 @@
1
1
  import { ATTR_HTTP_RESPONSE_STATUS_CODE } from "./attributes.mjs";
2
2
  import { SpanStatusCode, trace } from "@opentelemetry/api";
3
3
  //#region src/instrumentation/tracer.ts
4
- const tracer = trace.getTracer("better-auth", "1.6.0-beta.0");
4
+ const tracer = trace.getTracer("better-auth", "1.6.1");
5
5
  /**
6
6
  * Better-auth uses `throw ctx.redirect(url)` for flow control (e.g. OAuth
7
7
  * callbacks). These are APIErrors with 3xx status codes and should not be
@@ -50,7 +50,9 @@ type InferPluginOptions<O extends BetterAuthOptions, ID extends BetterAuthPlugin
50
50
  */
51
51
  interface BetterAuthPluginRegistry<AuthOptions, Options> {}
52
52
  type BetterAuthPluginRegistryIdentifier = keyof BetterAuthPluginRegistry<unknown, unknown>;
53
- type GenericEndpointContext<Options extends BetterAuthOptions = BetterAuthOptions> = EndpointContext<string, any, any, any, any, any, any, AuthContext<Options>>;
53
+ type GenericEndpointContext<Options extends BetterAuthOptions = BetterAuthOptions> = EndpointContext<string, any> & {
54
+ context: AuthContext<Options>;
55
+ };
54
56
  interface InternalAdapter<_Options extends BetterAuthOptions = BetterAuthOptions> {
55
57
  createOAuthUser(user: Omit<User, "id" | "createdAt" | "updatedAt">, account: Omit<Account, "userId" | "id" | "createdAt" | "updatedAt"> & Partial<Account>): Promise<{
56
58
  user: User;
@@ -9,10 +9,7 @@ import { Migration } from "kysely";
9
9
 
10
10
  //#region src/types/plugin.d.ts
11
11
  type DeepPartial<T> = T extends Function ? T : T extends object ? { [K in keyof T]?: DeepPartial<T[K]> } : T;
12
- type HookEndpointContext = Partial<EndpointContext<string, any, any, any, any, any, any, AuthContext & {
13
- returned?: unknown | undefined;
14
- responseHeaders?: Headers | undefined;
15
- }> & Omit<InputContext<string, any, any, any, any, any>, "method">> & {
12
+ type HookEndpointContext = Partial<EndpointContext<string, any> & Omit<InputContext<string, any>, "method">> & {
16
13
  path?: string;
17
14
  context: AuthContext & {
18
15
  returned?: unknown | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/core",
3
- "version": "1.6.0-beta.0",
3
+ "version": "1.6.1",
4
4
  "description": "The most comprehensive authentication framework for TypeScript.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -150,7 +150,7 @@
150
150
  "@opentelemetry/api": "^1.9.0",
151
151
  "@opentelemetry/sdk-trace-base": "^1.30.0",
152
152
  "@opentelemetry/sdk-trace-node": "^1.30.0",
153
- "better-call": "2.0.3",
153
+ "better-call": "1.3.5",
154
154
  "@cloudflare/workers-types": "^4.20250121.0",
155
155
  "jose": "^6.1.3",
156
156
  "kysely": "^0.28.14",
@@ -161,7 +161,7 @@
161
161
  "@better-auth/utils": "0.4.0",
162
162
  "@better-fetch/fetch": "1.1.21",
163
163
  "@opentelemetry/api": "^1.9.0",
164
- "better-call": "2.0.3",
164
+ "better-call": "1.3.5",
165
165
  "@cloudflare/workers-types": ">=4",
166
166
  "jose": "^6.1.0",
167
167
  "kysely": "^0.28.5",
package/src/api/index.ts CHANGED
@@ -1,15 +1,7 @@
1
1
  import type {
2
- Endpoint,
3
2
  EndpointContext,
4
- EndpointMetadata,
5
- EndpointRuntimeOptions,
6
- HTTPMethod,
7
- Middleware,
8
- ResolveBodyInput,
9
- ResolveErrorInput,
10
- ResolveMetaInput,
11
- ResolveQueryInput,
12
- StandardSchemaV1,
3
+ EndpointOptions,
4
+ StrictEndpoint,
13
5
  } from "better-call";
14
6
  import { createEndpoint, createMiddleware } from "better-call";
15
7
  import { runWithEndpointContext } from "../context";
@@ -41,145 +33,47 @@ export const createAuthMiddleware = createMiddleware.create({
41
33
 
42
34
  const use = [optionsMiddleware];
43
35
 
44
- type BodyOption<M, B extends object | undefined = undefined> = M extends
45
- | "GET"
46
- | "HEAD"
47
- | ("GET" | "HEAD")[]
48
- ? { body?: never }
49
- : { body?: B };
50
-
51
- type AuthEndpointOptions<
52
- Method extends HTTPMethod | HTTPMethod[] | "*",
53
- BodySchema extends object | undefined,
54
- QuerySchema extends object | undefined,
55
- Use extends Middleware[],
56
- ReqHeaders extends boolean,
57
- ReqRequest extends boolean,
58
- Meta extends EndpointMetadata | undefined,
59
- ErrorSchema extends StandardSchemaV1 | undefined = undefined,
60
- > = { method: Method } & BodyOption<Method, BodySchema> & {
61
- query?: QuerySchema;
62
- use?: [...Use];
63
- requireHeaders?: ReqHeaders;
64
- requireRequest?: ReqRequest;
65
- error?: ErrorSchema;
66
- cloneRequest?: boolean;
67
- disableBody?: boolean;
68
- metadata?: Meta;
69
- [key: string]: any;
70
- };
71
-
72
- /**
73
- * Normalize readonly tuples produced by `const` type parameters
74
- * into mutable arrays so downstream `M extends Array<any>` checks work.
75
- */
76
- type NormalizeMethod<M> = M extends readonly (infer E)[] ? E[] : M;
36
+ type EndpointHandler<
37
+ Path extends string,
38
+ Options extends EndpointOptions,
39
+ R,
40
+ > = (context: EndpointContext<Path, Options, AuthContext>) => Promise<R>;
77
41
 
78
- // Path + options + handler overload
79
42
  export function createAuthEndpoint<
80
43
  Path extends string,
81
- const Method extends HTTPMethod | HTTPMethod[] | "*",
82
- BodySchema extends object | undefined = undefined,
83
- QuerySchema extends object | undefined = undefined,
84
- Use extends Middleware[] = [],
85
- ReqHeaders extends boolean = false,
86
- ReqRequest extends boolean = false,
87
- R = unknown,
88
- Meta extends EndpointMetadata | undefined = undefined,
89
- ErrorSchema extends StandardSchemaV1 | undefined = undefined,
44
+ Options extends EndpointOptions,
45
+ R,
90
46
  >(
91
47
  path: Path,
92
- options: AuthEndpointOptions<
93
- Method,
94
- BodySchema,
95
- QuerySchema,
96
- Use,
97
- ReqHeaders,
98
- ReqRequest,
99
- Meta,
100
- ErrorSchema
101
- >,
102
- handler: (
103
- ctx: EndpointContext<
104
- Path,
105
- Method,
106
- BodySchema,
107
- QuerySchema,
108
- Use,
109
- ReqHeaders,
110
- ReqRequest,
111
- AuthContext,
112
- Meta
113
- >,
114
- ) => Promise<R>,
115
- ): Endpoint<
116
- Path,
117
- NormalizeMethod<Method>,
118
- ResolveBodyInput<BodySchema, Meta>,
119
- ResolveQueryInput<QuerySchema, Meta>,
120
- Use,
121
- R,
122
- ResolveMetaInput<Meta>,
123
- ResolveErrorInput<ErrorSchema, Meta>
124
- >;
48
+ options: Options,
49
+ handler: EndpointHandler<Path, Options, R>,
50
+ ): StrictEndpoint<Path, Options, R>;
125
51
 
126
- // Options-only (virtual/path-less) overload
127
52
  export function createAuthEndpoint<
128
- const Method extends HTTPMethod | HTTPMethod[] | "*",
129
- BodySchema extends object | undefined = undefined,
130
- QuerySchema extends object | undefined = undefined,
131
- Use extends Middleware[] = [],
132
- ReqHeaders extends boolean = false,
133
- ReqRequest extends boolean = false,
134
- R = unknown,
135
- Meta extends EndpointMetadata | undefined = undefined,
136
- ErrorSchema extends StandardSchemaV1 | undefined = undefined,
137
- >(
138
- options: AuthEndpointOptions<
139
- Method,
140
- BodySchema,
141
- QuerySchema,
142
- Use,
143
- ReqHeaders,
144
- ReqRequest,
145
- Meta,
146
- ErrorSchema
147
- >,
148
- handler: (
149
- ctx: EndpointContext<
150
- string,
151
- Method,
152
- BodySchema,
153
- QuerySchema,
154
- Use,
155
- ReqHeaders,
156
- ReqRequest,
157
- AuthContext,
158
- Meta
159
- >,
160
- ) => Promise<R>,
161
- ): Endpoint<
162
- string,
163
- NormalizeMethod<Method>,
164
- ResolveBodyInput<BodySchema, Meta>,
165
- ResolveQueryInput<QuerySchema, Meta>,
166
- Use,
53
+ Path extends string,
54
+ Options extends EndpointOptions,
167
55
  R,
168
- ResolveMetaInput<Meta>,
169
- ResolveErrorInput<ErrorSchema, Meta>
170
- >;
56
+ >(
57
+ options: Options,
58
+ handler: EndpointHandler<Path, Options, R>,
59
+ ): StrictEndpoint<Path, Options, R>;
171
60
 
172
- // Implementation
173
- export function createAuthEndpoint(
174
- pathOrOptions: any,
175
- handlerOrOptions: any,
61
+ export function createAuthEndpoint<
62
+ Path extends string,
63
+ Opts extends EndpointOptions,
64
+ R,
65
+ >(
66
+ pathOrOptions: Path | Opts,
67
+ handlerOrOptions: EndpointHandler<Path, Opts, R> | Opts,
176
68
  handlerOrNever?: any,
177
69
  ) {
178
- const path: string | undefined =
70
+ const path: Path | undefined =
179
71
  typeof pathOrOptions === "string" ? pathOrOptions : undefined;
180
- const options: EndpointRuntimeOptions =
181
- typeof handlerOrOptions === "object" ? handlerOrOptions : pathOrOptions;
182
- const handler =
72
+ const options: Opts =
73
+ typeof handlerOrOptions === "object"
74
+ ? handlerOrOptions
75
+ : (pathOrOptions as Opts);
76
+ const handler: EndpointHandler<Path, Opts, R> =
183
77
  typeof handlerOrOptions === "function" ? handlerOrOptions : handlerOrNever;
184
78
 
185
79
  if (path) {
@@ -188,9 +82,9 @@ export function createAuthEndpoint(
188
82
  {
189
83
  ...options,
190
84
  use: [...(options?.use || []), ...use],
191
- } as any,
85
+ },
192
86
  // todo: prettify the code, we want to call `runWithEndpointContext` to top level
193
- async (ctx: any) => runWithEndpointContext(ctx, () => handler(ctx)),
87
+ async (ctx) => runWithEndpointContext(ctx as any, () => handler(ctx)),
194
88
  );
195
89
  }
196
90
 
@@ -198,19 +92,15 @@ export function createAuthEndpoint(
198
92
  {
199
93
  ...options,
200
94
  use: [...(options?.use || []), ...use],
201
- } as any,
95
+ },
202
96
  // todo: prettify the code, we want to call `runWithEndpointContext` to top level
203
- async (ctx: any) => runWithEndpointContext(ctx, () => handler(ctx)),
97
+ async (ctx) => runWithEndpointContext(ctx as any, () => handler(ctx)),
204
98
  );
205
99
  }
206
100
 
207
- export type AuthEndpoint = ReturnType<typeof createAuthEndpoint>;
208
- /**
209
- * The handler type for plugin hooks.
210
- *
211
- * Accepts both `Middleware` instances (from `createAuthMiddleware`)
212
- * and plain async functions for better-call v1/v2 compatibility.
213
- */
214
- export type AuthMiddleware = (
215
- inputContext: Record<string, any>,
216
- ) => Promise<unknown>;
101
+ export type AuthEndpoint<
102
+ Path extends string,
103
+ Opts extends EndpointOptions,
104
+ R,
105
+ > = ReturnType<typeof createAuthEndpoint<Path, Opts, R>>;
106
+ export type AuthMiddleware = ReturnType<typeof createAuthMiddleware>;
@@ -5,8 +5,7 @@ import type { AuthContext } from "../types";
5
5
  import { __getBetterAuthGlobal } from "./global";
6
6
 
7
7
  export type AuthEndpointContext = Partial<
8
- InputContext<string, any, any, any, any, any> &
9
- EndpointContext<string, any, any, any, any, any, any, AuthContext>
8
+ InputContext<string, any> & EndpointContext<string, any>
10
9
  > & {
11
10
  context: AuthContext;
12
11
  };
@@ -80,7 +80,9 @@ export type BetterAuthPluginRegistryIdentifier = keyof BetterAuthPluginRegistry<
80
80
 
81
81
  export type GenericEndpointContext<
82
82
  Options extends BetterAuthOptions = BetterAuthOptions,
83
- > = EndpointContext<string, any, any, any, any, any, any, AuthContext<Options>>;
83
+ > = EndpointContext<string, any> & {
84
+ context: AuthContext<Options>;
85
+ };
84
86
 
85
87
  export interface InternalAdapter<
86
88
  _Options extends BetterAuthOptions = BetterAuthOptions,
@@ -19,20 +19,7 @@ type DeepPartial<T> = T extends Function
19
19
  : T;
20
20
 
21
21
  export type HookEndpointContext = Partial<
22
- EndpointContext<
23
- string,
24
- any,
25
- any,
26
- any,
27
- any,
28
- any,
29
- any,
30
- AuthContext & {
31
- returned?: unknown | undefined;
32
- responseHeaders?: Headers | undefined;
33
- }
34
- > &
35
- Omit<InputContext<string, any, any, any, any, any>, "method">
22
+ EndpointContext<string, any> & Omit<InputContext<string, any>, "method">
36
23
  > & {
37
24
  path?: string;
38
25
  context: AuthContext & {