summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/lib/utils/users_cache.js
blob: b01ec6b81a386614a8a0ac630101fdab54ffd050 (plain)
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
import Api from '../../api';
import Cache from './cache';

class UsersCache extends Cache {
  retrieve(username) {
    if (this.hasData(username)) {
      return Promise.resolve(this.get(username));
    }

    return Api.users('', { username })
      .then(({ data }) => {
        if (!data.length) {
          throw new Error(`User "${username}" could not be found!`);
        }

        if (data.length > 1) {
          throw new Error(`Expected username "${username}" to be unique!`);
        }

        const user = data[0];
        this.internalStorage[username] = user;
        return user;
      });
      // missing catch is intentional, error handling depends on use case
  }
}

export default new UsersCache();