UNPKG

msg91-webrtc-call

Version:

**msg91-webrtc-call** is a lightweight JavaScript SDK that enables you to easily add peer-to-peer WebRTC audio/video calling functionality to your web applications using the MSG91 infrastructure.

143 lines (142 loc) 4.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.endCall = exports.startProducing = exports.joinCall = exports.rejoinCall = exports.startNewCall = void 0; exports.getUserData = getUserData; exports.sendUserContext = sendUserContext; const axios_1 = require("axios"); const util_1 = require("../util"); // const BASE_URL = "http://localhost:4000"; // export const startNewCall = async (payload: NewCallPayload) => { // try { // const response = await axios.post(`${BASE_URL}/call/new`, payload, { // headers: { // "Content-Type": "application/json", // }, // }); // return response.data; // } catch (error: any) { // console.error("Error creating new call:", error?.response?.data || error.message); // throw error; // } // }; async function getUserData(userToken) { try { const response = await axios_1.default.get(`${(0, util_1.getBaseURL)()}/utility/user`, { params: { token: userToken } }); return response.data; } catch (error) { console.error('Error fetching user data:', error); throw error; } } async function sendUserContext(userToken, data) { try { const response = await axios_1.default.post(`${(0, util_1.getBaseURL)()}/utility/user`, Object.assign({}, data), { params: { token: userToken } }); return response.data; } catch (error) { console.error('Error fetching user data:', error); throw error; } } const startNewCall = async (callToken, ignoreBot = false) => { const data = { token: callToken, ignoreBot }; try { const response = await axios_1.default.post(`${(0, util_1.getBaseURL)()}/call/new`, data, { headers: { 'Content-Type': 'application/json' } }); console.log('Response:', response.data); return response.data; } catch (error) { console.error('Error creating call:', error); } }; exports.startNewCall = startNewCall; const rejoinCall = async (callId, userToken) => { var _a; try { const response = await axios_1.default.post(`${(0, util_1.getBaseURL)()}/call/${callId}/rejoin`, {}, { headers: { 'Content-Type': 'application/json', "Authorization": `Bearer ${userToken}` } }); console.log('Response:', response.data); return response.data; } catch (error) { console.error('Error rejoining call:', error); return Object.assign(Object.assign({}, (_a = error === null || error === void 0 ? void 0 : error.response) === null || _a === void 0 ? void 0 : _a.data), { error: true }); } }; exports.rejoinCall = rejoinCall; const joinCall = async (uid, callId, dtls, type) => { const data = { uid: uid, dtls: dtls, type: type }; try { const response = await axios_1.default.post(`${(0, util_1.getBaseURL)()}/call/${callId}/join`, data, { headers: { 'Content-Type': 'application/json' } }); console.log('Join Response:', response.data); } catch (error) { console.error('Error joining call:', error); } }; exports.joinCall = joinCall; const startProducing = async (uid, callId, kind, rtp) => { const data = { uid: uid, kind: kind, rtp: rtp }; try { const response = await axios_1.default.post(`${(0, util_1.getBaseURL)()}/call/${callId}/produce`, data, { headers: { 'Content-Type': 'application/json' } }); console.log('Join Response:', response.data); return response.data; } catch (error) { console.error('Error joining call:', error); } }; exports.startProducing = startProducing; const endCall = async (callId, userToken) => { try { const response = await axios_1.default.delete(`${(0, util_1.getBaseURL)()}/call/${callId}/end`, { headers: { 'Content-Type': 'application/json' }, params: { token: userToken } }); console.log('End Call Response:', response.data); } catch (error) { console.error('Error ending call:', error); } }; exports.endCall = endCall;