]>
git.immae.eu Git - github/bastienwirtz/homer.git/blob - src/mixins/service.js
b47f756586a8f5f428bfa7d75ebd1ea8fde6dc14
1 const merge
= require("lodash.merge");
8 // custom service often consume info from an API using the item link (url) as a base url,
9 // but sometimes the base url is different. An optional alternative URL can be provided with the "endpoint" key.
10 this.endpoint
= this.item
.endpoint
|| this.item
.url
;
12 if (this.endpoint
.endsWith("/")) {
13 this.endpoint
= this.endpoint
.slice(0, -1);
17 fetch: function (path
, init
, json
= true) {
20 if (this.proxy
?.useCredentials
) {
21 options
.credentials
= "include";
24 // Each item can override the credential settings
25 if (this.item
.useCredentials
!== undefined) {
27 this.item
.useCredentials
=== true ? "include" : "omit";
30 if (this.proxy
?.apikey
) {
32 "X-Homer-Api-Key": this.proxy
.apikey
,
36 if (path
.startsWith("/")) {
40 let url
= path
? `${this.endpoint}/${path}` : this.endpoint
;
42 if (this.proxy
?.url
) {
44 ...(options
.headers
|| {}),
45 "X-Homer-Api-Url": url
,
50 options
= merge(options
, init
);
52 return fetch(url
, options
).then((response
) => {
54 throw new Error("Not 2xx response");
57 return json
? response
.json() : response
;