smartfeed/ts/smartfeed.classes.feed.ts

73 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-10-25 14:02:03 +00:00
import * as plugins from './smartfeed.plugins';
export interface IFeedOptions {
domain: string;
title: string;
description: string;
2020-10-25 20:07:35 +00:00
category: string;
2020-10-25 14:02:03 +00:00
company: string;
companyEmail: string;
companyDomain: string;
}
2020-10-25 20:07:35 +00:00
export interface IFeedItem {
title: string;
timestamp: number;
url: string;
authorName: string;
imageUrl: string;
}
2020-10-25 14:02:03 +00:00
export class Feed {
options: IFeedOptions;
2020-10-25 20:07:35 +00:00
items: IFeedItem[] = [];
2020-10-25 14:02:03 +00:00
constructor(optionsArg: IFeedOptions) {
this.options = optionsArg;
}
2020-10-25 20:07:35 +00:00
public addItem(itemArg: IFeedItem) {
this.items.push(itemArg);
2020-10-25 14:02:03 +00:00
}
2020-10-25 20:07:35 +00:00
private getFeedObject() {
2020-10-25 14:02:03 +00:00
const feed = new plugins.feed.Feed({
copyright: `All rights reserved, ${this.options.company}`,
id: this.options.domain,
title: this.options.title,
author: {
name: this.options.company,
email: this.options.companyEmail,
link: this.options.companyEmail
},
description: this.options.description,
generator: '@pushrocks/smartfeed',
language: "en"
});
2020-10-25 20:07:35 +00:00
feed.addCategory(this.options.category);
for (const itemArg of this.items) {
feed.addItem({
title: itemArg.title,
date: new Date(itemArg.timestamp),
link: itemArg.url,
image: itemArg.imageUrl,
author: [{
name: itemArg.authorName
}]
});
}
return feed;
}
public exportRssFeedString(): string {
return this.getFeedObject().rss2();
}
public exportAtomFeed(): string {
return this.getFeedObject().atom1();
}
public exportJsonFeed(): string {
return this.getFeedObject().json1();
2020-10-25 14:02:03 +00:00
}
}