Add Api.getResource method
authorFrank Bessou <frank.bessou@logilab.fr>
Tue, 04 Apr 2017 15:24:01 +0200
changeset 72 dec42889e222
parent 71 407e49c80ec2
child 73 a366599c0e9a
Add Api.getResource method
src/Api.js
--- a/src/Api.js	Tue Apr 04 14:10:49 2017 +0200
+++ b/src/Api.js	Tue Apr 04 15:24:01 2017 +0200
@@ -69,6 +69,24 @@
         return this.jsonSchemaFetch(url);
     }
 
+    getResource(resourceUrl) {
+        const schemaPromise = this.getSchema(resourceUrl);
+        const fetchPromise = this.jsonFetchResponse(resourceUrl);
+        const dataPromise = fetchPromise.then( response => response.json() );
+        const allowedActionsPromise = fetchPromise.then( response => this.extractAllowedActions(response) );
+
+        const entityPromise = Promise.all([schemaPromise, dataPromise, allowedActionsPromise]).then(
+                ([schema, data, allowedActions]) => {
+                    const wrappedData = wrapEntityData(data, schema);
+                    return {
+                        url: resourceUrl,
+                        data: wrappedData,
+                        allowedActions: allowedActions,
+                    };
+                });
+        return entityPromise;
+    }
+
     getRelatedSchema(etype, rtype, role = 'creation', targetType = null) {
         let url = `/${etype}/relationships/${rtype}/schema?role=${role}`;
         if (targetType !== null) {
@@ -101,25 +119,12 @@
     getEntity(etype, eid) {
         const route = `/${etype}/${eid}`;
         const url = route;
-
-        const schemaPromise = this.getSchema(url);
-        const fetchPromise = this.jsonFetchResponse(url);
-        const dataPromise = fetchPromise.then( response => response.json() );
-        const allowedActionsPromise = fetchPromise.then( response => this.extractAllowedActions(response) );
-
-        const entityPromise = Promise.all([schemaPromise, dataPromise, allowedActionsPromise]).then(
-                ([schema, data, allowedActions]) => {
-                    const wrappedData = wrapEntityData(data, schema);
-                    return {
-                        url: route,
-                        route: route,
-                        etype: etype,
-                        eid: eid,
-                        data: wrappedData,
-                        allowedActions: allowedActions,
-                    };
-                });
-        return entityPromise;
+        return this.getResource(url).then(resource => {
+            resource.etype = etype;
+            resource.eid = eid;
+            resource.route = route;
+            return resource;
+        });
     }
 
     extractAllowedActions(response) {