Repository: goenning/google-indexing-script
File: src/shared/gsc.ts (TypeScript)

Code before:
      Authorization: `Bearer ${accessToken}`,
    },
  });

  if (sitesResponse.status === 403) {
    console.error('🔐 This service account doesn\'t have access to any sites.');
    return [];
  }

  const sitesBody: webmasters_v3.Schema$SitesListResponse = await sitesResponse.json();

  if (!sitesBody.siteEntry) {
    console.error('❌ No sites found, add them to Google Search Console and try again.');
    return [];
  }

  return sitesBody.siteEntry.map((x) => x.siteUrl);
}

/**
 * Checks if the site URL is valid and accessible by the service account.
 * @param accessToken - The access token for authentication.
 * @param siteUrl - The URL of the site to check.
 * @returns The corrected URL if found, otherwise the original site URL.
 */
export async function checkSiteUrl(
  accessToken: string,
  siteUrl: string
) {
  const sites = await getSites(accessToken);
  
  if (!sites.includes(siteUrl)) {
    if (siteUrl.startsWith("sc-domain:")) {
      if (sites.includes(convertToHTTP(siteUrl.replace("sc-domain:", "")))) {
        const correctUrl = convertToHTTP(siteUrl.replace("sc-domain:", ""));
        console.warn(`🚨 Found HTTP version of the site, please next time use this format instead: ${correctUrl}`);
        return correctUrl;
      } else if (sites.includes(convertToHTTPS(siteUrl.replace("sc-domain:", "")))) {
        const correctUrl = convertToHTTPS(siteUrl.replace("sc-domain:", ""));
        console.warn(`🚨 Found HTTPS version of the site, please next time use this format instead: ${correctUrl}`);
        return  correctUrl;
      } else {
        console.error("❌ This service account doesn't have access to this site.");
        console.error("");
        process.exit(1);
      }
    } else if (siteUrl.startsWith("https://")) {
      if (sites.includes(convertToHTTP(siteUrl))) {
        const correctUrl = convertToHTTP(siteUrl);
        console.warn(`🚨 Found HTTP version of the site, please next time use this format instead: ${correctUrl}`);
        return correctUrl;

Review comment (suggestion):
this function is very hard to read 😅

maybe we could start this function by converting to all 3 possible format/values and then check which one matches. 

And then just carry on with the process, I think notifications like this are unnecessary, most people won't care to change if they know the script works anyway
```
console.warn(`🚨 Found HTTPS version of the site, please next time use this format instead: ${correctUrl}`);
```


Code after:
      Authorization: `Bearer ${accessToken}`,
    },
  });

  if (sitesResponse.status === 403) {
    console.error("🔐 This service account doesn't have access to any sites.");
    return [];
  }

  const sitesBody: webmasters_v3.Schema$SitesListResponse = await sitesResponse.json();

  if (!sitesBody.siteEntry) {
    console.error("❌ No sites found, add them to Google Search Console and try again.");
    return [];
  }

  return sitesBody.siteEntry.map((x) => x.siteUrl);
}

/**
 * Checks if the site URL is valid and accessible by the service account.
 * @param accessToken - The access token for authentication.
 * @param siteUrl - The URL of the site to check.
 * @returns The corrected URL if found, otherwise the original site URL.
 */
export async function checkSiteUrl(accessToken: string, siteUrl: string) {
  const sites = await getSites(accessToken);
  let formattedUrls: string[] = [];

  // Convert the site URL into all possible formats
  if (siteUrl.startsWith("https://")) {
    formattedUrls.push(siteUrl);
    formattedUrls.push(convertToHTTP(siteUrl));
    formattedUrls.push(convertToSCDomain(siteUrl));
  } else if (siteUrl.startsWith("http://")) {
    formattedUrls.push(siteUrl);
    formattedUrls.push(convertToHTTPS(siteUrl));
    formattedUrls.push(convertToSCDomain(siteUrl));
  } else if (siteUrl.startsWith("sc-domain:")) {
    formattedUrls.push(siteUrl);
    formattedUrls.push(convertToHTTP(siteUrl.replace("sc-domain:", "")));
    formattedUrls.push(convertToHTTPS(siteUrl.replace("sc-domain:", "")));
  } else {
    console.error("❌ Unknown site URL format.");
    console.error("");
    process.exit(1);
  }

  // Check if any of the formatted URLs are accessible
  for (const formattedUrl of formattedUrls) {
    if (sites.includes(formattedUrl)) {