Move extractAllowedActions and extractSchemaRoute under getResource
authorFrank Bessou <frank.bessou@logilab.fr>
Tue, 18 Apr 2017 13:56:04 +0200
changeset 123 d8396b0cb475
parent 122 a704e7916827
child 124 11d6f7145eb7
Move extractAllowedActions and extractSchemaRoute under getResource
src/Api.js
--- a/src/Api.js	Tue Apr 18 12:22:40 2017 +0200
+++ b/src/Api.js	Tue Apr 18 13:56:04 2017 +0200
@@ -83,6 +83,41 @@
         return resourcePromise;
     }
 
+    extractAllowedActions(response) {
+        if (!response.headers.has('Allow')) {
+            return ['view'];
+        }
+
+        const allowHeader = response.headers.get('Allow');
+        const allowedMethods = allowHeader.split(/[ ,]+/);
+        return allowedMethods.map((method) => {
+            switch(method) {
+                case 'POST':
+                    return 'create';
+                case 'GET':
+                    return 'view';
+                case 'PUT':
+                case 'PATCH':
+                    return 'edit';
+                case 'DELETE':
+                    return 'delete';
+            }
+        });
+    }
+
+    extractSchemaRoute(response) {
+        const linkHeader = response.headers.get("Link");
+        if (linkHeader === null) {
+            throw new Error(`"Link" header does not exist on resource's HTTP header`)
+        }
+        try {
+            const links = LinkParser.parse(linkHeader);
+            return links.rel('describedby')[0].uri;
+        } catch (e) {
+            throw new Error(`Cannot find 'describedby' link in ${linkHeader}`);
+        }
+    }
+
     getRelatedSchema(etype, rtype, role = 'creation', targetType = null) {
         let url = `/${etype}/relationships/${rtype}/schema?role=${role}`;
         if (targetType !== null) {
@@ -124,41 +159,6 @@
         });
     }
 
-    extractAllowedActions(response) {
-        if (!response.headers.has('Allow')) {
-            return ['view'];
-        }
-
-        const allowHeader = response.headers.get('Allow');
-        const allowedMethods = allowHeader.split(/[ ,]+/);
-        return allowedMethods.map((method) => {
-            switch(method) {
-                case 'POST':
-                    return 'create';
-                case 'GET':
-                    return 'view';
-                case 'PUT':
-                case 'PATCH':
-                    return 'edit';
-                case 'DELETE':
-                    return 'delete';
-            }
-        });
-    }
-
-    extractSchemaRoute(response) {
-        const linkHeader = response.headers.get("Link");
-        if (linkHeader === null) {
-            throw new Error(`"Link" header does not exist on resource's HTTP header`)
-        }
-        try {
-            const links = LinkParser.parse(linkHeader);
-            return links.rel('describedby')[0].uri;
-        } catch (e) {
-            throw new Error(`Cannot find 'describedby' link in ${linkHeader}`);
-        }
-    }
-
     createEntity(etype, attributes, ...files) {
         const url = `/${etype}/`;
         const headers = {};