mirror of
https://github.com/bastienwirtz/homer.git
synced 2024-12-24 15:48:55 +01:00
Factorize fetch options
This commit is contained in:
parent
a25e1b1a70
commit
0a3be103dc
@ -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>
|
||||
@ -102,7 +103,8 @@
|
||||
<Service
|
||||
v-for="(item, index) in group.items"
|
||||
:key="index"
|
||||
v-bind:item="item"
|
||||
:item="item"
|
||||
:proxy="config.proxy"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -42,3 +42,6 @@ colors:
|
||||
message: ~
|
||||
links: []
|
||||
services: []
|
||||
|
||||
|
||||
proxy: ~
|
@ -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() {
|
||||
|
@ -20,10 +20,12 @@
|
||||
</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)
|
||||
);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
28
src/mixins/service.js
Normal file
28
src/mixins/service.js
Normal file
@ -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();
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
Loading…
Reference in New Issue
Block a user