Created
July 1, 2024 19:40
-
-
Save JiveOff/261a83ab9ed9eb38f0e3d5f80c250d01 to your computer and use it in GitHub Desktop.
ChatPlex LLS TS Client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { appConfig } from "../config/app"; | |
| import axios from "./axios"; | |
| import { logger } from "./consola"; | |
| type ChatPlexMethods = "Account_AuthByEmailPassword" | "LLS_GetMyInfos" | "LLS_CreateChannel" | "LLS_RenewChannel" | "LLS_DeleteChannel" | "LLS_GetChannelInfo"; | |
| type ChatPlexMethodMapping = { | |
| Account_AuthByEmailPassword: { | |
| params: { | |
| Email: string; | |
| Password: string; | |
| }; | |
| result: { | |
| SessionToken: string; | |
| UserName: string; | |
| }; | |
| }; | |
| LLS_GetMyInfos: { | |
| params: Record<string, never>; | |
| result: { | |
| Result: boolean; | |
| Servers: { | |
| ID: number; | |
| Name: string; | |
| Host: string; | |
| RTMPIngress: string; | |
| WHIPIngress: string; | |
| WebRTCIngress: string; | |
| Online: number; | |
| }[]; | |
| Channels: { | |
| ID: number; | |
| AccountID: number; | |
| ServerID: number; | |
| Name: string; | |
| StreamKey: string; | |
| Online: number; | |
| StartTime: number; | |
| Source: string; | |
| VideoCodec: string; | |
| VideoBitrate: number; | |
| Width: number; | |
| Height: number; | |
| Framerate: number; | |
| AudioCodec: string; | |
| AudioBitrate: number; | |
| Channels: number; | |
| SampleRate: number; | |
| LastError: string; | |
| }[]; | |
| Restreams: []; | |
| AccountLimitations: { | |
| Name: string; | |
| ChannelCount: number; | |
| ReStreamCount: number; | |
| VideoBitrateLimit: number; | |
| AudioBitrateLimit: number; | |
| }; | |
| }; | |
| }; | |
| LLS_CreateChannel: { | |
| params: { | |
| ServerID: number; | |
| Suffix: string; | |
| }; | |
| result: { | |
| Result: boolean; | |
| Error: string; | |
| }; | |
| }; | |
| LLS_RenewChannel: { | |
| params: { | |
| ChannelID: number; | |
| }; | |
| result: { | |
| Result: boolean; | |
| Error: string; | |
| }; | |
| }; | |
| LLS_DeleteChannel: { | |
| params: { | |
| ChannelID: number; | |
| }; | |
| result: { | |
| Result: boolean; | |
| Error: string; | |
| }; | |
| }; | |
| LLS_GetChannelInfo: { | |
| params: { | |
| ChannelName: string; | |
| }; | |
| result: { | |
| Result: boolean; | |
| Name: string; | |
| WebRTCPlaybackURL: string; | |
| }; | |
| }; | |
| }; | |
| export default class ChatPlexClient { | |
| private token: string | null = null; | |
| private username: string | null = null; | |
| async fetchToken() { | |
| if (!appConfig.chatplex.user || !appConfig.chatplex.password) { | |
| logger.error("ChatPlex credentials are not set"); | |
| return; | |
| } | |
| try { | |
| const res = await this.request("Account_AuthByEmailPassword", { | |
| Email: appConfig.chatplex.user, | |
| Password: appConfig.chatplex.password | |
| }); | |
| if (!res) { | |
| throw new Error("Failed to fetch ChatPlex token"); | |
| } | |
| this.token = res.SessionToken; | |
| this.username = res.UserName; | |
| logger.info(`Fetched ChatPlex token for user ${this.username}`); | |
| } catch (error) { | |
| logger.error("Failed to fetch ChatPlex token", error); | |
| } | |
| } | |
| async getMyInfos() { | |
| return await this.request("LLS_GetMyInfos"); | |
| } | |
| async createChannel(suffix: string) { | |
| return this.request("LLS_CreateChannel", { | |
| ServerID: 1, | |
| Suffix: suffix | |
| }); | |
| } | |
| async renewChannel(channelID: number) { | |
| return this.request("LLS_RenewChannel", { | |
| ChannelID: channelID | |
| }); | |
| } | |
| async deleteChannel(channelID: number) { | |
| return this.request("LLS_DeleteChannel", { | |
| ChannelID: channelID | |
| }); | |
| } | |
| async getChannelInfo(channelName: string) { | |
| return this.request("LLS_GetChannelInfo", { | |
| ChannelName: channelName | |
| }); | |
| } | |
| private async request<M extends ChatPlexMethods>(method: M, params?: ChatPlexMethodMapping[M]["params"]): Promise<ChatPlexMethodMapping[M]["result"]> { | |
| if (!this.token && method !== "Account_AuthByEmailPassword") { | |
| await this.fetchToken(); | |
| } | |
| const payload = { | |
| jsonrpc: "2.0", | |
| id: new Date().getTime(), | |
| method, | |
| params: this.token | |
| ? { | |
| SessionToken: this.token, | |
| ...params | |
| } | |
| : params | |
| }; | |
| const response = await axios | |
| .post(`${appConfig.chatplex.apiUrl}`, payload, { | |
| headers: { | |
| "Content-Type": "application/json", | |
| Authorization: this.token ? `AdminSessionToken ${this.token}` : undefined | |
| } | |
| }) | |
| .catch((error: unknown) => { | |
| logger.error("Failed to make ChatPlex request", error); | |
| }); | |
| if (!response) { | |
| throw new Error("Failed to make ChatPlex request"); | |
| } | |
| return response.data.result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment