Browse Source

Merge "Passed tokens may now be promises."

tags/0.0.1
Jenkins 2 years ago
parent
commit
50c952b19a
1 changed files with 35 additions and 14 deletions
  1. 35
    14
      src/keystone.js

+ 35
- 14
src/keystone.js View File

@@ -56,6 +56,30 @@ export default class Keystone {
56 56
     return pointer;
57 57
   }
58 58
 
59
+  /**
60
+   * This method builds common components of a keystone request. It converts any passed token
61
+   * into a promise, resolves the base URL, and then passes the results as an .all() promise,
62
+   * which may be destructured in a followup request.
63
+   *
64
+   * @param {Promise|String} token A promise, or string, representing a token.
65
+   * @returns {Promise} A promise which resolves with [url, token].
66
+   * @private
67
+   */
68
+  _requestComponents (token = null) {
69
+    // Make sure the token is a promise.
70
+    let headerPromise = Promise
71
+      .resolve(token)
72
+      .then((token) => {
73
+        if (token) {
74
+          return {
75
+            'X-Auth-Token': token
76
+          };
77
+        }
78
+        return {};
79
+      });
80
+    return Promise.all([this.serviceEndpoint(), headerPromise]);
81
+  }
82
+
59 83
   /**
60 84
    * Retrieve all the API versions available.
61 85
    *
@@ -186,13 +210,15 @@ export default class Keystone {
186 210
    * @returns {Promise.<T>} A promise which will resolve if the token has been successfully revoked.
187 211
    */
188 212
   tokenRevoke (token, adminToken = null) {
189
-    let headers = {
190
-      'X-Subject-Token': token,
191
-      'X-Auth-Token': adminToken || token
192
-    };
193
-
194
-    return this.serviceEndpoint()
195
-      .then((url) => this.http.httpRequest('DELETE', `${url}auth/tokens`, headers));
213
+    return Promise
214
+      .all([this.serviceEndpoint(), token, adminToken])
215
+      .then(([url, token, adminToken]) => {
216
+        return [url, {
217
+          'X-Subject-Token': token,
218
+          'X-Auth-Token': adminToken || token
219
+        }];
220
+      })
221
+      .then(([url, headers]) => this.http.httpRequest('DELETE', `${url}auth/tokens`, headers));
196 222
   }
197 223
 
198 224
   /**
@@ -202,14 +228,9 @@ export default class Keystone {
202 228
    * @returns {Promise.<T>} A promise which will resolve with the service catalog.
203 229
    */
204 230
   catalogList (token = null) {
205
-    const headers = {};
206
-    if (token) {
207
-      headers['X-Auth-Token'] = token;
208
-    }
209
-
210 231
     return this
211
-      .serviceEndpoint()
212
-      .then((url) => this.http.httpRequest('GET', `${url}auth/catalog`, headers))
232
+      ._requestComponents(token)
233
+      .then(([url, headers]) => this.http.httpRequest('GET', `${url}auth/catalog`, headers))
213 234
       .then((response) => response.json())
214 235
       .then((body) => body.catalog);
215 236
   }

Loading…
Cancel
Save