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
132 lines (109 loc) · 3.51 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { debug } from 'debug';
import { ColorsVariables, ColorsVariablesNames } from '../common/types/colors.types';
import { EnvironmentTypes, SuperVizSdkOptions } from '../common/types/sdk-options.types';
import ApiService from '../services/api';
import AuthService from '../services/auth-service';
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,
customColors,
}: SuperVizSdkOptions): void => {
if (customColors) {
validadeColorsVariablesNames(customColors);
}
if (!group || !group.name || !group.id) {
throw new Error('Group fields is required');
}
if (!participant || !participant.id || !participant.name) {
throw new Error('Participant name and id is required');
}
if (!roomId) {
throw new Error('Room id is required');
}
};
/**
* @function validadeColorsVariablesNames
* @description validade if the custom colors variables names are valid
* @param colors {ColorsVariables}
*/
const validadeColorsVariablesNames = (colors: ColorsVariables) => {
Object.entries(colors).forEach(([key, value]) => {
if (!Object.values(ColorsVariablesNames).includes(key as ColorsVariablesNames)) {
throw new Error(
`Color ${key} is not a valid color variable name. Please check the documentation for more information.`,
);
}
if (!/^(\d{1,3}\s){2}\d{1,3}$/.test(value)) {
throw new Error(
`Color ${key} is not a valid color variable value. Please check the documentation for more information.`,
);
}
});
};
/**
* @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 as EnvironmentTypes,
);
const isValid = await AuthService(apiUrl, apiKey);
if (!isValid) {
throw new Error('Failed to validate API key');
}
const [environment, limits, waterMark] = await Promise.all([
ApiService.fetchConfig(apiUrl, apiKey),
ApiService.fetchLimits(apiUrl, apiKey),
ApiService.fetchWaterMark(apiUrl, apiKey),
]).catch(() => {
throw new Error('Failed to load configuration from server');
});
if (!environment || !environment.ablyKey) {
throw new Error('Failed to load configuration from server');
}
const { ablyKey } = environment;
const { participant, roomId } = options;
config.setConfig({
apiUrl,
ablyKey,
apiKey,
conferenceLayerUrl,
environment,
roomId: options.roomId,
debug: options.debug,
limits,
waterMark,
colors: options.customColors,
});
ApiService.createOrUpdateParticipant({
name: participant.name,
participantId: participant.id,
avatar: participant.avatar?.imageUrl,
});
return LauncherFacade(options);
};
export default init;