mirror of
https://github.com/LemmyNet/lemmy-js-client.git
synced 2024-11-22 12:21:12 +00:00
Including API version in HTTP constructor.
This commit is contained in:
parent
414c601429
commit
9be225add3
3 changed files with 9 additions and 4 deletions
|
@ -32,6 +32,7 @@ this.ws.send(client.login(form));
|
||||||
```js
|
```js
|
||||||
import { LemmyHttp } from 'lemmy-js-client';
|
import { LemmyHttp } from 'lemmy-js-client';
|
||||||
|
|
||||||
|
let baseUrl = 'https://lemmy.ml';
|
||||||
let client: LemmyHttp = new LemmyHttp(baseUrl, headers?);
|
let client: LemmyHttp = new LemmyHttp(baseUrl, headers?);
|
||||||
let jwt = await client.httpLogin(loginForm).jwt;
|
let jwt = await client.httpLogin(loginForm).jwt;
|
||||||
```
|
```
|
||||||
|
|
10
src/http.ts
10
src/http.ts
|
@ -92,6 +92,8 @@ import {
|
||||||
PersonMentionResponse,
|
PersonMentionResponse,
|
||||||
} from './interfaces/api/person';
|
} from './interfaces/api/person';
|
||||||
|
|
||||||
|
import { VERSION } from './interfaces/others';
|
||||||
|
|
||||||
enum HttpType {
|
enum HttpType {
|
||||||
Get = 'GET',
|
Get = 'GET',
|
||||||
Post = 'POST',
|
Post = 'POST',
|
||||||
|
@ -99,16 +101,16 @@ enum HttpType {
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LemmyHttp {
|
export class LemmyHttp {
|
||||||
private baseUrl: string;
|
private apiUrl: string;
|
||||||
private headers: { [key: string]: string } = {};
|
private headers: { [key: string]: string } = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a new instance of LemmyHttp
|
* 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
|
* @param headers optional headers. Should contain x-real-ip and x-forwarded-for
|
||||||
*/
|
*/
|
||||||
constructor(baseUrl: string, headers?: { [key: string]: string }) {
|
constructor(baseUrl: string, headers?: { [key: string]: string }) {
|
||||||
this.baseUrl = baseUrl;
|
this.apiUrl = `${baseUrl}/api/${VERSION}`;
|
||||||
|
|
||||||
if (headers) {
|
if (headers) {
|
||||||
this.headers = headers;
|
this.headers = headers;
|
||||||
|
@ -366,7 +368,7 @@ export class LemmyHttp {
|
||||||
}
|
}
|
||||||
|
|
||||||
private buildFullUrl(endpoint: string): string {
|
private buildFullUrl(endpoint: string): string {
|
||||||
return `${this.baseUrl}${endpoint}`;
|
return `${this.apiUrl}${endpoint}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async wrapper<ResponseType, MessageType>(
|
private async wrapper<ResponseType, MessageType>(
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
export const VERSION = 'v3';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All of the websocket operations available
|
* All of the websocket operations available
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in a new issue