Skip to content
Snippets Groups Projects
Select Git revision
  • f19eb5b841c0643705b6316a615b05ffa08c77fc
  • master default
2 results

common.ts

Blame
  • user avatar
    Sergei Zharinov authored and GitHub committed
    * fix(github): Don't attach `/api/v3/` to the endpoint twice
    
    * Add test
    
    * More correct replacing
    f19eb5b8
    History
    common.ts 931 B
    import { ensureTrailingSlash } from '../../../util/url';
    
    const defaultSourceUrlBase = 'https://github.com/';
    const defaultApiBaseUrl = 'https://api.github.com/';
    
    export function getSourceUrlBase(registryUrl: string | undefined): string {
      // default to GitHub.com if no GHE host is specified.
      return ensureTrailingSlash(registryUrl ?? defaultSourceUrlBase);
    }
    
    export function getApiBaseUrl(registryUrl: string | undefined): string {
      const sourceUrlBase = getSourceUrlBase(registryUrl);
    
      if (
        sourceUrlBase === defaultSourceUrlBase ||
        sourceUrlBase === defaultApiBaseUrl
      ) {
        return defaultApiBaseUrl;
      }
    
      if (sourceUrlBase.endsWith('/api/v3/')) {
        return sourceUrlBase;
      }
    
      return `${sourceUrlBase}api/v3/`;
    }
    
    export function getSourceUrl(
      packageName: string,
      registryUrl?: string
    ): string {
      const sourceUrlBase = getSourceUrlBase(registryUrl);
      return `${sourceUrlBase}${packageName}`;
    }