]>
git.immae.eu Git - github/bastienwirtz/homer.git/blob - src/mixins/service.js
1 const merge
= require("lodash.merge");
9 // custom service often consume info from an API using the item link (url) as a base url,
10 // but sometimes the base url is different. An optional alternative URL can be provided with the "endpoint" key.
11 this.endpoint
= this.item
.endpoint
|| this.item
.url
;
13 if (this.endpoint
.endsWith("/")) {
14 this.endpoint
= this.endpoint
.slice(0, -1);
18 fetch: function (path
, init
, json
= true) {
21 if (this.proxy
?.useCredentials
) {
22 options
.credentials
= "include";
25 // Each item can override the credential settings
26 if (this.item
.useCredentials
!== undefined) {
28 this.item
.useCredentials
=== true ? "include" : "omit";
31 if (this.forwarder
?.apikey
) {
33 "X-Homer-Forwarder-Api-Key": this.forwarder
.apikey
,
37 if (path
.startsWith("/")) {
41 let url
= path
? `${this.endpoint}/${path}` : this.endpoint
;
43 if (this.forwarder
?.url
) {
45 ...(options
.headers
|| {}),
46 "X-Homer-Forwarder-Url": url
,
48 url
= this.forwarder
.url
;
51 options
= merge(options
, init
);
53 return fetch(url
, options
).then((response
) => {
55 throw new Error("Not 2xx response");
58 return json
? response
.json() : response
;