]> git.immae.eu Git - github/bastienwirtz/homer.git/blobdiff - src/mixins/service.js
No authentication required for public endpoint
[github/bastienwirtz/homer.git] / src / mixins / service.js
index 99d8c0187b21791bf728f126a01afc2a82ec6dfd..abc708c9d326549aa74ab4ac330e92bd49f3ca3e 100644 (file)
@@ -6,22 +6,43 @@ 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) {
+    fetch: function (path, init, json = true) {
       let options = {};
 
       if (this.proxy?.useCredentials) {
         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);
 
-      return fetch(`${this.endpoint}/${path}`, options).then((response) => {
+      if (path.startsWith("/")) {
+        path = path.slice(1);
+      }
+
+      let url = this.endpoint;
+
+      if (path) {
+        url = `${this.endpoint}/${path}`;
+      }
+
+      return fetch(url, options).then((response) => {
         if (!response.ok) {
           throw new Error("Not 2xx response");
         }
-        return response.json();
+
+        return json ? response.json() : response;
       });
     },
   },