rbren/rss-parser

Explain custom fields in TypeScript sample.

danielniccoli opened this issue · 1 comments

I'm trying to understand the following statement in the TypeScript sample code (see below) of the README:

console.log(feed.title); // feed will have a foo property, type as a string

Looking at the object, there is no foo property. feed.title.foo is undefined. Same with feed.item[0].bar. It's not evident what the CustomFeed and CustomItem objects are doing.

Sample code of the README for your convenience:

import Parser from 'rss-parser';

type CustomFeed = {foo: string};
type CustomItem = {bar: number};

const parser: Parser<CustomFeed, CustomItem> = new Parser({
  customFields: {
    feed: ['foo', 'baz'],
    //            ^ will error because `baz` is not a key of CustomFeed
    item: ['bar']
  }
});

(async () => {

  const feed = await parser.parseURL('https://www.reddit.com/.rss');
  console.log(feed.title); // feed will have a `foo` property, type as a string

  feed.items.forEach(item => {
    console.log(item.title + ':' + item.link) // item will have a `bar` property type as a number
  });
})();
rbren commented

customFields is helpful if there are XML attributes available in the RSS which are non-standard RSS fields.

Check out this test case which parses this XML