Merge pull request #16513 from cortopy/node-feedparser

added type definitions for node-feedparser
This commit is contained in:
Arthur Ozga
2017-05-19 13:14:18 -07:00
committed by GitHub
4 changed files with 225 additions and 0 deletions

166
node-feedparser/index.d.ts vendored Normal file
View File

@@ -0,0 +1,166 @@
// Type definitions for feedparser 2.2
// Project: http://github.com/danmactough/node-feedparser
// Definitions by: Juan J. Jimenez-Anca <https://github.com/cortopy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/** Declaration file generated by dts-gen */
/// <reference types="node" />
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;
}
}

View File

@@ -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);
}
});

View File

@@ -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"
]
}

View File

@@ -0,0 +1,5 @@
{ "extends": "dtslint/dt.json",
"rules": {
"no-conditional-assignment": false
}
}