ensembledata 0.0.3

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/index.js ADDED
@@ -0,0 +1 @@
1
+ export { EDClient } from "./client.js";
@@ -0,0 +1,85 @@
1
+ const BASE_URL = "https://ensembledata.com/apis";
2
+
3
+ export class Requester {
4
+ /**
5
+ * @type {string}
6
+ * @readonly
7
+ */
8
+ #token;
9
+
10
+ /** @param {{ token: string }} options */
11
+ constructor({ token }) {
12
+ this.#token = token;
13
+ }
14
+
15
+ /**
16
+ * @param {string} path
17
+ * @param {Record<string, any>} params
18
+ * @param {boolean} returnTopLevelData
19
+ * @returns {Promise<EDResponse>}
20
+ */
21
+ async get(path, params, returnTopLevelData = false) {
22
+ params = { ...params, token: this.#token };
23
+ const queryParams = new URLSearchParams(params).toString();
24
+ return fetch(`${BASE_URL}/${path}?${queryParams}`).then((res) =>
25
+ this.handle_response(res, returnTopLevelData),
26
+ );
27
+ }
28
+
29
+ /**
30
+ * @param {Response} response
31
+ * @param {boolean} returnTopLevelData
32
+ * @returns {Promise<EDResponse>}
33
+ */
34
+ async handle_response(response, returnTopLevelData) {
35
+ const json = await response.json();
36
+ const unitsCharged = Number(response.headers.get("units_charged"));
37
+
38
+ if ("data" in json) {
39
+ if (returnTopLevelData) {
40
+ return new EDResponse(response.status, json, unitsCharged);
41
+ }
42
+
43
+ return new EDResponse(response.status, json.data, unitsCharged);
44
+ }
45
+ throw new EDError(response.status, json.detail, unitsCharged);
46
+ }
47
+ }
48
+
49
+ export class EDResponse {
50
+ /**
51
+ * @param {number} statusCode
52
+ * @param {any} data
53
+ * @param {number} unitsCharged
54
+ */
55
+ constructor(statusCode, data, unitsCharged) {
56
+ /** @type {number} */
57
+ this.statusCode = statusCode;
58
+
59
+ /** @type {any} */
60
+ this.data = data;
61
+
62
+ /** @type {number} */
63
+ this.unitsCharged = unitsCharged;
64
+ }
65
+ }
66
+
67
+ class EDError extends Error {
68
+ /**
69
+ * @param {number} statusCode
70
+ * @param {string} detail
71
+ * @param {number} unitsCharged
72
+ */
73
+ constructor(statusCode, detail, unitsCharged) {
74
+ super(detail);
75
+
76
+ /** @type {string} */
77
+ this.detail = detail;
78
+
79
+ /** @type {number} */
80
+ this.statusCode = statusCode;
81
+
82
+ /** @type {number} */
83
+ this.unitsCharged = unitsCharged;
84
+ }
85
+ }
package/src/utils.js ADDED
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @param {string} str
3
+ * @returns {string}
4
+ */
5
+ function toSnakeCase(str) {
6
+ return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
7
+ }
8
+
9
+ /**
10
+ * @param {Record<string, any>} obj
11
+ * @returns {Record<string, any>}
12
+ */
13
+ export function toSnakeCaseObject(obj) {
14
+ /** @type {Record<string, any>} */
15
+ const newObj = {};
16
+ for (let key in obj) {
17
+ const snakeKey = toSnakeCase(key);
18
+ newObj[snakeKey] = obj[key];
19
+ }
20
+ return newObj;
21
+ }
22
+
23
+ /**
24
+ * @param {Record<string, any>} obj
25
+ * @returns {Record<string, any>}
26
+ */
27
+ export function filterUndefinedValues(obj) {
28
+ /** @type {Record<string, any>} */
29
+ const newObj = {};
30
+ for (let key in obj) {
31
+ if (obj[key] !== undefined) {
32
+ newObj[key] = obj[key];
33
+ }
34
+ }
35
+ return newObj;
36
+ }