renovate
Version:
Automated dependency updates. Flexible so you don't need to be.
51 lines (50 loc) • 1.62 kB
JavaScript
import { HttpBase } from "./http.js";
import { isArray, isPlainObject } from "@sindresorhus/is";
//#region lib/util/http/forgejo.ts
let baseUrl;
const setBaseUrl = (newBaseUrl) => {
baseUrl = newBaseUrl.replace(/\/*$/, "/");
};
function getPaginationContainer(body) {
if (isArray(body) && body.length) return body;
if (isPlainObject(body) && isArray(body?.data) && body.data.length) return body.data;
return null;
}
var ForgejoHttp = class extends HttpBase {
get baseUrl() {
return baseUrl;
}
constructor(hostType, options) {
super(hostType ?? "forgejo", options);
}
extraOptions() {
return super.extraOptions().concat(["paginate"]);
}
async requestJsonUnsafe(method, options) {
const resolvedUrl = this.resolveUrl(options.url, options.httpOptions);
const opts = {
...options,
url: resolvedUrl
};
const res = await super.requestJsonUnsafe(method, opts);
const pc = getPaginationContainer(res.body);
if (opts.httpOptions?.paginate && pc) {
delete opts.httpOptions.cacheProvider;
opts.httpOptions.memCache = false;
delete opts.httpOptions.paginate;
const total = parseInt(res.headers["x-total-count"], 10);
let nextPage = parseInt(resolvedUrl.searchParams.get("page") ?? "1", 10);
while (total && pc.length < total) {
nextPage += 1;
resolvedUrl.searchParams.set("page", nextPage.toString());
const nextPc = getPaginationContainer((await super.requestJsonUnsafe(method, opts)).body);
if (nextPc === null) break;
pc.push(...nextPc);
}
}
return res;
}
};
//#endregion
export { ForgejoHttp, setBaseUrl };
//# sourceMappingURL=forgejo.js.map