mirror of
https://github.com/LemmyNet/lemmy-js-client.git
synced 2024-12-23 03:11:26 +00:00
add support for a custom fetch function (closing #2)
This commit is contained in:
parent
bad0ec49db
commit
44288b92ee
1 changed files with 13 additions and 6 deletions
19
src/http.ts
19
src/http.ts
|
@ -148,18 +148,25 @@ export class LemmyHttp {
|
||||||
#apiUrl: string;
|
#apiUrl: string;
|
||||||
#headers: { [key: string]: string } = {};
|
#headers: { [key: string]: string } = {};
|
||||||
#pictrsUrl: string;
|
#pictrsUrl: string;
|
||||||
|
#fetchFunction = fetch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a new instance of LemmyHttp.
|
* 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 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` .
|
* @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.#apiUrl = `${baseUrl}/api/${VERSION}`;
|
||||||
this.#pictrsUrl = `${baseUrl}/pictrs/image`;
|
this.#pictrsUrl = `${baseUrl}/pictrs/image`;
|
||||||
|
|
||||||
if (headers) {
|
if (options?.headers) {
|
||||||
this.#headers = headers;
|
this.#headers = options.headers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1265,7 +1272,7 @@ export class LemmyHttp {
|
||||||
let url: string | undefined = undefined;
|
let url: string | undefined = undefined;
|
||||||
let delete_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,
|
method: HttpType.Post,
|
||||||
body: formData as unknown as BodyInit,
|
body: formData as unknown as BodyInit,
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -1301,12 +1308,12 @@ export class LemmyHttp {
|
||||||
let response: Response;
|
let response: Response;
|
||||||
if (type_ === HttpType.Get) {
|
if (type_ === HttpType.Get) {
|
||||||
const getUrl = `${this.#buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
|
const getUrl = `${this.#buildFullUrl(endpoint)}?${encodeGetParams(form)}`;
|
||||||
response = await fetch(getUrl, {
|
response = await this.#fetchFunction(getUrl, {
|
||||||
method: HttpType.Get,
|
method: HttpType.Get,
|
||||||
headers: this.#headers,
|
headers: this.#headers,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
response = await fetch(this.#buildFullUrl(endpoint), {
|
response = await this.#fetchFunction(this.#buildFullUrl(endpoint), {
|
||||||
method: type_,
|
method: type_,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
|
|
Loading…
Reference in a new issue