Преглед на файлове

bugFix(restClient): fix non callable iterator error

drac преди 5 години
родител
ревизия
32a4b85b7e
променени са 1 файла, в които са добавени 72 реда и са изтрити 62 реда
  1. 72 62
      webui/ovpm/src/utils/restClient.js

+ 72 - 62
webui/ovpm/src/utils/restClient.js

@@ -1,79 +1,89 @@
-import axios from 'axios';
-import format from 'string-template';
+import axios from "axios";
+import format from "string-template";
 
 // API is an object that acts like a client for remote REST APIs.
 export class API {
-    // @param {String} baseURL - base url for the api, that prefixes the endpoints (e.g. https://api.example.com/v1)
-    // @param {Object{}} endpoints - endpoint objects. (e.g {"login": {"path": "/items/{item_id}", method: "POST"} ... etc})
-    // @param {String} authToken - this token will be used (if provided) with authenticated resources
-    constructor(baseURL, endpoints, authToken) {
-        this.baseURL = baseURL
-        this.endpoints = endpoints
-        this.authToken = authToken
+  // @param {String} baseURL - base url for the api, that prefixes the endpoints (e.g. https://api.example.com/v1)
+  // @param {Object{}} endpoints - endpoint objects. (e.g {"login": {"path": "/items/{item_id}", method: "POST"} ... etc})
+  // @param {String} authToken - this token will be used (if provided) with authenticated resources
+  constructor(baseURL, endpoints, authToken) {
+    this.baseURL = baseURL;
+    this.endpoints = endpoints;
+    this.authToken = authToken;
 
-        // validate endpoints
-        if (this.endpoints.constructor !== Object) {
-            throw "endpoints must be an object: " + this.baseURL
-        } else {
-            for (let endpointName in this.endpoints) {
-                if (endpoints[endpointName].constructor !== Object) {
-                    throw "items of 'endpoints' should be endpoint object: " + endpoints[endpointName]
-                }
-
-                let keysShouldExist = ['path', 'method']
-                for (let key in keysShouldExist) {
-                    if (!(keysShouldExist[key] in endpoints[endpointName])) {
-                        throw "endpoint object should have a key called: " + keysShouldExist[key]
-                    }
-                    if (!(endpoints[endpointName][keysShouldExist[key]].constructor === String)) {
-                        throw "endpoint object should have a key called: " + keysShouldExist[key]
-                    }
-                }
-            }
+    // validate endpoints
+    if (this.endpoints.constructor !== Object) {
+      throw "endpoints must be an object: " + this.baseURL;
+    } else {
+      for (let endpointName in this.endpoints) {
+        if (endpoints[endpointName].constructor !== Object) {
+          throw "items of 'endpoints' should be endpoint object: " +
+            endpoints[endpointName];
         }
 
-        // validate authToken
-        if (this.authToken && this.authToken.constructor !== String) {
-            throw "authToken should be a string: " + this.authToken
+        let keysShouldExist = ["path", "method"];
+        for (let key in keysShouldExist) {
+          if (!(keysShouldExist[key] in endpoints[endpointName])) {
+            throw "endpoint object should have a key called: " +
+              keysShouldExist[key];
+          }
+          if (
+            !(
+              endpoints[endpointName][keysShouldExist[key]].constructor ===
+              String
+            )
+          ) {
+            throw "endpoint object should have a key called: " +
+              keysShouldExist[key];
+          }
         }
+      }
     }
 
-    // setAuthToken receives and stores an Authorization Token
-    setAuthToken(authToken) {
-        if (authToken.constructor !== String) {
-            throw "authToken should be a string: " + authToken
-        }
-        this.authToken = authToken
+    // validate authToken
+    if (this.authToken && this.authToken.constructor !== String) {
+      throw "authToken should be a string: " + this.authToken;
     }
+  }
 
-    // call receives the endpoint name, data and handlers then performs the api call.
-    //
-    // @param {String} endpointName - name of the endpoint which corresponds
-    //   to the key of the endpoints object that is provided when constructing API object
-    // @param {object} data - data to pass
-    // @param {bool} performAuth - if set to true, send Authorization header with the authToken
-    // @param {func} onSuccess - success handler to call
-    // @param {func} onFailure - failure handler to call
-    call(endpointName, data, performAuth, onSuccess, onFailure) {
-        // validate endpointName
-        if (!(endpointName in this.endpoints)) {
-            throw endpointName +" is not available in " + this.endpoints
-        }
+  // setAuthToken receives and stores an Authorization Token
+  setAuthToken(authToken) {
+    if (authToken.constructor !== String) {
+      throw "authToken should be a string: " + authToken;
+    }
+    this.authToken = authToken;
+  }
 
-        let endpoint = this.endpoints[endpointName]
+  // call receives the endpoint name, data and handlers then performs the api call.
+  //
+  // @param {String} endpointName - name of the endpoint which corresponds
+  //   to the key of the endpoints object that is provided when constructing API object
+  // @param {object} data - data to pass
+  // @param {bool} performAuth - if set to true, send Authorization header with the authToken
+  // @param {func} onSuccess - success handler to call
+  // @param {func} onFailure - failure handler to call
+  call(endpointName, data, performAuth, onSuccess, onFailure) {
+    // validate endpointName
+    if (!(endpointName in this.endpoints)) {
+      throw endpointName + " is not available in " + this.endpoints;
+    }
 
-        let callConf = {
-            method: endpoint.method,
-            url: this.baseURL + format(endpoint.path, ...data),
-            data: data,
-        }
+    let endpoint = this.endpoints[endpointName];
 
-        // if auth is true set auth headers
-        if (performAuth) {
-            callConf.headers = {"Authorization": "Bearer " + this.authToken}
-        }
+    let callConf = {
+      method: endpoint.method,
+      url: this.baseURL + format(endpoint.path, { ...data }),
+      data: data
+    };
 
-        // actually perform the call
-        axios(callConf).then(onSuccess).catch(onFailure)
+    // if auth is true set auth headers
+    if (performAuth) {
+      callConf.headers = { Authorization: "Bearer " + this.authToken };
     }
+
+    // actually perform the call
+    axios(callConf)
+      .then(onSuccess)
+      .catch(onFailure);
+  }
 }