@@ -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
2134import { 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
4162Load 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
4576URL 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
55116Object with all or several of the following properties:
56117
@@ -62,67 +123,63 @@ Object with all or several of the following properties:
62123
63124Note 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
133190node eval.js --url=https://news.google.com/rss --normalization=y --useISODateFormat=y --includeEntryContent=n --includeOptionalElements=n
134191```
135192
136-
137193## License
138194The MIT License (MIT)
139195
0 commit comments