2026-03-10 19:19:23 +00:00
|
|
|
import { WeatherSource, type WeatherSourceOptions } from "@aelis/source-weatherkit"
|
2026-03-16 01:30:02 +00:00
|
|
|
import { type } from "arktype"
|
2026-02-18 00:41:20 +00:00
|
|
|
|
|
|
|
|
import type { FeedSourceProvider } from "../session/feed-source-provider.ts"
|
|
|
|
|
|
2026-03-16 01:30:02 +00:00
|
|
|
export interface WeatherSourceProviderOptions {
|
|
|
|
|
credentials: WeatherSourceOptions["credentials"]
|
|
|
|
|
client?: WeatherSourceOptions["client"]
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 17:57:54 +00:00
|
|
|
export const weatherConfig = type({
|
2026-03-22 22:45:17 +00:00
|
|
|
"+": "reject",
|
2026-03-16 01:30:02 +00:00
|
|
|
"units?": "'metric' | 'imperial'",
|
|
|
|
|
"hourlyLimit?": "number",
|
|
|
|
|
"dailyLimit?": "number",
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-18 00:41:20 +00:00
|
|
|
export class WeatherSourceProvider implements FeedSourceProvider {
|
2026-03-19 23:32:29 +00:00
|
|
|
readonly sourceId = "aelis.weather"
|
2026-03-22 17:57:54 +00:00
|
|
|
readonly configSchema = weatherConfig
|
2026-03-16 01:30:02 +00:00
|
|
|
private readonly credentials: WeatherSourceOptions["credentials"]
|
|
|
|
|
private readonly client: WeatherSourceOptions["client"]
|
2026-02-18 00:41:20 +00:00
|
|
|
|
2026-03-16 01:30:02 +00:00
|
|
|
constructor(options: WeatherSourceProviderOptions) {
|
|
|
|
|
this.credentials = options.credentials
|
|
|
|
|
this.client = options.client
|
2026-02-18 00:41:20 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-22 16:28:19 +00:00
|
|
|
async feedSourceForUser(_userId: string, config: unknown): Promise<WeatherSource> {
|
|
|
|
|
const parsed = weatherConfig(config)
|
2026-03-16 01:30:02 +00:00
|
|
|
if (parsed instanceof type.errors) {
|
2026-03-22 16:28:19 +00:00
|
|
|
throw new Error(`Invalid weather config: ${parsed.summary}`)
|
2026-03-16 01:30:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new WeatherSource({
|
|
|
|
|
credentials: this.credentials,
|
|
|
|
|
client: this.client,
|
|
|
|
|
units: parsed.units,
|
|
|
|
|
hourlyLimit: parsed.hourlyLimit,
|
|
|
|
|
dailyLimit: parsed.dailyLimit,
|
|
|
|
|
})
|
2026-02-18 00:41:20 +00:00
|
|
|
}
|
|
|
|
|
}
|