diff --git a/docs/config.md b/docs/config.md index d06fa14..93bd123 100644 --- a/docs/config.md +++ b/docs/config.md @@ -24,6 +24,7 @@ window.owntracks.config = {}; - `api` - [`baseUrl`](#apibaseurl) + - [`fetchOptions`](#apifetchoptions) - [`endDateTime`](#enddatetime) - [`ignorePingLocation`](#ignorepinglocation) - [`locale`](#locale) @@ -89,6 +90,26 @@ Base URL for the recorder's HTTP and WebSocket API. Keep CORS in mind. }; ``` +### `api.fetchOptions` + +Options for API requests (made with `fetch()`). See [`fetch()` docs on MDN] for details. + +You can use this for example to send custom HTTP headers or to include cookies in the request. + +- Type: [`Object`] +- Default: `{}` +- Example: + ```js + // Include credentials (e.g. cookies) + window.owntracks.config = { + api: { + fetchOptions: { + credentials: "include" + } + } + }; + ``` + ### `endDateTime` Initial end date and time (browser timezone) for fetched data. @@ -458,3 +479,4 @@ Whether to enable verbose mode or not. [`object`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object [`string`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String [css ``]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value +[`fetch()` docs on MDN]: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters diff --git a/src/api.js b/src/api.js index ec433b1..c355984 100644 --- a/src/api.js +++ b/src/api.js @@ -1,3 +1,4 @@ +import config from "@/config"; import { log, logLevels } from "@/logging"; import { getApiUrl, getLocationHistoryCount } from "@/util"; @@ -12,7 +13,9 @@ const fetchApi = (path, params = {}) => { const url = getApiUrl(path); Object.keys(params).forEach(key => url.searchParams.append(key, params[key])); log("HTTP", `GET ${url.href}`); - return fetch(url.href).catch(error => log("HTTP", error, logLevels.ERROR)); + return fetch(url.href, config.api.fetchOptions).catch(error => + log("HTTP", error, logLevels.ERROR) + ); }; /** diff --git a/src/config.js b/src/config.js index 065bf3e..2abb640 100644 --- a/src/config.js +++ b/src/config.js @@ -10,6 +10,7 @@ startDateTime.setHours(0, 0, 0, 0); const DEFAULT_CONFIG = { api: { baseUrl: `${window.location.protocol}//${window.location.host}`, + fetchOptions: {}, }, endDateTime, ignorePingLocation: true,