brawlstatsjs Documentation

website·Indexed 30 days ago

https://brawlstatsjs.netlify.app

An advanced and lightweight JavaScript wrapper around the Brawl Stars API. The library provides managers for players, clubs, battle logs, rotations, and leaderboards, featuring built-in caching and support for fetching multiple entities simultaneously.

Tokens
5.8K
Snippets
53
Records
71
Agent score
74%

What's inside brawlstatsjs

  1. Understand the brawlstatsjs caching mechanism

    The brawlstatsjs library implements a custom caching system to avoid redundant API calls and improve performance. The system works by extending the Node.js EventEmitter class and utilizing an internal Map to store cached data. To ensure data freshness, the library uses timers to automatically delete cached keys after a specific duration. By default, the cache expires after 10 minutes unless a specific Time-To-Live (TTL) is provided. This prevents the library from returning stale stats after a user has played new games.
  2. Initialize and use the brawlstats Client

    The Client class is the main entry point for interacting with the Brawl Stars API. It requires an API token, which can be passed explicitly in the configuration object or provided via the BRAWLSTARS_TOKEN environment variable.
    import { Client } from 'brawlstats';
    
    const client = new Client({
      /**
       * Your Brawl Stars API key.
       * @defaults to the environment variable `BRAWLSTARS_TOKEN`
       */
      token: 'your-api-key'
    });
    
    // Fetch a player by tag
    const player = await client.players.fetch('#22QJ0JPVJ');
    
    // Fetch a club by tag
    const club = await client.clubs.fetch('#XYZ');
    
    // Get the current events rotation
    const events = await client.rotation.get();
    
    // Access all brawlers available in the game
    const brawlers = client.brawlers;
  3. Fetch data for multiple players simultaneously

    Use the client.players.fetchMany() method to retrieve information for multiple players in a single request. Pass the player tags as a comma-separated list of arguments. Like the single fetch method, tags can be provided as raw strings, prefixed with #, or URL-encoded as %23.
    client.players
      .fetchMany('player-tag-1', 'player-tag-2', 'player-tag-3')
      .then((players) => {
        console.log(players);
      })
      .catch((error) => {
        console.error(error);
      });
  4. Migrate from v2 to v3 manager-based API

    In v3, direct client methods like getBrawlers, getRotation, getPlayer, and getClub have been removed and replaced by a manager system. This system implements internal caching to reduce data call times and provides more specialized fetch methods.
    // v2 (Removed)
    await client.getBrawlers()
    await client.getRotation()
    await client.getPlayer()
    await client.getClub()
    
    // v3 (Replacement)
    client.brawlers // Now synchronous
    await client.rotation.get()
    await client.players.fetch()
    await client.clubs.fetch()
  5. Fetch Player or Club leaderboards

    To retrieve leaderboards for players or clubs, use the client.leaderboards.fetch method. You must provide a mode (either 'players' or 'clubs') and a region (e.g., 'global' or a two-letter country code).
    client.leaderboards
      .fetch({ mode: 'players', region: 'global' }) // Use 'clubs' for club leaderboards
      .then((leaderboard) => {
        console.log(leaderboard);
      })
      .catch((error) => {
        console.error(error);
      });
  6. Fetch information for multiple clubs

    Use the client.clubs.fetchMany() method to retrieve data for multiple clubs simultaneously. Pass the club tags as a comma-separated list of arguments. Similar to single club fetching, tags can be provided as plain strings, prefixed with #, or as %23 encoded strings.
    client.clubs
      .fetchMany('club-tag-1', 'club-tag-2', 'club-tag-3')
      .then((clubs) => {
        console.log(clubs);
      })
      .catch((error) => {
        console.error(error);
      });