Files
aris/apps/aelis-backend/src/tfl/provider.ts
kenneth 6b8b3bbdbe feat(session): query enabled sources before providers
UserSessionManager now queries the user_sources table for enabled
sources before calling any provider. Providers receive the per-user
JSON config directly instead of querying the DB themselves, removing
their db dependency and eliminating redundant round-trips.

Co-authored-by: Ona <no-reply@ona.com>
2026-03-22 16:07:41 +00:00

37 lines
1.1 KiB
TypeScript

import { TflSource, type ITflApi, type TflLineId } from "@aelis/source-tfl"
import { type } from "arktype"
import type { FeedSourceProvider } from "../session/feed-source-provider.ts"
export type TflSourceProviderOptions =
| { apiKey: string; client?: never }
| { apiKey?: never; client: ITflApi }
const tflConfig = type({
"lines?": "string[]",
})
export class TflSourceProvider implements FeedSourceProvider {
readonly sourceId = "aelis.tfl"
private readonly apiKey: string | undefined
private readonly client: ITflApi | undefined
constructor(options: TflSourceProviderOptions) {
this.apiKey = "apiKey" in options ? options.apiKey : undefined
this.client = "client" in options ? options.client : undefined
}
async feedSourceForUser(_userId: string, config: unknown): Promise<TflSource> {
const parsed = tflConfig(config)
if (parsed instanceof type.errors) {
throw new Error(`Invalid TFL config: ${parsed.summary}`)
}
return new TflSource({
apiKey: this.apiKey,
client: this.client,
lines: parsed.lines as TflLineId[] | undefined,
})
}
}