mediasfu-reactnative
Version:
MediaSFU Prebuilt React Native SDK
80 lines • 3.15 kB
JavaScript
/**
* Asynchronously joins a room on MediaSFU.
*
* This function sends a POST request to the MediaSFU API to join an existing room.
* It validates the provided credentials and constructs the appropriate API endpoint,
* including support for the Community Edition via a custom `localLink`.
*
* @param {object} options - Configuration options for joining the room.
* @param {JoinMediaSFURoomOptions | CreateMediaSFURoomOptions} options.payload - The payload containing the room details and action (`join` or `create`).
* @param {string} options.apiUserName - The API username, used for authentication.
* @param {string} options.apiKey - The API key, used for authentication.
* @param {string} [options.localLink=''] - The local link for Community Edition users. If provided, it overrides the default API URL.
*
* @returns {Promise<{
* data: CreateJoinRoomResponse | CreateJoinRoomError | null;
* success: boolean;
* }>} A promise resolving to an object containing the API response:
* - `data`: The response object, either `CreateJoinRoomResponse` or `CreateJoinRoomError`.
* - `success`: Boolean indicating whether the operation was successful.
*
* @throws {Error} Throws an error if the request fails or if the provided credentials are invalid.
*
* @example
* ```ts
* const response = await joinRoomOnMediaSFU({
* payload: {
* action: 'join',
* meetingID: '123456',
* userName: 'user123',
* },
* apiUserName: 'yourAPIUSERNAME',
* apiKey: 'yourAPIKEY',
* localLink: 'http://localhost:3000', // Optional for Community Edition
* });
*
* if (response.success) {
* console.log('Joined room:', response.data);
* } else {
* console.error('Failed to join room:', response.data?.error);
* }
* ```
*/
export const joinRoomOnMediaSFU = async ({ payload, apiUserName, apiKey, localLink = '', }) => {
try {
if (!apiUserName ||
!apiKey ||
apiUserName === 'yourAPIUSERNAME' ||
apiKey === 'yourAPIKEY' ||
apiKey.length !== 64 ||
apiUserName.length < 6) {
return { data: { error: 'Invalid credentials' }, success: false };
}
let finalLink = 'https://mediasfu.com/v1/rooms/';
if (localLink && localLink.trim() !== '' && !localLink.includes('mediasfu.com')) {
localLink = localLink.replace(/\/$/, '');
finalLink = localLink + '/joinRoom';
}
const response = await fetch(finalLink, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiUserName}:${apiKey}`,
},
body: JSON.stringify(payload),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return { data, success: true };
}
catch (error) {
const errorMessage = error.message || 'unknown error';
return {
data: { error: `Unable to join room, ${errorMessage}` },
success: false,
};
}
};
//# sourceMappingURL=joinRoomOnMediaSFU.js.map