From 4dd7a8481ebfed6694f032b063649c8d82669b41 Mon Sep 17 00:00:00 2001 From: Christoph Krey Date: Mon, 5 Dec 2022 20:38:23 +0100 Subject: [PATCH] [FIX] wrong API URL when called from /index.html https://github.com/owntracks/talk/issues/151 --- docroot/index.html | 2 +- docroot/utils/network.js | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/docroot/index.html b/docroot/index.html index 563d7b5..83c0a8c 100644 --- a/docroot/index.html +++ b/docroot/index.html @@ -50,7 +50,7 @@ const f7d = new Date(Date.now() - 7 * days).toISOString(); const f30d = new Date(Date.now() - 24 * days).toISOString(); - const data = await fetchApiData({ endpoint: "last", includeSearchParams: true }); + const data = await( fetchApiData({ endpoint: "last", includeSearchParams: true, rootLevel: true })); for (const d of data) { if (!d._type) { diff --git a/docroot/utils/network.js b/docroot/utils/network.js index 30b020d..625b1e4 100644 --- a/docroot/utils/network.js +++ b/docroot/utils/network.js @@ -7,12 +7,13 @@ import { debug } from "./debug.js"; * @param {string} endpoint - The name of the API endpoint. * @param {boolean} options.useWebsocket - Connect over the websocket protocol instead of standard HTTP. * @param {boolean} options.includeSearchParams - Copy over any search parameters that are defined on the page URL. + * @param {boolean} options.rootLevel - indicate function is called from rootLevel URL * * @returns {URL} - A full URL for the API endpoint. */ -export function getApiUrl(endpoint, { useWebsocket = false, includeSearchParams = false }){ +export function getApiUrl(endpoint, { useWebsocket = false, includeSearchParams = false, rootLevel = false }){ - const apiUrl = new URL(`../${ useWebsocket ? "ws" : "api/0" }${endpoint === undefined ? "" : `/${endpoint}`}`, window.location); + const apiUrl = new URL(`${ rootLevel ? "./" : "../" }${ useWebsocket ? "ws" : "api/0" }${endpoint === undefined ? "" : `/${endpoint}`}`, window.location); if(useWebsocket) apiUrl.protocol = window.location.protocol === "https:" ? "wss:" : "ws:"; @@ -31,13 +32,14 @@ export function getApiUrl(endpoint, { useWebsocket = false, includeSearchParams * @param {RequestInfo} options.url - A full URL/Request to the API endpoint. * @param {string} options.endpoint - The name of the API endpoint. * @param {boolean} options.includeSearchParams - Copy over any search parameters that are defined on the page URL. + * @param {boolean} options.rootLevel - indicate function is called from rootLevel URL * * @returns {Promise} - The data returned by the API. */ -export async function fetchApiData({ url, endpoint, includeSearchParams }){ +export async function fetchApiData({ url, endpoint, includeSearchParams, rootLevel }){ if(url === undefined){ - url = getApiUrl(endpoint, { includeSearchParams }); + url = getApiUrl(endpoint, { includeSearchParams, rootLevel }); } debug("Connecting to API endpoint:", url);