@irfanshadikrishad/anilist 1.1.0-forbidden.0 → 1.1.0-forbidden.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,11 +2,12 @@
2
2
  * Sends a GraphQL request to the AniList API.
3
3
  *
4
4
  * This function constructs a request with the provided query and variables,
5
- * handles authorization, and processes the API response.
5
+ * handles authorization, and processes the API response. If a rate-limit error (429) is returned,
6
+ * it waits for 1 minute and retries the request.
6
7
  *
7
8
  * @param {string} query - The AniList GraphQL query to be executed.
8
9
  * @param {object} variables - An object containing the variables for the query.
9
10
  * @returns {Promise<object|null>} The response from the API as a JSON object if successful; otherwise, null.
10
11
  */
11
- declare function fetcher(query: string, variables?: object): Promise<any | null>;
12
+ declare function fetcher(query: string, variables: object): Promise<object | null>;
12
13
  export { fetcher };
@@ -9,11 +9,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  import fetch from "node-fetch";
11
11
  import { Auth } from "./auth.js";
12
+ import { aniListEndpoint, handleRateLimitRetry } from "./workers.js";
12
13
  /**
13
14
  * Sends a GraphQL request to the AniList API.
14
15
  *
15
16
  * This function constructs a request with the provided query and variables,
16
- * handles authorization, and processes the API response.
17
+ * handles authorization, and processes the API response. If a rate-limit error (429) is returned,
18
+ * it waits for 1 minute and retries the request.
17
19
  *
18
20
  * @param {string} query - The AniList GraphQL query to be executed.
19
21
  * @param {object} variables - An object containing the variables for the query.
@@ -21,34 +23,38 @@ import { Auth } from "./auth.js";
21
23
  */
22
24
  function fetcher(query, variables) {
23
25
  return __awaiter(this, void 0, void 0, function* () {
24
- const headers = {
25
- "content-type": "application/json",
26
- };
27
- if (yield Auth.isLoggedIn()) {
28
- headers["Authorization"] = `Bearer ${yield Auth.RetriveAccessToken()}`;
29
- }
30
- const response = yield fetch("https://graphql.anilist.co", {
31
- method: "POST",
32
- headers: headers,
33
- body: JSON.stringify({
34
- query,
35
- variables,
36
- }),
37
- });
38
- // Check if the response is successful
39
- if (response.status !== 200) {
40
- // If the status is 429, handle the rate limit
41
- if (response.status === 429) {
42
- console.warn("Rate limit hit. Waiting for 1 minute before retrying...");
43
- yield new Promise((resolve) => setTimeout(resolve, 60000)); // Wait for 1 minute
44
- return fetcher(query, variables); // Retry the request
26
+ var _a, _b;
27
+ try {
28
+ const headers = {
29
+ "content-type": "application/json",
30
+ };
31
+ const token = (yield Auth.isLoggedIn())
32
+ ? yield Auth.RetriveAccessToken()
33
+ : null;
34
+ if (token)
35
+ headers["Authorization"] = `Bearer ${token}`;
36
+ const request = yield fetch(aniListEndpoint, {
37
+ method: "POST",
38
+ headers: headers,
39
+ body: JSON.stringify({ query, variables }),
40
+ });
41
+ const response = yield request.json();
42
+ if (request.status === 200) {
43
+ return response;
44
+ }
45
+ else if (request.status === 429) {
46
+ yield handleRateLimitRetry(60);
47
+ return yield fetcher(query, variables);
45
48
  }
46
49
  else {
47
- throw new Error(`\nError fetching data: ${response.statusText}`);
50
+ console.error(`\n${request.status} ${((_b = (_a = response === null || response === void 0 ? void 0 : response.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message) || "Unknown error"}.`);
51
+ return null;
48
52
  }
49
53
  }
50
- const data = yield response.json();
51
- return data;
54
+ catch (error) {
55
+ console.error(`\nSomething went wrong. ${error.message}.`);
56
+ return null;
57
+ }
52
58
  });
53
59
  }
54
60
  export { fetcher };
@@ -10,6 +10,7 @@ declare class AniList {
10
10
  static getUpcomingAnime(count: number): Promise<void>;
11
11
  static getUserByUsername(username: string): Promise<void>;
12
12
  static getAnimeDetailsByID(anilistID: number): Promise<void>;
13
+ static getMangaDetailsByID(mangaID: number): Promise<void>;
13
14
  static searchAnime(search: string, count: number): Promise<void>;
14
15
  static searchManga(search: string, count: number): Promise<void>;
15
16
  }
@@ -19,4 +20,7 @@ declare class MyAnimeList {
19
20
  static exportAnime(): Promise<void>;
20
21
  static exportManga(): Promise<void>;
21
22
  }
22
- export { AniList, MyAnimeList };
23
+ declare class AniDB {
24
+ static importAnime(): Promise<void>;
25
+ }
26
+ export { AniDB, AniList, MyAnimeList };