chore: move fetch with cors bypass into a separate service

feat: fetch rss directly and if facing a cors issue try again with the bypass
This commit is contained in:
2024-04-20 23:33:42 +02:00
parent 17f7421746
commit 6ad5740392
4 changed files with 36 additions and 22 deletions

View File

@@ -0,0 +1,9 @@
class BypassCorsService {
async fetchBypassingCors(url: string) {
return await fetch(
`https://api.allorigins.win/raw?url=${encodeURIComponent(url)}`,
);
}
}
export const bypassCorsService = new BypassCorsService();

View File

@@ -0,0 +1 @@
export * from "./bypass-cors.service";

View File

@@ -2,6 +2,7 @@ import { GetEpisodesResponse, GetTopPodcastsResponse } from "./podcasts.types";
import { PodcastDTO } from "./dto/podcast.dto"; import { PodcastDTO } from "./dto/podcast.dto";
import { RssParser } from "../rss-parser"; import { RssParser } from "../rss-parser";
import { PodcastExtraDTO } from "./dto/podcast-extra.dto"; import { PodcastExtraDTO } from "./dto/podcast-extra.dto";
import { bypassCorsService } from "../bypass-cors";
class PodcastsService { class PodcastsService {
baseUrl = "https://itunes.apple.com"; baseUrl = "https://itunes.apple.com";
@@ -27,12 +28,13 @@ class PodcastsService {
url.search = new URLSearchParams(params).toString(); url.search = new URLSearchParams(params).toString();
const response: GetEpisodesResponse = await this.fetchWithoutCors( const response = await bypassCorsService.fetchBypassingCors(
url.toString(), url.toString(),
); );
const podcastExtra = new PodcastExtraDTO(response.results[0]); const json: GetEpisodesResponse = await response.json();
const podcastExtra = new PodcastExtraDTO(json.results[0]);
const podcastFromFeed = await this.getPodcastFeed( const podcastFromFeed = await this.getPodcastFeed(
response.results[0].feedUrl, json.results[0].feedUrl,
); );
return { ...podcastExtra, ...podcastFromFeed }; return { ...podcastExtra, ...podcastFromFeed };
} catch (error) { } catch (error) {
@@ -42,28 +44,30 @@ class PodcastsService {
} }
async getPodcastFeed(sourceURL: string) { async getPodcastFeed(sourceURL: string) {
try { let response: Response;
// todo: only use allorigins after getting a cors error
// const response = await fetch(
// `https://api.allorigins.win/raw?url=${encodeURIComponent(sourceURL)}`,
// );
const response = await fetch(sourceURL);
const rss = await response.text();
try {
response = await fetch(sourceURL);
if (!response.ok) {
throw new Error("CORS error or other network issue");
}
const rss = await response.text();
return RssParser.parse(rss); return RssParser.parse(rss);
} catch (error) { } catch (error) {
console.error("Error fetching and parsing data:", error); console.warn("Error fetching directly:", error);
throw error; try {
} console.log("Attempting to fetch using CORS bypass...");
}
//TODO: move into a separate service response = await bypassCorsService.fetchBypassingCors(sourceURL);
private async fetchWithoutCors(url: string) { const rss = await response.text();
const response = await fetch( return RssParser.parse(rss);
`https://api.allorigins.win/get?url=${encodeURIComponent(url)}`, } catch (bypassError) {
); console.error("Error fetching with CORS bypass:", bypassError);
const data = await response.json(); throw bypassError;
return JSON.parse(data.contents); }
}
} }
} }

View File

@@ -43,6 +43,6 @@
## Other ## Other
- [ ] Close all todos - [x] Close all todos
- [ ] Add styles - [ ] Add styles
- [ ] Refactor to use better names - [ ] Refactor to use better names