newsnow/server/utils/source.ts
2024-10-21 17:00:51 +08:00

49 lines
1.5 KiB
TypeScript

import type { AllSourceID } from "@shared/types"
import defu from "defu"
import type { RSSHubOption, RSSHubInfo as RSSHubResponse, SourceGetter, SourceOption } from "#/types"
type X = SourceGetter | Partial<Record<AllSourceID, SourceGetter>>
export function defineSource<T extends X>(source: T): T {
return source
}
export function defineRSSSource(url: string, option?: SourceOption): SourceGetter {
return async () => {
const data = await rss2json(url)
if (!data?.items.length) throw new Error("Cannot fetch data")
return data.items.map(item => ({
title: item.title,
url: item.link,
id: item.link,
extra: {
date: !option?.hiddenDate && item.created,
},
}))
}
}
export function defineRSSHubSource(route: string, RSSHubOptions?: RSSHubOption, sourceOption?: SourceOption): SourceGetter {
return async () => {
// "https://rsshub.pseudoyu.com"
const RSSHubBase = "https://rsshub.rssforever.com"
const url = new URL(route, RSSHubBase)
url.searchParams.set("format", "json")
RSSHubOptions = defu<RSSHubOption, RSSHubOption[]>(RSSHubOptions, {
sorted: true,
})
Object.entries(RSSHubOptions).forEach(([key, value]) => {
url.searchParams.set(key, value.toString())
})
const data: RSSHubResponse = await $fetch(url)
return data.items.map(item => ({
title: item.title,
url: item.url,
id: item.id ?? item.url,
extra: {
date: !sourceOption?.hiddenDate && item.date_published,
},
}))
}
}