Merge branch 'main' into remove_websocket

This commit is contained in:
Dessalines 2023-06-21 11:39:52 -04:00 committed by GitHub
commit 3583bbb664
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View file

@ -148,18 +148,28 @@ export class LemmyHttp {
#apiUrl: string;
#headers: { [key: string]: string } = {};
#pictrsUrl: string;
#fetchFunction = fetch;
/**
* Generates a new instance of LemmyHttp.
* @param baseUrl the base url, without the vX version: https://lemmy.ml -> goes to https://lemmy.ml/api/vX
* @param headers optional headers. Should contain `x-real-ip` and `x-forwarded-for` .
*/
constructor(baseUrl: string, headers?: { [key: string]: string }) {
constructor(
baseUrl: string,
options?: {
fetchFunction: typeof fetch;
headers?: { [key: string]: string };
}
) {
this.#apiUrl = `${baseUrl.replace(/\/+$/, "")}/api/${VERSION}`;
this.#pictrsUrl = `${baseUrl}/pictrs/image`;
if (headers) {
this.#headers = headers;
if (options?.headers) {
this.#headers = options.headers;
}
if (options?.fetchFunction) {
this.#fetchFunction = options.fetchFunction;
}
}
@ -1265,7 +1275,7 @@ export class LemmyHttp {
let url: string | undefined = undefined;
let delete_url: string | undefined = undefined;
const response = await fetch(this.#pictrsUrl, {
const response = await this.#fetchFunction(this.#pictrsUrl, {
method: HttpType.Post,
body: formData as unknown as BodyInit,
headers: {
@ -1301,12 +1311,12 @@ export class LemmyHttp {
let response: Response;
if (type_ === HttpType.Get) {
const getUrl = `${this.#buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
response = await fetch(getUrl, {
response = await this.#fetchFunction(getUrl, {
method: HttpType.Get,
headers: this.#headers,
});
} else {
response = await fetch(this.#buildFullUrl(endpoint), {
response = await this.#fetchFunction(this.#buildFullUrl(endpoint), {
method: type_,
headers: {
"Content-Type": "application/json",

View file

@ -11,4 +11,7 @@ export type SortType =
| "TopYear"
| "TopAll"
| "MostComments"
| "NewComments";
| "NewComments"
| "TopHour"
| "TopSixHour"
| "TopTwelveHour";