forked from ker0olos/fable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
256 lines (230 loc) · 5.61 KB
/
api.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { gql, request } from './graphql.ts';
import { AniListCharacter, AniListMedia } from './types.ts';
import { Character, CharacterRole, Media } from '../../src/types.ts';
const url = 'https://graphql.anilist.co';
/** Order by trending than popularity */
const mediaDefaultSort = gql`[ TRENDING_DESC, POPULARITY_DESC ]`;
/** Order manually decided by anilist moderators */
const characterDefaultSort = gql`[ RELEVANCE, ROLE ]`;
const mediaDefaultQuery = gql`
id
type
format
popularity
description
title {
romaji
english
native
}
genres
synonyms
isAdult
coverImage {
extraLarge
}
externalLinks {
site
url
}
trailer {
id
site
}
tags {
name
}
`;
const characterDefaultQuery = gql`
id
age
gender
description
name {
full
native
alternative
}
image {
large
}
`;
export function transform<T>(
{ item }: { item: AniListMedia | AniListCharacter },
): T {
if ('title' in item) {
const t: Media = {
...item,
packId: 'anilist',
title: {
english: item.title?.english,
romaji: item.title?.romaji,
native: item.title?.native,
alternative: item.synonyms,
},
images: item.coverImage?.extraLarge &&
!item.coverImage?.extraLarge.endsWith('default.jpg')
? [{
url: item.coverImage.extraLarge,
}]
: undefined,
relations: undefined,
characters: undefined,
};
if (item.relations?.edges?.length) {
t.relations = {
edges: item.relations.edges
.map((edge) => ({
relation: edge.relationType,
node: transform({ item: edge.node as AniListMedia }),
})),
};
}
if (item.characters?.edges?.length) {
t.characters = {
// used for query media character
// deno-lint-ignore ban-ts-comment
// @ts-ignore
pageInfo: item.characters.pageInfo,
edges: item.characters.edges
.map((edge) => ({
role: edge.role,
node: transform({ item: edge.node as AniListCharacter }),
})),
};
}
// get rid of undefined values
return JSON.parse(JSON.stringify(t)) as T;
} else if ('name' in item) {
const t: Character = {
...item,
packId: 'anilist',
name: {
english: item.name?.full,
native: item.name?.native,
alternative: item.name?.alternative,
},
images: item.image?.large &&
!item.image?.large.endsWith('default.jpg')
? [{
url: item.image.large,
}]
: undefined,
media: undefined,
};
if (item.media?.edges?.length) {
t.media = {
edges: item.media.edges
.map((edge) => ({
role: edge.characterRole,
node: transform({ item: edge.node as AniListMedia }),
})),
};
if (t.media.edges.length) {
let index = 0;
// check if a primary media swap is required
t.media.edges.forEach((e, i) => {
if (
// ignore background roles
(t.media?.edges[index].role === CharacterRole.Background) ||
// sort by popularity
(
e.role !== CharacterRole.Background &&
// deno-lint-ignore no-non-null-assertion
e.node.popularity! > t.media!.edges[index].node.popularity!
)
) {
index = i;
}
});
// swap position
const _ = t.media.edges[0];
t.media.edges[0] = t.media.edges[index];
t.media.edges[index] = _;
}
}
Object.keys(t).forEach((key) =>
t[key as keyof Character] === undefined &&
delete t[key as keyof Character]
);
// get rid of undefined values
return JSON.parse(JSON.stringify(t)) as T;
}
return item as T;
}
export async function media(
variables: {
ids?: number[];
search?: string;
perPage?: number;
page?: number;
},
): Promise<AniListMedia[]> {
if (!variables.search && !variables.ids?.length) {
return [];
}
const query = gql`
query ($ids: [Int], $search: String) {
Page {
media(search: $search, id_in: $ids, sort: ${mediaDefaultSort}, isAdult: false) {
${mediaDefaultQuery}
relations {
edges {
node { ${mediaDefaultQuery} }
relationType
}
}
characters(
sort: ${characterDefaultSort},
page: ${variables.page ?? 1},
perPage: ${variables.perPage ?? 25}
) {
pageInfo {
hasNextPage
}
edges {
node { ${characterDefaultQuery} }
role
}
}
}
}
}
`;
const data: {
Page: {
media: AniListMedia[];
};
} = await request({ url, query, variables });
return data.Page.media;
}
export async function characters(
variables: { ids?: number[]; search?: string },
): Promise<AniListCharacter[]> {
if (!variables.search && !variables.ids?.length) {
return [];
}
const query = gql`
query ($ids: [Int], $search: String) {
Page {
# match the search query
characters(search: $search, id_in: $ids, sort: [ SEARCH_MATCH ]) {
${characterDefaultQuery}
media(sort: POPULARITY_DESC) {
edges {
node { ${mediaDefaultQuery} }
characterRole
}
}
}
}
}
`;
const data: {
Page: {
characters: AniListCharacter[];
};
} = await request({ url, query, variables });
return data.Page.characters
.filter((character) => !character.media?.edges?.[0]?.node?.isAdult);
}