]> git.immae.eu Git - github/bastienwirtz/homer.git/commitdiff
Factorize fetch options
authorBastien Wirtz <bastien.wirtz@gmail.com>
Sun, 10 Oct 2021 07:26:02 +0000 (09:26 +0200)
committerBastien Wirtz <bastien.wirtz@gmail.com>
Sun, 10 Oct 2021 07:26:02 +0000 (09:26 +0200)
src/App.vue
src/assets/defaults.yml
src/components/Service.vue
src/components/services/AdGuardHome.vue
src/mixins/service.js [new file with mode: 0644]

index 1f4f5099625a26583174a504bf862d0497e32661..c263c8a0572ee096ba8ca6e17c63cf09568a378f 100644 (file)
@@ -74,7 +74,8 @@
               <Service
                 v-for="(item, index) in group.items"
                 :key="index"
-                v-bind:item="item"
+                :item="item"
+                :proxy="config.proxy"
                 :class="['column', `is-${12 / config.columns}`]"
               />
             </template>
               <Service
                 v-for="(item, index) in group.items"
                 :key="index"
-                v-bind:item="item"
+                :item="item"
+                :proxy="config.proxy"
               />
             </div>
           </div>
index f011346cf174724e730517dff5d589fada0cc628..ae4f52367fa5f3a88b446c9e1cf7c0496cc54a50 100644 (file)
@@ -42,3 +42,6 @@ colors:
 message: ~
 links: []
 services: []
+
+
+proxy: ~
\ No newline at end of file
index 39a9ac41e0df81855e4970f012d775eb2b09a398..25b86d5f9e685924b2dcf2bdd7b251547ed045fd 100644 (file)
@@ -1,5 +1,5 @@
 <template>
-  <component v-bind:is="component" :item="item"></component>
+  <component v-bind:is="component" :item="item" :proxy="proxy"></component>
 </template>
 
 <script>
@@ -9,6 +9,7 @@ export default {
   name: "Service",
   props: {
     item: Object,
+    proxy: Object,
   },
   computed: {
     component() {
index 16881fa59ea6d93ceb43bd4573e785fb1107cce1..b01f0f49a56c9f262bfc45d8409e0ae8fe464ec3 100644 (file)
 </template>
 
 <script>
+import service from "@/mixins/service.js";
 import Generic from "./Generic.vue";
 
 export default {
   name: "AdGuardHome",
+  mixins: [service],
   props: {
     item: Object,
   },
@@ -60,18 +62,14 @@ export default {
   },
   methods: {
     fetchStatus: async function () {
-      this.status = await fetch(`${this.item.url}/control/status`, {
-        credentials: "include",
-      })
-        .then((response) => response.json())
-        .catch((e) => console.log(e));
+      this.status = await this.fetch("/control/status").catch((e) =>
+        console.log(e)
+      );
     },
     fetchStats: async function () {
-      this.stats = await fetch(`${this.item.url}/control/stats`, {
-        credentials: "include",
-      })
-        .then((response) => response.json())
-        .catch((e) => console.log(e));
+      this.stats = await this.fetch("/control/stats").catch((e) =>
+        console.log(e)
+      );
     },
   },
 };
diff --git a/src/mixins/service.js b/src/mixins/service.js
new file mode 100644 (file)
index 0000000..99d8c01
--- /dev/null
@@ -0,0 +1,28 @@
+export default {
+  props: {
+    proxy: Object,
+  },
+  created: function () {
+    // custom service often consume info from an API using the item link (url) as a base url,
+    // but sometimes the base url is different. An optional alternative URL can be provided with the "endpoint" key.
+    this.endpoint = this.item.endpoint || this.item.url;
+  },
+  methods: {
+    fetch: function (path, init) {
+      let options = {};
+
+      if (this.proxy?.useCredentials) {
+        options.credentials = "include";
+      }
+
+      options = Object.assign(options, init);
+
+      return fetch(`${this.endpoint}/${path}`, options).then((response) => {
+        if (!response.ok) {
+          throw new Error("Not 2xx response");
+        }
+        return response.json();
+      });
+    },
+  },
+};