ensembledata 0.0.3 → 0.1.1
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/LICENCE +9 -0
- package/README.md +40 -8
- package/package.json +6 -3
- package/src/client.js +310 -207
- package/src/errors.js +19 -0
- package/src/index.js +2 -0
- package/src/requester.js +52 -7
- package/src/version.js +1 -0
- package/types/client.d.ts +261 -106
- package/types/client.d.ts.map +1 -1
- package/types/errors.d.ts +20 -0
- package/types/errors.d.ts.map +1 -0
- package/types/index.d.ts +2 -0
- package/types/requester.d.ts +29 -4
- package/types/requester.d.ts.map +1 -1
- package/types/version.d.ts +2 -0
- package/types/version.d.ts.map +1 -0
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
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,65 @@ export class Requester {
|
|
|
7
9
|
*/
|
|
8
10
|
#token;
|
|
9
11
|
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @type {number}
|
|
14
|
+
* @readonly
|
|
15
|
+
*/
|
|
16
|
+
#timeout;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @type {number}
|
|
20
|
+
* @readonly
|
|
21
|
+
*/
|
|
22
|
+
#maxNetworkRetries;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {{
|
|
26
|
+
* token: string;
|
|
27
|
+
* timeout: number;
|
|
28
|
+
* maxNetworkRetries: number;
|
|
29
|
+
* }} options
|
|
30
|
+
*/
|
|
31
|
+
constructor({ token, timeout, maxNetworkRetries }) {
|
|
12
32
|
this.#token = token;
|
|
33
|
+
this.#timeout = timeout;
|
|
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}
|
|
40
|
+
* @param {{ timeout?: number; returnTopLevelData?: boolean }} options
|
|
19
41
|
* @returns {Promise<EDResponse>}
|
|
20
42
|
*/
|
|
21
|
-
async get(path, params,
|
|
43
|
+
async get(path, params, options = {}) {
|
|
22
44
|
params = { ...params, token: this.#token };
|
|
23
45
|
const queryParams = new URLSearchParams(params).toString();
|
|
24
|
-
|
|
25
|
-
this.
|
|
46
|
+
const timeoutMS =
|
|
47
|
+
(options.timeout === undefined ? this.#timeout : options.timeout) *
|
|
48
|
+
1_000;
|
|
49
|
+
const returnTopLevelData = options.returnTopLevelData || false;
|
|
50
|
+
|
|
51
|
+
for (let i = 0; i < this.#maxNetworkRetries; i++) {
|
|
52
|
+
try {
|
|
53
|
+
const res = await fetch(`${BASE_URL}/${path}?${queryParams}`, {
|
|
54
|
+
signal: AbortSignal.timeout(timeoutMS),
|
|
55
|
+
headers: {
|
|
56
|
+
"User-Agent": `ensembledata-node/${VERSION}`,
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
return this.handle_response(res, returnTopLevelData);
|
|
60
|
+
} catch (/** @type {any} */ e) {
|
|
61
|
+
if (e.name === "TimeoutError") {
|
|
62
|
+
throw new Error(
|
|
63
|
+
"Request to EnsembleData timed out. Contact support if this issue persists.",
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
throw new Error(
|
|
70
|
+
`Failed to fetch data after ${this.#maxNetworkRetries} attempts.`,
|
|
26
71
|
);
|
|
27
72
|
}
|
|
28
73
|
|
|
@@ -64,7 +109,7 @@ export class EDResponse {
|
|
|
64
109
|
}
|
|
65
110
|
}
|
|
66
111
|
|
|
67
|
-
class EDError extends Error {
|
|
112
|
+
export class EDError extends Error {
|
|
68
113
|
/**
|
|
69
114
|
* @param {number} statusCode
|
|
70
115
|
* @param {string} detail
|
package/src/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const VERSION = "0.1.1";
|