|
| 1 | +import FeedParser = require('../index'); |
| 2 | + |
| 3 | +// Constructor with no options |
| 4 | +const fp1 = new FeedParser(); |
| 5 | + |
| 6 | +// Constructor with all options |
| 7 | +const fp2 = new FeedParser({ |
| 8 | + strict: false, |
| 9 | + normalize: true, |
| 10 | + addmeta: true, |
| 11 | + feedurl: 'https://example.com/feed', |
| 12 | + resume_saxerror: true, |
| 13 | + MAX_BUFFER_LENGTH: 1024 * 1024, |
| 14 | +}); |
| 15 | + |
| 16 | +// meta event |
| 17 | +fp2.on('meta', (meta: FeedParser.Meta) => { |
| 18 | + const title: string = meta.title; |
| 19 | + const description: string = meta.description; |
| 20 | + const type: FeedParser.Type = meta['#type']; |
| 21 | + const version: string = meta['#version']; |
| 22 | + const ns: Array<{ [key: string]: string }> = meta['#ns']; |
| 23 | + const attrs: { [key: string]: any } = meta['@']; |
| 24 | + const date: Date | null = meta.date; |
| 25 | + const pubdate: Date | null = meta.pubdate; |
| 26 | + const link: string = meta.link; |
| 27 | + const xmlurl: string = meta.xmlurl; |
| 28 | + const author: string = meta.author; |
| 29 | + const language: string = meta.language; |
| 30 | + const image: FeedParser.Image = meta.image; |
| 31 | + const imageUrl: string = image.url; |
| 32 | + const favicon: string = meta.favicon; |
| 33 | + const copyright: string = meta.copyright; |
| 34 | + const generator: string = meta.generator; |
| 35 | + const categories: string[] = meta.categories; |
| 36 | + // namespace-prefixed properties via index signature |
| 37 | + const itunesAuthor = meta['itunes:author']; |
| 38 | +}); |
| 39 | + |
| 40 | +// error event |
| 41 | +fp2.on('error', (err: Error) => { |
| 42 | + err.message; |
| 43 | +}); |
| 44 | + |
| 45 | +// readable event + read() |
| 46 | +fp2.on('readable', () => { |
| 47 | + let item: FeedParser.Item | null; |
| 48 | + while ((item = fp2.read()) !== null) { |
| 49 | + const title: string = item.title; |
| 50 | + const description: string = item.description; |
| 51 | + const summary: string = item.summary; |
| 52 | + const date: Date | null = item.date; |
| 53 | + const pubdate: Date | null = item.pubdate; |
| 54 | + const link: string = item.link; |
| 55 | + const origlink: string = item.origlink; |
| 56 | + const author: string = item.author; |
| 57 | + const guid: string = item.guid; |
| 58 | + const comments: string = item.comments; |
| 59 | + const image: { url: string } = item.image; |
| 60 | + const categories: string[] = item.categories; |
| 61 | + const source: FeedParser.Source = item.source; |
| 62 | + const sourceTitle: string = source.title; |
| 63 | + const sourceUrl: string = source.url; |
| 64 | + const enclosures: FeedParser.Enclosure[] = item.enclosures; |
| 65 | + const enc: FeedParser.Enclosure = enclosures[0]; |
| 66 | + const encUrl: string = enc.url; |
| 67 | + const meta: FeedParser.Meta = item.meta; |
| 68 | + // namespace-prefixed properties via index signature |
| 69 | + const mediaContent = item['media:content']; |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +// once and addListener overloads |
| 74 | +fp2.once('meta', (_meta: FeedParser.Meta) => {}); |
| 75 | +fp2.addListener('error', (_err: Error) => {}); |
| 76 | + |
| 77 | +fp2.resumeSaxError(); |
0 commit comments