hive-bedrock-api 4.0.1 → 4.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/lib/index.js CHANGED
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
3
  if (k2 === undefined) k2 = k;
15
4
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -24,283 +13,154 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
24
13
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
25
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
26
15
  };
27
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
28
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
29
- return new (P || (P = Promise))(function (resolve, reject) {
30
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
31
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
32
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
33
- step((generator = generator.apply(thisArg, _arguments || [])).next());
34
- });
35
- };
36
- var __generator = (this && this.__generator) || function (thisArg, body) {
37
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
38
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
39
- function verb(n) { return function (v) { return step([n, v]); }; }
40
- function step(op) {
41
- if (f) throw new TypeError("Generator is already executing.");
42
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
43
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
44
- if (y = 0, t) op = [op[0] & 2, t.value];
45
- switch (op[0]) {
46
- case 0: case 1: t = op; break;
47
- case 4: _.label++; return { value: op[1], done: false };
48
- case 5: _.label++; y = op[1]; op = [0]; continue;
49
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
50
- default:
51
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
52
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
53
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
54
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
55
- if (t[2]) _.ops.pop();
56
- _.trys.pop(); continue;
57
- }
58
- op = body.call(thisArg, _);
59
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
60
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
61
- }
62
- };
63
- var __read = (this && this.__read) || function (o, n) {
64
- var m = typeof Symbol === "function" && o[Symbol.iterator];
65
- if (!m) return o;
66
- var i = m.call(o), r, ar = [], e;
67
- try {
68
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
69
- }
70
- catch (error) { e = { error: error }; }
71
- finally {
72
- try {
73
- if (r && !r.done && (m = i["return"])) m.call(i);
74
- }
75
- finally { if (e) throw e.error; }
76
- }
77
- return ar;
78
- };
79
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
80
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
81
18
  };
82
19
  Object.defineProperty(exports, "__esModule", { value: true });
83
- var hive_bedrock_data_1 = require("hive-bedrock-data");
84
- var player_1 = __importDefault(require("./processors/player"));
85
- var player_search_1 = __importDefault(require("./processors/player_search"));
86
- var game_1 = require("./processors/game");
20
+ const hive_bedrock_data_1 = require("hive-bedrock-data");
21
+ const player_1 = __importDefault(require("./processors/player"));
22
+ const player_search_1 = __importDefault(require("./processors/player_search"));
23
+ const game_1 = require("./processors/game");
87
24
  __exportStar(require("./processors/game"), exports);
88
25
  __exportStar(require("./processors/global_statistics"), exports);
89
26
  __exportStar(require("./processors/map"), exports);
90
27
  __exportStar(require("./processors/meta"), exports);
91
28
  __exportStar(require("./processors/player"), exports);
92
29
  __exportStar(require("./processors/player_search"), exports);
93
- var HiveAPI = /** @class */ (function () {
94
- function HiveAPI(options) {
95
- if (options === void 0) { options = {}; }
96
- var _a;
30
+ class HiveAPI {
31
+ options;
32
+ constructor(options = {}) {
97
33
  this.options = options;
98
34
  this.options = options;
99
- this.options.apiBaseEndpoint = (_a = this.options.apiBaseEndpoint) !== null && _a !== void 0 ? _a : hive_bedrock_data_1.API_BASE_ENDPOINT;
35
+ this.options.apiBaseEndpoint = this.options.apiBaseEndpoint ?? hive_bedrock_data_1.API_BASE_ENDPOINT;
100
36
  }
101
- HiveAPI.prototype._fetchAPI = function (endpoint) {
102
- return __awaiter(this, void 0, void 0, function () {
103
- var url, start, request, meta, response, err_1;
104
- var _a, _b;
105
- return __generator(this, function (_c) {
106
- switch (_c.label) {
107
- case 0:
108
- url = this.options.apiBaseEndpoint + endpoint;
109
- _c.label = 1;
110
- case 1:
111
- _c.trys.push([1, 4, , 5]);
112
- start = performance.now();
113
- return [4 /*yield*/, fetch(url, __assign(__assign({}, this.options.requestInit), { headers: __assign({ "X-Hive-Resolve-Stat-Track": ((_a = this.options.resolveDynamicTitles) !== null && _a !== void 0 ? _a : true).toString(), "X-Hive-Leaderboard-Source": this.options.useModernLeaderboardSource ? "modern" : "legacy" }, (_b = this.options.requestInit) === null || _b === void 0 ? void 0 : _b.headers) }))];
114
- case 2:
115
- request = _c.sent();
116
- if (!request.ok)
117
- return [2 /*return*/, { data: null, error: { status: request.status } }];
118
- meta = {
119
- duration: Math.ceil(performance.now() - start),
120
- ratelimit: {
121
- limit: Number(request.headers.get("X-Ratelimit-Limit")),
122
- remaining: Number(request.headers.get("X-Ratelimit-Remaining")),
123
- reset: Number(request.headers.get("X-Ratelimit-Reset")),
124
- },
125
- retryAfter: Number(request.headers.get("Retry-After")),
126
- };
127
- return [4 /*yield*/, request.json()];
128
- case 3:
129
- response = _c.sent();
130
- return [2 /*return*/, { data: response, error: null, meta: meta }];
131
- case 4:
132
- err_1 = _c.sent();
133
- return [2 /*return*/, { data: null, error: { status: 500, message: err_1.message } }];
134
- case 5: return [2 /*return*/];
135
- }
37
+ async _fetchAPI(endpoint) {
38
+ const url = this.options.apiBaseEndpoint + endpoint;
39
+ try {
40
+ const start = performance.now();
41
+ const request = await fetch(url, {
42
+ ...this.options.requestInit,
43
+ headers: {
44
+ "X-Hive-Resolve-Stat-Track": (this.options.resolveDynamicTitles ?? true).toString(),
45
+ "X-Hive-Leaderboard-Source": this.options.useModernLeaderboardSource ? "modern" : "legacy",
46
+ ...this.options.requestInit?.headers,
47
+ },
136
48
  });
137
- });
138
- };
49
+ if (!request.ok)
50
+ return { data: null, error: { status: request.status } };
51
+ const meta = {
52
+ duration: Math.ceil(performance.now() - start),
53
+ ratelimit: {
54
+ limit: Number(request.headers.get("X-Ratelimit-Limit")),
55
+ remaining: Number(request.headers.get("X-Ratelimit-Remaining")),
56
+ reset: Number(request.headers.get("X-Ratelimit-Reset")),
57
+ },
58
+ retryAfter: Number(request.headers.get("Retry-After")),
59
+ };
60
+ const response = await request.json();
61
+ return { data: response, error: null, meta };
62
+ }
63
+ catch (err) {
64
+ return { data: null, error: { status: 500, message: err.message } };
65
+ }
66
+ }
139
67
  /**
140
68
  * `/game/all/main/{identifier}` Gets information about a player
141
69
  * @param identifier Username or UUID of the player
142
70
  * @returns The player information
143
71
  */
144
- HiveAPI.prototype.getPlayer = function (identifier) {
145
- return __awaiter(this, void 0, void 0, function () {
146
- var _a, response, error, meta, data;
147
- return __generator(this, function (_b) {
148
- switch (_b.label) {
149
- case 0: return [4 /*yield*/, this._fetchAPI("/game/all/main/".concat(identifier))];
150
- case 1:
151
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
152
- if (error)
153
- return [2 /*return*/, { data: null, error: error, meta: meta }];
154
- data = (0, player_1.default)(response);
155
- if (!data)
156
- return [2 /*return*/, { data: null, error: { status: 500, message: "Failed to process player data" }, meta: meta }];
157
- return [2 /*return*/, { data: data, error: null, meta: meta }];
158
- }
159
- });
160
- });
161
- };
72
+ async getPlayer(identifier) {
73
+ const { data: response, error, meta } = await this._fetchAPI(`/game/all/main/${identifier}`);
74
+ if (error)
75
+ return { data: null, error, meta };
76
+ const data = (0, player_1.default)(response);
77
+ if (!data)
78
+ return { data: null, error: { status: 500, message: "Failed to process player data" }, meta };
79
+ return { data, error: null, meta };
80
+ }
162
81
  /**
163
82
  * `/player/search/{prefix}` Gets a list of players that match the prefix
164
83
  * @param prefix The prefix to search for (must be at least 4 characters long)
165
84
  * @returns A list of players that match the prefix
166
85
  */
167
- HiveAPI.prototype.getPlayerSearch = function (prefix) {
168
- return __awaiter(this, void 0, void 0, function () {
169
- var _a, response, error, meta, data;
170
- return __generator(this, function (_b) {
171
- switch (_b.label) {
172
- case 0: return [4 /*yield*/, this._fetchAPI("/player/search/".concat(prefix))];
173
- case 1:
174
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
175
- if (error)
176
- return [2 /*return*/, { data: null, error: error, meta: meta }];
177
- data = (0, player_search_1.default)(response);
178
- if (!data)
179
- return [2 /*return*/, { data: null, error: { status: 500, message: "Failed to process player search data" }, meta: meta }];
180
- return [2 /*return*/, { data: data, error: null, meta: meta }];
181
- }
182
- });
183
- });
184
- };
185
- HiveAPI.prototype.getStatistics = function (identifier, timeframe, options) {
186
- return __awaiter(this, void 0, void 0, function () {
187
- return __generator(this, function (_a) {
188
- if (timeframe === hive_bedrock_data_1.Timeframe.AllTime) {
189
- if (options === null || options === void 0 ? void 0 : options.game) {
190
- if (!hive_bedrock_data_1.Games[options.game].statistics)
191
- throw new Error("Statistics are not available for the game: ".concat(options.game));
192
- return [2 /*return*/, this._getStatisticsAllTime(identifier, options.game)];
193
- }
194
- return [2 /*return*/, this._getStatisticsAllTime(identifier)];
195
- }
196
- else if (timeframe === hive_bedrock_data_1.Timeframe.Seasonal) {
197
- if (!(options === null || options === void 0 ? void 0 : options.game))
198
- throw new Error("Game must be specified for seasonal statistics");
199
- if (!hive_bedrock_data_1.Games[options.game].statistics)
200
- throw new Error("Statistics are not available for the game: ".concat(options.game));
201
- return [2 /*return*/, this._getStatisticsSeasonal(identifier, options === null || options === void 0 ? void 0 : options.game, (options === null || options === void 0 ? void 0 : options.season) || 1)];
202
- }
203
- if (options === null || options === void 0 ? void 0 : options.game)
204
- return [2 /*return*/, this._getStatisticsMonthly(identifier, options.game, options.month, options.year)];
205
- return [2 /*return*/, this._getStatisticsMonthly(identifier, undefined, options === null || options === void 0 ? void 0 : options.month, options === null || options === void 0 ? void 0 : options.year)];
206
- });
207
- });
208
- };
209
- HiveAPI.prototype._getStatisticsAllTime = function (identifier, game) {
210
- return __awaiter(this, void 0, void 0, function () {
211
- var _a, response, error, meta, data;
212
- return __generator(this, function (_b) {
213
- switch (_b.label) {
214
- case 0: return [4 /*yield*/, this._fetchAPI("/game/all/".concat(game !== null && game !== void 0 ? game : "all", "/").concat(identifier))];
215
- case 1:
216
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
217
- if (error)
218
- return [2 /*return*/, { data: null, error: error, meta: meta }];
219
- if (game)
220
- return [2 /*return*/, { data: (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.AllTime, false, response), error: null, meta: meta }];
221
- data = Object.fromEntries(Object.entries(response).map(function (_a) {
222
- var _b;
223
- var _c = __read(_a, 2), id = _c[0], res = _c[1];
224
- return [id, id === "main" ? null : (_b = (0, game_1.processGame)(id, hive_bedrock_data_1.Timeframe.AllTime, false, res)) !== null && _b !== void 0 ? _b : null];
225
- }));
226
- delete data.main;
227
- return [2 /*return*/, {
228
- data: {
229
- player: (0, player_1.default)(response),
230
- statistics: data,
231
- },
232
- error: null,
233
- meta: meta,
234
- }];
235
- }
236
- });
237
- });
238
- };
239
- HiveAPI.prototype._getStatisticsMonthly = function (identifier, game, month, year) {
240
- return __awaiter(this, void 0, void 0, function () {
241
- var _a, response, error, meta, data;
242
- return __generator(this, function (_b) {
243
- switch (_b.label) {
244
- case 0:
245
- if (game && !hive_bedrock_data_1.Games[game].statistics)
246
- throw new Error("Statistics are not available for the game: ".concat(game));
247
- return [4 /*yield*/, this._fetchAPI("/game/monthly/player/".concat(game !== null && game !== void 0 ? game : "all", "/").concat(identifier, "/").concat(year !== null && year !== void 0 ? year : new Date().getFullYear(), "/").concat(month !== null && month !== void 0 ? month : new Date().getMonth() + 1))];
248
- case 1:
249
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
250
- if (error)
251
- return [2 /*return*/, { data: null, error: error, meta: meta }];
252
- if (game)
253
- return [2 /*return*/, { data: (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, false, response), error: null, meta: meta }];
254
- data = Object.fromEntries(Object.entries(response).map(function (_a) {
255
- var _b;
256
- var _c = __read(_a, 2), id = _c[0], res = _c[1];
257
- return [id, id === "main" ? null : (_b = (0, game_1.processGame)(id, hive_bedrock_data_1.Timeframe.Monthly, false, res)) !== null && _b !== void 0 ? _b : null];
258
- }));
259
- delete data.main;
260
- return [2 /*return*/, {
261
- data: {
262
- statistics: data,
263
- },
264
- error: null,
265
- meta: meta,
266
- }];
267
- }
268
- });
269
- });
270
- };
271
- HiveAPI.prototype._getStatisticsSeasonal = function (identifier, game, season) {
272
- return __awaiter(this, void 0, void 0, function () {
273
- var _a, response, error, meta;
274
- return __generator(this, function (_b) {
275
- switch (_b.label) {
276
- case 0:
277
- if (!hive_bedrock_data_1.Games[game].statistics)
278
- throw new Error("Statistics are not available for the game: ".concat(game));
279
- return [4 /*yield*/, this._fetchAPI("/game/season/player/".concat(game, "/").concat(identifier, "/").concat(season !== null && season !== void 0 ? season : 1))];
280
- case 1:
281
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
282
- if (error)
283
- return [2 /*return*/, { data: null, error: error, meta: meta }];
284
- return [2 /*return*/, { data: (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Seasonal, false, response), error: null, meta: meta }];
285
- }
286
- });
287
- });
288
- };
289
- HiveAPI.prototype.getAvailableMonthlyLeaderboards = function (game) {
290
- return __awaiter(this, void 0, void 0, function () {
291
- var _a, response, error, meta;
292
- return __generator(this, function (_b) {
293
- switch (_b.label) {
294
- case 0: return [4 /*yield*/, this._fetchAPI("/game/monthly/".concat(game, "/available"))];
295
- case 1:
296
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
297
- if (error)
298
- return [2 /*return*/, { data: null, error: error, meta: meta }];
299
- return [2 /*return*/, { data: response, error: null, meta: meta }];
300
- }
301
- });
302
- });
303
- };
86
+ async getPlayerSearch(prefix) {
87
+ const { data: response, error, meta } = await this._fetchAPI(`/player/search/${prefix}`);
88
+ if (error)
89
+ return { data: null, error, meta };
90
+ const data = (0, player_search_1.default)(response);
91
+ if (!data)
92
+ return { data: null, error: { status: 500, message: "Failed to process player search data" }, meta };
93
+ return { data, error: null, meta };
94
+ }
95
+ async getStatistics(identifier, timeframe, options) {
96
+ if (timeframe === hive_bedrock_data_1.Timeframe.AllTime) {
97
+ if (options?.game) {
98
+ if (!hive_bedrock_data_1.Games[options.game].statistics)
99
+ throw new Error(`Statistics are not available for the game: ${options.game}`);
100
+ return this._getStatisticsAllTime(identifier, options.game);
101
+ }
102
+ return this._getStatisticsAllTime(identifier);
103
+ }
104
+ else if (timeframe === hive_bedrock_data_1.Timeframe.Seasonal) {
105
+ if (!options?.game)
106
+ throw new Error("Game must be specified for seasonal statistics");
107
+ if (!hive_bedrock_data_1.Games[options.game].statistics)
108
+ throw new Error(`Statistics are not available for the game: ${options.game}`);
109
+ return this._getStatisticsSeasonal(identifier, options?.game, options?.season || 1);
110
+ }
111
+ if (options?.game)
112
+ return this._getStatisticsMonthly(identifier, options.game, options.month, options.year);
113
+ return this._getStatisticsMonthly(identifier, undefined, options?.month, options?.year);
114
+ }
115
+ async _getStatisticsAllTime(identifier, game) {
116
+ const { data: response, error, meta } = await this._fetchAPI(`/game/all/${game ?? "all"}/${identifier}`);
117
+ if (error)
118
+ return { data: null, error, meta };
119
+ if (game)
120
+ return { data: (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.AllTime, false, response), error: null, meta };
121
+ let data = Object.fromEntries(Object.entries(response).map(([id, res]) => [id, id === "main" ? null : (0, game_1.processGame)(id, hive_bedrock_data_1.Timeframe.AllTime, false, res) ?? null]));
122
+ delete data.main;
123
+ return {
124
+ data: {
125
+ player: (0, player_1.default)(response),
126
+ statistics: data,
127
+ },
128
+ error: null,
129
+ meta,
130
+ };
131
+ }
132
+ async _getStatisticsMonthly(identifier, game, month, year) {
133
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
134
+ throw new Error(`Statistics are not available for the game: ${game}`);
135
+ const { data: response, error, meta, } = await this._fetchAPI(`/game/monthly/player/${game ?? "all"}/${identifier}/${year ?? new Date().getFullYear()}/${month ?? new Date().getMonth() + 1}`);
136
+ if (error)
137
+ return { data: null, error, meta };
138
+ if (game)
139
+ return { data: (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, false, response), error: null, meta };
140
+ let data = Object.fromEntries(Object.entries(response).map(([id, res]) => [id, id === "main" ? null : (0, game_1.processGame)(id, hive_bedrock_data_1.Timeframe.Monthly, false, res) ?? null]));
141
+ delete data.main;
142
+ return {
143
+ data: {
144
+ statistics: data,
145
+ },
146
+ error: null,
147
+ meta,
148
+ };
149
+ }
150
+ async _getStatisticsSeasonal(identifier, game, season) {
151
+ if (!hive_bedrock_data_1.Games[game].statistics)
152
+ throw new Error(`Statistics are not available for the game: ${game}`);
153
+ const { data: response, error, meta } = await this._fetchAPI(`/game/season/player/${game}/${identifier}/${season ?? 1}`);
154
+ if (error)
155
+ return { data: null, error, meta };
156
+ return { data: (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Seasonal, false, response), error: null, meta };
157
+ }
158
+ async getAvailableMonthlyLeaderboards(game) {
159
+ const { data: response, error, meta } = await this._fetchAPI(`/game/monthly/${game}/available`);
160
+ if (error)
161
+ return { data: null, error, meta };
162
+ return { data: response, error: null, meta };
163
+ }
304
164
  /**
305
165
  * `/game/season/player/{game}/{player}/{season}` Gets seasonal statistics for a player
306
166
  * @param identifier Username or UUID of the player
@@ -308,188 +168,103 @@ var HiveAPI = /** @class */ (function () {
308
168
  * @param season The season to get the statistics for
309
169
  * @returns The player's seasonal statistics
310
170
  */
311
- HiveAPI.prototype.getSeasonalStatistics = function (identifier_1, game_2) {
312
- return __awaiter(this, arguments, void 0, function (identifier, game, season) {
313
- var _a, response, error, meta, data;
314
- if (season === void 0) { season = 1; }
315
- return __generator(this, function (_b) {
316
- switch (_b.label) {
317
- case 0:
318
- if (game && !hive_bedrock_data_1.Games[game].statistics)
319
- throw new Error("Statistics are not available for the game: ".concat(game));
320
- return [4 /*yield*/, this._fetchAPI("/game/season/player/".concat(game, "/").concat(identifier, "/").concat(season))];
321
- case 1:
322
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
323
- if (error)
324
- return [2 /*return*/, { data: null, error: error, meta: meta }];
325
- data = (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, true, response);
326
- return [2 /*return*/, { data: data, error: null, meta: meta }];
327
- }
328
- });
329
- });
330
- };
331
- HiveAPI.prototype.getLeaderboard = function (timeframe, game, options) {
332
- return __awaiter(this, void 0, void 0, function () {
333
- return __generator(this, function (_a) {
334
- if (game && !hive_bedrock_data_1.Games[game].statistics)
335
- throw new Error("Statistics are not available for the game: ".concat(game));
336
- if (timeframe === hive_bedrock_data_1.Timeframe.Monthly)
337
- return [2 /*return*/, this._getLeaderboardMonthly(game, (options === null || options === void 0 ? void 0 : options.month) || new Date().getMonth() + 1, (options === null || options === void 0 ? void 0 : options.year) || new Date().getFullYear())];
338
- if (timeframe === hive_bedrock_data_1.Timeframe.Seasonal)
339
- return [2 /*return*/, this._getLeaderboardSeasonal(game, (options === null || options === void 0 ? void 0 : options.season) || 1)];
340
- return [2 /*return*/, this._getLeaderboardAllTime(game)];
341
- });
342
- });
343
- };
171
+ async getSeasonalStatistics(identifier, game, season = 1) {
172
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
173
+ throw new Error(`Statistics are not available for the game: ${game}`);
174
+ const { data: response, error, meta } = await this._fetchAPI(`/game/season/player/${game}/${identifier}/${season}`);
175
+ if (error)
176
+ return { data: null, error, meta };
177
+ let data = (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, true, response);
178
+ return { data, error: null, meta };
179
+ }
180
+ async getLeaderboard(timeframe, game, options) {
181
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
182
+ throw new Error(`Statistics are not available for the game: ${game}`);
183
+ if (timeframe === hive_bedrock_data_1.Timeframe.Monthly)
184
+ return this._getLeaderboardMonthly(game, options?.month || new Date().getMonth() + 1, options?.year || new Date().getFullYear());
185
+ if (timeframe === hive_bedrock_data_1.Timeframe.Seasonal)
186
+ return this._getLeaderboardSeasonal(game, options?.season || 1);
187
+ return this._getLeaderboardAllTime(game);
188
+ }
344
189
  // /game/all/{game}
345
- HiveAPI.prototype._getLeaderboardAllTime = function (game) {
346
- return __awaiter(this, void 0, void 0, function () {
347
- var _a, response, error, meta, data;
348
- return __generator(this, function (_b) {
349
- switch (_b.label) {
350
- case 0: return [4 /*yield*/, this._fetchAPI("/game/all/".concat(game))];
351
- case 1:
352
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
353
- if (error)
354
- return [2 /*return*/, { data: null, error: error, meta: meta }];
355
- data = response.map(function (res) { return (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.AllTime, true, res); });
356
- return [2 /*return*/, { data: data, error: null, meta: meta }];
357
- }
358
- });
359
- });
360
- };
190
+ async _getLeaderboardAllTime(game) {
191
+ const { data: response, error, meta } = await this._fetchAPI(`/game/all/${game}`);
192
+ if (error)
193
+ return { data: null, error, meta };
194
+ let data = response.map((res) => (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.AllTime, true, res));
195
+ return { data, error: null, meta };
196
+ }
361
197
  // /game/monthly/{game}/{year}/{month}
362
- HiveAPI.prototype._getLeaderboardMonthly = function (game, month, year) {
363
- return __awaiter(this, void 0, void 0, function () {
364
- var _a, response, error, meta, data;
365
- return __generator(this, function (_b) {
366
- switch (_b.label) {
367
- case 0:
368
- if (game && !hive_bedrock_data_1.Games[game].statistics)
369
- throw new Error("Statistics are not available for the game: ".concat(game));
370
- return [4 /*yield*/, this._fetchAPI("/game/monthly/".concat(game, "/").concat(year, "/").concat(month))];
371
- case 1:
372
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
373
- if (error)
374
- return [2 /*return*/, { data: null, error: error, meta: meta }];
375
- data = response.map(function (res) { return (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, true, res); });
376
- return [2 /*return*/, { data: data, error: null, meta: meta }];
377
- }
378
- });
379
- });
380
- };
198
+ async _getLeaderboardMonthly(game, month, year) {
199
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
200
+ throw new Error(`Statistics are not available for the game: ${game}`);
201
+ const { data: response, error, meta } = await this._fetchAPI(`/game/monthly/${game}/${year}/${month}`);
202
+ if (error)
203
+ return { data: null, error, meta };
204
+ let data = response.map((res) => (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, true, res));
205
+ return { data, error: null, meta };
206
+ }
381
207
  // /game/season/{game}/season
382
- HiveAPI.prototype._getLeaderboardSeasonal = function (game_2) {
383
- return __awaiter(this, arguments, void 0, function (game, season) {
384
- var _a, response, error, meta, data;
385
- if (season === void 0) { season = 1; }
386
- return __generator(this, function (_b) {
387
- switch (_b.label) {
388
- case 0:
389
- if (game && !hive_bedrock_data_1.Games[game].statistics)
390
- throw new Error("Statistics are not available for the game: ".concat(game));
391
- return [4 /*yield*/, this._fetchAPI("/game/season/".concat(game, "/").concat(season))];
392
- case 1:
393
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
394
- if (error)
395
- return [2 /*return*/, { data: null, error: error, meta: meta }];
396
- data = response.map(function (res) { return (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Seasonal, true, res); });
397
- return [2 /*return*/, { data: data, error: null, meta: meta }];
398
- }
399
- });
400
- });
401
- };
208
+ async _getLeaderboardSeasonal(game, season = 1) {
209
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
210
+ throw new Error(`Statistics are not available for the game: ${game}`);
211
+ const { data: response, error, meta } = await this._fetchAPI(`/game/season/${game}/${season}`);
212
+ if (error)
213
+ return { data: null, error, meta };
214
+ let data = response.map((res) => (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Seasonal, true, res));
215
+ return { data, error: null, meta };
216
+ }
402
217
  /**
403
218
  * `/game/season/{game}/{season}` Gets seasonal statistics for a game
404
219
  * @param game The game to get the statistics for
405
220
  * @param season The season to get the statistics for
406
221
  * @returns The seasonal statistics for the game
407
222
  */
408
- HiveAPI.prototype.getSeasonalLeaderboard = function (game_2) {
409
- return __awaiter(this, arguments, void 0, function (game, season) {
410
- var _a, response, error, meta, data;
411
- if (season === void 0) { season = 1; }
412
- return __generator(this, function (_b) {
413
- switch (_b.label) {
414
- case 0:
415
- if (game && !hive_bedrock_data_1.Games[game].statistics)
416
- throw new Error("Statistics are not available for the game: ".concat(game));
417
- return [4 /*yield*/, this._fetchAPI("/game/season/".concat(game, "/").concat(season !== null && season !== void 0 ? season : 1))];
418
- case 1:
419
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
420
- if (error)
421
- return [2 /*return*/, { data: null, error: error, meta: meta }];
422
- data = response.map(function (res) { return (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, true, res); });
423
- return [2 /*return*/, { data: data, error: null, meta: meta }];
424
- }
425
- });
426
- });
427
- };
223
+ async getSeasonalLeaderboard(game, season = 1) {
224
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
225
+ throw new Error(`Statistics are not available for the game: ${game}`);
226
+ const { data: response, error, meta } = await this._fetchAPI(`/game/season/${game}/${season ?? 1}`);
227
+ if (error)
228
+ return { data: null, error, meta };
229
+ let data = response.map((res) => (0, game_1.processGame)(game, hive_bedrock_data_1.Timeframe.Monthly, true, res));
230
+ return { data, error: null, meta };
231
+ }
428
232
  /**
429
233
  * `/global/statistics` Gets global statistics
430
234
  * @returns The global statistics
431
235
  */
432
- HiveAPI.prototype.getGlobalStatistics = function () {
433
- return __awaiter(this, void 0, void 0, function () {
434
- var _a, response, error, meta;
435
- return __generator(this, function (_b) {
436
- switch (_b.label) {
437
- case 0: return [4 /*yield*/, this._fetchAPI("/global/statistics")];
438
- case 1:
439
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
440
- if (error)
441
- return [2 /*return*/, { data: null, error: error, meta: meta }];
442
- return [2 /*return*/, { data: response, error: null, meta: meta }];
443
- }
444
- });
445
- });
446
- };
236
+ async getGlobalStatistics() {
237
+ const { data: response, error, meta } = await this._fetchAPI("/global/statistics");
238
+ if (error)
239
+ return { data: null, error, meta };
240
+ return { data: response, error: null, meta };
241
+ }
447
242
  /**
448
243
  * `/game/map/{game}` Gets information about the maps in a game
449
244
  * @param game The game to get the maps for (fails for games without maps)
450
245
  * @returns A list of maps in the game
451
246
  */
452
- HiveAPI.prototype.getGameMaps = function (game) {
453
- return __awaiter(this, void 0, void 0, function () {
454
- var _a, response, error, meta;
455
- return __generator(this, function (_b) {
456
- switch (_b.label) {
457
- case 0:
458
- if (game && !hive_bedrock_data_1.Games[game].statistics)
459
- throw new Error("Statistics are not available for the game: ".concat(game));
460
- return [4 /*yield*/, this._fetchAPI("/game/map/".concat(game))];
461
- case 1:
462
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
463
- if (error)
464
- return [2 /*return*/, { data: null, error: error, meta: meta }];
465
- return [2 /*return*/, { data: response, error: null, meta: meta }];
466
- }
467
- });
468
- });
469
- };
247
+ async getGameMaps(game) {
248
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
249
+ throw new Error(`Statistics are not available for the game: ${game}`);
250
+ const { data: response, error, meta } = await this._fetchAPI(`/game/map/${game}`);
251
+ if (error)
252
+ return { data: null, error, meta };
253
+ return { data: response, error: null, meta };
254
+ }
470
255
  /**
471
256
  * `/game/meta/{game}` Gets metadata about a game
472
257
  * @param game The game to get the metadata for
473
258
  * @returns The metadata for the game
474
259
  */
475
- HiveAPI.prototype.getGameMetadata = function (game) {
476
- return __awaiter(this, void 0, void 0, function () {
477
- var _a, response, error, meta;
478
- return __generator(this, function (_b) {
479
- switch (_b.label) {
480
- case 0:
481
- if (game && !hive_bedrock_data_1.Games[game].statistics)
482
- throw new Error("Statistics are not available for the game: ".concat(game));
483
- return [4 /*yield*/, this._fetchAPI("/game/meta/".concat(game))];
484
- case 1:
485
- _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
486
- if (error)
487
- return [2 /*return*/, { data: null, error: error, meta: meta }];
488
- return [2 /*return*/, { data: response, error: null, meta: meta }];
489
- }
490
- });
491
- });
492
- };
493
- return HiveAPI;
494
- }());
260
+ async getGameMetadata(game) {
261
+ if (game && !hive_bedrock_data_1.Games[game].statistics)
262
+ throw new Error(`Statistics are not available for the game: ${game}`);
263
+ const { data: response, error, meta } = await this._fetchAPI(`/game/meta/${game}`);
264
+ if (error)
265
+ return { data: null, error, meta };
266
+ return { data: response, error: null, meta };
267
+ }
268
+ }
495
269
  exports.default = HiveAPI;
270
+ //# sourceMappingURL=index.js.map