/** * Everything a request handler or job needs, passed in rather than imported, so tests can run the * whole app against an in-memory database and a stand-in Accounts (tests/support/). */ import type { PrismaClient } from './generated/prisma/client.js'; import type { Config } from './lib/config.js'; import { Secrets } from './lib/crypto.js'; import { Settings } from './lib/settings.js'; import { loadAuthClient, type AuthClientModule } from './auth/accounts.js'; export type Db = PrismaClient; export type Ctx = { config: Config; db: Db; secrets: Secrets; settings: Settings; /** `@crosscode/auth-client` (tests give a stand-in). */ authClient: () => Promise; now: () => Date; /** Fetches what visitors are shown from outside (YouTube's thumbnails); tests give a stand-in. */ fetch?: (url: string) => Promise; }; export function createCtx(config: Config, db: Db, opts: { now?: () => Date; authClient?: () => Promise; fetch?: (url: string) => Promise } = {}): Ctx { const secrets = new Secrets(config.secret); const partial = { config, db, secrets } as Ctx; partial.settings = new Settings(partial); partial.authClient = opts.authClient ?? loadAuthClient; partial.now = opts.now ?? (() => new Date()); if (opts.fetch) partial.fetch = opts.fetch; return partial; }