]>
Commit | Line | Data |
---|---|---|
0a3be103 BW |
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; | |
efc2bbb8 BW |
9 | |
10 | if (this.endpoint.endsWith("/")) { | |
11 | this.endpoint = this.endpoint.slice(0, -1); | |
12 | } | |
0a3be103 BW |
13 | }, |
14 | methods: { | |
2fba0435 | 15 | fetch: function (path, init, json = true) { |
0a3be103 BW |
16 | let options = {}; |
17 | ||
18 | if (this.proxy?.useCredentials) { | |
19 | options.credentials = "include"; | |
20 | } | |
21 | ||
efc2bbb8 BW |
22 | // Each item can override the credential settings |
23 | if (this.item.useCredentials !== undefined) { | |
24 | options.credentials = | |
25 | this.item.useCredentials === true ? "include" : "omit"; | |
26 | } | |
27 | ||
d48fc7ca | 28 | options = Object.assign(options, init); |
0a3be103 | 29 | |
efc2bbb8 BW |
30 | if (path.startsWith("/")) { |
31 | path = path.slice(1); | |
32 | } | |
33 | ||
d48fc7ca | 34 | let url = this.endpoint; |
1340a8e6 | 35 | |
d48fc7ca ES |
36 | if (path) { |
37 | url = `${this.endpoint}/${path}`; | |
1340a8e6 JW |
38 | } |
39 | ||
40 | return fetch(url, options).then((response) => { | |
0a3be103 BW |
41 | if (!response.ok) { |
42 | throw new Error("Not 2xx response"); | |
43 | } | |
2fba0435 BW |
44 | |
45 | return json ? response.json() : response; | |
0a3be103 BW |
46 | }); |
47 | }, | |
48 | }, | |
49 | }; |