Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#63757 [rss] Fix: add items parameter by @P…
Browse files Browse the repository at this point in the history
…artyLich

* fix: add items parameter to constructor

* test: update rss tests

* refactor: use class declaration

As called out in the DT common mistakes section, we should prefer to use
a class declaration instead of a new-able.

* review feedback
  • Loading branch information
PartyLich committed Jan 17, 2023
1 parent 522a694 commit fc41bdd
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 61 deletions.
67 changes: 33 additions & 34 deletions types/rss/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
// Definitions by: Second Datke <https://github.com/secondwtq>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

declare var factory: NodeRSS.RSSFactory;
export = factory;

declare namespace NodeRSS {
declare namespace RSS {
interface FeedOptions {
/**
* Title of your site or feed.
Expand Down Expand Up @@ -172,36 +169,38 @@ declare namespace NodeRSS {
*/
indent?: boolean | string | undefined;
}
}

interface RSS {
/**
* Add an item to a feed. An item can be used for a blog
* entry, project update, log entry, etc.
* @param {ItemOptions} itemOptions
* @returns {RSS}
*/
item(itemOptions: ItemOptions): RSS;
/**
* Generate XML and return as a string for this feed.
* @returns {string}
*/
xml(): string;
/**
* Generate XML and return as a string for this feed.
*
* @param {XmlOptions} xmlOptions - You can use indent
* option to specify the tab character to use.
* @returns {string}
*/
xml(xmlOptions: XmlOptions): string;
}
// export declare class RSS {
declare class RSS {
/**
* Create an RSS feed with options.
* @param {FeedOptions} feedOptions - Options for the RSS feed.
* @param {ItemOptions[]} feedItems - Array of items for the RSS feed.
* @returns {RSS}
*/
constructor(feedOptions: RSS.FeedOptions, feedItems?: RSS.ItemOptions[]);

interface RSSFactory {
/**
* Create an RSS feed with options.
* @param {FeedOptions} feedOptions - Options for the RSS feed.
* @returns {RSS}
*/
new(feedOptions: FeedOptions): RSS;
}
/**
* Add an item to a feed. An item can be used for a blog
* entry, project update, log entry, etc.
* @param {ItemOptions} itemOptions
* @returns {RSS}
*/
item(itemOptions: RSS.ItemOptions): RSS;
/**
* Generate XML and return as a string for this feed.
* @returns {string}
*/
xml(): string;
/**
* Generate XML and return as a string for this feed.
*
* @param {XmlOptions} xmlOptions - You can use indent
* option to specify the tab character to use.
* @returns {string}
*/
xml(xmlOptions: RSS.XmlOptions): string;
}

export = RSS;
63 changes: 36 additions & 27 deletions types/rss/rss-tests.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
// this test is copied from https://github.com/dylang/node-rss
// it basically:
//
// * creates an RSS feed with some attributes,
// * add an item to it
// * then generates XML string

// this test is adapted from https://github.com/dylang/node-rss
import RSS = require('rss');

var feed = new RSS({
const feedOptions = {
title: 'title',
description: 'description',
feed_url: 'http://example.com/rss.xml',
Expand All @@ -18,11 +12,11 @@ var feed = new RSS({
webMaster: 'Dylan Greene',
copyright: '2013 Dylan Greene',
language: 'en',
categories: ['Category 1','Category 2','Category 3'],
categories: ['Category 1', 'Category 2', 'Category 3'],
pubDate: 'May 20, 2012 04:00:00 GMT',
ttl: 60,
custom_namespaces: {
'itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd'
itunes: 'http://www.itunes.com/dtds/podcast-1.0.dtd',
},
custom_elements: [
{ 'itunes:subtitle': 'A show about everything' },
Expand All @@ -35,35 +29,50 @@ var feed = new RSS({
},
{ 'itunes:image': {
_attr: {
href: 'http://example.com/podcasts/everything/AllAboutEverything.jpg'
}
}
}
]
});
href: 'http://example.com/podcasts/everything/AllAboutEverything.jpg',
},
},
},
],
};

feed.item({
const feedItem = {
title: 'item title',
description: 'use this for the content. It can include html.',
url: 'http://example.com/article4?this&that',
guid: '1123',
categories: ['Category 1','Category 2','Category 3','Category 4'],
categories: ['Category 1', 'Category 2', 'Category 3', 'Category 4'],
author: 'Guest Author',
date: 'May 27, 2012',
lat: 33.417974,
long: -111.933231,
enclosure: { url:'...', file:'path-to-file' },
enclosure: { url: '...', file: 'path-to-file' },
custom_elements: [
{ 'itunes:author': 'John Doe' },
{ 'itunes:subtitle': 'A short primer on table spices' },
{ 'itunes:image': {
{
'itunes:image': {
_attr: {
href: 'http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg'
}
}
href: 'http://example.com/podcasts/everything/AllAboutEverything/Episode1.jpg',
},
},
},
{ 'itunes:duration': '7:04' }
]
});
{ 'itunes:duration': '7:04' },
],
};

// creates an RSS feed with some attributes,
const feed = new RSS(feedOptions); // $ExpectType RSS

// add an item to the feed
feed.item(feedItem); // $ExpectType RSS

// generate an XML string from the feed
const xml = feed.xml(); // $ExpectType string

// creates an RSS feed with some attributes and items
const feedWithItems = new RSS(feedOptions, [feedItem]); // $ExpectType RSS
feedWithItems.xml(); // $ExpectType string

var xml = feed.xml();
// test type exports
type Types = typeof RSS | RSS | RSS.FeedOptions | RSS.EnclosureObject | RSS.ItemOptions | RSS.XmlOptions;

0 comments on commit fc41bdd

Please sign in to comment.