From afa576a536152531eba4565e67d21f319418fdb0 Mon Sep 17 00:00:00 2001 From: Juan Jimenez-Anca Date: Sun, 14 May 2017 14:18:08 +0200 Subject: [PATCH] added type definitions for node-feedparser --- node-feedparser/index.d.ts | 166 +++++++++++++++++++++++ node-feedparser/node-feedparser-tests.ts | 32 +++++ node-feedparser/tsconfig.json | 22 +++ node-feedparser/tslint.json | 5 + 4 files changed, 225 insertions(+) create mode 100644 node-feedparser/index.d.ts create mode 100644 node-feedparser/node-feedparser-tests.ts create mode 100644 node-feedparser/tsconfig.json create mode 100644 node-feedparser/tslint.json diff --git a/node-feedparser/index.d.ts b/node-feedparser/index.d.ts new file mode 100644 index 0000000000..bed830265c --- /dev/null +++ b/node-feedparser/index.d.ts @@ -0,0 +1,166 @@ +// Type definitions for feedparser 2.2 +// Project: http://github.com/danmactough/node-feedparser +// Definitions by: Juan J. Jimenez-Anca +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/** Declaration file generated by dts-gen */ + +/// + +import stream = require('stream'); +import { SAXStream } from 'sax'; +export = FeedParser; + +declare class FeedParser extends stream.Duplex { + constructor(options: FeedParser.Options); + options: FeedParser.Options; + stream: SAXStream; + meta: object; + _emitted_meta: boolean; + stack: any[]; + xmlbase: any[]; + in_xhtml: boolean; + xhtml: object; + errors: Error[]; + + addListener(ev: any, fn: any): any; + + cork(): void; + + eventNames(): any; + + getMaxListeners(): any; + + handleAttributes(attrs: FeedParser.Attrs, el: string): any; + + handleCloseTag(el: string): void; + + handleEnd(): any; + + handleError(e: Error): void; + + handleItem(node: FeedParser.Node, type: FeedParser.Type, options: FeedParser.Options): FeedParser.Item; + + handleMeta(node: FeedParser.Node, type: FeedParser.Type, options: FeedParser.Options): FeedParser.Meta; + + handleOpenTag(node: FeedParser.Node): void; + + handleProcessingInstruction(node: FeedParser.Node): void; + + handleSaxError(e: Error): void; + + handleText(text: string): void; + + init(): void; + + isPaused(): any; + + listenerCount(type: FeedParser.Type): any; + + listeners(type: FeedParser.Type): any; + + on(ev: any, fn: any): any; + + pause(): any; + + pipe(dest: any, pipeOpts: any): any; + + push(chunk: any, encoding: any): any; + + read(n?: number): FeedParser.Item; + + removeAllListeners(type: FeedParser.Type, ...args: any[]): any; + + resume(): any; + + resumeSaxError(): void; + + setDefaultEncoding(encoding: any): any; + + setEncoding(enc: any): any; + + setMaxListeners(n: any): any; + + uncork(): void; + + unpipe(dest: any): any; + + unshift(chunk: any): any; + + wrap(stream: SAXStream, ...args: any[]): any; + + private _transform(data: any, encoding: string, done: Function): void; + private _flush(done: Function): void; + +} + +declare namespace FeedParser { + type Type = "atom" | "rss" | "rdf"; + + interface Options { + normalize?: boolean; + addmeta?: boolean; + feedurl?: string; + resume_saxerror?: boolean; + MAX_BUFFER_LENGTH?: number; + } + + + interface Node { + [key: string]: any; + } + + interface Attrs { + name: string; + value: any; + prefix: string; + local: string; + uri: string; + } + + interface NS { + [key: string]: string; + } + + interface Image { + url: string; + title: string; + } + + interface Meta { + "#ns": NS[]; + "#type": Type; + "#version": string; + title: string; + description: string; + date: Date | null; + pubdate: Date | null; + link: string; + xmlurl: string; + author: string; + language: string; + image: Image; + favicon: string; + copyright: string; + generator: string; + categories: string[]; + } + + interface Item { + title: string; + description: string; + summary: string; + date: Date | null; + pubdate: Date | null; + link: string; + origlink: string; + author: string; + guid: string; + comments: string; + image: Image; + categories: string[]; + enclosures: string[]; + meta: Meta; + } + +} + diff --git a/node-feedparser/node-feedparser-tests.ts b/node-feedparser/node-feedparser-tests.ts new file mode 100644 index 0000000000..25d652de8c --- /dev/null +++ b/node-feedparser/node-feedparser-tests.ts @@ -0,0 +1,32 @@ +import request = require('request'); +import * as FeedParser from "feedparser"; + +const req = request('https://news.google.com/news?cf=all&hl=en&pz=1&ned=us&output=rss'); +const feedparser = new FeedParser({}); + +req.on("error", error => { + // handle any request errors +}); + +req.on("response", res => { + if (res.statusCode !== 200) { + req.emit('error', new Error('Bad status code')); + } else { + req.pipe(feedparser); + } +}); + +feedparser.on('error', (error: Error) => { + // always handle errors +}); + +feedparser.on('readable', () => { + // This is where the action is! + const stream = feedparser; + const meta = feedparser.meta; // **NOTE** the "meta" is always available in the context of the feedparser instance + let item: FeedParser.Item; + + while (item = stream.read()) { + console.log(item); + } +}); diff --git a/node-feedparser/tsconfig.json b/node-feedparser/tsconfig.json new file mode 100644 index 0000000000..31cb0cd3b7 --- /dev/null +++ b/node-feedparser/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-feedparser-tests.ts" + ] +} diff --git a/node-feedparser/tslint.json b/node-feedparser/tslint.json new file mode 100644 index 0000000000..5d0b359e47 --- /dev/null +++ b/node-feedparser/tslint.json @@ -0,0 +1,5 @@ +{ "extends": "dtslint/dt.json", + "rules": { + "no-conditional-assignment": false + } +}