Skip to content

Commit 06b6651

Browse files
authored
Refactor all code to something more legible
Merge pull request #59 from ErickLimaS/refactor-code
2 parents 1070c33 + 4b316dc commit 06b6651

File tree

130 files changed

+6589
-5825
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+6589
-5825
lines changed

app/api/anilist.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ import {
55
defaultApiQueryRequest, getCurrentSeason,
66
mediaAiringApiQueryRequest, mediaByIdQueryRequest,
77
mediaTrendingApiQueryRequest
8-
} from './anilistQueryFunctions'
8+
} from './anilistQueryConstants'
99
import { cache } from 'react'
1010
import axiosRetry from 'axios-retry'
1111

12-
const BASE_URL: string = 'https://graphql.anilist.co/'
12+
const BASE_URL = 'https://graphql.anilist.co/'
1313

1414
const headers = {
1515
'Content-Type': 'application/json',
1616
}
1717

1818
// returns medias with adult content
19-
function filterAdultContent(data: ApiDefaultResult[] | ApiAiringMidiaResults[], reponseType?: "mediaByFormat") {
19+
function filterMediasWithAdultContent(mediasList: ApiDefaultResult[] | ApiAiringMidiaResults[], reponseType?: "mediaByFormat") {
2020

2121
if (reponseType == "mediaByFormat") {
22-
const filteredData = (data as ApiDefaultResult[]).filter((item) => item.isAdult == false)
22+
const mediasFiltered = (mediasList as ApiDefaultResult[]).filter((item) => item.isAdult == false)
2323

24-
return filteredData
24+
return mediasFiltered
2525
}
2626
else {
27-
const filteredData = (data as ApiAiringMidiaResults[]).filter((item) => item.media.isAdult == false)
27+
const mediasFiltered = (mediasList as ApiAiringMidiaResults[]).filter((item) => item.media.isAdult == false)
2828

29-
return filteredData
29+
return mediasFiltered
3030
}
3131

3232
}
@@ -43,17 +43,17 @@ axiosRetry(Axios, {
4343
export default {
4444

4545
// HOME PAGE
46-
getNewReleases: cache(async (
46+
getNewReleases: cache(async ({ type, format, sort, showAdultContent, status, page, perPage }: {
4747
type: string,
4848
format?: string,
4949
sort?: string,
5050
showAdultContent?: boolean,
5151
status?: "FINISHED" | "RELEASING" | "NOT_YET_RELEASED" | "CANCELLED" | "HIATUS",
5252
page?: number,
5353
perPage?: number
54-
) => {
54+
}) => {
5555

56-
const season: string = getCurrentSeason()
56+
const season = getCurrentSeason()
5757

5858
try {
5959

@@ -92,7 +92,7 @@ export default {
9292
}),
9393

9494
//SEARCH
95-
getSeachResults: cache(async (query: string, showAdultContent?: boolean) => {
95+
getSeachResults: cache(async ({ query, showAdultContent }: { query: string, showAdultContent?: boolean }) => {
9696

9797
try {
9898

@@ -115,7 +115,7 @@ export default {
115115
})
116116

117117
return showAdultContent ?
118-
data.data.Page.media as ApiDefaultResult[] : filterAdultContent(data.data.Page.media, "mediaByFormat")
118+
data.data.Page.media as ApiDefaultResult[] : filterMediasWithAdultContent(data.data.Page.media, "mediaByFormat")
119119

120120
}
121121
catch (error: any) {
@@ -128,7 +128,7 @@ export default {
128128
}),
129129

130130
// RELEASING THIS WEEK
131-
getReleasingThisWeek: cache(async (type: string, format?: string, page?: number, showAdultContent?: boolean) => {
131+
getReleasingThisWeek: cache(async ({ type, format, page, showAdultContent }: { type: string, format?: string, page?: number, showAdultContent?: boolean }) => {
132132

133133
try {
134134

@@ -168,7 +168,13 @@ export default {
168168
}),
169169

170170
// RELEASING BY DAYS RANGE
171-
getReleasingByDaysRange: cache(async (type: string, days: 1 | 7 | 30, pageNumber?: number, perPage?: number, showAdultContent?: boolean) => {
171+
getReleasingByDaysRange: cache(async ({ type, days, pageNumber, perPage, showAdultContent }: {
172+
type: string,
173+
days: 1 | 7 | 30,
174+
pageNumber?: number,
175+
perPage?: number,
176+
showAdultContent?: boolean
177+
}) => {
172178

173179
try {
174180

@@ -198,7 +204,7 @@ export default {
198204
})
199205

200206
return showAdultContent ?
201-
data.data.Page.airingSchedules as ApiAiringMidiaResults[] : filterAdultContent(data.data.Page.airingSchedules) as ApiAiringMidiaResults[]
207+
data.data.Page.airingSchedules as ApiAiringMidiaResults[] : filterMediasWithAdultContent(data.data.Page.airingSchedules) as ApiAiringMidiaResults[]
202208

203209
}
204210
catch (error: any) {
@@ -212,7 +218,7 @@ export default {
212218
}),
213219

214220
// TRENDING
215-
getTrendingMedia: cache(async (sort?: string, showAdultContent?: boolean) => {
221+
getTrendingMedia: cache(async ({ sort, showAdultContent }: { sort?: string, showAdultContent?: boolean }) => {
216222

217223
try {
218224

@@ -251,7 +257,13 @@ export default {
251257
}),
252258

253259
// MEDIAS WITH INDICATED FORMAT
254-
getMediaForThisFormat: cache(async (type: string, sort?: string, pageNumber?: number, perPage?: number, showAdultContent?: boolean) => {
260+
getMediaForThisFormat: cache(async ({ type, sort, pageNumber, perPage, showAdultContent }: {
261+
type: string,
262+
sort?: string,
263+
pageNumber?: number,
264+
perPage?: number,
265+
showAdultContent?: boolean
266+
}) => {
255267

256268
try {
257269

@@ -274,7 +286,7 @@ export default {
274286
})
275287

276288
return showAdultContent ?
277-
data.data.Page.media as ApiDefaultResult[] : filterAdultContent(data.data.Page.media, "mediaByFormat") as ApiDefaultResult[]
289+
data.data.Page.media as ApiDefaultResult[] : filterMediasWithAdultContent(data.data.Page.media, "mediaByFormat") as ApiDefaultResult[]
278290

279291
}
280292
catch (error: any) {
@@ -288,7 +300,7 @@ export default {
288300
}),
289301

290302
// GET MEDIA INFO BY ID
291-
getMediaInfo: cache(async (id: number, showAdultContent?: boolean) => {
303+
getMediaInfo: cache(async ({ id, showAdultContent }: { id: number, showAdultContent?: boolean }) => {
292304

293305
try {
294306

File renamed without changes.

app/api/animes-database/route.ts

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,96 @@ import { MediaDbOffline } from "@/app/ts/interfaces/dbOffilineInterface";
44

55
export async function GET(request: NextRequest) {
66

7-
const params = request.nextUrl.searchParams
7+
const searchParams = request.nextUrl.searchParams
88

9-
const resultLimit = 12
9+
const resultsLimit = 12
1010

11-
let dataSort = (AnimeDataOffline as { data: MediaDbOffline[] }).data
11+
let dataToBeSorted = (AnimeDataOffline as { data: MediaDbOffline[] }).data
1212

13-
if (params.get("type")) dataSort = dataSort.filter((item: { type: string }) => item.type == params.get("type")!.toUpperCase())
13+
if (searchParams.get("type")) {
14+
dataToBeSorted = dataToBeSorted.filter((media) => media.type == searchParams.get("type")!.toUpperCase())
15+
}
1416

15-
if (params.get("genre")) dataSort = dataSort.filter((item: { tags: string[] }) => item.tags.some(a => (params.get("genre")!.includes(a))))
17+
if (searchParams.get("year")) {
18+
dataToBeSorted = dataToBeSorted.filter((media) => media.animeSeason.year == Number(searchParams.get("year")))
19+
}
1620

17-
if (params.get("status")) dataSort = dataSort.filter((item: { status: string }) => item.status == params.get("status")!.toUpperCase())
21+
if (searchParams.get("genre")) {
22+
dataToBeSorted = dataToBeSorted.filter((media) => media.tags.some(genreName => (searchParams.get("genre")!.includes(genreName))))
23+
}
1824

19-
if (params.get("year")) dataSort = dataSort.filter((item: { animeSeason: { year: number } }) => item.animeSeason.year == Number(params.get("year")))
25+
if (searchParams.get("status")) {
26+
dataToBeSorted = dataToBeSorted.filter((media) => media.status == searchParams.get("status")!.toUpperCase())
27+
}
2028

21-
if (params.get("title")) dataSort = dataSort.filter((item: { title: string }) => item.title.toLowerCase().includes(params.get("title")!.toLowerCase()))
29+
if (searchParams.get("title")) {
30+
dataToBeSorted = dataToBeSorted.filter((media) => media.title.toLowerCase().includes(searchParams.get("title")!.toLowerCase()))
31+
}
2232

23-
if (params.get("season")) dataSort = dataSort.filter((item: { animeSeason: { season: string } }) =>
24-
item.animeSeason.season.toLocaleLowerCase() == params.get("season")?.toLocaleLowerCase()
25-
)
33+
if (searchParams.get("season")) {
34+
dataToBeSorted = dataToBeSorted.filter((media) =>
35+
media.animeSeason.season.toLocaleLowerCase() == searchParams.get("season")?.toLocaleLowerCase()
36+
)
37+
}
2638

27-
if (params.get("sort")) {
28-
if (params.get("sort") == "releases_desc") {
29-
dataSort = dataSort.sort(
30-
(a: { animeSeason: { year: number } }, b: { animeSeason: { year: number } }) => a.animeSeason.year - b.animeSeason.year
31-
).reverse()
32-
}
33-
else if (params.get("sort") == "releases_asc") {
34-
dataSort = dataSort.sort(
35-
(a: { animeSeason: { year: number } }, b: { animeSeason: { year: number } }) => a.animeSeason.year - b.animeSeason.year
36-
)
37-
}
39+
switch (searchParams.get("sort")) {
3840

39-
if (params.get("sort") == "title_desc") {
40-
dataSort = dataSort.sort(
41-
(a: { title: string }, b: { title: string }) => a.title > b.title ? -1 : 1
42-
)
43-
}
44-
else if (params.get("sort") == "title_asc") {
45-
dataSort = dataSort.sort(
46-
(a: { title: string }, b: { title: string }) => a.title > b.title ? -1 : 1
47-
).reverse()
48-
}
49-
}
41+
case "releases_desc":
42+
43+
dataToBeSorted = dataToBeSorted.sort((a, b) => a.animeSeason.year - b.animeSeason.year).reverse()
44+
45+
break
46+
47+
case "releases_asc":
5048

51-
const totalLength = dataSort.length
49+
dataToBeSorted = dataToBeSorted.sort((a, b) => a.animeSeason.year - b.animeSeason.year)
5250

53-
dataSort = dataSort.slice(0, resultLimit * Number(params.get("page") || 1))
51+
break
52+
53+
case "title_desc":
54+
55+
dataToBeSorted = dataToBeSorted.sort((a, b) => a.title > b.title ? -1 : 1)
56+
57+
break
58+
59+
case "title_asc":
60+
61+
dataToBeSorted = dataToBeSorted.sort((a, b) => a.title > b.title ? -1 : 1).reverse()
62+
63+
break
64+
65+
default:
66+
break
67+
68+
}
69+
70+
const totalResultsLength = dataToBeSorted.length
5471

5572
// GET ANILIST ID FOR EACH MEDIA
56-
if (dataSort) {
73+
if (dataToBeSorted.length > 0) {
5774

58-
let sortHasAnilistId
75+
const mediasWithAnilistId = dataToBeSorted.filter((media) => media.sources.map(source => {
5976

60-
sortHasAnilistId = dataSort.filter((item) => item.sources.map(a => {
77+
if (source.includes("https://anilist.co/anime")) {
6178

62-
if (a.includes("https://anilist.co/anime")) {
63-
const foundUrl: string | null = a.slice(a.search(/\banime\b/))
64-
item.anilistId = foundUrl!.slice(6)
65-
}
79+
const urlWithAnilistId = source.slice(source.search(/\banime\b/))
6680

67-
}))
81+
media.anilistId = urlWithAnilistId!.slice(6)
6882

69-
// filter only itens which has the anilist ID
70-
sortHasAnilistId = sortHasAnilistId.filter((item) => item.anilistId)
83+
}
84+
85+
})).filter((item) => item.anilistId)
7186

72-
dataSort = sortHasAnilistId
87+
dataToBeSorted = mediasWithAnilistId
7388

7489
}
7590

91+
const resultsLimitedByPage = dataToBeSorted.slice(0, resultsLimit * Number(searchParams.get("page") || 1))
92+
7693
return NextResponse.json(
7794
{
78-
data: dataSort,
79-
allResultsLength: totalLength,
95+
data: resultsLimitedByPage,
96+
allResultsLength: totalResultsLength,
8097
lastUpdate: (AnimeDataOffline as { lastUpdate: string }).lastUpdate
8198
}
8299
)

app/api/aniwatch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ axiosRetry(Axios, {
1717
export default {
1818

1919
// SEARCH MEDIA
20-
searchMedia: cache(async (query: string, page?: number) => {
20+
searchMedia: cache(async ({ query, page }: { query: string, page?: number }) => {
2121

2222
try {
2323

@@ -38,7 +38,7 @@ export default {
3838
}),
3939

4040
// GET EPISODES, NO LINKS INCLUDED
41-
getEpisodes: cache(async (episodeId: string) => {
41+
getEpisodes: cache(async ({ episodeId }: { episodeId: string }) => {
4242

4343
try {
4444

@@ -59,7 +59,7 @@ export default {
5959
}),
6060

6161
// GET EPISODES, NO LINKS INCLUDED
62-
episodesLinks: cache(async (episodeId: string, server?: string, category?: "dub" | "sub") => {
62+
episodesLinks: cache(async ({ episodeId, server, category }: { episodeId: string, server?: string, category?: "dub" | "sub" }) => {
6363

6464
try {
6565

app/api/consumetGoGoAnime.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ axiosRetry(Axios, {
2020
export default {
2121

2222
// SEARCH ANIME BY QUERY
23-
searchMedia: cache(async (query: string, page?: number) => {
23+
searchMedia: cache(async ({ query, page }: { query: string, page?: number }) => {
2424

2525
try {
2626

@@ -43,7 +43,7 @@ export default {
4343
}),
4444

4545
// GET ANIME INFO
46-
getInfoFromThisMedia: cache(async (id: string | number) => {
46+
getInfoFromThisMedia: cache(async ({ id }: { id: string | number }) => {
4747

4848
try {
4949

@@ -65,7 +65,7 @@ export default {
6565
}),
6666

6767
// GET EPISODES LINKS FOR ANIMES AND MOVIES
68-
getEpisodeStreamingLinks: cache(async (episodeId: string | number, serverName?: string) => {
68+
getEpisodeStreamingLinks: cache(async ({ episodeId, serverName }: { episodeId: string | number, serverName?: string }) => {
6969

7070
try {
7171
const { data } = await Axios({
@@ -86,7 +86,7 @@ export default {
8686
}),
8787

8888
// ALTERNATIVE: GET EPISODES LINKS FOR ANIMES AND MOVIES
89-
getEpisodeStreamingLinks2: cache(async (episodeId: string) => {
89+
getEpisodeStreamingLinks2: cache(async ({ episodeId }: { episodeId: string }) => {
9090

9191
try {
9292
const { data } = await Axios({

app/api/consumetImdb.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import stringToOnlyAlphabetic from "@/app/lib/convertStringsTo"
1+
import stringToOnlyAlphabetic from "@/app/lib/convertStrings"
22
import { ImdbMediaInfo, ImdbSearchItem } from "@/app/ts/interfaces/apiImdbInterface"
33
import Axios from "axios"
44
import axiosRetry from "axios-retry"
@@ -15,7 +15,7 @@ axiosRetry(Axios, {
1515
})
1616

1717
// SEARCH BY MEDIA TITLE
18-
export const searchMedia = cache(async (mediaTitle: string) => {
18+
export const searchMedia = cache(async ({ mediaTitle }: { mediaTitle: string }) => {
1919

2020
try {
2121

@@ -37,7 +37,13 @@ export const searchMedia = cache(async (mediaTitle: string) => {
3737
})
3838

3939
// GET INFO FOR THIS MEDIA
40-
export const getMediaInfo = cache(async (search: boolean, mediaId?: string, type?: "TV Series", seachTitle?: string, releaseYear?: number) => {
40+
export const getMediaInfo = cache(async ({ search, mediaId, type, seachTitle, releaseYear }: {
41+
search: boolean,
42+
mediaId?: string,
43+
type?: "TV Series",
44+
seachTitle?: string,
45+
releaseYear?: number
46+
}) => {
4147

4248
try {
4349

@@ -46,7 +52,7 @@ export const getMediaInfo = cache(async (search: boolean, mediaId?: string, type
4652

4753
if (search && seachTitle) {
4854

49-
const searchResults: ImdbSearchItem[] = await searchMedia(stringToOnlyAlphabetic(seachTitle)).then(res => res.results)
55+
const searchResults: ImdbSearchItem[] = await searchMedia({ mediaTitle: stringToOnlyAlphabetic(seachTitle) }).then(res => res.results)
5056

5157
const filteredRes = searchResults.find((item) => Number(item.releaseDate) == releaseYear)
5258

0 commit comments

Comments
 (0)