feed-client 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +121 -2
- package/dist/index.d.mts +41 -0
- package/dist/index.mjs +236 -0
- package/package.json +30 -4
package/README.md
CHANGED
|
@@ -1,3 +1,122 @@
|
|
|
1
|
-
#
|
|
1
|
+
# feed-client
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A client that fetches and parses JSON Feed, Atom, and RSS (2.0, 0.9x, 1.0), normalizing them into a common shape.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add feed-client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Fetch from a URL
|
|
14
|
+
|
|
15
|
+
The format is detected automatically.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { fetchFeed } from "feed-client";
|
|
19
|
+
|
|
20
|
+
const feed = await fetchFeed("https://example.com/feed.xml");
|
|
21
|
+
|
|
22
|
+
console.log(feed.format); // "json" | "atom" | "rss"
|
|
23
|
+
for (const item of feed.items) {
|
|
24
|
+
console.log(item.title, item.link, item.published?.toISOString());
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The second argument is passed straight to `fetch` as `RequestInit` (timeouts, headers, etc.).
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
await fetchFeed(url, {
|
|
32
|
+
signal: AbortSignal.timeout(5_000),
|
|
33
|
+
headers: { "User-Agent": "my-reader/1.0" },
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Parse a string
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { parseFeed } from "feed-client";
|
|
41
|
+
|
|
42
|
+
const feed = parseFeed(xmlOrJsonString); // auto-detected
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
If you already know the format, use a specific parser.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { parseAtom, parseJsonFeed, parseRdf, parseRss } from "feed-client";
|
|
49
|
+
|
|
50
|
+
parseJsonFeed(jsonString); // JSON Feed 1.0 / 1.1 (an already-parsed object also works)
|
|
51
|
+
parseAtom(xmlString); // Atom 1.0
|
|
52
|
+
parseRss(xmlString); // RSS 2.0 / 0.9x
|
|
53
|
+
parseRdf(xmlString); // RSS 1.0 (RDF)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Return value
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
interface Feed {
|
|
60
|
+
format: "json" | "atom" | "rss";
|
|
61
|
+
title?: string;
|
|
62
|
+
link?: string;
|
|
63
|
+
description?: string;
|
|
64
|
+
items: FeedItem[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
interface FeedItem {
|
|
68
|
+
id?: string;
|
|
69
|
+
title?: string;
|
|
70
|
+
link?: string;
|
|
71
|
+
content?: string; // may be HTML
|
|
72
|
+
published?: Date;
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Fields are mapped from each format as follows (`→` means "falls back to").
|
|
77
|
+
|
|
78
|
+
| FeedItem | JSON Feed | Atom | RSS 2.0 | RSS 1.0 |
|
|
79
|
+
| ----------- | ------------------------------------------- | ----------------------- | --------------------------------- | --------------------------------- |
|
|
80
|
+
| `id` | `id` | `id` | `guid` → `link` | `rdf:about` → `link` |
|
|
81
|
+
| `link` | `url` | `link[rel=alternate]` | `link` | `link` |
|
|
82
|
+
| `content` | `content_html` → `content_text` → `summary` | `content` → `summary` | `content:encoded` → `description` | `content:encoded` → `description` |
|
|
83
|
+
| `published` | `date_published` | `published` → `updated` | `pubDate` → `dc:date` | `dc:date` |
|
|
84
|
+
|
|
85
|
+
Atom elements with `type="xhtml"` are returned as a markup string with the wrapping `<div>` removed.
|
|
86
|
+
|
|
87
|
+
> [!WARNING]
|
|
88
|
+
> `content` is the feed's HTML as-is. Sanitize it before rendering.
|
|
89
|
+
|
|
90
|
+
## Errors
|
|
91
|
+
|
|
92
|
+
Every error extends `OriginalError`.
|
|
93
|
+
|
|
94
|
+
| Class | Thrown when | Properties |
|
|
95
|
+
| -------------------- | ---------------------------------------------------- | --------------------------------------------------- |
|
|
96
|
+
| `UnknownFormatError` | The input doesn't match any supported format | — |
|
|
97
|
+
| `FetchError` | Network failure or a non-2xx response | `url`, `status` (`undefined` on network failure) |
|
|
98
|
+
| `ParseError` | Invalid JSON / XML, or the document fails the schema | `format`, `cause` (the original error or zod error) |
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { FetchError, OriginalError, ParseError, fetchFeed } from "feed-client";
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
await fetchFeed(url);
|
|
105
|
+
} catch (e) {
|
|
106
|
+
if (e instanceof FetchError) console.error(e.status);
|
|
107
|
+
else if (e instanceof ParseError) console.error(e.format, e.cause);
|
|
108
|
+
else if (e instanceof OriginalError) console.error(e.message);
|
|
109
|
+
else throw e;
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Development
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
bun install
|
|
117
|
+
bun run test # vitest
|
|
118
|
+
bun run lint # oxlint
|
|
119
|
+
bun run fmt # oxfmt
|
|
120
|
+
bun run typecheck # tsc
|
|
121
|
+
bun run build # tsdown → dist/
|
|
122
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
type FeedFormat = "json" | "atom" | "rss";
|
|
2
|
+
interface FeedItem {
|
|
3
|
+
id?: string;
|
|
4
|
+
title?: string;
|
|
5
|
+
link?: string;
|
|
6
|
+
content?: string;
|
|
7
|
+
published?: Date;
|
|
8
|
+
}
|
|
9
|
+
interface Feed {
|
|
10
|
+
format: FeedFormat;
|
|
11
|
+
title?: string;
|
|
12
|
+
link?: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
items: FeedItem[];
|
|
15
|
+
}
|
|
16
|
+
export declare function parseAtom(input: string): Feed;
|
|
17
|
+
export declare function parseJsonFeed(input: string | unknown): Feed;
|
|
18
|
+
export declare function parseRss(input: string): Feed;
|
|
19
|
+
export declare function parseRdf(input: string): Feed;
|
|
20
|
+
/** Base class for every error this library throws. */
|
|
21
|
+
export declare class OriginalError extends Error {
|
|
22
|
+
constructor(message: string, options?: ErrorOptions);
|
|
23
|
+
}
|
|
24
|
+
export declare class UnknownFormatError extends OriginalError {
|
|
25
|
+
constructor();
|
|
26
|
+
}
|
|
27
|
+
/** Network failure (`status` undefined) or non-2xx response. */
|
|
28
|
+
export declare class FetchError extends OriginalError {
|
|
29
|
+
readonly url: string;
|
|
30
|
+
readonly status?: number | undefined;
|
|
31
|
+
constructor(url: string, status?: number | undefined, options?: ErrorOptions);
|
|
32
|
+
}
|
|
33
|
+
/** Invalid JSON/XML or a document that doesn't match the format's schema; see `cause`. */
|
|
34
|
+
export declare class ParseError extends OriginalError {
|
|
35
|
+
readonly format: FeedFormat;
|
|
36
|
+
constructor(format: FeedFormat, options?: ErrorOptions);
|
|
37
|
+
}
|
|
38
|
+
/** Detects the format and parses. */
|
|
39
|
+
export declare function parseFeed(input: string): Feed;
|
|
40
|
+
export declare function fetchFeed(url: string | URL, init?: RequestInit): Promise<Feed>;
|
|
41
|
+
export type { Feed, FeedFormat, FeedItem };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { XMLParser } from "fast-xml-parser";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
// src/error.ts
|
|
4
|
+
/** Base class for every error this library throws. */
|
|
5
|
+
var OriginalError = class extends Error {
|
|
6
|
+
constructor(message, options) {
|
|
7
|
+
super(message, options);
|
|
8
|
+
this.name = new.target.name;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
var UnknownFormatError = class extends OriginalError {
|
|
12
|
+
constructor() {
|
|
13
|
+
super("Unknown feed format");
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
/** Network failure (`status` undefined) or non-2xx response. */
|
|
17
|
+
var FetchError = class extends OriginalError {
|
|
18
|
+
url;
|
|
19
|
+
status;
|
|
20
|
+
constructor(url, status, options) {
|
|
21
|
+
super(`Failed to fetch ${url}${status ? `: ${status}` : ""}`, options);
|
|
22
|
+
this.url = url;
|
|
23
|
+
this.status = status;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
/** Invalid JSON/XML or a document that doesn't match the format's schema; see `cause`. */
|
|
27
|
+
var ParseError = class extends OriginalError {
|
|
28
|
+
format;
|
|
29
|
+
constructor(format, options) {
|
|
30
|
+
super(`Failed to parse ${format} feed`, options);
|
|
31
|
+
this.format = format;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const parseAs = (format, fn) => {
|
|
35
|
+
try {
|
|
36
|
+
return fn();
|
|
37
|
+
} catch (cause) {
|
|
38
|
+
throw new ParseError(format, { cause });
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// src/feed.ts
|
|
42
|
+
const toDate = (s) => {
|
|
43
|
+
const d = s ? new Date(s) : void 0;
|
|
44
|
+
return d && !Number.isNaN(d.getTime()) ? d : void 0;
|
|
45
|
+
};
|
|
46
|
+
// src/xml.ts
|
|
47
|
+
const xmlOptions = {
|
|
48
|
+
ignoreAttributes: false,
|
|
49
|
+
parseTagValue: false,
|
|
50
|
+
isArray: (_name, jpath) => [
|
|
51
|
+
"feed.link",
|
|
52
|
+
"feed.entry",
|
|
53
|
+
"feed.entry.link",
|
|
54
|
+
"rss.channel.item",
|
|
55
|
+
"rdf:RDF.item"
|
|
56
|
+
].includes(String(jpath))
|
|
57
|
+
};
|
|
58
|
+
const xml = new XMLParser(xmlOptions);
|
|
59
|
+
const toText = (v) => v && typeof v === "object" ? v["#text"] : v;
|
|
60
|
+
const text = z.preprocess(toText, z.string().optional());
|
|
61
|
+
// src/atom.ts
|
|
62
|
+
const atomLink = z.object({
|
|
63
|
+
"@_href": z.string(),
|
|
64
|
+
"@_rel": z.string().optional()
|
|
65
|
+
});
|
|
66
|
+
const atomSchema = z.object({ feed: z.object({
|
|
67
|
+
title: text,
|
|
68
|
+
subtitle: text,
|
|
69
|
+
link: z.array(atomLink).default([]),
|
|
70
|
+
entry: z.array(z.object({
|
|
71
|
+
id: text,
|
|
72
|
+
title: text,
|
|
73
|
+
link: z.array(atomLink).default([]),
|
|
74
|
+
content: text,
|
|
75
|
+
summary: text,
|
|
76
|
+
published: text,
|
|
77
|
+
updated: text
|
|
78
|
+
})).default([])
|
|
79
|
+
}) });
|
|
80
|
+
const xmlRaw = new XMLParser({
|
|
81
|
+
...xmlOptions,
|
|
82
|
+
stopNodes: [
|
|
83
|
+
"feed.title",
|
|
84
|
+
"feed.subtitle",
|
|
85
|
+
"feed.entry.title",
|
|
86
|
+
"feed.entry.summary",
|
|
87
|
+
"feed.entry.content"
|
|
88
|
+
]
|
|
89
|
+
});
|
|
90
|
+
const inlineXhtml = (node, raw, keys) => {
|
|
91
|
+
for (const k of keys) {
|
|
92
|
+
if (node?.[k]?.["@_type"] !== "xhtml") continue;
|
|
93
|
+
node[k] = String(raw?.[k]?.["#text"] ?? "").replace(/^\s*<div[^>]*>([\s\S]*)<\/div>\s*$/, "$1").trim();
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
const altLink = (links) => (links.find((l) => !l["@_rel"] || l["@_rel"] === "alternate") ?? links[0])?.["@_href"];
|
|
97
|
+
function parseAtom(input) {
|
|
98
|
+
const { feed } = parseAs("atom", () => {
|
|
99
|
+
const doc = xml.parse(input);
|
|
100
|
+
if (input.includes("xhtml")) {
|
|
101
|
+
const raw = xmlRaw.parse(input);
|
|
102
|
+
inlineXhtml(doc.feed, raw.feed, ["title", "subtitle"]);
|
|
103
|
+
doc.feed?.entry?.forEach((e, i) => inlineXhtml(e, raw.feed?.entry?.[i], [
|
|
104
|
+
"title",
|
|
105
|
+
"summary",
|
|
106
|
+
"content"
|
|
107
|
+
]));
|
|
108
|
+
}
|
|
109
|
+
return atomSchema.parse(doc);
|
|
110
|
+
});
|
|
111
|
+
return {
|
|
112
|
+
format: "atom",
|
|
113
|
+
title: feed.title,
|
|
114
|
+
link: altLink(feed.link),
|
|
115
|
+
description: feed.subtitle,
|
|
116
|
+
items: feed.entry.map((e) => ({
|
|
117
|
+
id: e.id,
|
|
118
|
+
title: e.title,
|
|
119
|
+
link: altLink(e.link),
|
|
120
|
+
content: e.content ?? e.summary,
|
|
121
|
+
published: toDate(e.published ?? e.updated)
|
|
122
|
+
}))
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// src/json.ts
|
|
126
|
+
const jsonFeedSchema = z.object({
|
|
127
|
+
version: z.string().startsWith("https://jsonfeed.org/version/"),
|
|
128
|
+
title: z.string().optional(),
|
|
129
|
+
home_page_url: z.string().optional(),
|
|
130
|
+
description: z.string().optional(),
|
|
131
|
+
items: z.array(z.object({
|
|
132
|
+
id: z.coerce.string(),
|
|
133
|
+
url: z.string().optional(),
|
|
134
|
+
title: z.string().optional(),
|
|
135
|
+
content_html: z.string().optional(),
|
|
136
|
+
content_text: z.string().optional(),
|
|
137
|
+
summary: z.string().optional(),
|
|
138
|
+
date_published: z.string().optional()
|
|
139
|
+
}))
|
|
140
|
+
});
|
|
141
|
+
function parseJsonFeed(input) {
|
|
142
|
+
const f = parseAs("json", () => jsonFeedSchema.parse(typeof input === "string" ? JSON.parse(input) : input));
|
|
143
|
+
return {
|
|
144
|
+
format: "json",
|
|
145
|
+
title: f.title,
|
|
146
|
+
link: f.home_page_url,
|
|
147
|
+
description: f.description,
|
|
148
|
+
items: f.items.map((i) => ({
|
|
149
|
+
id: i.id,
|
|
150
|
+
title: i.title,
|
|
151
|
+
link: i.url,
|
|
152
|
+
content: i.content_html ?? i.content_text ?? i.summary,
|
|
153
|
+
published: toDate(i.date_published)
|
|
154
|
+
}))
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
// src/rss.ts
|
|
158
|
+
const rssSchema = z.object({ rss: z.object({ channel: z.object({
|
|
159
|
+
title: text,
|
|
160
|
+
link: text,
|
|
161
|
+
description: text,
|
|
162
|
+
item: z.array(z.object({
|
|
163
|
+
title: text,
|
|
164
|
+
link: text,
|
|
165
|
+
description: text,
|
|
166
|
+
"content:encoded": text,
|
|
167
|
+
guid: text,
|
|
168
|
+
pubDate: text,
|
|
169
|
+
"dc:date": text
|
|
170
|
+
})).default([])
|
|
171
|
+
}) }) });
|
|
172
|
+
const rdfSchema = z.object({ "rdf:RDF": z.object({
|
|
173
|
+
channel: z.object({
|
|
174
|
+
title: text,
|
|
175
|
+
link: text,
|
|
176
|
+
description: text
|
|
177
|
+
}),
|
|
178
|
+
item: z.array(z.object({
|
|
179
|
+
"@_rdf:about": z.string().optional(),
|
|
180
|
+
title: text,
|
|
181
|
+
link: text,
|
|
182
|
+
description: text,
|
|
183
|
+
"content:encoded": text,
|
|
184
|
+
"dc:date": text
|
|
185
|
+
})).default([])
|
|
186
|
+
}) });
|
|
187
|
+
function parseRss(input) {
|
|
188
|
+
const { channel } = parseAs("rss", () => rssSchema.parse(xml.parse(input))).rss;
|
|
189
|
+
return {
|
|
190
|
+
format: "rss",
|
|
191
|
+
title: channel.title,
|
|
192
|
+
link: channel.link,
|
|
193
|
+
description: channel.description,
|
|
194
|
+
items: channel.item.map((i) => ({
|
|
195
|
+
id: i.guid ?? i.link,
|
|
196
|
+
title: i.title,
|
|
197
|
+
link: i.link,
|
|
198
|
+
content: i["content:encoded"] ?? i.description,
|
|
199
|
+
published: toDate(i.pubDate ?? i["dc:date"])
|
|
200
|
+
}))
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function parseRdf(input) {
|
|
204
|
+
const rdf = parseAs("rss", () => rdfSchema.parse(xml.parse(input)))["rdf:RDF"];
|
|
205
|
+
return {
|
|
206
|
+
format: "rss",
|
|
207
|
+
title: rdf.channel.title,
|
|
208
|
+
link: rdf.channel.link,
|
|
209
|
+
description: rdf.channel.description,
|
|
210
|
+
items: rdf.item.map((i) => ({
|
|
211
|
+
id: i["@_rdf:about"] ?? i.link,
|
|
212
|
+
title: i.title,
|
|
213
|
+
link: i.link,
|
|
214
|
+
content: i["content:encoded"] ?? i.description,
|
|
215
|
+
published: toDate(i["dc:date"])
|
|
216
|
+
}))
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
// src/index.ts
|
|
220
|
+
/** Detects the format and parses. */
|
|
221
|
+
function parseFeed(input) {
|
|
222
|
+
const s = input.trimStart();
|
|
223
|
+
if (s.startsWith("{")) return parseJsonFeed(s);
|
|
224
|
+
if (/<feed[\s>]/.test(s)) return parseAtom(s);
|
|
225
|
+
if (/<rss[\s>]/.test(s)) return parseRss(s);
|
|
226
|
+
if (/<rdf:RDF[\s>]/.test(s)) return parseRdf(s);
|
|
227
|
+
throw new UnknownFormatError();
|
|
228
|
+
}
|
|
229
|
+
async function fetchFeed(url, init) {
|
|
230
|
+
const res = await fetch(url, init).catch((cause) => {
|
|
231
|
+
throw new FetchError(String(url), void 0, { cause });
|
|
232
|
+
});
|
|
233
|
+
if (!res.ok) throw new FetchError(String(url), res.status);
|
|
234
|
+
return parseFeed(await res.text());
|
|
235
|
+
}
|
|
236
|
+
export { FetchError, OriginalError, ParseError, UnknownFormatError, fetchFeed, parseAtom, parseFeed, parseJsonFeed, parseRdf, parseRss };
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "feed-client",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"type": "module",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./dist/index.mjs",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsdown",
|
|
14
|
+
"test": "vitest run",
|
|
15
|
+
"lint": "oxlint",
|
|
16
|
+
"fmt": "oxfmt",
|
|
17
|
+
"fmt:check": "oxfmt --check",
|
|
18
|
+
"typecheck": "tsc --noEmit"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"fast-xml-parser": "^5.12.1",
|
|
22
|
+
"zod": "^4.6.7"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/bun": "latest",
|
|
26
|
+
"oxfmt": "^0.81.0",
|
|
27
|
+
"oxlint": "^1.96.0",
|
|
28
|
+
"tsdown": "^0.23.8",
|
|
29
|
+
"typescript": "^7.0.2",
|
|
30
|
+
"vitest": "^5.1.2"
|
|
31
|
+
}
|
|
32
|
+
}
|