Including API version in HTTP constructor.

This commit is contained in:
Dessalines 2021-03-29 10:29:27 -04:00
parent 414c601429
commit 9be225add3
3 changed files with 9 additions and 4 deletions

View file

@ -32,6 +32,7 @@ this.ws.send(client.login(form));
```js
import { LemmyHttp } from 'lemmy-js-client';
let baseUrl = 'https://lemmy.ml';
let client: LemmyHttp = new LemmyHttp(baseUrl, headers?);
let jwt = await client.httpLogin(loginForm).jwt;
```

View file

@ -92,6 +92,8 @@ import {
PersonMentionResponse,
} from './interfaces/api/person';
import { VERSION } from './interfaces/others';
enum HttpType {
Get = 'GET',
Post = 'POST',
@ -99,16 +101,16 @@ enum HttpType {
}
export class LemmyHttp {
private baseUrl: string;
private apiUrl: string;
private headers: { [key: string]: string } = {};
/**
* Generates a new instance of LemmyHttp
* @param baseUrl the full base url: https://lemmy.ml/api/v2
* @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 }) {
this.baseUrl = baseUrl;
this.apiUrl = `${baseUrl}/api/${VERSION}`;
if (headers) {
this.headers = headers;
@ -366,7 +368,7 @@ export class LemmyHttp {
}
private buildFullUrl(endpoint: string): string {
return `${this.baseUrl}${endpoint}`;
return `${this.apiUrl}${endpoint}`;
}
private async wrapper<ResponseType, MessageType>(

View file

@ -1,3 +1,5 @@
export const VERSION = 'v3';
/**
* All of the websocket operations available
*/