async function authenticatedFetch()

in cdslogviewer/frontend/app/common/authenticated_fetch.ts [9:33]


async function authenticatedFetch(input: string, init: any) {
  const token = localStorage.getItem("pluto:access-token");
  if (!token) {
    console.log("No local access token, performing request without it");
    return fetch(input, init);
  }

  const toAddTo = init ?? {};

  const existingHeaders = toAddTo.hasOwnProperty("headers")
    ? toAddTo.headers
    : {};

  const newInit = Object.assign({}, toAddTo, {
    headers: Object.assign({}, existingHeaders, {
      Authorization: `Bearer ${token}`,
    }),
  });

  const urlToCall = input.startsWith(deploymentRootPath)
    ? input
    : deploymentRootPath + "/" + input;

  return fetch(urlToCall, newInit);
}