mirror of
https://github.com/better-auth/better-auth.git
synced 2026-10-20 20:26:55 -05:00
chore(core): improve runInBackground API type (#7533)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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>;
|
||||
};
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user