Skip to content
Snippets Groups Projects
Select Git revision
  • 1f79f22e47b00c160fe6c3f2d30212160da8211d
  • main default protected
  • renovate/main-vitest-monorepo
  • next
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • renovate/main-redis-5.x
  • renovate/main-xmldoc-2.x
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • 41.11.1
  • 41.11.0
  • 41.10.1
  • 41.10.0
  • 41.9.0
  • 41.8.0
  • 41.7.2
  • 41.7.1
  • 41.7.0
  • 41.6.4
  • 41.6.3
  • 41.6.2
  • 41.6.1
  • 41.6.0
  • 41.5.0
  • 41.4.0
  • 41.3.0
  • 41.2.0
  • 41.1.4
  • 41.1.3
41 results

index.ts

Blame
  • gitea.ts 1.97 KiB
    import is from '@sindresorhus/is';
    import { PlatformId } from '../../constants';
    import { resolveBaseUrl } from '../url';
    import type { HttpOptions, HttpResponse, InternalHttpOptions } from './types';
    import { Http } from '.';
    
    let baseUrl: string;
    export const setBaseUrl = (newBaseUrl: string): void => {
      baseUrl = newBaseUrl.replace(/\/*$/, '/'); // TODO #12875
    };
    
    export interface GiteaHttpOptions extends InternalHttpOptions {
      paginate?: boolean;
      token?: string;
    }
    
    function getPaginationContainer<T = unknown>(body: unknown): T[] | null {
      if (is.array(body) && body.length) {
        return body as T[];
      }
    
      if (is.plainObject(body) && is.array(body?.data) && body.data.length) {
        return body.data as T[];
      }
    
      return null;
    }
    
    function resolveUrl(path: string, base: string): URL {
      const resolvedUrlString = resolveBaseUrl(base, path);
      return new URL(resolvedUrlString);
    }
    
    export class GiteaHttp extends Http<GiteaHttpOptions, GiteaHttpOptions> {
      constructor(options?: HttpOptions) {
        super(PlatformId.Gitea, options);
      }
    
      protected override async request<T>(
        path: string,
        options?: InternalHttpOptions & GiteaHttpOptions
      ): Promise<HttpResponse<T>> {
        const resolvedUrl = resolveUrl(path, options?.baseUrl ?? baseUrl);
        const opts = {
          baseUrl,
          ...options,
        };
        const res = await super.request<T>(resolvedUrl, opts);
        const pc = getPaginationContainer<T>(res.body);
        if (opts.paginate && pc) {
          const total = parseInt(res.headers['x-total-count'] as string, 10);
          let nextPage = parseInt(resolvedUrl.searchParams.get('page') ?? '1', 10);
    
          while (total && pc.length < total) {
            nextPage += 1;
            resolvedUrl.searchParams.set('page', nextPage.toString());
    
            const nextRes = await super.request<T>(resolvedUrl.toString(), opts);
            const nextPc = getPaginationContainer<T>(nextRes.body);
            if (nextPc === null) {
              break;
            }
    
            pc.push(...nextPc);
          }
        }
    
        return res;
      }
    }