This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
88 lines (69 loc) · 2.22 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { debug } from 'debug';
import { SuperVizSdkOptions } from '../common/types/sdk-options.types';
import ApiService from '../services/api';
import AuthService from '../services/auth-service';
import { BrowserService } from '../services/browser';
import config from '../services/config';
import RemoteConfigService from '../services/remote-config-service';
import LauncherFacade from './launcher';
import { LauncherFacade as LauncherFacadeType } from './launcher/types';
/**
* @function validateOptions
* @description Validate the options passed to the SDK
* @param {SuperVizSdkOptions} param
* @returns {void}
*/
const validateOptions = ({ group, participant, roomId }: SuperVizSdkOptions): void => {
if (!group || !group.name || !group.id) {
throw new Error('Group fields is required');
}
if (!participant || !participant.id) {
throw new Error('Participants fields is required');
}
if (!roomId) {
throw new Error('Room id is required');
}
};
/**
* @function init
* @description Initialize the SDK
* @param apiKey - API key
* @param options - SDK options
* @returns {LauncherFacadeType}
*/
const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<LauncherFacadeType> => {
const validApiKey = apiKey && apiKey.trim();
if (!validApiKey) throw new Error('API key is required');
if (!options) throw new Error('Options is required');
validateOptions(options);
if (options.debug) {
debug.enable('*');
} else {
debug.disable();
}
const { apiUrl, conferenceLayerUrl } = await RemoteConfigService.getRemoteConfig(
options.environment,
);
const isValid = await AuthService(apiUrl, apiKey);
if (!isValid) {
throw new Error('Failed to validate API key');
}
const environment = await ApiService.fetchConfig(apiUrl, apiKey);
if (!environment || !environment.ablyKey) {
throw new Error('Failed to load configuration from server');
}
const { ablyKey } = environment;
config.setConfig({
apiUrl,
ablyKey,
apiKey,
conferenceLayerUrl,
environment,
roomId: options.roomId,
debug: options.debug,
});
return LauncherFacade(
Object.assign({}, options, { apiKey, ablyKey, conferenceLayerUrl, apiUrl }),
);
};
export default init;