Skip to content

Commit 084a26d

Browse files
authored
Merge pull request #55 from ndaidong/6.1.1
v6.1.1
2 parents ad4911b + 910d313 commit 084a26d

File tree

16 files changed

+452
-121
lines changed

16 files changed

+452
-121
lines changed

README.md

Lines changed: 118 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,63 @@ To read & normalize RSS/ATOM/JSON feed data.
1515
- [Give it a try!](https://demos.pwshub.com/feed-reader)
1616
- [Example FaaS](https://readfeed.deta.dev/?url=https://news.google.com/rss)
1717

18-
### Usage
18+
## Install & Usage
19+
20+
### Node.js
21+
22+
```bash
23+
npm i feed-reader
24+
25+
# pnpm
26+
pnpm i feed-reader
27+
28+
# yarn
29+
yarn add feed-reader
30+
```
1931

2032
```js
33+
// es6 module
2134
import { read } from 'feed-reader'
2235

23-
// with CommonJS environments
24-
// const { extract } = require('feed-reader')
25-
// or specify exactly path to cjs variant
26-
// const { read } = require('feed-reader/dist/cjs/feed-reader.js')
36+
// CommonJS
37+
const { read } = require('feed-reader')
2738

28-
const url = 'https://news.google.com/rss'
39+
// or specify exactly path to CommonJS variant
40+
const { read } = require('feed-reader/dist/cjs/feed-reader.js')
41+
```
2942

30-
read(url).then((feed) => {
31-
console.log(feed)
32-
}).catch((err) => {
33-
console.log(err)
34-
})
43+
### Deno
44+
45+
```ts
46+
import { read } from 'https://esm.sh/feed-reader'
3547
```
3648

49+
### Browser
50+
51+
```js
52+
import { read } from 'https://unpkg.com/feed-reader@latest/dist/feed-reader.esm.js'
53+
```
54+
55+
Please check [the examples](https://github.com/ndaidong/feed-reader/tree/main/examples) for reference.
56+
57+
3758
## APIs
3859

39-
### `read(String url [, Object options])`
60+
### `read()`
4061

4162
Load and extract feed data from given RSS/ATOM/JSON source. Return a Promise object.
4263

43-
#### `url` *required*
64+
#### Syntax
65+
66+
```js
67+
read(String url)
68+
read(String url, Object options)
69+
read(String url, Object options, Object fetchOptions)
70+
```
71+
72+
#### Parameters
73+
74+
##### `url` *required*
4475

4576
URL of a valid feed source
4677

@@ -50,7 +81,37 @@ Feed content must be accessible and conform one of the following standards:
5081
- [ATOM Feed](https://datatracker.ietf.org/doc/html/rfc5023)
5182
- [JSON Feed](https://www.jsonfeed.org/version/1.1/)
5283

53-
#### `options` *optional*
84+
For example:
85+
86+
```js
87+
import { read } from 'feed-reader'
88+
89+
read('https://news.google.com/atom').then(result => console.log(result))
90+
```
91+
92+
Without any options, the result should have the following structure:
93+
94+
```js
95+
{
96+
title: String,
97+
link: String,
98+
description: String,
99+
generator: String,
100+
language: String,
101+
published: ISO Date String,
102+
entries: Array[
103+
{
104+
title: String,
105+
link: String,
106+
description: String,
107+
published: ISO Datetime String
108+
},
109+
// ...
110+
]
111+
}
112+
```
113+
114+
##### `options` *optional*
54115

55116
Object with all or several of the following properties:
56117

@@ -62,67 +123,63 @@ Object with all or several of the following properties:
62123

63124
Note that when `normalization` is set to `false`, other options will take no effect to the last output.
64125

65-
66-
Example:
126+
For example:
67127

68128
```js
69-
import {
70-
read
71-
} from 'feed-reader'
72-
73-
const getFeedData = async (url) => {
74-
try {
75-
console.log(`Get feed data from ${url}`)
76-
const result = await read(url)
77-
// result may be feed data or null
78-
console.log(result)
79-
return result
80-
} catch (err) {
81-
console.trace(err)
82-
}
83-
}
129+
import { read } from 'feed-reader'
130+
131+
read('https://news.google.com/atom', {
132+
useISODateFormat: false
133+
})
84134

85-
getFeedData('https://news.google.com/rss')
86-
getFeedData('https://news.google.com/atom')
87-
getFeedData('https://adactio.com/journal/feed.json')
135+
read('https://news.google.com/rss', {
136+
useISODateFormat: false,
137+
includeOptionalElements: true
138+
})
88139
```
89140

90-
### Deno
141+
##### `fetchOptions` *optional*
91142

92-
```ts
93-
import { read } from 'https://esm.sh/feed-reader'
143+
You can use this param to set request headers to fetch.
144+
145+
For example:
146+
147+
```js
148+
import { read } from 'feed-reader'
94149

95-
(async () => {
96-
const data = await read('https://news.google.com/rss')
97-
console.log(data)
98-
})();
150+
const url = 'https://news.google.com/rss'
151+
read(url, null, {
152+
headers: {
153+
'user-agent': 'Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1'
154+
}
155+
})
99156
```
100157

101-
View [more examples](https://github.com/ndaidong/feed-reader/tree/main/examples).
158+
You can also specify a proxy endpoint to load remote content, instead of fetching directly.
102159

160+
For example:
103161

104-
With default options, feed data object retuned by `read()` method should look like below:
162+
```js
163+
import { read } from 'feed-reader'
105164

106-
```json
107-
{
108-
"title": "Top stories - Google News",
109-
"link": "https://news.google.com/atom?hl=en-US&gl=US&ceid=US%3Aen",
110-
"description": "Google News",
111-
"generator": "NFE/5.0",
112-
"language": "",
113-
"published": "2021-12-23T15:01:12.000Z",
114-
"entries": [
115-
{
116-
"title": "Lone suspect in Waukesha parade crash to appear in court today, as Wisconsin reels from tragedy that left 5 dead and dozens more injured - CNN",
117-
"link": "https://news.google.com/__i/rss/rd/articles/CBMiTmh0dHBzOi8vd3d3LmNubi5jb20vMjAyMS8xMS8yMy91cy93YXVrZXNoYS1jYXItcGFyYWRlLWNyb3dkLXR1ZXNkYXkvaW5kZXguaHRtbNIBUmh0dHBzOi8vYW1wLmNubi5jb20vY25uLzIwMjEvMTEvMjMvdXMvd2F1a2VzaGEtY2FyLXBhcmFkZS1jcm93ZC10dWVzZGF5L2luZGV4Lmh0bWw?oc=5",
118-
"description": "Lone suspect in Waukesha parade crash to appear in court today, as Wisconsin reels from tragedy that left 5 dead and dozens more injured    CNN Waukesha Christmas parade attack: 5 dead, 48 injured, Darrell Brooks named as...",
119-
"published": "2021-12-21T22:30:00.000Z"
120-
},
121-
// ...
122-
]
123-
}
165+
const url = 'https://news.google.com/rss'
166+
167+
read(url, null, {
168+
headers: {
169+
'user-agent': 'Opera/9.60 (Windows NT 6.0; U; en) Presto/2.1.1'
170+
},
171+
proxy: {
172+
target: 'https://your-secret-proxy.io/loadXml?url=',
173+
headers: {
174+
'Proxy-Authorization': 'Bearer YWxhZGRpbjpvcGVuc2VzYW1l...'
175+
}
176+
}
177+
})
124178
```
125179

180+
Passing requests to proxy is useful while running `feed-reader` on browser. View `examples/browser-feed-reader` as reference example.
181+
182+
126183
## Quick evaluation
127184

128185
```bash
@@ -133,7 +190,6 @@ npm install
133190
node eval.js --url=https://news.google.com/rss --normalization=y --useISODateFormat=y --includeEntryContent=n --includeOptionalElements=n
134191
```
135192

136-
137193
## License
138194
The MIT License (MIT)
139195

dist/cjs/feed-reader.js

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/cjs/index.d.ts

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,66 @@
11
// Type definitions
22

33
export interface FeedEntry {
4-
link?: string;
5-
title?: string;
6-
description?: string;
7-
published?: Date;
4+
link?: string;
5+
title?: string;
6+
description?: string;
7+
published?: Date;
88
}
99

1010
export interface FeedData {
11-
link?: string;
12-
title?: string;
13-
description?: string;
14-
generator?: string;
15-
language?: string;
16-
published?: Date;
17-
entries?: Array<FeedEntry>;
11+
link?: string;
12+
title?: string;
13+
description?: string;
14+
generator?: string;
15+
language?: string;
16+
published?: Date;
17+
entries?: Array<FeedEntry>;
18+
}
19+
20+
export interface ProxyConfig {
21+
target?: string;
22+
headers?: string[];
1823
}
1924

2025
export interface ReaderOptions {
2126
/**
2227
* normalize feed data or keep original
2328
* default: true
2429
*/
25-
normalization?: Boolean;
30+
normalization?: boolean;
2631
/**
2732
* include full content of feed entry if present
2833
* default: false
2934
*/
30-
includeEntryContent?: Boolean;
35+
includeEntryContent?: boolean;
3136
/**
3237
* include optional elements if any
3338
* default: false
3439
*/
35-
includeOptionalElements?: Boolean;
40+
includeOptionalElements?: boolean;
3641
/**
3742
* convert datetime to ISO format
3843
* default: true
3944
*/
40-
useISODateFormat?: Boolean;
45+
useISODateFormat?: boolean;
4146
/**
4247
* to truncate description
4348
* default: 210
4449
*/
45-
descriptionMaxLen?: number;
50+
descriptionMaxLen?: number;
51+
}
52+
53+
export interface FetchOptions {
54+
/**
55+
* list of request headers
56+
* default: null
57+
*/
58+
headers?: string[];
59+
/**
60+
* the values to configure proxy
61+
* default: null
62+
*/
63+
proxy?: ProxyConfig;
4664
}
4765

48-
export function read(url: string, options?: ReaderOptions): Promise<FeedData>;
66+
export function read(url: string, options?: ReaderOptions, fetchOptions?: FetchOptions): Promise<FeedData>;

dist/cjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "feed-reader",
3-
"version": "6.1.0",
3+
"version": "6.1.1",
44
"main": "./feed-reader.js"
55
}

0 commit comments

Comments
 (0)