diff options
author | Robin Schneider <45321827+robinschneider@users.noreply.github.com> | 2021-10-10 21:41:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-10 21:41:23 +0200 |
commit | 7a4e78e8d0448541e76f6052c45f00b30cdf60e1 (patch) | |
tree | 77be480ad42c3e1a64005a1974206dc249273bbb /src/mixins | |
parent | 3faeac7e9fc3601283c58e2d585e00f346250e10 (diff) | |
parent | 66a434e7dba49011dd5401e32bb45ab180a73a11 (diff) | |
download | homer-7a4e78e8d0448541e76f6052c45f00b30cdf60e1.tar.gz homer-7a4e78e8d0448541e76f6052c45f00b30cdf60e1.tar.zst homer-7a4e78e8d0448541e76f6052c45f00b30cdf60e1.zip |
Merge branch 'bastienwirtz:main' into icon-color
Diffstat (limited to 'src/mixins')
-rw-r--r-- | src/mixins/service.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/mixins/service.js b/src/mixins/service.js new file mode 100644 index 0000000..17fa6fc --- /dev/null +++ b/src/mixins/service.js | |||
@@ -0,0 +1,43 @@ | |||
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 | if (this.endpoint.endsWith("/")) { | ||
11 | this.endpoint = this.endpoint.slice(0, -1); | ||
12 | } | ||
13 | }, | ||
14 | methods: { | ||
15 | fetch: function (path, init, json = true) { | ||
16 | let options = {}; | ||
17 | |||
18 | if (this.proxy?.useCredentials) { | ||
19 | options.credentials = "include"; | ||
20 | } | ||
21 | |||
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 | |||
28 | options = Object.assign(options, init); | ||
29 | |||
30 | if (path.startsWith("/")) { | ||
31 | path = path.slice(1); | ||
32 | } | ||
33 | |||
34 | return fetch(`${this.endpoint}/${path}`, options).then((response) => { | ||
35 | if (!response.ok) { | ||
36 | throw new Error("Not 2xx response"); | ||
37 | } | ||
38 | |||
39 | return json ? response.json() : response; | ||
40 | }); | ||
41 | }, | ||
42 | }, | ||
43 | }; | ||