Skip to content

Commit

Permalink
Merge pull request #43 from ndaidong/v6.x.x
Browse files Browse the repository at this point in the history
v6.x.x
  • Loading branch information
ndaidong authored Jul 12, 2022
2 parents 5c7ce09 + 0da91f2 commit bde2a87
Show file tree
Hide file tree
Showing 16 changed files with 396 additions and 257 deletions.
104 changes: 79 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ read(url).then((feed) => {
})
```

##### Note:

> 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).
> Just ensure that you are [using module system](https://nodejs.org/api/packages.html#determining-module-system) and enjoy with ES6 import/export syntax.

## APIs

- [.read(String url)](#readstring-url)
- [The events](#the-events)
- [.resetEvents()](#reset-event-listeners)
- [Configuration methods](#configuration-methods)

#### read(String url)
### read(String url)

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

Expand All @@ -55,9 +51,10 @@ import {
const getFeedData = async (url) => {
try {
console.log(`Get feed data from ${url}`)
const data = await read(url)
console.log(data)
return data
const result = await read(url)
// result may be feed data or null
console.log(result)
return result
} catch (err) {
console.trace(err)
}
Expand Down Expand Up @@ -89,27 +86,84 @@ Feed data object retuned by `read()` method should look like below:
}
```

#### Configuration methods
### The events

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

- getRequestOptions()
- setRequestOptions(Object requestOptions)
- `onSuccess(Function callback)`
- `onError(Function callback)`
- `onComplete(Function callback)`

#### Object `requestOptions`:
The following example will explain better than any word:

```js
{
headers: {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0'
},
responseType: 'text',
responseEncoding: 'utf8',
timeout: 6e4, // 1 minute
maxRedirects: 3
}
import { read, onSuccess, onError, onComplete } from 'feed-reader'

onSuccess((feed, url) => {
console.log(`Feed data from ${url} has been parsed successfully`)
console.log('`feed` is always an object that contains feed data')
console.log(feed)
})

onError((err, url) => {
console.log(`Error occurred while processing ${url}`)
console.log('There is a message and reason:')
console.log(err)
})

onComplete((result, url) => {
console.log(`Finish processing ${url}`)
console.log('There may or may not be an error')
console.log('`result` may be feed data or null')
console.log(result)
})

read('https://news.google.com/rss')
read('https://google.com')
```
Read [axios' request config](https://axios-http.com/docs/req_config) for more info.

We can mix both style together, for example to handle the error:

```js
import { read, onError } from 'feed-reader'

onError((err, url) => {
console.log(`Error occurred while processing ${url}`)
console.log('There is a message and reason:')
console.log(err)
})

const getFeedData = async (url) => {
const result = await read(url)
// `result` may be feed data or null
return result
}

getFeedData('https://news.google.com/rss')
````

#### Reset event listeners

Use method `resetEvents()` when you want to clear registered listeners from all events.

```js
import { resetEvents } from 'feed-reader'
resetEvents()
````
### Configuration methods
#### `setRequestOptions(Object requestOptions)`
Affect to the way how `axios` works. Please refer [axios' request config](https://axios-http.com/docs/req_config) for more info.
#### `getRequestOptions()`
Return current request options.
Default values can be found [here](https://github.com/ndaidong/feed-reader/blob/main/src/config.js#L5).
## Test
Expand Down
5 changes: 5 additions & 0 deletions build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import {
const pkg = JSON.parse(readFileSync('./package.json'))

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

describe('Validate commonjs version output', () => {
test(`Check if ${cjsFile} created`, () => {
expect(existsSync(cjsFile)).toBeTruthy()
})
test('Check if cjs package info updated', () => {
expect(cjsPkg.name).toEqual(`${pkg.name}-cjs`)
expect(cjsPkg.version).toEqual(pkg.version)
})
const constent = readFileSync(cjsFile, 'utf8')
const lines = constent.split('\n')
test('Check if file meta contains package info', () => {
Expand Down
25 changes: 16 additions & 9 deletions cjs-eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

const { writeFileSync } = require('fs')

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

const extractFromUrl = async (url) => {
try {
const art = await read(url)
console.log(art)
writeFileSync('./output.json', JSON.stringify(art), 'utf8')
} catch (err) {
console.trace(err)
}
onComplete((result, url) => {
console.log('onComplete', url)
})
onSuccess((feed, url) => {
console.log('onSuccess', url)
writeFileSync('./output.json', JSON.stringify(feed, undefined, 2), 'utf8')
})
onError((e, url) => {
console.log('onError', url)
console.log(e)
})
const feed = await read(url)
console.log(feed)
}

const init = (argv) => {
if (argv.length === 3) {
return extractFromUrl(argv[2])
const isUrl = argv[2]
return isUrl ? extractFromUrl(isUrl) : false
}
return 'Nothing to do!'
}
Expand Down
Loading

0 comments on commit bde2a87

Please sign in to comment.