chore: use Awaitable type (#6790)

This commit is contained in:
Alex Yang
2025-12-16 16:46:28 +08:00
committed by github-actions[bot]
parent 1c67165050
commit 5beabbdf97
28 changed files with 84 additions and 89 deletions

View File

@@ -1,4 +1,4 @@
import type { BetterAuthOptions } from "@better-auth/core";
import type { Awaitable, BetterAuthOptions } from "@better-auth/core";
import type {
AdapterFactoryCustomizeAdapterCreator,
AdapterFactoryOptions,
@@ -50,7 +50,7 @@ interface PrismaClient {}
type PrismaClientInternal = {
$transaction: (
callback: (db: PrismaClient) => Promise<any> | any,
callback: (db: PrismaClient) => Awaitable<any>,
) => Promise<any>;
} & {
[model: string]: {

View File

@@ -1,4 +1,4 @@
import type { BetterAuthOptions } from "@better-auth/core";
import type { Awaitable, BetterAuthOptions } from "@better-auth/core";
import type { DBAdapter } from "@better-auth/core/db/adapter";
import { deepmerge, initGetModelName } from "@better-auth/core/db/adapter";
import { TTY_COLORS } from "@better-auth/core/env";
@@ -38,9 +38,7 @@ export const testAdapter = async ({
*/
adapter: (
options: BetterAuthOptions,
) =>
| Promise<(options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>>
| ((options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>);
) => Awaitable<(options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>>;
/**
* A function that will run the database migrations.
*/

View File

@@ -1,4 +1,4 @@
import type { BetterAuthOptions } from "@better-auth/core";
import type { Awaitable, BetterAuthOptions } from "@better-auth/core";
import type { DBAdapter } from "@better-auth/core/db/adapter";
import { beforeAll, describe, expect, test } from "vitest";
import type { User } from "../types";
@@ -7,7 +7,7 @@ import { generateId } from "../utils";
interface AdapterTestOptions {
getAdapter: (
customOptions?: Omit<BetterAuthOptions, "database">,
) => Promise<DBAdapter<BetterAuthOptions>> | DBAdapter<BetterAuthOptions>;
) => Awaitable<DBAdapter<BetterAuthOptions>>;
disableTests?: Partial<Record<keyof typeof adapterTests, boolean>>;
testPrefix?: string;
}

View File

@@ -1,5 +1,6 @@
import type {
AuthContext,
Awaitable,
BetterAuthOptions,
BetterAuthPlugin,
} from "@better-auth/core";
@@ -159,7 +160,7 @@ To resolve this, you can:
}
export function getEndpoints<Option extends BetterAuthOptions>(
ctx: Promise<AuthContext> | AuthContext,
ctx: Awaitable<AuthContext>,
options: Option,
) {
const pluginEndpoints =

View File

@@ -33,7 +33,7 @@ export function InferAuth<O extends { options: BetterAuthOptions }>() {
//#region Necessary re-exports
export type * from "@better-auth/core/db";
export type { Primitive } from "@better-auth/core/db";
export type { DBPrimitive } from "@better-auth/core/db";
export type * from "@better-fetch/fetch";
// @ts-expect-error
export type * from "nanostores";

View File

@@ -1,4 +1,4 @@
import type { LiteralString } from "../../types/helper";
import type { LiteralString } from "@better-auth/core";
import type { AuthorizeResponse, createAccessControl } from "./access";
export type SubArray<T extends unknown[] | readonly unknown[] | any[]> =

View File

@@ -1,4 +1,8 @@
import type { AuthContext, GenericEndpointContext } from "@better-auth/core";
import type {
AuthContext,
Awaitable,
GenericEndpointContext,
} from "@better-auth/core";
import type { EndpointContext } from "better-call";
import type { InferOptionSchema, Session, User } from "../../types";
import type { schema } from "./schema";
@@ -29,7 +33,7 @@ export interface AnonymousOptions {
session: Session & Record<string, any>;
};
ctx: GenericEndpointContext;
}) => Promise<void> | void)
}) => Awaitable<void>)
| undefined;
/**
* Disable deleting the anonymous user after linking
@@ -49,7 +53,7 @@ export interface AnonymousOptions {
},
AuthContext
>,
) => Promise<string> | string)
) => Awaitable<string>)
| undefined;
/**
* A custom random email generation function.
@@ -57,7 +61,7 @@ export interface AnonymousOptions {
* You are responsible for ensuring the email is unique to avoid conflicts.
* @returns The email address for the anonymous user.
*/
generateRandomEmail?: (() => Promise<string> | string) | undefined;
generateRandomEmail?: (() => Awaitable<string>) | undefined;
/**
* Custom schema for the anonymous plugin
*/

View File

@@ -1,4 +1,4 @@
import type { AuthContext } from "@better-auth/core";
import type { AuthContext, Awaitable } from "@better-auth/core";
import { createAuthEndpoint } from "@better-auth/core/api";
import { safeJSONParse } from "@better-auth/core/utils";
import * as z from "zod";
@@ -102,7 +102,7 @@ export function createApiKey({
keyGenerator: (options: {
length: number;
prefix: string | undefined;
}) => Promise<string> | string;
}) => Awaitable<string>;
opts: PredefinedApiKeyOptions;
schema: ReturnType<typeof apiKeySchema>;
deleteAllExpiredApiKeys(

View File

@@ -1,4 +1,4 @@
import type { AuthContext } from "@better-auth/core";
import type { AuthContext, Awaitable } from "@better-auth/core";
import { API_KEY_TABLE_NAME } from "..";
import type { apiKeySchema } from "../schema";
import type { ApiKey, ApiKeyOptions } from "../types";
@@ -80,7 +80,7 @@ export function createApiKeyRoutes({
keyGenerator: (options: {
length: number;
prefix: string | undefined;
}) => Promise<string> | string;
}) => Awaitable<string>;
opts: PredefinedApiKeyOptions;
schema: ReturnType<typeof apiKeySchema>;
}) {

View File

@@ -1,7 +1,9 @@
import type {
Awaitable,
GenericEndpointContext,
HookEndpointContext,
} from "@better-auth/core";
import type { InferOptionSchema } from "../../types";
import type { Statements } from "../access";
import type { apiKeySchema } from "./schema";
@@ -33,7 +35,7 @@ export interface ApiKeyOptions {
| ((options: {
ctx: GenericEndpointContext;
key: string;
}) => boolean | Promise<boolean>)
}) => Awaitable<boolean>)
| undefined;
/**
* custom key generation function
@@ -47,7 +49,7 @@ export interface ApiKeyOptions {
* The prefix of the API key to generate
*/
prefix: string | undefined;
}) => string | Promise<string>;
}) => Awaitable<string>;
/**
* The configuration for storing the starting characters of the API key in the database.
*
@@ -206,7 +208,7 @@ export interface ApiKeyOptions {
| ((
userId: string,
ctx: GenericEndpointContext,
) => Statements | Promise<Statements>);
) => Awaitable<Statements>);
}
| undefined;
/**
@@ -240,7 +242,7 @@ export interface ApiKeyOptions {
/**
* Get a value from storage
*/
get: (key: string) => Promise<unknown> | unknown;
get: (key: string) => Awaitable<unknown>;
/**
* Set a value in storage
*/
@@ -248,11 +250,11 @@ export interface ApiKeyOptions {
key: string,
value: string,
ttl?: number | undefined,
) => Promise<void | null | unknown> | void;
) => Awaitable<void | null | unknown>;
/**
* Delete a value from storage
*/
delete: (key: string) => Promise<void | null | string> | void;
delete: (key: string) => Awaitable<void | null | string>;
}
| undefined;
}

View File

@@ -1,7 +1,7 @@
import type { Awaitable } from "@better-auth/core";
import type { JWTPayload } from "jose";
import type { GenericEndpointContext } from "../..";
import type { InferOptionSchema, Session, User } from "../../types";
import type { Awaitable } from "../../types/helper";
import type { schema } from "./schema";
export interface JwtOptions {
@@ -94,7 +94,7 @@ export interface JwtOptions {
definePayload?: (session: {
user: User & Record<string, any>;
session: Session & Record<string, any>;
}) => Promise<Record<string, any>> | Record<string, any> | undefined;
}) => Awaitable<Record<string, any>> | undefined;
/**
* A function that is called to get the subject of the JWT
*
@@ -103,7 +103,7 @@ export interface JwtOptions {
getSubject?: (session: {
user: User & Record<string, any>;
session: Session & Record<string, any>;
}) => Promise<string> | string | undefined;
}) => Awaitable<string> | undefined;
/**
* A custom function to remote sign the jwt payload.
*

View File

@@ -1,4 +1,5 @@
import type {
Awaitable,
BetterAuthPlugin,
GenericEndpointContext,
} from "@better-auth/core";
@@ -25,7 +26,7 @@ export interface MagicLinkOptions {
token: string;
},
ctx?: GenericEndpointContext | undefined,
) => Promise<void> | void;
) => Awaitable<void>;
/**
* Disable sign up if user is not found.
*
@@ -49,7 +50,7 @@ export interface MagicLinkOptions {
/**
* Custom function to generate a token
*/
generateToken?: ((email: string) => Promise<string> | string) | undefined;
generateToken?: ((email: string) => Awaitable<string>) | undefined;
/**
* This option allows you to configure how the token is stored in your database.

View File

@@ -1,7 +1,6 @@
import type { BetterAuthPlugin } from "@better-auth/core";
import type { BetterAuthPlugin, LiteralString } from "@better-auth/core";
import { createAuthEndpoint } from "@better-auth/core/api";
import { APIError } from "../../api";
import type { LiteralString } from "../../types/helper";
import { HIDE_METADATA } from "../../utils";
import { generator } from "./generator";
import { logo } from "./logo";

View File

@@ -1,3 +1,4 @@
import type { LiteralString } from "@better-auth/core";
import { createAuthEndpoint } from "@better-auth/core/api";
import { BASE_ERROR_CODES } from "@better-auth/core/error";
import { APIError } from "better-call";
@@ -5,7 +6,6 @@ import * as z from "zod";
import { getSessionFromCtx, sessionMiddleware } from "../../../api";
import type { InferAdditionalFieldsFromPluginOptions } from "../../../db";
import { toZodSchema } from "../../../db/to-zod";
import type { LiteralString } from "../../../types/helper";
import { getOrgAdapter } from "../adapter";
import { orgMiddleware, orgSessionMiddleware } from "../call";
import { ORGANIZATION_ERROR_CODES } from "../error-codes";

View File

@@ -1,4 +1,8 @@
import type { AuthContext, GenericEndpointContext } from "@better-auth/core";
import type {
AuthContext,
Awaitable,
GenericEndpointContext,
} from "@better-auth/core";
import type { DBFieldAttribute } from "@better-auth/core/db";
import type { Session, User } from "../../types";
import type { AccessControl, Role } from "../access";
@@ -26,10 +30,7 @@ export interface OrganizationOptions {
* @default true
*/
allowUserToCreateOrganization?:
| (
| boolean
| ((user: User & Record<string, any>) => Promise<boolean> | boolean)
)
| (boolean | ((user: User & Record<string, any>) => Awaitable<boolean>))
| undefined;
/**
* The maximum number of organizations a user can create.
@@ -37,7 +38,7 @@ export interface OrganizationOptions {
* You can also pass a function that returns a boolean
*/
organizationLimit?:
| (number | ((user: User) => Promise<boolean> | boolean))
| (number | ((user: User) => Awaitable<boolean>))
| undefined;
/**
* The role that is assigned to the creator of the
@@ -83,7 +84,7 @@ export interface OrganizationOptions {
*/
maximumRolesPerOrganization?:
| number
| ((organizationId: string) => Promise<number> | number);
| ((organizationId: string) => Awaitable<number>);
}
| undefined;
/**
@@ -133,7 +134,7 @@ export interface OrganizationOptions {
} | null;
},
ctx?: GenericEndpointContext,
) => number | Promise<number>)
) => Awaitable<number>)
| number;
/**
@@ -149,7 +150,7 @@ export interface OrganizationOptions {
teamId: string;
session: { user: User; session: Session };
organizationId: string;
}) => Promise<number> | number)
}) => Awaitable<number>)
| undefined;
/**
* By default, if an organization does only have one team, they'll not be able to remove it.
@@ -180,7 +181,7 @@ export interface OrganizationOptions {
member: Member & Record<string, any>;
},
ctx: AuthContext,
) => Promise<number> | number)
) => Awaitable<number>)
| undefined;
/**
* Cancel pending invitations on re-invite.

View File

@@ -1,6 +1,5 @@
import type { GenericEndpointContext } from "@better-auth/core";
import type { Awaitable, GenericEndpointContext } from "@better-auth/core";
import type { User } from "../../types";
import type { Awaitable } from "../../types/helper";
import type { InferOptionSchema } from "../../types/plugins";
import type { schema } from "./schema";

View File

@@ -1,4 +1,4 @@
import type { GenericEndpointContext } from "@better-auth/core";
import type { Awaitable, GenericEndpointContext } from "@better-auth/core";
import { createAuthEndpoint } from "@better-auth/core/api";
import { BASE_ERROR_CODES } from "@better-auth/core/error";
import { APIError } from "better-call";
@@ -52,7 +52,7 @@ export interface OTPOptions {
* The request object
*/
ctx?: GenericEndpointContext,
) => Promise<void> | void)
) => Awaitable<void>)
| undefined;
/**
* The number of allowed attempts for the OTP

View File

@@ -1,6 +1,5 @@
import type { BetterAuthPlugin } from "@better-auth/core";
import type { BetterAuthPlugin, LiteralString } from "@better-auth/core";
import type { InferOptionSchema, User } from "../../types";
import type { LiteralString } from "../../types/helper";
import type { BackupCodeOptions } from "./backup-codes";
import type { OTPOptions } from "./otp";
import type { schema } from "./schema";

View File

@@ -1,5 +1,6 @@
import { AsyncLocalStorage } from "node:async_hooks";
import type {
Awaitable,
BetterAuthClientOptions,
BetterAuthOptions,
} from "@better-auth/core";
@@ -314,7 +315,7 @@ export async function getTestInstance<
runWithUser: async (
email: string,
password: string,
fn: (headers: Headers) => Promise<void> | void,
fn: (headers: Headers) => Awaitable<void>,
) => {
const { headers } = await signInWithUser(email, password);
return currentUserContextStorage.run({ headers }, async () => {

View File

@@ -1,15 +1,7 @@
export type Primitive =
| string
| number
| symbol
| bigint
| boolean
| null
| undefined;
export type LiteralString = "" | (string & Record<never, never>);
import type { Primitive } from "@better-auth/core";
export type LiteralNumber = 0 | (number & Record<never, never>);
export type Awaitable<T> = Promise<T> | T;
export type OmitId<T extends { id: unknown }> = Omit<T, "id">;
export type Prettify<T> = Omit<T, never>;

View File

@@ -4,7 +4,6 @@ import type {
DBFieldAttribute,
DBFieldAttributeConfig,
DBFieldType,
DBPrimitive,
} from "./type";
export type { BetterAuthPluginDBSchema } from "./plugin";
@@ -41,10 +40,6 @@ export type FieldAttributeConfig = DBFieldAttributeConfig;
* @deprecated Backport for 1.3.x, we will remove this in 1.4.x
*/
export type FieldType = DBFieldType;
/**
* @deprecated Backport for 1.3.x, we will remove this in 1.4.x
*/
export type Primitive = DBPrimitive;
/**
* @deprecated Backport for 1.3.x, we will remove this in 1.4.x
*/

View File

@@ -1,5 +1,5 @@
import type { StandardSchemaV1 } from "@standard-schema/spec";
import type { LiteralString } from "../types";
import type { Awaitable, LiteralString } from "../types";
export type BaseModelNames = "user" | "account" | "session" | "verification";
@@ -63,8 +63,8 @@ export type DBFieldAttributeConfig = {
*/
transform?:
| {
input?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
output?: (value: DBPrimitive) => DBPrimitive | Promise<DBPrimitive>;
input?: (value: DBPrimitive) => Awaitable<DBPrimitive>;
output?: (value: DBPrimitive) => Awaitable<DBPrimitive>;
}
| undefined;
/**
@@ -157,7 +157,7 @@ export interface SecondaryStorage {
* @param key - Key to get
* @returns - Value of the key
*/
get: (key: string) => Promise<unknown> | unknown;
get: (key: string) => Awaitable<unknown>;
set: (
/**
* Key to store
@@ -171,10 +171,10 @@ export interface SecondaryStorage {
* Time to live in seconds
*/
ttl?: number | undefined,
) => Promise<void | null | unknown> | void;
) => Awaitable<void | null | unknown>;
/**
*
* @param key - Key to delete
*/
delete: (key: string) => Promise<void | null | string> | void;
delete: (key: string) => Awaitable<void | null | string>;
}

View File

@@ -1,4 +1,4 @@
import type { LiteralString } from "../types";
import type { Awaitable, LiteralString } from "../types";
export interface OAuth2Tokens {
tokenType?: string | undefined;
@@ -35,7 +35,7 @@ export interface OAuthProvider<
redirectURI: string;
display?: string | undefined;
loginHint?: string | undefined;
}) => Promise<URL> | URL;
}) => Awaitable<URL>;
name: string;
validateAuthorizationCode: (data: {
code: string;

View File

@@ -1,5 +1,13 @@
type Primitive = string | number | symbol | bigint | boolean | null | undefined;
export type Primitive =
| string
| number
| symbol
| bigint
| boolean
| null
| undefined;
export type Awaitable<T> = T | Promise<T>;
export type LiteralString = "" | (string & Record<never, never>);
export type LiteralUnion<LiteralType, BaseType extends Primitive> =
| LiteralType

View File

@@ -23,7 +23,7 @@ import type { DBAdapterDebugLogOption, DBAdapterInstance } from "../db/adapter";
import type { Logger } from "../env";
import type { SocialProviderList, SocialProviders } from "../social-providers";
import type { AuthContext, GenericEndpointContext } from "./context";
import type { LiteralUnion } from "./helper";
import type { Awaitable, LiteralUnion } from "./helper";
import type { BetterAuthPlugin } from "./plugin";
type KyselyDatabaseType = "postgres" | "mysql" | "sqlite" | "mssql";
@@ -971,7 +971,7 @@ export type BetterAuthOptions = {
* List of trusted origins.
*/
trustedOrigins?:
| (string[] | ((request: Request) => string[] | Promise<string[]>))
| (string[] | ((request: Request) => Awaitable<string[]>))
| undefined;
/**
* Rate limiting configuration

View File

@@ -8,10 +8,9 @@ import type { Migration } from "kysely";
import type { AuthMiddleware } from "../api";
import type { BetterAuthPluginDBSchema } from "../db";
import type { AuthContext } from "./context";
import type { LiteralString } from "./helper";
import type { Awaitable, LiteralString } from "./helper";
import type { BetterAuthOptions } from "./init-options";
type Awaitable<T> = T | Promise<T>;
type DeepPartial<T> = T extends Function
? T
: T extends object
@@ -153,6 +152,6 @@ export type BetterAuthPlugin = {
* This will override the default database operations
*/
adapter?: {
[key: string]: (...args: any[]) => Promise<any> | any;
[key: string]: (...args: any[]) => Awaitable<any>;
};
};

View File

@@ -1,10 +1,10 @@
import type { BetterAuthClientPlugin } from "@better-auth/core";
import type { Awaitable, BetterAuthClientPlugin } from "@better-auth/core";
export interface LastLoginMethodClientConfig {
storage: {
setItem: (key: string, value: string) => any;
getItem: (key: string) => string | null;
deleteItemAsync: (key: string) => Promise<void>;
deleteItemAsync: (key: string) => Awaitable<void>;
};
/**
* Prefix for local storage keys (e.g., "my-app_last_login_method")
@@ -15,9 +15,7 @@ export interface LastLoginMethodClientConfig {
* Custom resolve method for retrieving the last login method
*/
customResolveMethod?:
| ((
url: string | URL,
) => Promise<string | undefined | null> | string | undefined | null)
| ((url: string | URL) => Awaitable<string | undefined | null>)
| undefined;
}

View File

@@ -1,4 +1,4 @@
import type { OAuth2Tokens, User } from "better-auth";
import type { Awaitable, OAuth2Tokens, User } from "better-auth";
import type { AuthnRequestStore } from "./authn-request-store";
import type { AlgorithmValidationOptions } from "./saml/algorithms";
@@ -122,7 +122,7 @@ export interface SSOOptions {
* The SSO provider
*/
provider: SSOProvider<SSOOptions>;
}) => Promise<void>)
}) => Awaitable<void>)
| undefined;
/**
* Organization provisioning options
@@ -223,9 +223,7 @@ export interface SSOOptions {
* ```
* @default 10
*/
providersLimit?:
| (number | ((user: User) => Promise<number> | number))
| undefined;
providersLimit?: (number | ((user: User) => Awaitable<number>)) | undefined;
/**
* Trust the email verified flag from the provider.
*