@discordanalytics/core 2.6.1 → 2.7.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/dist/index.js +18 -51
- package/package.json +1 -4
package/dist/index.js
CHANGED
|
@@ -10,35 +10,12 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
10
10
|
if (k2 === undefined) k2 = k;
|
|
11
11
|
o[k2] = m[k];
|
|
12
12
|
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
36
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
37
15
|
};
|
|
38
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
17
|
exports.CustomEvent = exports.AnalyticsBase = void 0;
|
|
40
18
|
const types_1 = require("./types");
|
|
41
|
-
const node_fetch_1 = __importStar(require("node-fetch"));
|
|
42
19
|
/**
|
|
43
20
|
* DiscordAnalytics Base Class
|
|
44
21
|
* @class AnalyticsBase
|
|
@@ -155,37 +132,27 @@ class AnalyticsBase {
|
|
|
155
132
|
* @param body The body to send (optional)
|
|
156
133
|
* @param max_retries The maximum number of retries (default: 5)
|
|
157
134
|
* @param backoff_factor The backoff factor to use (default: 0.5)
|
|
158
|
-
* @returns {Promise<void |
|
|
135
|
+
* @returns {Promise<void | Response>} The response from the API
|
|
159
136
|
*/
|
|
160
137
|
async api_call_with_retries(method, url, body, max_retries = 5, backoff_factor = 0.5) {
|
|
161
138
|
let retries = 0;
|
|
162
139
|
let response;
|
|
163
140
|
while (retries < max_retries) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
return this.error(`[DISCORDANALYTICS] ${types_1.ErrorCodes.INVALID_RESPONSE}`);
|
|
180
|
-
}
|
|
181
|
-
catch (error) {
|
|
182
|
-
retries++;
|
|
183
|
-
const retry_after = Math.pow(2, retries) * backoff_factor;
|
|
184
|
-
this.error(`[DISCORDANALYTICS] Error: ${error}. Retrying in ${retry_after} seconds...`);
|
|
185
|
-
if (retries >= max_retries)
|
|
186
|
-
return this.error(`[DISCORDANALYTICS] ${types_1.ErrorCodes.MAX_RETRIES_EXCEEDED}`);
|
|
187
|
-
await new Promise((resolve) => setTimeout(resolve, retry_after * 1000));
|
|
188
|
-
}
|
|
141
|
+
response = await fetch(url, {
|
|
142
|
+
method,
|
|
143
|
+
headers: this._headers,
|
|
144
|
+
body,
|
|
145
|
+
});
|
|
146
|
+
if (response.ok)
|
|
147
|
+
return response;
|
|
148
|
+
else if (response.status === 401)
|
|
149
|
+
return this.error(`[DISCORDANALYTICS] ${types_1.ErrorCodes.INVALID_API_TOKEN}`);
|
|
150
|
+
else if (response.status === 423)
|
|
151
|
+
return this.error(`[DISCORDANALYTICS] ${types_1.ErrorCodes.SUSPENDED_BOT}`);
|
|
152
|
+
else if (response.status === 404 && url.includes('events'))
|
|
153
|
+
return this.error(`[DISCORDANALYTICS] ${types_1.ErrorCodes.INVALID_EVENT_KEY}`, true);
|
|
154
|
+
else if (response.status !== 200)
|
|
155
|
+
return this.error(`[DISCORDANALYTICS] ${types_1.ErrorCodes.INVALID_RESPONSE}`);
|
|
189
156
|
}
|
|
190
157
|
}
|
|
191
158
|
/**
|
|
@@ -250,11 +217,11 @@ class CustomEvent {
|
|
|
250
217
|
this.ensure();
|
|
251
218
|
}
|
|
252
219
|
async ensure() {
|
|
253
|
-
if (typeof this._analytics.stats_data.custom_events[this._event_key] !== 'number') {
|
|
220
|
+
if (typeof this._analytics.stats_data.custom_events[this._event_key] !== 'number' && process.env.NODE_ENV === 'production') {
|
|
254
221
|
this._analytics.debug(`[DISCORDANALYTICS] Fetching value for event ${this._event_key}`);
|
|
255
222
|
const url = types_1.ApiEndpoints.EVENT_URL.replace(':id', this._analytics.client_id).replace(':event', this._event_key);
|
|
256
223
|
const res = await this._analytics.api_call_with_retries('GET', url);
|
|
257
|
-
if (res instanceof
|
|
224
|
+
if (res instanceof Response && this._last_action !== 'set') {
|
|
258
225
|
const data = await res.json();
|
|
259
226
|
this._analytics.stats_data.custom_events[this._event_key] = (this._analytics.stats_data.custom_events[this._event_key] || 0) + (data.today_value || 0);
|
|
260
227
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discordanalytics/core",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "Core package to work with Discord Analytics",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"author": "Discord Analytics",
|
|
@@ -13,9 +13,6 @@
|
|
|
13
13
|
"bugs": {
|
|
14
14
|
"url": "https://github.com/DiscordAnalytics/node-package/issues"
|
|
15
15
|
},
|
|
16
|
-
"dependencies": {
|
|
17
|
-
"node-fetch": "^3.0.0"
|
|
18
|
-
},
|
|
19
16
|
"devDependencies": {
|
|
20
17
|
"@types/node": "*",
|
|
21
18
|
"typescript": "*"
|