Allow to read HTTP Link from a resource
authorPhilippe Pepiot <philippe.pepiot@logilab.fr>
Wed, 24 May 2017 17:38:42 +0200
changeset 222 46b85a896c43
parent 221 714b33df76bb
child 223 b7000ff42372
Allow to read HTTP Link from a resource
src/model.js
src/services/hypermedia.js
--- a/src/model.js	Tue May 30 17:37:41 2017 +0200
+++ b/src/model.js	Wed May 24 17:38:42 2017 +0200
@@ -13,6 +13,7 @@
     url: PropTypes.string.isRequired,
     data: PropTypeJsonaryWrapper.isRequired,
     allowedActions: PropTypes.arrayOf(PropTypeAction).isRequired,
+    links: PropTypes.object.isRequired,
 };
 
 export const PropTypesResourceModel = PropTypes.shape(RESOURCE_PROPERTIES);
--- a/src/services/hypermedia.js	Tue May 30 17:37:41 2017 +0200
+++ b/src/services/hypermedia.js	Wed May 24 17:38:42 2017 +0200
@@ -27,7 +27,8 @@
         const dataResponsePromise = this.jsonFetchResponse(resource.url);
         const resourcePromise = dataResponsePromise.then(response => {
             resource.allowedActions = this.extractAllowedActions(response);
-            const schemaRoute = this.extractSchemaRoute(response);
+            resource.links = this.extractLinks(response);
+            const schemaRoute = this.extractSchemaRoute(resource.links);
             const schemaPromise = this.getSchema(schemaRoute);
             const dataPromise = response.json().catch(()=>null);
 
@@ -83,17 +84,20 @@
         });
     }
 
-    extractSchemaRoute(response) {
+    extractSchemaRoute(links) {
+        try {
+            return links.rel('describedby')[0].uri;
+        } catch (e) {
+            throw new Error(`Cannot find 'describedby' link`);
+        }
+    }
+
+    extractLinks(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}`);
-        }
+        return LinkParser.parse(linkHeader);
     }
 
     jsonFetchResponse(url, options = {}) {