Adding optional headers to the http client.

This commit is contained in:
Dessalines 2020-11-12 13:42:31 -06:00
parent b673128013
commit 9f8c55402d
2 changed files with 9 additions and 2 deletions

View file

@ -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;
```

View file

@ -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<GetSiteResponse> {
@ -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());