diff options
Diffstat (limited to 'src/lib/Reader/resource.ts')
| -rw-r--r-- | src/lib/Reader/resource.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/lib/Reader/resource.ts b/src/lib/Reader/resource.ts new file mode 100644 index 00000000..b96a890f --- /dev/null +++ b/src/lib/Reader/resource.ts @@ -0,0 +1,40 @@ +export enum Resource { + MangaDex = 'MangaDex', + Rawkuma = 'Rawkuma' +} + +export const identify = (url: string): Resource | undefined => { + if (url.match(/mangadex\.org\/title\/([a-f0-9-]+)\/?/)?.[1]) { + return Resource.MangaDex; + } else if (url.match(/rawkuma\.com\/manga\/([a-z0-9-]+)\/?/)?.[1]) { + return Resource.Rawkuma; + } + + return undefined; +}; + +export const fetchResource = async (url: string) => { + const resource = identify(url); + + if (resource === Resource.MangaDex) { + return await fetch( + `https://api.mangadex.org/manga/${ + url.match(/mangadex\.org\/title\/([a-f0-9-]+)\/?/)?.[1] + }/feed?order[chapter]=desc&translatedLanguage[]=en` + ); + } else if (resource === Resource.Rawkuma) { + return await fetch(url); + } + + return fetch(url); +}; + +export const decodeResource = async (response: Response, url: string) => { + const resource = identify(url); + + if (resource === Resource.MangaDex) { + return await response.json(); + } else { + return await response.text(); + } +}; |