From ec159a34aab0ada1a72d147b1ea354783778a4a4 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Wed, 1 May 2024 09:46:54 -0600 Subject: [PATCH] Factor out fetch config stuff --- src/serviceworker/index.ts | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/serviceworker/index.ts b/src/serviceworker/index.ts index 25942bd3a5..fd674c1e4f 100644 --- a/src/serviceworker/index.ts +++ b/src/serviceworker/index.ts @@ -91,14 +91,7 @@ self.addEventListener("fetch", (event: FetchEvent) => { // Add authentication and send the request. We add authentication even if MSC3916 endpoints aren't // being used to ensure patches like this work: // https://github.com/matrix-org/synapse/commit/2390b66bf0ec3ff5ffb0c7333f3c9b239eeb92bb - const config = !accessToken - ? undefined - : { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }; - return fetch(url, config); + return fetch(url, fetchConfigForToken(accessToken)); })(), ); }); @@ -109,13 +102,7 @@ async function tryUpdateServerSupportMap(clientApiUrl: string, accessToken?: str return; // up to date } - const config = !accessToken - ? undefined - : { - headers: { - Authorization: `Bearer ${accessToken}`, - }, - }; + const config = fetchConfigForToken(accessToken); const versions = await (await fetch(`${clientApiUrl}/_matrix/client/versions`, config)).json(); serverSupportMap[clientApiUrl] = { @@ -185,3 +172,15 @@ async function askClientForUserIdParams(client: unknown): Promise<{ userId: stri (client as Window).postMessage({ responseKey, type: "userinfo" }); }); } + +function fetchConfigForToken(accessToken?: string): RequestInit | undefined { + if (!accessToken) { + return undefined; // no headers/config to specify + } + + return { + headers: { + Authorization: `Bearer ${accessToken}`, + }, + }; +}