aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/mixins/service.js
blob: 99d8c0187b21791bf728f126a01afc2a82ec6dfd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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();
      });
    },
  },
};