@irfanshadikrishad/anilist 1.0.0-forbidden.0 → 1.0.0-forbidden.2

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.
@@ -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 } 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,37 @@ 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...");
26
+ var _a, _b;
27
+ try {
28
+ const headers = {
29
+ "content-type": "application/json",
30
+ };
31
+ if (yield Auth.isLoggedIn()) {
32
+ headers["Authorization"] = `Bearer ${yield Auth.RetriveAccessToken()}`;
33
+ }
34
+ const request = yield fetch(aniListEndpoint, {
35
+ method: "POST",
36
+ headers: headers,
37
+ body: JSON.stringify({ query, variables }),
38
+ });
39
+ const response = yield request.json();
40
+ if (request.status === 200) {
41
+ return response;
42
+ }
43
+ else if (request.status === 429) {
44
+ console.warn("Rate limit reached. Retrying in 1 minute...");
43
45
  yield new Promise((resolve) => setTimeout(resolve, 60000)); // Wait for 1 minute
44
- return fetcher(query, variables); // Retry the request
46
+ return yield fetcher(query, variables); // Retry the request
45
47
  }
46
48
  else {
47
- throw new Error(`\nError fetching data: ${response.statusText}`);
49
+ 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"}.`);
50
+ return null;
48
51
  }
49
52
  }
50
- const data = yield response.json();
51
- return data;
53
+ catch (error) {
54
+ console.error(`\nSomething went wrong. ${error.message}.`);
55
+ return null;
56
+ }
52
57
  });
53
58
  }
54
59
  export { fetcher };
@@ -19,4 +19,7 @@ declare class MyAnimeList {
19
19
  static exportAnime(): Promise<void>;
20
20
  static exportManga(): Promise<void>;
21
21
  }
22
- export { AniList, MyAnimeList };
22
+ declare class AniDB {
23
+ static importAnime(): Promise<void>;
24
+ }
25
+ export { AniDB, AniList, MyAnimeList };