mirror of
https://github.com/LemmyNet/lemmy-js-client.git
synced 2024-11-25 22:01:12 +00:00
Adding optional headers to the http client.
This commit is contained in:
parent
b673128013
commit
9f8c55402d
2 changed files with 9 additions and 2 deletions
|
@ -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;
|
||||
```
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue