-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.ts
126 lines (109 loc) · 3.36 KB
/
index.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
import { MalAcount } from "..";
import { MalRequest } from "../request";
import { apiUrl } from "../api";
import { MangaFields, MangaDetailsFields } from "./fields";
import { MangaItem, UpdateMangaParams, MangaListStatusBase } from "./types";
import { WorkBase, Paging, RankingItem } from "../common";
import { queryEncode } from "../util";
import { AxiosRequestConfig } from "axios";
export * from "./fields";
export * from "./types";
export class MalManga {
private acount: MalAcount;
constructor(acount: MalAcount) {
this.acount = acount;
}
search<T>(
q: string,
fields?: MangaFields<T> | null,
limit?: number | null,
offset?: number | null,
nsfw?: boolean | null
): MalRequest<Paging<MangaItem<WorkBase & T>>> {
const config: AxiosRequestConfig = {
url: [apiUrl, "manga"].join("/"),
headers: this.acount.getHttpHeaders(),
params: {
q,
},
};
if (fields != null) config.params.fields = fields.toString();
if (limit != null) config.params.limit = limit;
if (offset != null) config.params.offset = offset;
if (nsfw != null) config.params.nsfw = nsfw;
return new MalRequest<any>(config);
}
details<T>(id: number, fields?: MangaDetailsFields<WorkBase & T>) {
const config: AxiosRequestConfig = {
url: [apiUrl, "manga", id.toString()].join("/"),
headers: this.acount.getHttpHeaders(),
params: {},
};
if (fields) config.params.fields = fields.toString();
return new MalRequest<any>(config);
}
/**
* | value | |
* | ---- | ---- |
* | all | Top Anime Series |
* | airing | Top Airing Anime |
* | upcoming | Top Upcoming Anime |
* | tv | Top Anime TV Series |
* | ova | Top Anime OVA Series |
* | movie | Top Anime Movies |
* | special | Top Anime Specials |
* | bypopularity | Top Anime by Popularity |
* | favorite | Top Favorited Anime |
*/
ranking<T>(
rankingType:
| "all"
| "airing"
| "upcoming"
| "tv"
| "ova"
| "movie"
| "special"
| "bypopularity"
| "favorite",
fields?: MangaFields<T> | null,
limit?: number | null,
offset?: number | null
): MalRequest<Paging<RankingItem & MangaItem<WorkBase & T>>> {
const config: AxiosRequestConfig = {
url: [apiUrl, "manga", "ranking"].join("/"),
headers: this.acount.getHttpHeaders(),
params: {
ranking_type: rankingType,
},
};
if (fields != null) config.params.fields = fields.toString();
if (limit != null) config.params.limit = limit;
if (offset != null) config.params.offset = offset;
return new MalRequest<any>(config);
}
updateMyManga(
id: number,
params?: UpdateMangaParams
): MalRequest<MangaListStatusBase> {
const config: AxiosRequestConfig = {
method: "PATCH",
url: [apiUrl, "manga", id.toString(), "my_list_status"].join("/"),
headers: {
...this.acount.getHttpHeaders(),
"content-type": "application/x-www-form-urlencoded",
},
params: {},
data: queryEncode(params),
};
return new MalRequest<any>(config);
}
deleteMyManga(id: number): MalRequest<any[]> {
const config: AxiosRequestConfig = {
method: "DELETE",
url: [apiUrl, "manga", id.toString(), "my_list_status"].join("/"),
headers: this.acount.getHttpHeaders(),
};
return new MalRequest<any[]>(config);
}
}