storage/ClanStore.js

const DataStore = require("./DataStore");
const Clan = require("../structures/Clan");
const { TypeError } = require("../errors");

/**
 * A data store to store Clan models.
 * @extends {DataStore}
 */
class ClanStore extends DataStore {
  constructor(thunderAPI, iterable) {
    super(thunderAPI, iterable, Clan);
  }

  /**
   * Resolves a ClanResolvable to a Clan object.
   * @param {string} clan The ClanResolvable to identify
   * @returns {?Clan}
   */
  resolve(clan) {
    return super.resolve(clan);
  }

  /**
   * Obtains a Clan from the site, or the Clan cache if it's already available
   * @param {string} name Name of the clan
   * @param {boolean} [cache=true] Whether to cache the new Clan object if it isn't already
   * @returns {Promise<Clan>}
   */
  fetch(name, cache = true) {
    if (typeof name === "undefined") return Promise.reject(new TypeError("INVALID_QUERY", "clan", "string"));
    const existing = this.get(name);
    if (existing) return Promise.resolve(existing);

    return this.thunderAPI.rest.request("get", `/en/community/claninfo/${encodeURIComponent(name)}`, "clan").then(data => {
      const clan = new Clan(this.thunderAPI, data);
      if (cache) this.set(name, clan);
      return clan;
    });
  }
}

module.exports = ClanStore;