Compare commits

...

12 Commits

Author SHA1 Message Date
maxswa
fc7897d4c7 v2.14.2 2023-07-15 20:48:08 -04:00
Max Swartwout
72ea31490f Merge pull request #80 from maxswa/update-test-lint-format-libs
Update testing, linting and formatting libraries
2023-07-13 16:40:08 -04:00
maxswa
66825955c1 Update linting and formatting libraries. 2023-07-13 16:36:37 -04:00
maxswa
4b4bf8c702 Update jest, jsdom, typescript 2023-07-13 16:16:07 -04:00
Max Swartwout
53981e7468 Merge pull request #78 from maxswa/dependabot/npm_and_yarn/semver-5.7.2
Bump semver from 5.7.1 to 5.7.2
2023-07-13 16:04:52 -04:00
dependabot[bot]
a90e5b98dd Bump semver from 5.7.1 to 5.7.2
Bumps [semver](https://github.com/npm/node-semver) from 5.7.1 to 5.7.2.
- [Release notes](https://github.com/npm/node-semver/releases)
- [Changelog](https://github.com/npm/node-semver/blob/v5.7.2/CHANGELOG.md)
- [Commits](https://github.com/npm/node-semver/compare/v5.7.1...v5.7.2)

---
updated-dependencies:
- dependency-name: semver
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-11 11:51:33 +00:00
maxswa
16a975f10b v2.14.1 2023-06-16 15:36:44 -04:00
Max Swartwout
c800501188 Merge pull request #77 from maxswa/axios-config
Add optional axios config argument
2023-06-16 15:36:14 -04:00
maxswa
8a320df015 Update necessary jsdoc comments. 2023-05-28 13:47:21 -04:00
maxswa
169462c446 Add optional config arg to all fns using httpGet 2023-05-28 13:42:37 -04:00
maxswa
317a972314 Add optional axios configs to GetStatsOptions 2023-05-28 13:41:53 -04:00
maxswa
c558f8402c Add optional axios config arg to httpGet 2023-05-28 13:41:02 -04:00
5 changed files with 2158 additions and 2825 deletions

View File

@@ -1,6 +1,6 @@
{ {
"name": "osrs-json-hiscores", "name": "osrs-json-hiscores",
"version": "2.14.0", "version": "2.14.2",
"description": "The Old School Runescape API wrapper that does more!", "description": "The Old School Runescape API wrapper that does more!",
"main": "lib/index.js", "main": "lib/index.js",
"types": "lib/index.d.ts", "types": "lib/index.d.ts",
@@ -61,7 +61,8 @@
"@typescript-eslint" "@typescript-eslint"
], ],
"extends": [ "extends": [
"eslint-config-airbnb-typescript", "airbnb-base",
"airbnb-typescript/base",
"prettier" "prettier"
], ],
"ignorePatterns": [ "ignorePatterns": [
@@ -72,7 +73,8 @@
"trailingComma": "none", "trailingComma": "none",
"tabWidth": 2, "tabWidth": 2,
"semi": true, "semi": true,
"singleQuote": true "singleQuote": true,
"endOfLine": "auto"
}, },
"jest": { "jest": {
"transform": { "transform": {
@@ -91,27 +93,24 @@
}, },
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"jsdom": "^16.3.0", "jsdom": "^22.1.0",
"useragent-generator": "^1.1.0" "useragent-generator": "^1.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^26.0.21", "@types/jest": "^29.5.3",
"@types/jsdom": "^16.2.3", "@types/jsdom": "^21.1.1",
"@typescript-eslint/eslint-plugin": "^4.19.0", "@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^4.19.0", "@typescript-eslint/parser": "^6.0.0",
"eslint": "^7.22.0", "eslint": "^8.44.0",
"eslint-config-airbnb-typescript": "^12.3.1", "eslint-config-airbnb-typescript": "^17.1.0",
"eslint-config-prettier": "^8.1.0", "eslint-config-prettier": "^8.8.0",
"eslint-plugin-import": "^2.22.1", "eslint-plugin-import": "^2.27.5",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.1",
"eslint-plugin-react-hooks": "^4.2.0",
"husky": "^5.2.0", "husky": "^5.2.0",
"jest": "^26.6.3", "jest": "^29.6.1",
"lint-staged": "^10.5.4", "lint-staged": "^13.2.3",
"np": "^7.7.0", "np": "^7.7.0",
"prettier": "^2.2.1", "prettier": "^3.0.0",
"ts-jest": "^26.5.4", "ts-jest": "^29.1.1",
"typescript": "^4.2.3" "typescript": "^5.1.6"
} }
} }

View File

@@ -1,4 +1,4 @@
import { AxiosResponse } from 'axios'; import { AxiosRequestConfig, AxiosResponse } from 'axios';
import { BinaryData, JSDOM } from 'jsdom'; import { BinaryData, JSDOM } from 'jsdom';
import { import {
Player, Player,
@@ -38,15 +38,20 @@ import {
* Screen scrapes the hiscores to get the formatted rsn of a player. * Screen scrapes the hiscores to get the formatted rsn of a player.
* *
* @param rsn Username of the player. * @param rsn Username of the player.
* @param config Optional axios request config object.
* @returns Formatted version of the rsn. * @returns Formatted version of the rsn.
*/ */
export async function getRSNFormat(rsn: string): Promise<string> { export async function getRSNFormat(
rsn: string,
config?: AxiosRequestConfig
): Promise<string> {
validateRSN(rsn); validateRSN(rsn);
const url = getPlayerTableURL('main', rsn); const url = getPlayerTableURL('main', rsn);
try { try {
const response = await httpGet<string | Buffer | BinaryData | undefined>( const response = await httpGet<string | Buffer | BinaryData | undefined>(
url url,
config
); );
const dom = new JSDOM(response.data); const dom = new JSDOM(response.data);
const anchor = dom.window.document.querySelector( const anchor = dom.window.document.querySelector(
@@ -106,12 +111,8 @@ export function parseStats(csv: string): Stats {
const [leaguePoints] = activityObjects.splice(0, 1); const [leaguePoints] = activityObjects.splice(0, 1);
const bhObjects = activityObjects.splice(0, BH_MODES.length); const bhObjects = activityObjects.splice(0, BH_MODES.length);
const clueObjects = activityObjects.splice(0, CLUES.length); const clueObjects = activityObjects.splice(0, CLUES.length);
const [ const [lastManStanding, pvpArena, soulWarsZeal, riftsClosed] =
lastManStanding, activityObjects.splice(0, 4);
pvpArena,
soulWarsZeal,
riftsClosed
] = activityObjects.splice(0, 4);
const bossObjects = activityObjects.splice(0, BOSSES.length); const bossObjects = activityObjects.splice(0, BOSSES.length);
const skills: Skills = skillObjects.reduce<Skills>((prev, curr, index) => { const skills: Skills = skillObjects.reduce<Skills>((prev, curr, index) => {
@@ -174,7 +175,10 @@ export async function getStats(
]; ];
const shouldGetFormattedRsn = options?.shouldGetFormattedRsn ?? true; const shouldGetFormattedRsn = options?.shouldGetFormattedRsn ?? true;
const mainRes = await httpGet<string>(getStatsURL('main', rsn)); const mainRes = await httpGet<string>(
getStatsURL('main', rsn),
options?.axiosConfigs?.main
);
if (mainRes.status === 200) { if (mainRes.status === 200) {
const emptyResponse: AxiosResponse<string> = { const emptyResponse: AxiosResponse<string> = {
status: 404, status: 404,
@@ -187,10 +191,15 @@ export async function getStats(
mode: Extract<Gamemode, 'ironman' | 'hardcore' | 'ultimate'> mode: Extract<Gamemode, 'ironman' | 'hardcore' | 'ultimate'>
): Promise<AxiosResponse<string>> => ): Promise<AxiosResponse<string>> =>
otherGamemodes.includes(mode) otherGamemodes.includes(mode)
? httpGet<string>(getStatsURL(mode, rsn)).catch((err) => err) ? httpGet<string>(
getStatsURL(mode, rsn),
options?.axiosConfigs?.[mode]
).catch((err) => err)
: emptyResponse; : emptyResponse;
const formattedName = shouldGetFormattedRsn const formattedName = shouldGetFormattedRsn
? await getRSNFormat(rsn).catch(() => undefined) ? await getRSNFormat(rsn, options?.axiosConfigs?.rsn).catch(
() => undefined
)
: undefined; : undefined;
const player: Player = { const player: Player = {
@@ -258,17 +267,19 @@ export async function getStats(
* *
* @param rsn Username of the player. * @param rsn Username of the player.
* @param mode Gamemode to fetch ranks for. * @param mode Gamemode to fetch ranks for.
* @param config Optional axios request config object.
* @returns Stats object. * @returns Stats object.
*/ */
export async function getStatsByGamemode( export async function getStatsByGamemode(
rsn: string, rsn: string,
mode: Gamemode = 'main' mode: Gamemode = 'main',
config?: AxiosRequestConfig
): Promise<Stats> { ): Promise<Stats> {
validateRSN(rsn); validateRSN(rsn);
if (!GAMEMODES.includes(mode)) { if (!GAMEMODES.includes(mode)) {
throw Error('Invalid game mode'); throw Error('Invalid game mode');
} }
const response = await httpGet<string>(getStatsURL(mode, rsn)); const response = await httpGet<string>(getStatsURL(mode, rsn), config);
if (response.status !== 200) { if (response.status !== 200) {
throw Error('Player not found'); throw Error('Player not found');
} }
@@ -280,7 +291,8 @@ export async function getStatsByGamemode(
export async function getSkillPage( export async function getSkillPage(
skill: SkillName, skill: SkillName,
mode: Gamemode = 'main', mode: Gamemode = 'main',
page: number = 1 page: number = 1,
config?: AxiosRequestConfig
): Promise<PlayerSkillRow[]> { ): Promise<PlayerSkillRow[]> {
if (!GAMEMODES.includes(mode)) { if (!GAMEMODES.includes(mode)) {
throw Error('Invalid game mode'); throw Error('Invalid game mode');
@@ -291,7 +303,10 @@ export async function getSkillPage(
} }
const url = getSkillPageURL(mode, skill, page); const url = getSkillPageURL(mode, skill, page);
const response = await httpGet<string | Buffer | BinaryData | undefined>(url); const response = await httpGet<string | Buffer | BinaryData | undefined>(
url,
config
);
const dom = new JSDOM(response.data); const dom = new JSDOM(response.data);
const playersHTML = dom.window.document.querySelectorAll( const playersHTML = dom.window.document.querySelectorAll(
'.personal-hiscores__row' '.personal-hiscores__row'
@@ -323,12 +338,14 @@ export async function getSkillPage(
* @param activity Name of the activity or boss to fetch hiscores for. * @param activity Name of the activity or boss to fetch hiscores for.
* @param mode Gamemode to fetch ranks for. * @param mode Gamemode to fetch ranks for.
* @param page Page number. * @param page Page number.
* @param config Optional axios request config object.
* @returns Array of `PlayerActivityRow` objects. * @returns Array of `PlayerActivityRow` objects.
*/ */
export async function getActivityPage( export async function getActivityPage(
activity: ActivityName, activity: ActivityName,
mode: Gamemode = 'main', mode: Gamemode = 'main',
page: number = 1 page: number = 1,
config?: AxiosRequestConfig
): Promise<PlayerActivityRow[]> { ): Promise<PlayerActivityRow[]> {
if (!GAMEMODES.includes(mode)) { if (!GAMEMODES.includes(mode)) {
throw Error('Invalid game mode'); throw Error('Invalid game mode');
@@ -339,7 +356,10 @@ export async function getActivityPage(
} }
const url = getActivityPageURL(mode, activity, page); const url = getActivityPageURL(mode, activity, page);
const response = await httpGet<string | Buffer | BinaryData | undefined>(url); const response = await httpGet<string | Buffer | BinaryData | undefined>(
url,
config
);
const dom = new JSDOM(response.data); const dom = new JSDOM(response.data);
const playersHTML = dom.window.document.querySelectorAll( const playersHTML = dom.window.document.querySelectorAll(
'.personal-hiscores__row' '.personal-hiscores__row'

View File

@@ -1,3 +1,5 @@
import { AxiosRequestConfig } from 'axios';
export type Gamemode = export type Gamemode =
| 'main' | 'main'
| 'ironman' | 'ironman'
@@ -183,4 +185,13 @@ export interface GetStatsOptions {
* @defaultvalue `true` * @defaultvalue `true`
*/ */
shouldGetFormattedRsn?: boolean; shouldGetFormattedRsn?: boolean;
/**
* Map of configs for each requests that can take place in the `getStats` function.
*/
axiosConfigs?: Partial<Record<Gamemode, AxiosRequestConfig>> & {
/**
* The axios request config object to use for the RSN format request.
*/
rsn?: AxiosRequestConfig;
};
} }

View File

@@ -1,4 +1,4 @@
import axios from 'axios'; import axios, { AxiosRequestConfig } from 'axios';
import * as ua from 'useragent-generator'; import * as ua from 'useragent-generator';
import { Gamemode, SkillName, ActivityName } from '../types'; import { Gamemode, SkillName, ActivityName } from '../types';
import { import {
@@ -96,12 +96,16 @@ export const rsnFromElement = (el: Element | null) => {
* @param url URL to run a `GET` request against. * @param url URL to run a `GET` request against.
* @returns Axios response. * @returns Axios response.
*/ */
export const httpGet = <Response>(url: string) => export const httpGet = <Response>(
url: string,
config: AxiosRequestConfig = {}
) =>
axios.get<Response>(url, { axios.get<Response>(url, {
headers: { headers: {
// without User-Agent header requests may be rejected by DDoS protection mechanism // without User-Agent header requests may be rejected by DDoS protection mechanism
'User-Agent': ua.firefox(80) 'User-Agent': ua.firefox(80)
} },
...config
}); });
/** /**

4867
yarn.lock

File diff suppressed because it is too large Load Diff