debian-mirror-gitlab/app/assets/javascripts/lib/utils/users_cache.js

28 lines
692 B
JavaScript
Raw Normal View History

2017-09-10 17:25:29 +05:30
import Api from '../../api';
import Cache from './cache';
class UsersCache extends Cache {
retrieve(username) {
if (this.hasData(username)) {
return Promise.resolve(this.get(username));
}
2018-12-13 13:39:08 +05:30
return Api.users('', { username }).then(({ data }) => {
if (!data.length) {
throw new Error(`User "${username}" could not be found!`);
}
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
if (data.length > 1) {
throw new Error(`Expected username "${username}" to be unique!`);
}
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
const user = data[0];
this.internalStorage[username] = user;
return user;
});
// missing catch is intentional, error handling depends on use case
2017-09-10 17:25:29 +05:30
}
}
export default new UsersCache();