From 6ad574039234f363157d721e6fb152d2e1940a79 Mon Sep 17 00:00:00 2001 From: andres Date: Sat, 20 Apr 2024 23:33:42 +0200 Subject: [PATCH] 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 --- .../bypass-cors/bypass-cors.service.ts | 9 ++++ src/services/bypass-cors/index.ts | 1 + src/services/podcasts/podcasts.service.ts | 46 ++++++++++--------- todo.md | 2 +- 4 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 src/services/bypass-cors/bypass-cors.service.ts create mode 100644 src/services/bypass-cors/index.ts diff --git a/src/services/bypass-cors/bypass-cors.service.ts b/src/services/bypass-cors/bypass-cors.service.ts new file mode 100644 index 0000000..f9a5581 --- /dev/null +++ b/src/services/bypass-cors/bypass-cors.service.ts @@ -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(); diff --git a/src/services/bypass-cors/index.ts b/src/services/bypass-cors/index.ts new file mode 100644 index 0000000..da88015 --- /dev/null +++ b/src/services/bypass-cors/index.ts @@ -0,0 +1 @@ +export * from "./bypass-cors.service"; diff --git a/src/services/podcasts/podcasts.service.ts b/src/services/podcasts/podcasts.service.ts index 5a98cba..e83e771 100644 --- a/src/services/podcasts/podcasts.service.ts +++ b/src/services/podcasts/podcasts.service.ts @@ -2,6 +2,7 @@ import { GetEpisodesResponse, GetTopPodcastsResponse } from "./podcasts.types"; import { PodcastDTO } from "./dto/podcast.dto"; import { RssParser } from "../rss-parser"; import { PodcastExtraDTO } from "./dto/podcast-extra.dto"; +import { bypassCorsService } from "../bypass-cors"; class PodcastsService { baseUrl = "https://itunes.apple.com"; @@ -27,12 +28,13 @@ class PodcastsService { url.search = new URLSearchParams(params).toString(); - const response: GetEpisodesResponse = await this.fetchWithoutCors( + const response = await bypassCorsService.fetchBypassingCors( 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( - response.results[0].feedUrl, + json.results[0].feedUrl, ); return { ...podcastExtra, ...podcastFromFeed }; } catch (error) { @@ -42,28 +44,30 @@ class PodcastsService { } async getPodcastFeed(sourceURL: string) { - try { - // 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(); + let response: Response; + 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); } catch (error) { - console.error("Error fetching and parsing data:", error); - throw error; - } - } + console.warn("Error fetching directly:", error); + try { + console.log("Attempting to fetch using CORS bypass..."); - //TODO: move into a separate service - private async fetchWithoutCors(url: string) { - const response = await fetch( - `https://api.allorigins.win/get?url=${encodeURIComponent(url)}`, - ); - const data = await response.json(); - return JSON.parse(data.contents); + response = await bypassCorsService.fetchBypassingCors(sourceURL); + const rss = await response.text(); + return RssParser.parse(rss); + } catch (bypassError) { + console.error("Error fetching with CORS bypass:", bypassError); + throw bypassError; + } + } } } diff --git a/todo.md b/todo.md index 11306e9..ef1eef8 100644 --- a/todo.md +++ b/todo.md @@ -43,6 +43,6 @@ ## Other -- [ ] Close all todos +- [x] Close all todos - [ ] Add styles - [ ] Refactor to use better names \ No newline at end of file