ensembledata 0.0.3 → 0.1.0

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.
package/src/errors.js ADDED
@@ -0,0 +1,19 @@
1
+ export const STATUS_520_POST_RESTRICTED = 520;
2
+ export const STATUS_500_INTERNAL_ERROR = 500;
3
+ export const STATUS_495_UNITS_DEPLETED = 495;
4
+ export const STATUS_493_SUBSCRIPTION_EXPIRED = 493;
5
+ export const STATUS_492_UNVERIFIED_EMAIL = 492;
6
+ export const STATUS_491_INVALID_TOKEN = 491;
7
+ export const STATUS_474_PROFILE_UNAVAILABLE = 474;
8
+ export const STATUS_473_USER_NOT_FOUND = 473;
9
+ export const STATUS_472_PRIVATE_USER = 472;
10
+ export const STATUS_471_USER_RESTRICTED = 471;
11
+ export const STATUS_469_TOPIC_RESTRICTED = 469;
12
+ export const STATUS_466_INVALID_COUNTRY_CODE = 466;
13
+ export const STATUS_465_INVALID_YOUTUBE_URL = 465;
14
+ export const STATUS_464_MAX_POST_IDS = 464;
15
+ export const STATUS_463_INVALID_USER = 463;
16
+ export const STATUS_462_INVALID_TIKTOK_URL = 462;
17
+ export const STATUS_429_RATE_LIMIT_EXCEEDED = 429;
18
+ export const STATUS_422_VALIDATION_ERROR = 422;
19
+ export const STATUS_404_NOT_FOUND = 404;
package/src/index.js CHANGED
@@ -1 +1,3 @@
1
1
  export { EDClient } from "./client.js";
2
+ export { EDError } from "./requester.js";
3
+ export * as errors from "./errors.js";
package/src/requester.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { VERSION } from "./version.js";
2
+
1
3
  const BASE_URL = "https://ensembledata.com/apis";
2
4
 
3
5
  export class Requester {
@@ -7,22 +9,66 @@ export class Requester {
7
9
  */
8
10
  #token;
9
11
 
10
- /** @param {{ token: string }} options */
11
- constructor({ token }) {
12
+ /**
13
+ * @type {number}
14
+ * @readonly
15
+ */
16
+ #timeoutSecs;
17
+
18
+ /**
19
+ * @type {number}
20
+ * @readonly
21
+ */
22
+ #maxNetworkRetries;
23
+
24
+ /**
25
+ * @param {{
26
+ * token: string;
27
+ * timeoutSecs: number;
28
+ * maxNetworkRetries: number;
29
+ * }} options
30
+ */
31
+ constructor({ token, timeoutSecs, maxNetworkRetries }) {
12
32
  this.#token = token;
33
+ this.#timeoutSecs = timeoutSecs;
34
+ this.#maxNetworkRetries = maxNetworkRetries;
13
35
  }
14
36
 
15
37
  /**
16
38
  * @param {string} path
17
39
  * @param {Record<string, any>} params
18
- * @param {boolean} returnTopLevelData
40
+ * @param {{ timeoutSecs?: number; returnTopLevelData?: boolean }} options
19
41
  * @returns {Promise<EDResponse>}
20
42
  */
21
- async get(path, params, returnTopLevelData = false) {
43
+ async get(path, params, options = {}) {
22
44
  params = { ...params, token: this.#token };
23
45
  const queryParams = new URLSearchParams(params).toString();
24
- return fetch(`${BASE_URL}/${path}?${queryParams}`).then((res) =>
25
- this.handle_response(res, returnTopLevelData),
46
+ const timeoutMS =
47
+ (options.timeoutSecs === undefined
48
+ ? this.#timeoutSecs
49
+ : options.timeoutSecs) * 1_000;
50
+ const returnTopLevelData = options.returnTopLevelData || false;
51
+
52
+ for (let i = 0; i < this.#maxNetworkRetries; i++) {
53
+ try {
54
+ const res = await fetch(`${BASE_URL}/${path}?${queryParams}`, {
55
+ signal: AbortSignal.timeout(timeoutMS),
56
+ headers: {
57
+ "User-Agent": `ensembledata-node/${VERSION}`,
58
+ },
59
+ });
60
+ return this.handle_response(res, returnTopLevelData);
61
+ } catch (/** @type {any} */ e) {
62
+ if (e.name === "TimeoutError") {
63
+ throw new Error(
64
+ "Request to EnsembleData timed out. Contact support if this issue persists.",
65
+ );
66
+ }
67
+ }
68
+ }
69
+
70
+ throw new Error(
71
+ `Failed to fetch data after ${this.#maxNetworkRetries} attempts.`,
26
72
  );
27
73
  }
28
74
 
@@ -64,7 +110,7 @@ export class EDResponse {
64
110
  }
65
111
  }
66
112
 
67
- class EDError extends Error {
113
+ export class EDError extends Error {
68
114
  /**
69
115
  * @param {number} statusCode
70
116
  * @param {string} detail
package/src/version.js ADDED
@@ -0,0 +1 @@
1
+ export const VERSION = "0.1.0";