aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Reader/resource.ts
blob: b96a890f3bde6166d50eb43211fbbf26d1f6a183 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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();
	}
};