From 0a3be103dc02182af7cd5e92fdd6130e3198e1a7 Mon Sep 17 00:00:00 2001 From: Bastien Wirtz Date: Sun, 10 Oct 2021 09:26:02 +0200 Subject: Factorize fetch options --- src/mixins/service.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/mixins/service.js (limited to 'src/mixins/service.js') 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 @@ +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(); + }); + }, + }, +}; -- cgit v1.2.3 From efc2bbb85673fb269e75d64655e433bed90a995a Mon Sep 17 00:00:00 2001 From: Bastien Wirtz Date: Sun, 10 Oct 2021 10:16:18 +0200 Subject: Allow any service to override the credentials option --- src/mixins/service.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/mixins/service.js') diff --git a/src/mixins/service.js b/src/mixins/service.js index 99d8c01..ae23a27 100644 --- a/src/mixins/service.js +++ b/src/mixins/service.js @@ -6,6 +6,10 @@ export default { // 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; + + if (this.endpoint.endsWith("/")) { + this.endpoint = this.endpoint.slice(0, -1); + } }, methods: { fetch: function (path, init) { @@ -15,8 +19,18 @@ export default { options.credentials = "include"; } + // Each item can override the credential settings + if (this.item.useCredentials !== undefined) { + options.credentials = + this.item.useCredentials === true ? "include" : "omit"; + } + options = Object.assign(options, init); + if (path.startsWith("/")) { + path = path.slice(1); + } + return fetch(`${this.endpoint}/${path}`, options).then((response) => { if (!response.ok) { throw new Error("Not 2xx response"); -- cgit v1.2.3 From 2fba043575e6a8db94434986c1781d2716c8f6d4 Mon Sep 17 00:00:00 2001 From: Bastien Wirtz Date: Sun, 10 Oct 2021 10:37:20 +0200 Subject: Allow non json reponse in fetch. --- src/mixins/service.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/mixins/service.js') diff --git a/src/mixins/service.js b/src/mixins/service.js index ae23a27..17fa6fc 100644 --- a/src/mixins/service.js +++ b/src/mixins/service.js @@ -12,7 +12,7 @@ export default { } }, methods: { - fetch: function (path, init) { + fetch: function (path, init, json = true) { let options = {}; if (this.proxy?.useCredentials) { @@ -35,7 +35,8 @@ export default { if (!response.ok) { throw new Error("Not 2xx response"); } - return response.json(); + + return json ? response.json() : response; }); }, }, -- cgit v1.2.3