Skip to content

Commit d25c173

Browse files
committed
feat: add RSS items resolver
1 parent 2a06256 commit d25c173

3 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Inject, Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
3+
4+
import { DevCommunityRssItems, DevCommunityRssParser, devCommunityRssParserToken } from './dev-community-rss-parser.token';
5+
6+
@Injectable({
7+
providedIn: 'root',
8+
})
9+
export class DevCommunityRssItemsResolver
10+
implements Resolve<DevCommunityRssItems> {
11+
constructor(
12+
@Inject(devCommunityRssParserToken) private rssParser: DevCommunityRssParser
13+
) {}
14+
15+
async resolve(route: ActivatedRouteSnapshot): Promise<DevCommunityRssItems> {
16+
const feed = await this.rssParser.parseURL(route.data.rssUrl);
17+
18+
return feed.items;
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { InjectionToken } from '@angular/core';
2+
import * as RssParser from 'rss-parser';
3+
4+
export interface DevCommunityRssFeedCustomFields {
5+
readonly language: string;
6+
}
7+
8+
export interface DevCommunityRssItemCustomFields {
9+
readonly author: string;
10+
readonly contentSnippet: string;
11+
readonly description: string;
12+
}
13+
14+
export type DevCommunityRssParser = RssParser<
15+
DevCommunityRssFeedCustomFields,
16+
DevCommunityRssItemCustomFields
17+
>;
18+
19+
export type DevCommunityRssItem = DevCommunityRssItemCustomFields &
20+
RssParser.Item;
21+
export type DevCommunityRssItems = readonly DevCommunityRssItem[];
22+
23+
export function devCommunityRssParserFactory(): DevCommunityRssParser {
24+
return new RssParser<
25+
DevCommunityRssFeedCustomFields,
26+
DevCommunityRssItemCustomFields
27+
>({
28+
customFields: {
29+
feed: ['language'],
30+
item: ['author', 'contentSnippet', 'description'],
31+
},
32+
});
33+
}
34+
35+
export const devCommunityRssParserToken = new InjectionToken<DevCommunityRssParser>(
36+
'DEV_Community_RSS_parser',
37+
{
38+
factory: devCommunityRssParserFactory,
39+
providedIn: 'root',
40+
}
41+
);

libs/publications/feature-this-is-angular/src/lib/shell/shell.module.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { NgModule } from '@angular/core';
22
import { RouterModule, Routes } from '@angular/router';
33

4+
import { DevCommunityRssItemsResolver } from '../dev-community-rss-items.resolver';
45
import { ShellComponent } from './shell.component';
56
import { ShellScam } from './shell.scam';
67

78
const routes: Routes = [
89
{
910
path: '',
1011
component: ShellComponent,
12+
data: {
13+
rssUrl: 'https://dev.to/feed/this-is-angular',
14+
},
15+
resolve: {
16+
rssItems: DevCommunityRssItemsResolver,
17+
},
1118
},
1219
];
1320

0 commit comments

Comments
 (0)