Do not look for the presence of a self link to retrieve resource
authorFrank Bessou <frank.bessou@logilab.fr>
Thu, 13 Apr 2017 11:28:25 +0200
changeset 114 82e5abf95f92
parent 113 8b4ab8ddb35d
child 115 706f374811ab
Do not look for the presence of a self link to retrieve resource
src/Api.js
test/index.js
--- a/src/Api.js	Thu Apr 13 15:32:33 2017 +0200
+++ b/src/Api.js	Thu Apr 13 11:28:25 2017 +0200
@@ -1,4 +1,4 @@
-/* global fetch, API_URL, SCRIPT_NAME, Jsonary*/
+/* global fetch, API_URL, SCRIPT_NAME */
 
 import 'whatwg-fetch';
 import {isEmpty} from 'lodash/lang';
@@ -64,22 +64,12 @@
     }
 
     getResource(resourceRoute) {
-        const resource = {};
+        const resource = {url: resourceRoute};
         // Fetch resource schema
         const schemaPromise = this.getSchema(appendPath(resourceRoute, '/schema'));
         const resourcePromise = schemaPromise.then(schema => {
-            const wrappedSchema = Jsonary.createSchema(schema);
-            const selfLink = wrappedSchema.getLink('self');
-            // If the schema does not contain self link
-            // The resource is empty
-            if (!selfLink) {
-                resource.data = wrapEntityData(null, schema);
-                resource.allowedActions = [];
-                return resource;
-            }
-            resource.url = selfLink.uriTemplate.fill();
             const fetchPromise = this.jsonFetchResponse(resource.url);
-            const dataPromise = fetchPromise.then( response => response.json() );
+            const dataPromise = fetchPromise.then( response => response.json() ).catch(() => null);
             const allowedActionsPromise = fetchPromise.then( response => this.extractAllowedActions(response) );
             // Load resource data and retrieve allowed actions
             return Promise.all([dataPromise, allowedActionsPromise]).then(
--- a/test/index.js	Thu Apr 13 15:32:33 2017 +0200
+++ b/test/index.js	Thu Apr 13 11:28:25 2017 +0200
@@ -402,8 +402,8 @@
     describe('getRoot', () => {
         it('should fetch the root schema ', done => {
             const rootSchema = JSON.stringify({foo: 'bar'});
-            const fakeFetch = sinon.stub();
             const options = {headers: {"Content-Type": "application/json"}};
+            const fakeFetch = sinon.stub().resolves(new Response('null', options));
             fakeFetch.withArgs(baseUrl+'/schema', sinon.match({}))
                     .resolves(new Response(rootSchema, options));
             const api = new Api(baseUrl, fakeFetch);
@@ -448,33 +448,6 @@
                 done();
             }).catch(done);
         });
-
-        it('should not fetch data if the schema does not contain a self link', done => {
-            api.getResource('').then( _ => {
-
-                assert.calledOnce(fakeFetch);
-                assert.calledWith(fakeFetch, baseUrl + '/schema');
-                done();
-            }).catch(done);
-        });
-
-        it('should fetch resource specified in schema\'s self link when it exists', done => {
-            remoteResource.schema = {
-                links: [{
-                    rel: 'self',
-                    href: '/bar',
-                }],
-            }
-
-            api.getResource('/foo').then( _ => {
-
-                assert.calledTwice(fakeFetch);
-                assert.calledWith(fakeFetch, baseUrl + '/foo/schema');
-                assert.calledWith(fakeFetch, baseUrl + '/bar');
-                done();
-            }).catch(done);
-        });
-
     });
 
     describe('getEntity', () => {