chore(core): improve runInBackground API type (#7533)

This commit is contained in:
Alex Yang
2026-01-28 17:06:03 -08:00
parent 1cfcc15ea2
commit 3100826d9b
4 changed files with 14 additions and 15 deletions
@@ -2917,11 +2917,11 @@ describe("api-key", async () => {
describe("deferUpdates option", () => {
it("should defer updates when deferUpdates is enabled with global backgroundTasks", async () => {
const deferredPromises: Array<Promise<void>> = [];
const deferredPromises: Array<Promise<unknown>> = [];
const { auth, signInWithTestUser } = await getTestInstance({
advanced: {
backgroundTasks: {
handler: (p: Promise<void>) => {
handler: (p: Promise<unknown>) => {
deferredPromises.push(p);
},
},
@@ -2957,11 +2957,11 @@ describe("api-key", async () => {
});
it("should still validate rate limits correctly with deferred updates", async () => {
const deferredPromises: Array<Promise<void>> = [];
const deferredPromises: Array<Promise<unknown>> = [];
const { auth, signInWithTestUser } = await getTestInstance({
advanced: {
backgroundTasks: {
handler: (p: Promise<void>) => {
handler: (p: Promise<unknown>) => {
deferredPromises.push(p);
},
},
@@ -3003,11 +3003,11 @@ describe("api-key", async () => {
});
it("should defer remaining count updates", async () => {
const deferredPromises: Array<Promise<void>> = [];
const deferredPromises: Array<Promise<unknown>> = [];
const { auth, signInWithTestUser } = await getTestInstance({
advanced: {
backgroundTasks: {
handler: (p: Promise<void>) => {
handler: (p: Promise<unknown>) => {
deferredPromises.push(p);
},
},
@@ -218,11 +218,9 @@ export async function validateApiKey({
if (opts.deferUpdates) {
ctx.context.runInBackground(
performUpdate()
.then(() => {})
.catch((error) => {
ctx.context.logger.error("Failed to update API key:", error);
}),
performUpdate().catch((error) => {
ctx.context.logger.error("Failed to update API key:", error);
}),
);
newApiKey = updated;
} else {
+4 -3
View File
@@ -12,6 +12,7 @@ import type { DBAdapter, Where } from "../db/adapter";
import type { createLogger } from "../env";
import type { OAuthProvider } from "../oauth2";
import type { BetterAuthCookie, BetterAuthCookies } from "./cookie";
import type { Awaitable } from "./helper";
import type {
BetterAuthOptions,
BetterAuthRateLimitOptions,
@@ -299,7 +300,7 @@ export type AuthContext<Options extends BetterAuthOptions = BetterAuthOptions> =
* This is inferred from the `options.advanced?.backgroundTasks?.handler` option.
* Defaults to a no-op that just runs the promise.
*/
runInBackground: (promise: Promise<void>) => void;
runInBackground: (promise: Promise<unknown>) => void;
/**
* Runs a task in the background if `runInBackground` is configured,
* otherwise awaits the task directly.
@@ -309,6 +310,6 @@ export type AuthContext<Options extends BetterAuthOptions = BetterAuthOptions> =
* mitigation), but still ensure the operation completes.
*/
runInBackgroundOrAwait: (
promise: Promise<unknown> | Promise<void> | void | unknown,
) => Promise<unknown>;
promise: Promise<unknown> | void,
) => Awaitable<unknown>;
};
+1 -1
View File
@@ -328,7 +328,7 @@ export type BetterAuthAdvancedOptions = {
* }
*/
backgroundTasks?: {
handler: (promise: Promise<void>) => void;
handler: (promise: Promise<unknown>) => void;
};
/**
* Skip trailing slash validation in route matching