aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorBastien Wirtz <bastien.wirtz@gmail.com>2021-10-10 09:26:02 +0200
committerBastien Wirtz <bastien.wirtz@gmail.com>2021-10-10 09:26:02 +0200
commit0a3be103dc02182af7cd5e92fdd6130e3198e1a7 (patch)
tree2808f1b15a98ce197e8a443a2a538866ca7e3c54
parenta25e1b1a70649f4a0341056cca0669d4b7d78fb5 (diff)
downloadhomer-0a3be103dc02182af7cd5e92fdd6130e3198e1a7.tar.gz
homer-0a3be103dc02182af7cd5e92fdd6130e3198e1a7.tar.zst
homer-0a3be103dc02182af7cd5e92fdd6130e3198e1a7.zip
Factorize fetch options
-rw-r--r--src/App.vue6
-rw-r--r--src/assets/defaults.yml3
-rw-r--r--src/components/Service.vue3
-rw-r--r--src/components/services/AdGuardHome.vue18
-rw-r--r--src/mixins/service.js28
5 files changed, 45 insertions, 13 deletions
diff --git a/src/App.vue b/src/App.vue
index 1f4f509..c263c8a 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -74,7 +74,8 @@
74 <Service 74 <Service
75 v-for="(item, index) in group.items" 75 v-for="(item, index) in group.items"
76 :key="index" 76 :key="index"
77 v-bind:item="item" 77 :item="item"
78 :proxy="config.proxy"
78 :class="['column', `is-${12 / config.columns}`]" 79 :class="['column', `is-${12 / config.columns}`]"
79 /> 80 />
80 </template> 81 </template>
@@ -102,7 +103,8 @@
102 <Service 103 <Service
103 v-for="(item, index) in group.items" 104 v-for="(item, index) in group.items"
104 :key="index" 105 :key="index"
105 v-bind:item="item" 106 :item="item"
107 :proxy="config.proxy"
106 /> 108 />
107 </div> 109 </div>
108 </div> 110 </div>
diff --git a/src/assets/defaults.yml b/src/assets/defaults.yml
index f011346..ae4f523 100644
--- a/src/assets/defaults.yml
+++ b/src/assets/defaults.yml
@@ -42,3 +42,6 @@ colors:
42message: ~ 42message: ~
43links: [] 43links: []
44services: [] 44services: []
45
46
47proxy: ~ \ No newline at end of file
diff --git a/src/components/Service.vue b/src/components/Service.vue
index 39a9ac4..25b86d5 100644
--- a/src/components/Service.vue
+++ b/src/components/Service.vue
@@ -1,5 +1,5 @@
1<template> 1<template>
2 <component v-bind:is="component" :item="item"></component> 2 <component v-bind:is="component" :item="item" :proxy="proxy"></component>
3</template> 3</template>
4 4
5<script> 5<script>
@@ -9,6 +9,7 @@ export default {
9 name: "Service", 9 name: "Service",
10 props: { 10 props: {
11 item: Object, 11 item: Object,
12 proxy: Object,
12 }, 13 },
13 computed: { 14 computed: {
14 component() { 15 component() {
diff --git a/src/components/services/AdGuardHome.vue b/src/components/services/AdGuardHome.vue
index 16881fa..b01f0f4 100644
--- a/src/components/services/AdGuardHome.vue
+++ b/src/components/services/AdGuardHome.vue
@@ -20,10 +20,12 @@
20</template> 20</template>
21 21
22<script> 22<script>
23import service from "@/mixins/service.js";
23import Generic from "./Generic.vue"; 24import Generic from "./Generic.vue";
24 25
25export default { 26export default {
26 name: "AdGuardHome", 27 name: "AdGuardHome",
28 mixins: [service],
27 props: { 29 props: {
28 item: Object, 30 item: Object,
29 }, 31 },
@@ -60,18 +62,14 @@ export default {
60 }, 62 },
61 methods: { 63 methods: {
62 fetchStatus: async function () { 64 fetchStatus: async function () {
63 this.status = await fetch(`${this.item.url}/control/status`, { 65 this.status = await this.fetch("/control/status").catch((e) =>
64 credentials: "include", 66 console.log(e)
65 }) 67 );
66 .then((response) => response.json())
67 .catch((e) => console.log(e));
68 }, 68 },
69 fetchStats: async function () { 69 fetchStats: async function () {
70 this.stats = await fetch(`${this.item.url}/control/stats`, { 70 this.stats = await this.fetch("/control/stats").catch((e) =>
71 credentials: "include", 71 console.log(e)
72 }) 72 );
73 .then((response) => response.json())
74 .catch((e) => console.log(e));
75 }, 73 },
76 }, 74 },
77}; 75};
diff --git a/src/mixins/service.js b/src/mixins/service.js
new file mode 100644
index 0000000..99d8c01
--- /dev/null
+++ b/src/mixins/service.js
@@ -0,0 +1,28 @@
1export default {
2 props: {
3 proxy: Object,
4 },
5 created: function () {
6 // custom service often consume info from an API using the item link (url) as a base url,
7 // but sometimes the base url is different. An optional alternative URL can be provided with the "endpoint" key.
8 this.endpoint = this.item.endpoint || this.item.url;
9 },
10 methods: {
11 fetch: function (path, init) {
12 let options = {};
13
14 if (this.proxy?.useCredentials) {
15 options.credentials = "include";
16 }
17
18 options = Object.assign(options, init);
19
20 return fetch(`${this.endpoint}/${path}`, options).then((response) => {
21 if (!response.ok) {
22 throw new Error("Not 2xx response");
23 }
24 return response.json();
25 });
26 },
27 },
28};