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
148 lines (122 loc) · 4.01 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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) {
validateColorsVariablesNames(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 validateColorsVariablesNames
* @description validate if the custom colors variables names are valid
* @param colors {ColorsVariables}
*/
const validateColorsVariablesNames = (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 setColorVariables
* @description - add color variables as variables in the root of the document
* @returns
*/
const setColorVariables = (colors: ColorsVariables): void => {
if (!colors) return;
Object.entries(colors).forEach(([key, value]) => {
const color = value.replace(/\s/g, ', ');
document.documentElement.style.setProperty(`--${key}`, color);
});
};
/**
* @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, customColors: colors } = options;
config.setConfig({
apiUrl,
ablyKey,
apiKey,
conferenceLayerUrl,
environment: (options.environment as EnvironmentTypes) ?? EnvironmentTypes.PROD,
roomId,
debug: options.debug,
limits,
waterMark,
colors: options.customColors,
});
setColorVariables(options.customColors);
ApiService.createOrUpdateParticipant({
name: participant.name,
participantId: participant.id,
avatar: participant.avatar?.imageUrl,
});
return LauncherFacade(options);
};
export default init;