UNPKG

@huddle01/server-sdk

Version:

The Huddle01 Server SDK allows you to perform protected admin actions on your server side, like generating peer access tokens and starting and stopping meeting recordings and livestreams.

281 lines (278 loc) 8.67 kB
import { estimateSize } from '../chunk-NAMYS2KV.js'; import { getInfraClient } from '../chunk-UUMIXEWS.js'; import { mainLogger } from '../chunk-76CTSAYR.js'; import { MAX_ROOM_METADATA_SIZE } from '../chunk-BIMOF3EO.js'; import '../chunk-3RG5ZIWI.js'; // src/api/index.ts var logger = mainLogger.createSubLogger("API"); var API = class { // API Key provided for the Registered HUB #apiKey; #infraClient; constructor(data) { this.#apiKey = data.apiKey; this.#infraClient = getInfraClient({ apiKey: this.#apiKey }); } /** * Create Room registers a new room in the developer hub. * The room can be used for multiple sessions. * @param data - metadata and roomLocked * @throws Error if the room creation fails */ createRoom = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk.rooms["create-room"].$post({ json: { metadata: data?.metadata, roomLocked: data?.roomLocked } }); if (resp.status !== 201) { throw new Error("Error creating room"); } if (data?.metadata && estimateSize(data.metadata) > MAX_ROOM_METADATA_SIZE) { throw new Error("Metadata size exceeds the limit of 1kb"); } const { data: respData, message } = await resp.json(); return { message, roomId: respData.roomId }; } catch (error) { logger.error("Error in createRoom", error); if (error instanceof Error) { throw error; } throw new Error("Error creating room"); } }; /** * Get Rooms gives the list of rooms registered in the developer hub * @param data - { * `limit` - Number of rooms to be fetched * `cursor` - Cursor for pagination * } * @throws Error if not able to get the rooms */ getRooms = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk.rooms["get-rooms"].$get({ query: { limit: data?.limit?.toString(), cursor: data?.cursor?.toString() } }); if (resp.status !== 200) { throw new Error("Error getting rooms"); } const getRoomsData = await resp.json(); return getRoomsData; } catch (error) { logger.error("Error in getRooms", error); if (error instanceof Error) { throw error; } throw new Error("Error getting rooms"); } }; /** * Get all the live sessions for a particular project or roomId * @description - All the live sessions for a particular project id can be fetched * using this API. If room id is provided, then the sessions for that room will be fetched. * @param data - {roomId?: string} * @returns - List of sessions * @throws Error if not able to get the sessions */ getLiveSessions = async (data) => { try { if (data?.roomId) { const resp2 = await this.#infraClient.api.v2.sdk["live-sessions"][":roomId"].$get({ param: { roomId: data?.roomId } }); if (resp2.status !== 200) { throw new Error("Error getting live sessions"); } const { liveSessions: liveSessions2 } = await resp2.json(); return liveSessions2; } const resp = await this.#infraClient.api.v2.sdk["live-sessions"].$get(); if (resp.status !== 200) { throw new Error("Error getting live sessions"); } const { liveSessions } = await resp.json(); return liveSessions; } catch (error) { logger.error("Error in getLiveSessions", error); if (error instanceof Error) { throw error; } throw new Error("Error getting live sessions"); } }; // Get the metrics for the Registered HUB getMetrics = async () => { try { const resp = await this.#infraClient.api.v2.sdk.metrics.$get(); if (resp.status !== 200) { throw new Error("Error getting metrics"); } const { livestreamCount, recordingCount, totalSessions, totalDuration, totalUsers } = await resp.json(); return { livestreamCount, recordingCount, totalSessions, totalDuration, totalUsers }; } catch (error) { logger.error("Error in getMetrics", error); if (error instanceof Error) { throw error; } throw new Error("Error getting metrics"); } }; /** * Get the room details for a particular roomId * @param data - {roomId: string} * @returns - Object containing the metadata, room id and room locked status of the room */ getRoomDetails = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk.rooms["room-details"][":roomId"].$get({ param: { roomId: data.roomId } }); if (resp.status !== 200) { throw new Error("Error getting room details"); } const roomDetails = await resp.json(); return roomDetails; } catch (error) { logger.error("Error in getRoomDetails", error); if (error instanceof Error) { throw error; } throw new Error("Error getting room details"); } }; /** * Get the participants details for a live room. * @description - Get the list of all the participants for a live room * @param data - {roomId: string} * @returns - List of participants */ getLivePartipantsDetails = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk["live-sessions"].participants[":roomId"].$get({ param: { roomId: data.roomId } }); if (resp.status === 404) { throw new Error("No ongoing session found for the room and project"); } if (resp.status !== 200) { throw new Error("Error getting participants details"); } const { participants } = await resp.json(); return participants; } catch (error) { logger.error("Error in fetching session participants", error); if (error instanceof Error) { throw error; } throw new Error("Error in fetching session participants"); } }; /** * Get the participants details for a session. * @description - Get the list of all the participants for a particular session * @param data - {sessionId: string} * @returns - List of participants */ getPartipantsDetails = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk.rooms["participant-list"].$get({ query: { sessionId: data.sessionId } }); if (resp.status === 404) { throw new Error("No ongoing session found for the room and project"); } if (resp.status !== 200) { throw new Error("Error getting participants details"); } const { participants } = await resp.json(); return participants; } catch (error) { logger.error("Error in fetching session participants", error); if (error instanceof Error) { throw error; } throw new Error("Error in fetching session participants"); } }; /** * Get the list of all the sessions for a particular roomId - Same Room can be used for multiple sessions. * @param data - {roomId: string} * @returns - List of sessions */ getRoomSessions = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk.rooms["session-details"].$get({ query: { roomId: data.roomId, limit: data.limit?.toString(), cursor: data.cursor?.toString() } }); if (resp.status !== 200) { throw new Error("Error getting room sessions"); } const { sessions } = await resp.json(); return sessions; } catch (error) { logger.error("Error in getRoomSessions", error); if (error instanceof Error) { throw error; } throw new Error("Error getting room sessions"); } }; getRecordings = async (data) => { try { const resp = await this.#infraClient.api.v2.sdk.recordings.$get({ query: { sessionId: data?.sessionId, limit: data?.limit?.toString(), cursor: data?.cursor?.toString() } }); if (resp.status !== 200) { throw new Error("\u274C Error getting recordings"); } const recordingData = await resp.json(); return { data: recordingData }; } catch (error) { logger.error("\u274C Error in fetching recordings", error); return { data: null, error: new Error("\u274C Error in fetching recordings") }; } }; }; export { API };