hive-bedrock-api 2.1.11 → 3.0.0-alpha.2

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,43 +1,386 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
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
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ 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);
24
+ return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
7
62
  }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
63
+ return ar;
15
64
  };
16
65
  var __importDefault = (this && this.__importDefault) || function (mod) {
17
66
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
67
  };
19
68
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.Game = exports.getSeasonStatistics = exports.getSeasonLeaderboard = exports.getPlayerInfomation = exports.getMetdata = exports.getMaps = exports.getGlobalStatistics = exports.getMonthlyStatistics = exports.getAllTimeStatistics = exports.getMonthlyLeaderboard = exports.getAllTimeLeaderboard = void 0;
21
- var getAllTimeLeaderboard_1 = __importDefault(require("./methods/getAllTimeLeaderboard"));
22
- exports.getAllTimeLeaderboard = getAllTimeLeaderboard_1.default;
23
- var getAllTimeStatistics_1 = __importDefault(require("./methods/getAllTimeStatistics"));
24
- exports.getAllTimeStatistics = getAllTimeStatistics_1.default;
25
- var getGlobalStatistics_1 = __importDefault(require("./methods/getGlobalStatistics"));
26
- exports.getGlobalStatistics = getGlobalStatistics_1.default;
27
- var getMaps_1 = __importDefault(require("./methods/getMaps"));
28
- exports.getMaps = getMaps_1.default;
29
- var getMetadata_1 = __importDefault(require("./methods/getMetadata"));
30
- exports.getMetdata = getMetadata_1.default;
31
- var getMonthlyLeaderboard_1 = __importDefault(require("./methods/getMonthlyLeaderboard"));
32
- exports.getMonthlyLeaderboard = getMonthlyLeaderboard_1.default;
33
- var getMonthlyStatistics_1 = __importDefault(require("./methods/getMonthlyStatistics"));
34
- exports.getMonthlyStatistics = getMonthlyStatistics_1.default;
35
- var getPlayerInfomation_1 = __importDefault(require("./methods/getPlayerInfomation"));
36
- exports.getPlayerInfomation = getPlayerInfomation_1.default;
37
- var getSeasonLeaderboard_1 = __importDefault(require("./methods/getSeasonLeaderboard"));
38
- exports.getSeasonLeaderboard = getSeasonLeaderboard_1.default;
39
- var getSeasonStatistics_1 = __importDefault(require("./methods/getSeasonStatistics"));
40
- exports.getSeasonStatistics = getSeasonStatistics_1.default;
41
69
  var hive_bedrock_data_1 = require("hive-bedrock-data");
42
- Object.defineProperty(exports, "Game", { enumerable: true, get: function () { return hive_bedrock_data_1.Game; } });
43
- __exportStar(require("./processors/index"), exports);
70
+ var player_1 = __importDefault(require("./processors/player"));
71
+ var player_search_1 = __importDefault(require("./processors/player_search"));
72
+ var game_1 = __importDefault(require("./processors/game"));
73
+ var HiveAPI = /** @class */ (function () {
74
+ function HiveAPI(options) {
75
+ if (options === void 0) { options = { resolveDynamicTitles: true, apiBaseEndpoint: hive_bedrock_data_1.API_BASE_ENDPOINT }; }
76
+ this.options = options;
77
+ }
78
+ HiveAPI.prototype._fetchAPI = function (endpoint) {
79
+ return __awaiter(this, void 0, void 0, function () {
80
+ var url, start, request, meta, response, err_1;
81
+ var _a, _b;
82
+ return __generator(this, function (_c) {
83
+ switch (_c.label) {
84
+ case 0:
85
+ url = this.options.apiBaseEndpoint + endpoint;
86
+ _c.label = 1;
87
+ case 1:
88
+ _c.trys.push([1, 4, , 5]);
89
+ start = performance.now();
90
+ 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() }, (_b = this.options.requestInit) === null || _b === void 0 ? void 0 : _b.headers) }))];
91
+ case 2:
92
+ request = _c.sent();
93
+ if (!request.ok)
94
+ return [2 /*return*/, { data: null, error: { status: request.status } }];
95
+ meta = {
96
+ duration: Math.ceil(performance.now() - start),
97
+ ratelimit: {
98
+ limit: Number(request.headers.get("X-Ratelimit-Limit")),
99
+ remaining: Number(request.headers.get("X-Ratelimit-Remaining")),
100
+ reset: Number(request.headers.get("X-Ratelimit-Reset")),
101
+ },
102
+ retryAfter: Number(request.headers.get("Retry-After")),
103
+ };
104
+ return [4 /*yield*/, request.json()];
105
+ case 3:
106
+ response = _c.sent();
107
+ return [2 /*return*/, { data: response, error: null, meta: meta }];
108
+ case 4:
109
+ err_1 = _c.sent();
110
+ return [2 /*return*/, { data: null, error: { status: 500, message: err_1.message } }];
111
+ case 5: return [2 /*return*/];
112
+ }
113
+ });
114
+ });
115
+ };
116
+ /**
117
+ * `/game/all/main/{identifier}` Gets information about a player
118
+ * @param identifier Username or UUID of the player
119
+ * @returns The player information
120
+ */
121
+ HiveAPI.prototype.getPlayer = function (identifier) {
122
+ return __awaiter(this, void 0, void 0, function () {
123
+ var _a, response, error, meta, data;
124
+ return __generator(this, function (_b) {
125
+ switch (_b.label) {
126
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/all/main/".concat(identifier))];
127
+ case 1:
128
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
129
+ if (error)
130
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
131
+ data = (0, player_1.default)(response);
132
+ if (!data)
133
+ return [2 /*return*/, { data: null, error: { status: 500, message: "Failed to process player data" }, meta: meta }];
134
+ return [2 /*return*/, { data: data, error: null, meta: meta }];
135
+ }
136
+ });
137
+ });
138
+ };
139
+ /**
140
+ * `/player/search/{prefix}` Gets a list of players that match the prefix
141
+ * @param prefix The prefix to search for (must be at least 4 characters long)
142
+ * @returns A list of players that match the prefix
143
+ */
144
+ HiveAPI.prototype.getPlayerSearch = function (prefix) {
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("/player/search/".concat(prefix))];
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_search_1.default)(response);
155
+ if (!data)
156
+ return [2 /*return*/, { data: null, error: { status: 500, message: "Failed to process player search data" }, meta: meta }];
157
+ return [2 /*return*/, { data: data, error: null, meta: meta }];
158
+ }
159
+ });
160
+ });
161
+ };
162
+ HiveAPI.prototype.getStatistics = function (identifier, timeframe, options) {
163
+ return __awaiter(this, void 0, void 0, function () {
164
+ return __generator(this, function (_a) {
165
+ if (timeframe === hive_bedrock_data_1.Timeframe.AllTime) {
166
+ if (options === null || options === void 0 ? void 0 : options.game)
167
+ return [2 /*return*/, this._getStatisticsAllTime(identifier, options.game)];
168
+ return [2 /*return*/, this._getStatisticsAllTime(identifier)];
169
+ }
170
+ if (options === null || options === void 0 ? void 0 : options.game)
171
+ return [2 /*return*/, this._getStatisticsMonthly(identifier, options.game, options.month, options.year)];
172
+ 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)];
173
+ });
174
+ });
175
+ };
176
+ HiveAPI.prototype._getStatisticsAllTime = function (identifier, game) {
177
+ return __awaiter(this, void 0, void 0, function () {
178
+ var _a, response, error, meta, data;
179
+ return __generator(this, function (_b) {
180
+ switch (_b.label) {
181
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/all/".concat(game !== null && game !== void 0 ? game : "all", "/").concat(identifier))];
182
+ case 1:
183
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
184
+ if (error)
185
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
186
+ if (game)
187
+ return [2 /*return*/, { data: (0, game_1.default)(game, hive_bedrock_data_1.Timeframe.AllTime, false, response), error: null, meta: meta }];
188
+ data = Object.fromEntries(Object.entries(response).map(function (_a) {
189
+ var _b;
190
+ var _c = __read(_a, 2), id = _c[0], res = _c[1];
191
+ return [id, id === "main" ? null : (_b = (0, game_1.default)(id, hive_bedrock_data_1.Timeframe.AllTime, false, res)) !== null && _b !== void 0 ? _b : null];
192
+ }));
193
+ delete data.main;
194
+ return [2 /*return*/, {
195
+ data: {
196
+ player: (0, player_1.default)(response),
197
+ statistics: data,
198
+ },
199
+ error: null,
200
+ meta: meta,
201
+ }];
202
+ }
203
+ });
204
+ });
205
+ };
206
+ HiveAPI.prototype._getStatisticsMonthly = function (identifier, game, month, year) {
207
+ return __awaiter(this, void 0, void 0, function () {
208
+ var _a, response, error, meta, data;
209
+ return __generator(this, function (_b) {
210
+ switch (_b.label) {
211
+ case 0: 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))];
212
+ case 1:
213
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
214
+ if (error)
215
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
216
+ if (game)
217
+ return [2 /*return*/, { data: (0, game_1.default)(game, hive_bedrock_data_1.Timeframe.Monthly, false, response), error: null, meta: meta }];
218
+ data = Object.fromEntries(Object.entries(response).map(function (_a) {
219
+ var _b;
220
+ var _c = __read(_a, 2), id = _c[0], res = _c[1];
221
+ return [id, id === "main" ? null : (_b = (0, game_1.default)(id, hive_bedrock_data_1.Timeframe.Monthly, false, res)) !== null && _b !== void 0 ? _b : null];
222
+ }));
223
+ delete data.main;
224
+ return [2 /*return*/, {
225
+ data: {
226
+ statistics: data,
227
+ },
228
+ error: null,
229
+ meta: meta,
230
+ }];
231
+ }
232
+ });
233
+ });
234
+ };
235
+ /**
236
+ * `/game/season/player/{game}/{player}/{season}` Gets seasonal statistics for a player
237
+ * @param identifier Username or UUID of the player
238
+ * @param game The game to get the statistics for
239
+ * @param season The season to get the statistics for
240
+ * @returns The player's seasonal statistics
241
+ */
242
+ HiveAPI.prototype.getSeasonalStatistics = function (identifier_1, game_2) {
243
+ return __awaiter(this, arguments, void 0, function (identifier, game, season) {
244
+ var _a, response, error, meta, data;
245
+ if (season === void 0) { season = 1; }
246
+ return __generator(this, function (_b) {
247
+ switch (_b.label) {
248
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/season/player/".concat(game, "/").concat(identifier, "/").concat(season))];
249
+ case 1:
250
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
251
+ if (error)
252
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
253
+ data = (0, game_1.default)(game, hive_bedrock_data_1.Timeframe.Monthly, true, response);
254
+ return [2 /*return*/, { data: data, error: null, meta: meta }];
255
+ }
256
+ });
257
+ });
258
+ };
259
+ HiveAPI.prototype.getLeaderboard = function (timeframe, game, options) {
260
+ return __awaiter(this, void 0, void 0, function () {
261
+ return __generator(this, function (_a) {
262
+ if (timeframe === hive_bedrock_data_1.Timeframe.Monthly)
263
+ 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())];
264
+ return [2 /*return*/, this._getLeaderboardAllTime(game)];
265
+ });
266
+ });
267
+ };
268
+ // /game/all/{game}
269
+ HiveAPI.prototype._getLeaderboardAllTime = function (game) {
270
+ return __awaiter(this, void 0, void 0, function () {
271
+ var _a, response, error, meta, data;
272
+ return __generator(this, function (_b) {
273
+ switch (_b.label) {
274
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/all/".concat(game))];
275
+ case 1:
276
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
277
+ if (error)
278
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
279
+ data = response.map(function (res) { return (0, game_1.default)(game, hive_bedrock_data_1.Timeframe.AllTime, true, res); });
280
+ return [2 /*return*/, { data: data, error: null, meta: meta }];
281
+ }
282
+ });
283
+ });
284
+ };
285
+ // /game/monthly/{game}/{year}/{month}
286
+ HiveAPI.prototype._getLeaderboardMonthly = function (game, month, year) {
287
+ return __awaiter(this, void 0, void 0, function () {
288
+ var _a, response, error, meta, data;
289
+ return __generator(this, function (_b) {
290
+ switch (_b.label) {
291
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/monthly/".concat(game, "/").concat(year, "/").concat(month))];
292
+ case 1:
293
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
294
+ if (error)
295
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
296
+ data = response.map(function (res) { return (0, game_1.default)(game, hive_bedrock_data_1.Timeframe.Monthly, true, res); });
297
+ return [2 /*return*/, { data: data, error: null, meta: meta }];
298
+ }
299
+ });
300
+ });
301
+ };
302
+ /**
303
+ * `/game/season/{game}/{season}` Gets seasonal statistics for a game
304
+ * @param game The game to get the statistics for
305
+ * @param season The season to get the statistics for
306
+ * @returns The seasonal statistics for the game
307
+ */
308
+ HiveAPI.prototype.getSeasonalLeaderboard = function (game_2) {
309
+ return __awaiter(this, arguments, void 0, function (game, season) {
310
+ var _a, response, error, meta, data;
311
+ if (season === void 0) { season = 1; }
312
+ return __generator(this, function (_b) {
313
+ switch (_b.label) {
314
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/season/".concat(game, "/").concat(season !== null && season !== void 0 ? season : 1))];
315
+ case 1:
316
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
317
+ if (error)
318
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
319
+ data = response.map(function (res) { return (0, game_1.default)(game, hive_bedrock_data_1.Timeframe.Monthly, true, res); });
320
+ return [2 /*return*/, { data: data, error: null, meta: meta }];
321
+ }
322
+ });
323
+ });
324
+ };
325
+ /**
326
+ * `/global/statistics` Gets global statistics
327
+ * @returns The global statistics
328
+ */
329
+ HiveAPI.prototype.getGlobalStatistics = function () {
330
+ return __awaiter(this, void 0, void 0, function () {
331
+ var _a, response, error, meta;
332
+ return __generator(this, function (_b) {
333
+ switch (_b.label) {
334
+ case 0: return [4 /*yield*/, this._fetchAPI("/global/statistics")];
335
+ case 1:
336
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
337
+ if (error)
338
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
339
+ return [2 /*return*/, { data: response, error: null, meta: meta }];
340
+ }
341
+ });
342
+ });
343
+ };
344
+ /**
345
+ * `/game/map/{game}` Gets information about the maps in a game
346
+ * @param game The game to get the maps for (fails for games without maps)
347
+ * @returns A list of maps in the game
348
+ */
349
+ HiveAPI.prototype.getGameMaps = function (game) {
350
+ return __awaiter(this, void 0, void 0, function () {
351
+ var _a, response, error, meta;
352
+ return __generator(this, function (_b) {
353
+ switch (_b.label) {
354
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/map/".concat(game))];
355
+ case 1:
356
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
357
+ if (error)
358
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
359
+ return [2 /*return*/, { data: response, error: null, meta: meta }];
360
+ }
361
+ });
362
+ });
363
+ };
364
+ /**
365
+ * `/game/meta/{game}` Gets metadata about a game
366
+ * @param game The game to get the metadata for
367
+ * @returns The metadata for the game
368
+ */
369
+ HiveAPI.prototype.getGameMetadata = function (game) {
370
+ return __awaiter(this, void 0, void 0, function () {
371
+ var _a, response, error, meta;
372
+ return __generator(this, function (_b) {
373
+ switch (_b.label) {
374
+ case 0: return [4 /*yield*/, this._fetchAPI("/game/meta/".concat(game))];
375
+ case 1:
376
+ _a = _b.sent(), response = _a.data, error = _a.error, meta = _a.meta;
377
+ if (error)
378
+ return [2 /*return*/, { data: null, error: error, meta: meta }];
379
+ return [2 /*return*/, { data: response, error: null, meta: meta }];
380
+ }
381
+ });
382
+ });
383
+ };
384
+ return HiveAPI;
385
+ }());
386
+ exports.default = HiveAPI;