osrs-json-hiscores 2.14.0 → 2.14.1

Sign up to get free protection for your applications and to get access to all the features.
package/lib/hiscores.d.ts CHANGED
@@ -1,11 +1,13 @@
1
+ import { AxiosRequestConfig } from 'axios';
1
2
  import { Player, Stats, Gamemode, SkillName, PlayerSkillRow, ActivityName, PlayerActivityRow, GetStatsOptions } from './types';
2
3
  /**
3
4
  * Screen scrapes the hiscores to get the formatted rsn of a player.
4
5
  *
5
6
  * @param rsn Username of the player.
7
+ * @param config Optional axios request config object.
6
8
  * @returns Formatted version of the rsn.
7
9
  */
8
- export declare function getRSNFormat(rsn: string): Promise<string>;
10
+ export declare function getRSNFormat(rsn: string, config?: AxiosRequestConfig): Promise<string>;
9
11
  /**
10
12
  * Parses CSV string of raw stats and returns a stats object.
11
13
  *
@@ -28,16 +30,18 @@ export declare function getStats(rsn: string, options?: GetStatsOptions): Promis
28
30
  *
29
31
  * @param rsn Username of the player.
30
32
  * @param mode Gamemode to fetch ranks for.
33
+ * @param config Optional axios request config object.
31
34
  * @returns Stats object.
32
35
  */
33
- export declare function getStatsByGamemode(rsn: string, mode?: Gamemode): Promise<Stats>;
34
- export declare function getSkillPage(skill: SkillName, mode?: Gamemode, page?: number): Promise<PlayerSkillRow[]>;
36
+ export declare function getStatsByGamemode(rsn: string, mode?: Gamemode, config?: AxiosRequestConfig): Promise<Stats>;
37
+ export declare function getSkillPage(skill: SkillName, mode?: Gamemode, page?: number, config?: AxiosRequestConfig): Promise<PlayerSkillRow[]>;
35
38
  /**
36
39
  * Screen scrapes a hiscores page of an activity or boss and returns an array of up to 25 players.
37
40
  *
38
41
  * @param activity Name of the activity or boss to fetch hiscores for.
39
42
  * @param mode Gamemode to fetch ranks for.
40
43
  * @param page Page number.
44
+ * @param config Optional axios request config object.
41
45
  * @returns Array of `PlayerActivityRow` objects.
42
46
  */
43
- export declare function getActivityPage(activity: ActivityName, mode?: Gamemode, page?: number): Promise<PlayerActivityRow[]>;
47
+ export declare function getActivityPage(activity: ActivityName, mode?: Gamemode, page?: number, config?: AxiosRequestConfig): Promise<PlayerActivityRow[]>;
package/lib/hiscores.js CHANGED
@@ -54,9 +54,10 @@ var utils_1 = require("./utils");
54
54
  * Screen scrapes the hiscores to get the formatted rsn of a player.
55
55
  *
56
56
  * @param rsn Username of the player.
57
+ * @param config Optional axios request config object.
57
58
  * @returns Formatted version of the rsn.
58
59
  */
59
- function getRSNFormat(rsn) {
60
+ function getRSNFormat(rsn, config) {
60
61
  return __awaiter(this, void 0, void 0, function () {
61
62
  var url, response, dom, anchor, _a;
62
63
  return __generator(this, function (_b) {
@@ -67,7 +68,7 @@ function getRSNFormat(rsn) {
67
68
  _b.label = 1;
68
69
  case 1:
69
70
  _b.trys.push([1, 3, , 4]);
70
- return [4 /*yield*/, utils_1.httpGet(url)];
71
+ return [4 /*yield*/, utils_1.httpGet(url, config)];
71
72
  case 2:
72
73
  response = _b.sent();
73
74
  dom = new jsdom_1.JSDOM(response.data);
@@ -170,12 +171,12 @@ exports.parseStats = parseStats;
170
171
  * @returns Player object.
171
172
  */
172
173
  function getStats(rsn, options) {
173
- var _a, _b;
174
+ var _a, _b, _c, _d;
174
175
  return __awaiter(this, void 0, void 0, function () {
175
- var otherGamemodes, shouldGetFormattedRsn, mainRes, emptyResponse_1, getModeStats, formattedName, _c, player, ironRes, hcRes, ultRes;
176
+ var otherGamemodes, shouldGetFormattedRsn, mainRes, emptyResponse_1, getModeStats, formattedName, _e, player, ironRes, hcRes, ultRes;
176
177
  var _this = this;
177
- return __generator(this, function (_d) {
178
- switch (_d.label) {
178
+ return __generator(this, function (_f) {
179
+ switch (_f.label) {
179
180
  case 0:
180
181
  utils_1.validateRSN(rsn);
181
182
  otherGamemodes = (_a = options === null || options === void 0 ? void 0 : options.otherGamemodes) !== null && _a !== void 0 ? _a : [
@@ -184,9 +185,9 @@ function getStats(rsn, options) {
184
185
  'ultimate'
185
186
  ];
186
187
  shouldGetFormattedRsn = (_b = options === null || options === void 0 ? void 0 : options.shouldGetFormattedRsn) !== null && _b !== void 0 ? _b : true;
187
- return [4 /*yield*/, utils_1.httpGet(utils_1.getStatsURL('main', rsn))];
188
+ return [4 /*yield*/, utils_1.httpGet(utils_1.getStatsURL('main', rsn), (_c = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _c === void 0 ? void 0 : _c.main)];
188
189
  case 1:
189
- mainRes = _d.sent();
190
+ mainRes = _f.sent();
190
191
  if (!(mainRes.status === 200)) return [3 /*break*/, 9];
191
192
  emptyResponse_1 = {
192
193
  status: 404,
@@ -196,22 +197,23 @@ function getStats(rsn, options) {
196
197
  config: {}
197
198
  };
198
199
  getModeStats = function (mode) { return __awaiter(_this, void 0, void 0, function () {
199
- return __generator(this, function (_a) {
200
+ var _a;
201
+ return __generator(this, function (_b) {
200
202
  return [2 /*return*/, otherGamemodes.includes(mode)
201
- ? utils_1.httpGet(utils_1.getStatsURL(mode, rsn)).catch(function (err) { return err; })
203
+ ? utils_1.httpGet(utils_1.getStatsURL(mode, rsn), (_a = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _a === void 0 ? void 0 : _a[mode]).catch(function (err) { return err; })
202
204
  : emptyResponse_1];
203
205
  });
204
206
  }); };
205
207
  if (!shouldGetFormattedRsn) return [3 /*break*/, 3];
206
- return [4 /*yield*/, getRSNFormat(rsn).catch(function () { return undefined; })];
208
+ return [4 /*yield*/, getRSNFormat(rsn, (_d = options === null || options === void 0 ? void 0 : options.axiosConfigs) === null || _d === void 0 ? void 0 : _d.rsn).catch(function () { return undefined; })];
207
209
  case 2:
208
- _c = _d.sent();
210
+ _e = _f.sent();
209
211
  return [3 /*break*/, 4];
210
212
  case 3:
211
- _c = undefined;
212
- _d.label = 4;
213
+ _e = undefined;
214
+ _f.label = 4;
213
215
  case 4:
214
- formattedName = _c;
216
+ formattedName = _e;
215
217
  player = {
216
218
  name: formattedName !== null && formattedName !== void 0 ? formattedName : rsn,
217
219
  mode: 'main',
@@ -222,15 +224,15 @@ function getStats(rsn, options) {
222
224
  player.main = parseStats(mainRes.data);
223
225
  return [4 /*yield*/, getModeStats('ironman')];
224
226
  case 5:
225
- ironRes = _d.sent();
227
+ ironRes = _f.sent();
226
228
  if (!(ironRes.status === 200)) return [3 /*break*/, 8];
227
229
  player.ironman = parseStats(ironRes.data);
228
230
  return [4 /*yield*/, getModeStats('hardcore')];
229
231
  case 6:
230
- hcRes = _d.sent();
232
+ hcRes = _f.sent();
231
233
  return [4 /*yield*/, getModeStats('ultimate')];
232
234
  case 7:
233
- ultRes = _d.sent();
235
+ ultRes = _f.sent();
234
236
  if (hcRes.status === 200) {
235
237
  player.mode = 'hardcore';
236
238
  player.hardcore = parseStats(hcRes.data);
@@ -262,7 +264,7 @@ function getStats(rsn, options) {
262
264
  player.mode = 'main';
263
265
  }
264
266
  }
265
- _d.label = 8;
267
+ _f.label = 8;
266
268
  case 8: return [2 /*return*/, player];
267
269
  case 9: throw Error('Player not found');
268
270
  }
@@ -275,9 +277,10 @@ exports.getStats = getStats;
275
277
  *
276
278
  * @param rsn Username of the player.
277
279
  * @param mode Gamemode to fetch ranks for.
280
+ * @param config Optional axios request config object.
278
281
  * @returns Stats object.
279
282
  */
280
- function getStatsByGamemode(rsn, mode) {
283
+ function getStatsByGamemode(rsn, mode, config) {
281
284
  if (mode === void 0) { mode = 'main'; }
282
285
  return __awaiter(this, void 0, void 0, function () {
283
286
  var response, stats;
@@ -288,7 +291,7 @@ function getStatsByGamemode(rsn, mode) {
288
291
  if (!utils_1.GAMEMODES.includes(mode)) {
289
292
  throw Error('Invalid game mode');
290
293
  }
291
- return [4 /*yield*/, utils_1.httpGet(utils_1.getStatsURL(mode, rsn))];
294
+ return [4 /*yield*/, utils_1.httpGet(utils_1.getStatsURL(mode, rsn), config)];
292
295
  case 1:
293
296
  response = _a.sent();
294
297
  if (response.status !== 200) {
@@ -301,7 +304,7 @@ function getStatsByGamemode(rsn, mode) {
301
304
  });
302
305
  }
303
306
  exports.getStatsByGamemode = getStatsByGamemode;
304
- function getSkillPage(skill, mode, page) {
307
+ function getSkillPage(skill, mode, page, config) {
305
308
  if (mode === void 0) { mode = 'main'; }
306
309
  if (page === void 0) { page = 1; }
307
310
  return __awaiter(this, void 0, void 0, function () {
@@ -319,7 +322,7 @@ function getSkillPage(skill, mode, page) {
319
322
  throw Error('Invalid skill');
320
323
  }
321
324
  url = utils_1.getSkillPageURL(mode, skill, page);
322
- return [4 /*yield*/, utils_1.httpGet(url)];
325
+ return [4 /*yield*/, utils_1.httpGet(url, config)];
323
326
  case 1:
324
327
  response = _a.sent();
325
328
  dom = new jsdom_1.JSDOM(response.data);
@@ -351,9 +354,10 @@ exports.getSkillPage = getSkillPage;
351
354
  * @param activity Name of the activity or boss to fetch hiscores for.
352
355
  * @param mode Gamemode to fetch ranks for.
353
356
  * @param page Page number.
357
+ * @param config Optional axios request config object.
354
358
  * @returns Array of `PlayerActivityRow` objects.
355
359
  */
356
- function getActivityPage(activity, mode, page) {
360
+ function getActivityPage(activity, mode, page, config) {
357
361
  if (mode === void 0) { mode = 'main'; }
358
362
  if (page === void 0) { page = 1; }
359
363
  return __awaiter(this, void 0, void 0, function () {
@@ -371,7 +375,7 @@ function getActivityPage(activity, mode, page) {
371
375
  throw Error('Invalid activity');
372
376
  }
373
377
  url = utils_1.getActivityPageURL(mode, activity, page);
374
- return [4 /*yield*/, utils_1.httpGet(url)];
378
+ return [4 /*yield*/, utils_1.httpGet(url, config)];
375
379
  case 1:
376
380
  response = _a.sent();
377
381
  dom = new jsdom_1.JSDOM(response.data);
package/lib/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { AxiosRequestConfig } from 'axios';
1
2
  export declare type Gamemode = 'main' | 'ironman' | 'ultimate' | 'hardcore' | 'deadman' | 'seasonal' | 'tournament' | 'skiller' | 'oneDefence' | 'freshStart';
2
3
  export interface Skill {
3
4
  rank: number;
@@ -65,4 +66,13 @@ export interface GetStatsOptions {
65
66
  * @defaultvalue `true`
66
67
  */
67
68
  shouldGetFormattedRsn?: boolean;
69
+ /**
70
+ * Map of configs for each requests that can take place in the `getStats` function.
71
+ */
72
+ axiosConfigs?: Partial<Record<Gamemode, AxiosRequestConfig>> & {
73
+ /**
74
+ * The axios request config object to use for the RSN format request.
75
+ */
76
+ rsn?: AxiosRequestConfig;
77
+ };
68
78
  }
@@ -1,3 +1,4 @@
1
+ import { AxiosRequestConfig } from 'axios';
1
2
  import { Gamemode, SkillName, ActivityName } from '../types';
2
3
  /**
3
4
  * Will generate a stats URL for the official OSRS API.
@@ -53,7 +54,7 @@ export declare const rsnFromElement: (el: Element | null) => string;
53
54
  * @param url URL to run a `GET` request against.
54
55
  * @returns Axios response.
55
56
  */
56
- export declare const httpGet: <Response_1>(url: string) => Promise<import("axios").AxiosResponse<Response_1>>;
57
+ export declare const httpGet: <Response_1>(url: string, config?: AxiosRequestConfig) => Promise<import("axios").AxiosResponse<Response_1>>;
57
58
  /**
58
59
  * Validates that a provided RSN has the same username restrictions as Jagex.
59
60
  * @param rsn Username to validate.
@@ -1,4 +1,15 @@
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
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
14
  exports.validateRSN = exports.httpGet = exports.rsnFromElement = exports.numberFromElement = exports.getActivityPageURL = exports.getSkillPageURL = exports.getPlayerTableURL = exports.getStatsURL = void 0;
4
15
  var axios_1 = require("axios");
@@ -81,13 +92,12 @@ exports.rsnFromElement = rsnFromElement;
81
92
  * @param url URL to run a `GET` request against.
82
93
  * @returns Axios response.
83
94
  */
84
- var httpGet = function (url) {
85
- return axios_1.default.get(url, {
86
- headers: {
95
+ var httpGet = function (url, config) {
96
+ if (config === void 0) { config = {}; }
97
+ return axios_1.default.get(url, __assign({ headers: {
87
98
  // without User-Agent header requests may be rejected by DDoS protection mechanism
88
99
  'User-Agent': ua.firefox(80)
89
- }
90
- });
100
+ } }, config));
91
101
  };
92
102
  exports.httpGet = httpGet;
93
103
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "osrs-json-hiscores",
3
- "version": "2.14.0",
3
+ "version": "2.14.1",
4
4
  "description": "The Old School Runescape API wrapper that does more!",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",