From 31949ae3cb951e8bbef4e7c0bb89d45deadf7b4a Mon Sep 17 00:00:00 2001 From: spacemeowx2 Date: Sun, 4 Dec 2022 03:02:49 +0800 Subject: [PATCH] feat(stat.ink): add status check --- src/exporters/stat.ink.ts | 43 ++++++++++++++------------------------- src/types.ts | 6 ------ 2 files changed, 15 insertions(+), 34 deletions(-) diff --git a/src/exporters/stat.ink.ts b/src/exporters/stat.ink.ts index 68db33b..ae4b0ed 100644 --- a/src/exporters/stat.ink.ts +++ b/src/exporters/stat.ink.ts @@ -22,7 +22,6 @@ import { StatInkPlayer, StatInkPostBody, StatInkPostResponse, - StatInkSpecialWeapon, StatInkStage, StatInkUuidList, StatInkWeapon, @@ -49,6 +48,18 @@ const COOP_POINT_MAP: Record = { 3: 20, }; +async function checkResponse(resp: Response) { + // 200~299 + if (Math.floor(resp.status / 100) !== 2) { + const json = await resp.json().catch(() => undefined); + throw new APIError({ + response: resp, + json, + message: "Failed to fetch data from stat.ink", + }); + } +} + class StatInkAPI { statInk = "https://stat.ink"; FETCH_LOCK = new Mutex(); @@ -75,6 +86,8 @@ class StatInkAPI { : `${this.statInk}/api/v3/salmon/uuid-list`, headers: this.requestHeaders(), }); + await checkResponse(response); + const uuidResult: StatInkUuidList = await response.json(); if (!Array.isArray(uuidResult)) { @@ -163,6 +176,7 @@ class StatInkAPI { url, headers: this.requestHeaders(), }); + await checkResponse(resp); const json = await resp.json(); this.cache[url] = json; return json; @@ -184,29 +198,6 @@ class StatInkAPI { } } - _specialMap = new Map(); - async getSpecialMap() { - if (this._specialMap.size === 0) { - const specials = await this.getSpecial(); - for (const special of specials) { - for ( - const name of Object.values(special.name).flatMap((n) => - this._getAliasName(n) - ) - ) { - const prevKey = this._specialMap.get(name); - if (prevKey !== undefined && prevKey !== special.key) { - console.warn(`Duplicate weapon name: ${name}`); - } - this._specialMap.set(name, special.key); - } - } - if (this._specialMap.size === 0) { - throw new Error("Failed to get salmon weapon map"); - } - } - return this._specialMap; - } _salmonWeaponMap = new Map(); async getSalmonWeaponMap() { if (this._salmonWeaponMap.size === 0) { @@ -230,10 +221,6 @@ class StatInkAPI { } return this._salmonWeaponMap; } - getSpecial = (): Promise => { - // TODO: fix this after stat.ink supports special API - throw new Error("Not implemented"); - }; getSalmonWeapon = () => this._getCached( `${this.statInk}/api/v3/salmon/weapon?full=1`, diff --git a/src/types.ts b/src/types.ts index a5880d8..c58add7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -601,12 +601,6 @@ export type StatInkWeapon = { name: Record; }[]; -// TODO: Change when the API is released -export type StatInkSpecialWeapon = { - key: string; - name: Record; -}[]; - export type StatInkGear = { primary_ability: string; secondary_abilities: (string | null)[];