Select Git revision
-
MaronHatoum authored
* refactor:util/http move interfaces and types from index.ts to types.ts * refactor:reorder imports * refactor:move HttpError from type.ts to index.ts * refactor:change imports after moving HttpError from type.ts to index.ts * refactor:revert white spaces * refactor:revert moving httpError from types.ts to index.ts * refactor: moving httpError from types.ts to index.ts * refactor: change import * refactor: change import * refactor: change import * refactor: fix circular dependencies (lint build) Co-authored-by:
Michael Kriese <michael.kriese@visualon.de> Co-authored-by:
Rhys Arkins <rhys@arkins.net>
MaronHatoum authored* refactor:util/http move interfaces and types from index.ts to types.ts * refactor:reorder imports * refactor:move HttpError from type.ts to index.ts * refactor:change imports after moving HttpError from type.ts to index.ts * refactor:revert white spaces * refactor:revert moving httpError from types.ts to index.ts * refactor: moving httpError from types.ts to index.ts * refactor: change import * refactor: change import * refactor: change import * refactor: fix circular dependencies (lint build) Co-authored-by:
Michael Kriese <michael.kriese@visualon.de> Co-authored-by:
Rhys Arkins <rhys@arkins.net>
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;
}
}