]> git.immae.eu Git - github/bastienwirtz/homer.git/blobdiff - src/mixins/service.js
Added url and apikey config options under proxy
[github/bastienwirtz/homer.git] / src / mixins / service.js
index abc708c9d326549aa74ab4ac330e92bd49f3ca3e..b47f756586a8f5f428bfa7d75ebd1ea8fde6dc14 100644 (file)
@@ -1,3 +1,5 @@
+const merge = require("lodash.merge");
+
 export default {
   props: {
     proxy: Object,
@@ -25,18 +27,28 @@ export default {
           this.item.useCredentials === true ? "include" : "omit";
       }
 
-      options = Object.assign(options, init);
+      if (this.proxy?.apikey) {
+        options.headers = {
+          "X-Homer-Api-Key": this.proxy.apikey,
+        };
+      }
 
       if (path.startsWith("/")) {
         path = path.slice(1);
       }
 
-      let url = this.endpoint;
+      let url = path ? `${this.endpoint}/${path}` : this.endpoint;
 
-      if (path) {
-        url = `${this.endpoint}/${path}`;
+      if (this.proxy?.url) {
+        options.headers = {
+          ...(options.headers || {}),
+          "X-Homer-Api-Url": url,
+        };
+        url = this.proxy.url;
       }
 
+      options = merge(options, init);
+
       return fetch(url, options).then((response) => {
         if (!response.ok) {
           throw new Error("Not 2xx response");