summaryrefslogtreecommitdiff
path: root/app/assets/javascripts
diff options
context:
space:
mode:
authorwinh <winnie@gitlab.com>2017-05-16 12:56:28 +0200
committerwinh <winnie@gitlab.com>2017-05-23 10:45:59 +0200
commitd7f9b408bf330ec53d641525dd4905f9344bf4e0 (patch)
tree6ca416a4496662bb82ec1c62ce0c43f30d430a33 /app/assets/javascripts
parentc102656736ad4d8ea24b66b93a76234cff11e4bb (diff)
downloadgitlab-ce-d7f9b408bf330ec53d641525dd4905f9344bf4e0.tar.gz
Add frontend UsersCache class (!11404)winh-frontend-user-cache
Diffstat (limited to 'app/assets/javascripts')
-rw-r--r--app/assets/javascripts/lib/utils/users_cache.js28
1 files changed, 28 insertions, 0 deletions
diff --git a/app/assets/javascripts/lib/utils/users_cache.js b/app/assets/javascripts/lib/utils/users_cache.js
new file mode 100644
index 00000000000..88f8a622c00
--- /dev/null
+++ b/app/assets/javascripts/lib/utils/users_cache.js
@@ -0,0 +1,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((users) => {
+ if (!users.length) {
+ throw new Error(`User "${username}" could not be found!`);
+ }
+
+ if (users.length > 1) {
+ throw new Error(`Expected username "${username}" to be unique!`);
+ }
+
+ const user = users[0];
+ this.internalStorage[username] = user;
+ return user;
+ });
+ // missing catch is intentional, error handling depends on use case
+ }
+}
+
+export default new UsersCache();