From 44288b92ee712dcbb5c73d250379d6f5b6bdd4f1 Mon Sep 17 00:00:00 2001 From: Gingka/Ginger Pepper <33764485+ExperiBass@users.noreply.github.com> Date: Wed, 14 Jun 2023 15:08:52 -0400 Subject: [PATCH 1/4] add support for a custom fetch function (closing #2) --- src/http.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/http.ts b/src/http.ts index e5e36ce..c2349ad 100644 --- a/src/http.ts +++ b/src/http.ts @@ -148,18 +148,25 @@ 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}/api/${VERSION}`; this.#pictrsUrl = `${baseUrl}/pictrs/image`; - if (headers) { - this.#headers = headers; + if (options?.headers) { + this.#headers = options.headers; } } @@ -1265,7 +1272,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 +1308,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", From 6ea0d653c4bec8c39b158d45ba69eb50baafb7a9 Mon Sep 17 00:00:00 2001 From: Gingka/Ginger Pepper <33764485+ExperiBass@users.noreply.github.com> Date: Wed, 14 Jun 2023 21:45:11 -0400 Subject: [PATCH 2/4] DERP, forgot to actually set #fetchFunction --- src/http.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/http.ts b/src/http.ts index c2349ad..b8a227a 100644 --- a/src/http.ts +++ b/src/http.ts @@ -168,6 +168,9 @@ export class LemmyHttp { if (options?.headers) { this.#headers = options.headers; } + if (options?.fetchFunction) { + this.#fetchFunction = options.fetchFunction; + } } /** From 27d84695a2a0cde8e8c4c31cb7d9710306164222 Mon Sep 17 00:00:00 2001 From: Ivo Barros Date: Tue, 20 Jun 2023 22:24:54 +0100 Subject: [PATCH 3/4] Add Hour Sort types --- src/types/SortType.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/types/SortType.ts b/src/types/SortType.ts index de64d22..bf91ef8 100644 --- a/src/types/SortType.ts +++ b/src/types/SortType.ts @@ -11,4 +11,7 @@ export type SortType = | "TopYear" | "TopAll" | "MostComments" - | "NewComments"; + | "NewComments" + | "TopHour" + | "TopSixHour" + | "TopTwelveHour"; \ No newline at end of file From 3a2a6454871b7c0e695ae5cb6111146d8dd3ad16 Mon Sep 17 00:00:00 2001 From: Ivo Barros Date: Tue, 20 Jun 2023 22:37:59 +0100 Subject: [PATCH 4/4] add ending empty line --- src/types/SortType.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/SortType.ts b/src/types/SortType.ts index bf91ef8..f88bd26 100644 --- a/src/types/SortType.ts +++ b/src/types/SortType.ts @@ -14,4 +14,4 @@ export type SortType = | "NewComments" | "TopHour" | "TopSixHour" - | "TopTwelveHour"; \ No newline at end of file + | "TopTwelveHour";