Skip to content

Commit 6e205c9

Browse files
committed
refactor: simplify types and getters
Always check for possible string value first before AtomElement struct
1 parent 52d08b1 commit 6e205c9

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

src/lib/api/fetchRSS.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { parseString } from "xml2js"
22

3-
import type { AtomResult, RSSChannel, RSSItem, RSSResult } from "../types"
3+
import type {
4+
AtomElement,
5+
AtomResult,
6+
RSSChannel,
7+
RSSItem,
8+
RSSResult,
9+
} from "../types"
410
import { isValidDate } from "../utils/date"
511

612
/**
@@ -71,17 +77,13 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
7177
})
7278
// Map to RSSItem object
7379
.map((entry) => {
74-
const getImgSrc = (): string => {
75-
const imgRegEx = /https?:\/\/[^"]*?\.(jpe?g|png|webp)/g
76-
const content = entry.content?.[0]?._ || ""
77-
const summary = entry.summary?.[0]?._ || ""
78-
const contentMatch = content.match(imgRegEx)
79-
const summaryMatch = summary.match(imgRegEx)
80-
if (contentMatch) return contentMatch[0]
81-
if (summaryMatch) return summaryMatch[0]
82-
return feedImage || ""
80+
const getString = (el?: AtomElement[]): string => {
81+
if (!el) return ""
82+
const [firstEl] = el
83+
if (typeof firstEl === "string") return firstEl
84+
return firstEl._ || ""
8385
}
84-
const getLink = (): string => {
86+
const getHref = (): string => {
8587
if (!entry.link) {
8688
console.warn(`No link found for RSS url: ${url}`)
8789
return ""
@@ -90,15 +92,18 @@ export const fetchRSS = async (xmlUrl: string | string[]) => {
9092
if (typeof link === "string") return link
9193
return link.$.href || ""
9294
}
93-
const getTitle = (): string => {
94-
const title = entry.title[0]
95-
if (typeof title === "string") return title
96-
return title._ || ""
95+
const getImgSrc = (): string => {
96+
const imgRegEx = /https?:\/\/[^"]*?\.(jpe?g|png|webp)/g
97+
const contentMatch = getString(entry.content).match(imgRegEx)
98+
if (contentMatch) return contentMatch[0]
99+
const summaryMatch = getString(entry.summary).match(imgRegEx)
100+
if (summaryMatch) return summaryMatch[0]
101+
return feedImage || ""
97102
}
98103
return {
99104
pubDate: entry.updated[0],
100-
title: getTitle(),
101-
link: getLink(),
105+
title: getString(entry.title),
106+
link: getHref(),
102107
imgSrc: getImgSrc(),
103108
source,
104109
sourceUrl,

src/lib/types.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -803,21 +803,21 @@ export type RSSResult = {
803803
}
804804
}
805805

806-
export type AtomItem = {
807-
_?: string
808-
$: {
809-
type?: string
810-
href?: string
811-
rel?: string
812-
}
813-
}
806+
export type AtomElement =
807+
| string
808+
| {
809+
_?: string // children
810+
$: {
811+
href?: string
812+
}
813+
}
814814
export type AtomEntry = {
815815
id: string[]
816-
title: (AtomItem | string)[]
816+
title: AtomElement[]
817817
updated: string[]
818-
content?: AtomItem[]
819-
link?: (AtomItem | string)[]
820-
summary?: AtomItem[]
818+
content?: AtomElement[]
819+
link?: AtomElement[]
820+
summary?: AtomElement[]
821821
}
822822

823823
export type AtomResult = {

0 commit comments

Comments
 (0)