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