1 | import { AtomFeed } from './AtomFeed';
|
2 | import { parseAtomFeed } from './Parser';
|
3 | import useSWR, { SWRConfiguration } from 'swr';
|
4 |
|
5 | export interface Response<Data> {
|
6 | data?: Data;
|
7 | error?: Error;
|
8 | isValidating: boolean;
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export function useAtomFeed(feedURL: string, options?: SWRConfiguration): Response<AtomFeed> {
|
19 | const fetcher = (url: string) => fetch(url).then(res => res.text());
|
20 | const { data, error, isValidating } = useSWR(feedURL, fetcher, options);
|
21 |
|
22 |
|
23 | if(data) {
|
24 |
|
25 | try {
|
26 | const decoded = parseAtomFeed(data);
|
27 |
|
28 | return {
|
29 | data: decoded,
|
30 | error,
|
31 | isValidating
|
32 | }
|
33 | }
|
34 | catch(parseError) {
|
35 |
|
36 | return {
|
37 | data: undefined,
|
38 | error: parseError,
|
39 | isValidating
|
40 | }
|
41 | }
|
42 | }
|
43 | else {
|
44 |
|
45 | return {
|
46 | data: undefined,
|
47 | error,
|
48 | isValidating
|
49 | }
|
50 | }
|
51 | }
|