storage/UserStore.js

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

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

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

    return this.thunderAPI.rest.request("get", "/en/community/userinfo/", "user", { query: { nick: name } }).then(data => {
      const user = new User(this.thunderAPI, data);
      if (cache) this.set(name, user);
      return user;
    });
  }
}

module.exports = UserStore;