]> git.immae.eu Git - github/bastienwirtz/homer.git/blob - src/mixins/service.js
Factorize fetch options
[github/bastienwirtz/homer.git] / src / mixins / service.js
1 export 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 };