Compare commits

...

3 Commits

Author SHA1 Message Date
maxswa
79c067152c v2.21.1 2025-02-24 19:46:38 -05:00
Max Swartwout
e51e7bc32e Merge pull request #107 from maxswa/max/rsn-format-errors
Fix RSN format errors, add mode argument.
2025-02-24 19:45:26 -05:00
maxswa
7d7c7f9b08 Fix RSN format errors, add mode. 2025-02-24 19:41:27 -05:00
3 changed files with 27 additions and 7 deletions

View File

@@ -15,7 +15,10 @@ import {
InvalidFormatError,
BH_MODES,
parseJsonStats,
HiscoresResponse
HiscoresResponse,
InvalidRSNError,
PlayerNotFoundError,
HiScoresError
} from '../src/index';
const B0ATY_NAME = 'B0ATY';
@@ -24,6 +27,8 @@ const LYNX_TITAN_SPACE_NAME = 'lYnX tiTaN';
const LYNX_TITAN_UNDERSCORE_NAME = 'lYnX_tiTaN';
const LYNX_TITAN_HYPHEN_NAME = 'lYnX-tiTaN';
const LYNX_TITAN_FORMATTED_NAME = 'Lynx Titan';
const NON_EXISTENT_NAME = 'nonExistent';
const ERROR_NAME = 'errorName';
const attackTopPage = readFileSync(`${__dirname}/attackTopPage.html`, 'utf8');
const b0atyNamePage = readFileSync(`${__dirname}/b0atyNamePage.html`, 'utf8');
@@ -57,6 +62,12 @@ jest.spyOn(axios, 'get').mockImplementation((url) => {
if (getStatsURL('main', LYNX_TITAN_FORMATTED_NAME, true) === url) {
return Promise.resolve({ status: 200, data: lynxTitanStats });
}
if (getPlayerTableURL('main', NON_EXISTENT_NAME) === url) {
return Promise.resolve({ data: '<html></html>' });
}
if (getPlayerTableURL('main', ERROR_NAME)) {
return Promise.reject();
}
throw new Error(`No mock response for URL: ${url}`);
});
@@ -322,7 +333,15 @@ describe('Get name format', () => {
expect(data).toBe(B0ATY_FORMATTED_NAME);
});
it('throws an error for a name with invalid characters', async () => {
await expect(getRSNFormat('b&aty')).rejects.toBeTruthy();
await expect(getRSNFormat('b&aty')).rejects.toThrow(InvalidRSNError);
});
it('throws an error for a non-existent player', async () => {
await expect(getRSNFormat(NON_EXISTENT_NAME)).rejects.toThrow(
PlayerNotFoundError
);
});
it('throws an error for a hiscores issue', async () => {
await expect(getRSNFormat(ERROR_NAME)).rejects.toThrow(HiScoresError);
});
});
@@ -509,7 +528,7 @@ test('Get attack top page', async () => {
test('Get non-existent player', async () => {
getStats('fishy').catch((err) => {
if (err.response) {
if (err?.response) {
expect(err.response.status).toBe(404);
}
});

View File

@@ -1,6 +1,6 @@
{
"name": "osrs-json-hiscores",
"version": "2.21.0",
"version": "2.21.1",
"description": "The Old School Runescape API wrapper that does more!",
"main": "lib/index.js",
"types": "lib/index.d.ts",

View File

@@ -86,11 +86,12 @@ export async function getOfficialStats(
*/
export async function getRSNFormat(
rsn: string,
config?: AxiosRequestConfig
config?: AxiosRequestConfig,
mode: Gamemode = 'main'
): Promise<string> {
validateRSN(rsn);
const url = getPlayerTableURL('main', rsn);
const url = getPlayerTableURL(mode, rsn);
try {
const response = await httpGet<string | Buffer | BinaryData | undefined>(
url,
@@ -103,10 +104,10 @@ export async function getRSNFormat(
if (anchor) {
return rsnFromElement(anchor);
}
throw new PlayerNotFoundError();
} catch {
throw new HiScoresError();
}
throw new PlayerNotFoundError();
}
/**