All files / libs api.ts

53.97% Statements 34/63
54.17% Branches 13/24
53.33% Functions 16/30
53.97% Lines 34/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269          1x 1x 1x                           1x   1x   1x 21x     21x 21x       4x 1x     3x           3x 2x 2x   1x         1x       1x       1x   1x   1x                   1x       1x           1x           1x       1x       1x             1x             1x             1x             1x             1x           1x                                                                                                                                                                                                                                                         12x      
/**
 * @packageDocumentation
 * @module ErBsClient
 **/
 
import { GameModes } from '../constants/GameModes';
import { MetaTypes } from '../constants/MetaTypes';
import { ROUTES } from '../constants/Routes';
import { IArmor } from '../interfaces/IArmor';
import { ICharacterAttribute } from '../interfaces/ICharacterAttribute';
import { ICharacterData } from '../interfaces/ICharacterData';
import { ICharacterLevelUpStat } from '../interfaces/ICharacterLevelUpStat';
import { IConsumable } from '../interfaces/IConsumable';
import { IItemLocation } from '../interfaces/IItemLocation';
import { IItemMisc } from '../interfaces/IItemMisc';
import { IItemSpawns } from '../interfaces/IItemSpawns';
import { ISpecialItem } from '../interfaces/ISpecialItem';
import { IUserGameHistory } from '../interfaces/IUserGameHistory';
import { IUserRank } from '../interfaces/IUserRank';
import { IUserRecord } from '../interfaces/IUserRecord';
import { IWeapon } from '../interfaces/IWeapon';
import fetch from 'node-fetch';
 
let metaCache = {};
 
export class ErBsClient {
  private baseURL = 'https://open-api.bser.io';
 
  constructor(
    private apiKey = process.env.API_KEY,
    private apiVersion = process.env.API_VER || 'v1'
  ) {}
 
  private async call<T = any>(route, complete = false) {
    if (!this.apiKey) {
      throw new Error('No Api Key Passed In');
    }
 
    const response = await fetch(`${this.baseURL}/${this.apiVersion}${route}`, {
      headers: {
        'x-api-key': this.apiKey || process.env.API_KEY
      }
    });
 
    if (response.ok) {
      const results = await response.json();
      return complete ? results : (results.data as T);
    } else {
      throw new Error(await response.text());
    }
  }
 
  private generateDataRoute(type: MetaTypes) {
    return ROUTES.data.default.replace('{metaType}', type);
  }
 
  public async getMetaData(type: MetaTypes) {
    Iif (metaCache[type]) {
      return metaCache[type];
    }
 
    const data = await this.call(this.generateDataRoute(type));
 
    metaCache[type] = data;
 
    return data;
  }
 
  public async getMetaDataHash() {
    const route = ROUTES.data.default.replace('{metaType}', 'hash');
 
    return await this.call(route);
  }
 
  public async getCharacterData() {
    return (await this.getMetaData(MetaTypes.Character)) as ICharacterData[];
  }
 
  public async getCharacterAttributes() {
    return (await this.getMetaData(
      MetaTypes.CharacterAttributes
    )) as ICharacterAttribute[];
  }
 
  public async getCharacterLevelUpStats() {
    return (await this.getMetaData(
      MetaTypes.CharacterLevelUpStat
    )) as ICharacterLevelUpStat[];
  }
 
  public async getItemSpawns() {
    return (await this.getMetaData(MetaTypes.ItemSpawn)) as IItemSpawns[];
  }
 
  public async getArmors() {
    return (await this.getMetaData(MetaTypes.ItemArmor)) as IArmor[];
  }
 
  public async getWeapons() {
    return (await this.getMetaData(MetaTypes.ItemWeapon)) as IWeapon[];
  }
 
  /**
   * Returns all consumables (foods and drinks)
   */
  public async getConsumables() {
    return (await this.getMetaData(MetaTypes.ItemConsumable)) as IConsumable[];
  }
 
  /**
   * Returns all misc (material) items
   */
  public async getMaterials() {
    return (await this.getMetaData(MetaTypes.ItemMisc)) as IItemMisc[];
  }
 
  /**
   * Returns all special (trap) items
   */
  public async getSpecialItems() {
    return (await this.getMetaData(MetaTypes.ItemSpecial)) as ISpecialItem[];
  }
 
  /**
   * Returns where all items can be found or looted
   */
  public async getItemLocations() {
    return (await this.getMetaData(MetaTypes.HowToFindItem)) as IItemLocation[];
  }
 
  /**
   * Returns all characters and their attributes and level up stats
   */
  public async getCharacters() {
    const [data, attributes, stats] = await Promise.all([
      this.getCharacterData(),
      this.getCharacterAttributes(),
      this.getCharacterLevelUpStats()
    ]);
 
    return data.map((character) => {
      const { code } = character;
 
      return {
        ...character,
        levelUpStats: stats
          .filter((stat) => stat.code === code)
          .map(({ code, name, ...stat }) => stat),
        attributes: attributes
          .filter(({ characterCode }) => characterCode === code)
          .map(({ characterCode, character, ...attr }) => attr)
      };
    });
  }
 
  /**
   * Gets top 1000 plkayers for a given season and mode
   *
   * @param season ER:BS Season Number
   * @param mode Solos/Duos/Squads
   */
  public async getTopPlayers(season: number, mode: GameModes) {
    if (season === null || season === undefined || !mode) {
      throw new Error(
        `Invalid Parameters Passed to getTopPlayers: Season = ${season} and Mode = ${mode}`
      );
    }
    const route = ROUTES.rank.top
      .replace('{seasonId}', season.toString())
      .replace('{matchingTeamMode}', mode.toString());
 
    const response = await this.call(route, true);
 
    return response.topRanks as IUserRank[];
  }
 
  /**
   * Shorthand for getTopPlayers(season, GameModes.Solos)
   * @param season ER:BS Season Number
   */
  public async getTopSolos(season = 0) {
    return await this.getTopPlayers(season, GameModes.Solo);
  }
 
  /**
   * Shorthand for getTopPlayers(season, GameModes.Duos)
   * @param season ER:BS Season Number
   */
  public async getTopDuos(season = 0) {
    return await this.getTopPlayers(season, GameModes.Duos);
  }
 
  /**
   * Shorthand for getTopPlayers(season, GameModes.Squads)
   * @param season ER:BS Season Number
   */
  public async getTopSquads(season = 0) {
    return await this.getTopPlayers(season, GameModes.Squads);
  }
 
  /**
   * Get rank for a given player in given season and mode
   *
   * @param userId ER:BS userNum
   * @param season ER:BS Season Number
   * @param mode Solos/Duos/Squads
   */
  public async getRankForPlayer(
    userId: number,
    season: number,
    mode: GameModes
  ) {
    const route = ROUTES.rank.user
      .replace('{seasonId}', season.toString())
      .replace('{matchingTeamMode}', mode.toString())
      .replace('{userNum}', userId.toString());
 
    const response = await this.call(route, true);
 
    return response.userRank as IUserRank;
  }
 
  /**
   * Returns all games for the selected player (pagination WIP)
   *
   * @param userId ER:BS userNum
   */
  public async getGamesForPlayer(userId: number) {
    const route = ROUTES.user.games.replace('{userNum}', userId.toString());
 
    const response = await this.call(route, true);
 
    return response.userGames as IUserGameHistory[];
  }
 
  /**
   * Returns all records for a given player/season combination
   *
   * @param userId ER:BS userNum
   * @param season ER:BS Season Number
   */
  public async getPlayerRecord(userId: number, season = 0) {
    const route = ROUTES.user.stats
      .replace('{seasonId}', season.toString())
      .replace('{userNum}', userId.toString());
 
    const response = await this.call(route, true);
 
    return response.userStats as IUserRecord[];
  }
 
  /**
   * Player number lookup
   *
   * @param name Player name
   */
  public async getPlayerNumber(name: string) {
    const route = ROUTES.user.number.replace('{name}', name);
 
    const response = await this.call(route, true);
 
    return response.user as { userNum: number; nickname: string };
  }
 
  public flushCache() {
    metaCache = {};
  }
}