Add jsonFetchResponse to Api
authorFrank Bessou <frank.bessou@logilab.fr>
Mon, 03 Apr 2017 13:42:11 +0200
changeset 62 7708e602ffde
parent 61 dae8abf45c58
child 63 4ab0deb4a9f4
Add jsonFetchResponse to Api It is useful to process response headers for example.
src/Api.js
--- a/src/Api.js	Fri Mar 31 14:57:56 2017 +0200
+++ b/src/Api.js	Mon Apr 03 13:42:11 2017 +0200
@@ -24,7 +24,7 @@
         return prefix + uri;
     }
 
-    jsonFetch(url, options = {}) {
+    jsonFetchResponse(url, options = {}) {
         const fullUrl = this.buildUrl(url);
         defaultsDeep(options, {credentials: 'same-origin',
             headers: {Accept: 'application/json'}});
@@ -32,7 +32,7 @@
             .then(response => {
                 if(response.headers.get("content-type") &&
                         response.headers.get("content-type").toLowerCase().indexOf("application/json") >= 0) {
-                    return response.json();
+                    return response;
                 }
                 const method = options.method || 'GET';
                 throw new Error(
@@ -40,6 +40,10 @@
             });
     }
 
+    jsonFetch(url, options = {}) {
+        return this.jsonFetchResponse(url, options).then(response => response.json());
+    }
+
     jsonSchemaFetch(url, options = {}) {
         defaultsDeep(options, {headers: {Accept: 'application/schema+json'}});
         return this.jsonFetch(url, options);