mirror of
https://github.com/maxswa/osrs-json-hiscores.git
synced 2025-10-15 10:19:04 +00:00
Merge pull request #77 from maxswa/axios-config
Add optional axios config argument
This commit is contained in:
@@ -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(
|
||||||
@@ -174,7 +179,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 +195,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 +271,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 +295,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 +307,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 +342,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 +360,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'
|
||||||
|
11
src/types.ts
11
src/types.ts
@@ -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;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user