Api use fetch function provided in constructor
authorFrank Bessou <frank.bessou@logilab.fr>
Fri, 31 Mar 2017 13:20:17 +0200
changeset 59 51b18c27cc76
parent 58 31a55e924862
child 60 c7d925006198
Api use fetch function provided in constructor
src/Api.js
--- a/src/Api.js	Thu Mar 30 16:47:17 2017 +0200
+++ b/src/Api.js	Fri Mar 31 13:20:17 2017 +0200
@@ -6,7 +6,7 @@
 
 export class Api {
 
-    constructor(baseUrl) {
+    constructor(baseUrl, fetchFunc) {
         if (baseUrl) {
             this.baseUrl = baseUrl;
         } else if (typeof SCRIPT_NAME !== 'undefined') {
@@ -14,6 +14,8 @@
         } else if (typeof API_URL !== 'undefined') {
             this.baseUrl = API_URL;
         }
+        fetchFunc = fetchFunc || fetch;
+        this.fetch = fetchFunc.bind(window);
     }
 
     buildUrl(path) {
@@ -26,7 +28,7 @@
         const fullUrl = this.buildUrl(url);
         defaultsDeep(options, {credentials: 'same-origin',
             headers: {Accept: 'application/json'}});
-        return fetch(fullUrl, options)
+        return this.fetch(fullUrl, options)
             .then(response => {
                 if(response.headers.get("content-type") &&
                         response.headers.get("content-type").toLowerCase().indexOf("application/json") >= 0) {