Skip to content

Commit bde2a87

Browse files
authored
Merge pull request #43 from ndaidong/v6.x.x
v6.x.x
2 parents 5c7ce09 + 0da91f2 commit bde2a87

16 files changed

+396
-257
lines changed

README.md

Lines changed: 79 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,14 @@ read(url).then((feed) => {
3030
})
3131
```
3232

33-
##### Note:
34-
35-
> Since Node.js v14, ECMAScript modules [have became the official standard format](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_modules_ecmascript_modules).
36-
> Just ensure that you are [using module system](https://nodejs.org/api/packages.html#determining-module-system) and enjoy with ES6 import/export syntax.
37-
38-
3933
## APIs
4034

4135
- [.read(String url)](#readstring-url)
36+
- [The events](#the-events)
37+
- [.resetEvents()](#reset-event-listeners)
4238
- [Configuration methods](#configuration-methods)
4339

44-
#### read(String url)
40+
### read(String url)
4541

4642
Load and extract feed data from given RSS/ATOM source. Return a Promise object.
4743

@@ -55,9 +51,10 @@ import {
5551
const getFeedData = async (url) => {
5652
try {
5753
console.log(`Get feed data from ${url}`)
58-
const data = await read(url)
59-
console.log(data)
60-
return data
54+
const result = await read(url)
55+
// result may be feed data or null
56+
console.log(result)
57+
return result
6158
} catch (err) {
6259
console.trace(err)
6360
}
@@ -89,27 +86,84 @@ Feed data object retuned by `read()` method should look like below:
8986
}
9087
```
9188

92-
#### Configuration methods
89+
### The events
9390

94-
In addition, this lib provides some methods to customize default settings. Don't touch them unless you have reason to do that.
91+
Since v6.0.0, `feed-reader` supports event-driven pattern for easier writing code with more control.
9592

96-
- getRequestOptions()
97-
- setRequestOptions(Object requestOptions)
93+
- `onSuccess(Function callback)`
94+
- `onError(Function callback)`
95+
- `onComplete(Function callback)`
9896

99-
#### Object `requestOptions`:
97+
The following example will explain better than any word:
10098

10199
```js
102-
{
103-
headers: {
104-
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0'
105-
},
106-
responseType: 'text',
107-
responseEncoding: 'utf8',
108-
timeout: 6e4, // 1 minute
109-
maxRedirects: 3
110-
}
100+
import { read, onSuccess, onError, onComplete } from 'feed-reader'
101+
102+
onSuccess((feed, url) => {
103+
console.log(`Feed data from ${url} has been parsed successfully`)
104+
console.log('`feed` is always an object that contains feed data')
105+
console.log(feed)
106+
})
107+
108+
onError((err, url) => {
109+
console.log(`Error occurred while processing ${url}`)
110+
console.log('There is a message and reason:')
111+
console.log(err)
112+
})
113+
114+
onComplete((result, url) => {
115+
console.log(`Finish processing ${url}`)
116+
console.log('There may or may not be an error')
117+
console.log('`result` may be feed data or null')
118+
console.log(result)
119+
})
120+
121+
read('https://news.google.com/rss')
122+
read('https://google.com')
111123
```
112-
Read [axios' request config](https://axios-http.com/docs/req_config) for more info.
124+
125+
We can mix both style together, for example to handle the error:
126+
127+
```js
128+
import { read, onError } from 'feed-reader'
129+
130+
onError((err, url) => {
131+
console.log(`Error occurred while processing ${url}`)
132+
console.log('There is a message and reason:')
133+
console.log(err)
134+
})
135+
136+
const getFeedData = async (url) => {
137+
const result = await read(url)
138+
// `result` may be feed data or null
139+
return result
140+
}
141+
142+
getFeedData('https://news.google.com/rss')
143+
````
144+
145+
#### Reset event listeners
146+
147+
Use method `resetEvents()` when you want to clear registered listeners from all events.
148+
149+
```js
150+
import { resetEvents } from 'feed-reader'
151+
152+
resetEvents()
153+
````
154+
155+
### Configuration methods
156+
157+
#### `setRequestOptions(Object requestOptions)`
158+
159+
Affect to the way how `axios` works. Please refer [axios' request config](https://axios-http.com/docs/req_config) for more info.
160+
161+
#### `getRequestOptions()`
162+
163+
Return current request options.
164+
165+
Default values can be found [here](https://github.com/ndaidong/feed-reader/blob/main/src/config.js#L5).
166+
113167
114168
## Test
115169

build.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import {
1010
const pkg = JSON.parse(readFileSync('./package.json'))
1111

1212
const cjsFile = `./dist/cjs/${pkg.name}.js`
13+
const cjsPkg = JSON.parse(readFileSync('./dist/cjs/package.json'))
1314

1415
describe('Validate commonjs version output', () => {
1516
test(`Check if ${cjsFile} created`, () => {
1617
expect(existsSync(cjsFile)).toBeTruthy()
1718
})
19+
test('Check if cjs package info updated', () => {
20+
expect(cjsPkg.name).toEqual(`${pkg.name}-cjs`)
21+
expect(cjsPkg.version).toEqual(pkg.version)
22+
})
1823
const constent = readFileSync(cjsFile, 'utf8')
1924
const lines = constent.split('\n')
2025
test('Check if file meta contains package info', () => {

cjs-eval.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,28 @@
22

33
const { writeFileSync } = require('fs')
44

5-
const { read } = require('./dist/cjs/feed-reader.js')
5+
const { read, onSuccess, onError, onComplete } = require('./dist/cjs/feed-reader.js')
66

77
const extractFromUrl = async (url) => {
8-
try {
9-
const art = await read(url)
10-
console.log(art)
11-
writeFileSync('./output.json', JSON.stringify(art), 'utf8')
12-
} catch (err) {
13-
console.trace(err)
14-
}
8+
onComplete((result, url) => {
9+
console.log('onComplete', url)
10+
})
11+
onSuccess((feed, url) => {
12+
console.log('onSuccess', url)
13+
writeFileSync('./output.json', JSON.stringify(feed, undefined, 2), 'utf8')
14+
})
15+
onError((e, url) => {
16+
console.log('onError', url)
17+
console.log(e)
18+
})
19+
const feed = await read(url)
20+
console.log(feed)
1521
}
1622

1723
const init = (argv) => {
1824
if (argv.length === 3) {
19-
return extractFromUrl(argv[2])
25+
const isUrl = argv[2]
26+
return isUrl ? extractFromUrl(isUrl) : false
2027
}
2128
return 'Nothing to do!'
2229
}

0 commit comments

Comments
 (0)