Skip to content

Commit

Permalink
Merge pull request #44 from ndaidong/v6.x.x
Browse files Browse the repository at this point in the history
v6.0.0rc2
  • Loading branch information
ndaidong authored Jul 16, 2022
2 parents ae66da5 + 940747f commit fb75425
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 50 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,27 @@ The following example will explain better than any word:
```js
import { read, onSuccess, onError, onComplete } from 'feed-reader'

onSuccess((feed, url) => {
onSuccess((url, feed) => {
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) => {
onError((url, err) => {
console.log(`Error occurred while processing ${url}`)
console.log('There is a message and reason:')
console.log(err)
})

onComplete((result, url) => {
onComplete((url, result, error) => {
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)
console.log('`error` may be an error object or null')
if (error) {
console.log(error.message)
console.log(error.reason)
}
})

read('https://news.google.com/rss')
Expand All @@ -127,7 +131,7 @@ We can mix both style together, for example to handle the error:
```js
import { read, onError } from 'feed-reader'

onError((err, url) => {
onError((url, err) => {
console.log(`Error occurred while processing ${url}`)
console.log('There is a message and reason:')
console.log(err)
Expand All @@ -142,6 +146,26 @@ const getFeedData = async (url) => {
getFeedData('https://news.google.com/rss')
````

In almost cases, using just `onComplete` is enough:

```js
import { read, onComplete } from 'feed-reader'
onComplete((url, result, error) => {
console.log(`Finish processing ${url}`)
if (result) {
// save feed data
console.log(result)
}
if (error) {
// handle error info
console.log(error)
}
})
read('https://news.google.com/rss')
````
#### Reset event listeners
Use method `resetEvents()` when you want to clear registered listeners from all events.
Expand Down
38 changes: 21 additions & 17 deletions dist/cjs/feed-reader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// [email protected].0rc1, by @ndaidong - built with esbuild at 2022-07-12T14:34:09.025Z - published under MIT license
// [email protected].0rc2, by @ndaidong - built with esbuild at 2022-07-16T03:09:04.163Z - published under MIT license
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
Expand Down Expand Up @@ -14585,46 +14585,50 @@ var isValidUrl_default = (url = "") => {

// src/main.js
var eventEmitter = new import_events.default();
var runWhenComplete = (result, url) => {
eventEmitter.emit("complete", result, url);
var runWhenComplete = (url, result = null, error2 = null) => {
eventEmitter.emit("complete", url, result, error2);
return result;
};
var read = async (url) => {
try {
if (!isValidUrl_default(url)) {
eventEmitter.emit("error", {
const erdata = {
error: "Error occurred while verifying feed URL",
reason: "Invalid URL"
}, url);
return runWhenComplete(null, url);
};
eventEmitter.emit("error", url, erdata);
return runWhenComplete(url, null, erdata);
}
const xml = await retrieve_default(url);
if (!validate(xml)) {
eventEmitter.emit("error", {
const erdata = {
error: "Error occurred while validating XML format",
reason: "The XML document is not well-formed"
}, url);
return runWhenComplete(null, url);
};
eventEmitter.emit("error", url, erdata);
return runWhenComplete(url, null, erdata);
}
try {
const feed = parse(xml);
if (feed) {
eventEmitter.emit("success", feed, url);
eventEmitter.emit("success", url, feed);
}
return runWhenComplete(feed, url);
return runWhenComplete(url, feed);
} catch (er) {
eventEmitter.emit("error", {
const erdata = {
error: "Error occurred while parsing XML structure",
reason: er.message
}, url);
return runWhenComplete(null, url);
};
eventEmitter.emit("error", url, erdata);
return runWhenComplete(url, null, erdata);
}
} catch (err) {
eventEmitter.emit("error", {
const erdata = {
error: "Error occurred while retrieving remote XML data",
reason: err.message
}, url);
return runWhenComplete(null, url);
};
eventEmitter.emit("error", url, erdata);
return runWhenComplete(url, null, erdata);
}
};
var onComplete = (fn) => {
Expand Down
2 changes: 1 addition & 1 deletion dist/cjs/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "feed-reader-cjs",
"version": "6.0.0rc1",
"version": "6.0.0rc2",
"main": "./feed-reader.js"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "6.0.0rc1",
"version": "6.0.0rc2",
"name": "feed-reader",
"description": "Load and parse ATOM/RSS data from given feed url",
"homepage": "https://www.npmjs.com/package/feed-reader",
Expand Down
39 changes: 20 additions & 19 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,55 @@ export {

const eventEmitter = new EventEmitter()

const runWhenComplete = (result, url) => {
eventEmitter.emit('complete', result, url)
const runWhenComplete = (url, result = null, error = null) => {
eventEmitter.emit('complete', url, result, error)
return result
}

export const read = async (url) => {
try {
if (!isValidUrl(url)) {
eventEmitter.emit('error', {
const erdata = {
error: 'Error occurred while verifying feed URL',
reason: 'Invalid URL'
}, url)

return runWhenComplete(null, url)
}
eventEmitter.emit('error', url, erdata)
return runWhenComplete(url, null, erdata)
}
const xml = await getXML(url)

if (!validate(xml)) {
eventEmitter.emit('error', {
const erdata = {
error: 'Error occurred while validating XML format',
reason: 'The XML document is not well-formed'
}, url)

return runWhenComplete(null, url)
}
eventEmitter.emit('error', url, erdata)
return runWhenComplete(url, null, erdata)
}

try {
const feed = parse(xml)
if (feed) {
eventEmitter.emit('success', feed, url)
eventEmitter.emit('success', url, feed)
}

return runWhenComplete(feed, url)
return runWhenComplete(url, feed)
} catch (er) {
eventEmitter.emit('error', {
const erdata = {
error: 'Error occurred while parsing XML structure',
reason: er.message
}, url)
}
eventEmitter.emit('error', url, erdata)

return runWhenComplete(null, url)
return runWhenComplete(url, null, erdata)
}
} catch (err) {
eventEmitter.emit('error', {
const erdata = {
error: 'Error occurred while retrieving remote XML data',
reason: err.message
}, url)

return runWhenComplete(null, url)
}
eventEmitter.emit('error', url, erdata)
return runWhenComplete(url, null, erdata)
}
}

Expand Down
20 changes: 13 additions & 7 deletions src/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ const parseUrl = (url) => {

test('test read a non-string link', async () => {
const url = []
onError((err, rss) => {
onError((rss, err) => {
expect(rss).toEqual(url)
expect(err.error).toEqual('Error occurred while verifying feed URL')
expect(err.reason).toEqual('Invalid URL')
})
onComplete((feed, rss) => {
onComplete((rss, feed, err) => {
expect(rss).toEqual(url)
expect(feed).toEqual(null)
expect(err.error).toEqual('Error occurred while verifying feed URL')
expect(err.reason).toEqual('Invalid URL')
})
const re = await read(url)
expect(re).toEqual(null)
Expand All @@ -41,14 +43,16 @@ test('test read a fake link', async () => {
nock(baseUrl).head(path).reply(404)
nock(baseUrl).get(path).reply(404)

onError((err, rss) => {
onError((rss, err) => {
expect(rss).toEqual(url)
expect(err.error).toEqual('Error occurred while retrieving remote XML data')
expect(err.reason).toEqual('Request failed with status code 404')
})
onComplete((feed, rss) => {
onComplete((rss, feed, err) => {
expect(rss).toEqual(url)
expect(feed).toEqual(null)
expect(err.error).toEqual('Error occurred while retrieving remote XML data')
expect(err.reason).toEqual('Request failed with status code 404')
})
const re = await read(url)
expect(re).toEqual(null)
Expand All @@ -63,14 +67,16 @@ test('test read from invalid xml', async () => {
nock(baseUrl).get(path).reply(200, xml, {
'Content-Type': 'application/xml'
})
onError((err, rss) => {
onError((rss, err) => {
expect(rss).toEqual(url)
expect(err.error).toEqual('Error occurred while validating XML format')
expect(err.reason).toEqual('The XML document is not well-formed')
})
onComplete((feed, rss) => {
onComplete((rss, feed, err) => {
expect(rss).toEqual(url)
expect(feed).toEqual(null)
expect(err.error).toEqual('Error occurred while validating XML format')
expect(err.reason).toEqual('The XML document is not well-formed')
})
const re = await read(url)
expect(re).toEqual(null)
Expand Down Expand Up @@ -145,7 +151,7 @@ test('test read from a more complicate atom source', async () => {
'Content-Type': 'application/xml'
})

onSuccess((feed, rss) => {
onSuccess((rss, feed) => {
expect(rss).toEqual(url)
expect(feed).toBeInstanceOf(Object)
feedAttrs.forEach((k) => {
Expand Down

0 comments on commit fb75425

Please sign in to comment.