diff options
author | Bastien Wirtz <bastien.wirtz@gmail.com> | 2021-10-10 09:26:02 +0200 |
---|---|---|
committer | Bastien Wirtz <bastien.wirtz@gmail.com> | 2021-10-10 09:26:02 +0200 |
commit | 0a3be103dc02182af7cd5e92fdd6130e3198e1a7 (patch) | |
tree | 2808f1b15a98ce197e8a443a2a538866ca7e3c54 /src/mixins/service.js | |
parent | a25e1b1a70649f4a0341056cca0669d4b7d78fb5 (diff) | |
download | homer-0a3be103dc02182af7cd5e92fdd6130e3198e1a7.tar.gz homer-0a3be103dc02182af7cd5e92fdd6130e3198e1a7.tar.zst homer-0a3be103dc02182af7cd5e92fdd6130e3198e1a7.zip |
Factorize fetch options
Diffstat (limited to 'src/mixins/service.js')
-rw-r--r-- | src/mixins/service.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/mixins/service.js b/src/mixins/service.js new file mode 100644 index 0000000..99d8c01 --- /dev/null +++ b/src/mixins/service.js | |||
@@ -0,0 +1,28 @@ | |||
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 | }; | ||