diff --git a/README.md b/README.md index a1a8695..dfd43a9 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ this.ws.send(client.login(form)); ```js import { LemmyHttp } from 'lemmy-js-client'; -let client: LemmyHttp = new LemmyHttp(baseUrl); +let client: LemmyHttp = new LemmyHttp(baseUrl, headers?); let jwt = await client.httpLogin(loginForm).jwt; ``` diff --git a/src/http.ts b/src/http.ts index 474af47..912e9b7 100644 --- a/src/http.ts +++ b/src/http.ts @@ -96,9 +96,14 @@ enum HttpType { export class LemmyHttp { private baseUrl: string; + private headers: { [key: string]: string } = {}; - constructor(baseUrl: string) { + constructor(baseUrl: string, headers?: { [key: string]: string }) { this.baseUrl = baseUrl; + + if (headers) { + this.headers = headers; + } } async getSite(form: GetSiteForm): Promise { @@ -380,12 +385,14 @@ export class LemmyHttp { let getUrl = `${this.buildFullUrl(endpoint)}?${encodeGetParams(form)}`; return fetch(getUrl, { method: 'GET', + headers: this.headers, }).then(d => d.json()); } else { return fetch(this.buildFullUrl(endpoint), { method: type_, headers: { 'Content-Type': 'application/json', + ...this.headers, }, body: JSON.stringify(form), }).then(d => d.json());