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));
|
|
|
|
}
|
|
|
|
|
|
|
|
return Api.users('', { username })
|
2018-03-17 18:26:18 +05:30
|
|
|
.then(({ data }) => {
|
|
|
|
if (!data.length) {
|
2017-09-10 17:25:29 +05:30
|
|
|
throw new Error(`User "${username}" could not be found!`);
|
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if (data.length > 1) {
|
2017-09-10 17:25:29 +05:30
|
|
|
throw new Error(`Expected username "${username}" to be unique!`);
|
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
const user = data[0];
|
2017-09-10 17:25:29 +05:30
|
|
|
this.internalStorage[username] = user;
|
|
|
|
return user;
|
|
|
|
});
|
|
|
|
// missing catch is intentional, error handling depends on use case
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new UsersCache();
|