@@ -31,6 +31,11 @@ function validateId(id: string): boolean {
31
31
return true ;
32
32
}
33
33
34
+ function validateEmail ( email : string ) : boolean {
35
+ const emailPattern = / ^ [ a - z A - Z 0 - 9 . _ % + - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - Z ] { 2 , } $ / ;
36
+ return emailPattern . test ( email ) ;
37
+ }
38
+
34
39
/**
35
40
* @function validateOptions
36
41
* @description Validate the options passed to the SDK
@@ -51,8 +56,8 @@ const validateOptions = ({
51
56
throw new Error ( '[SuperViz] Group fields is required' ) ;
52
57
}
53
58
54
- if ( ! participant || ! participant . id || ! participant . name ) {
55
- throw new Error ( '[SuperViz] Participant name and id is required' ) ;
59
+ if ( ! participant || ! participant . id ) {
60
+ throw new Error ( '[SuperViz] Participant id is required' ) ;
56
61
}
57
62
58
63
if ( ! roomId ) {
@@ -70,6 +75,10 @@ const validateOptions = ({
70
75
'[SuperViz] Participant id is invalid, it should be between 2 and 64 characters and only accept letters, numbers and special characters: -_&@+=,(){}[]/«».:|\'"' ,
71
76
) ;
72
77
}
78
+
79
+ if ( participant . email && ! validateEmail ( participant . email ) ) {
80
+ throw new Error ( '[SuperViz] Participant email is invalid' ) ;
81
+ }
73
82
} ;
74
83
75
84
/**
@@ -81,13 +90,13 @@ const validateColorsVariablesNames = (colors: ColorsVariables) => {
81
90
Object . entries ( colors ) . forEach ( ( [ key , value ] ) => {
82
91
if ( ! Object . values ( ColorsVariablesNames ) . includes ( key as ColorsVariablesNames ) ) {
83
92
throw new Error (
84
- `Color ${ key } is not a valid color variable name. Please check the documentation for more information.` ,
93
+ `[SuperViz] Color ${ key } is not a valid color variable name. Please check the documentation for more information.` ,
85
94
) ;
86
95
}
87
96
88
97
if ( ! / ^ ( \d { 1 , 3 } \s ) { 2 } \d { 1 , 3 } $ / . test ( value ) ) {
89
98
throw new Error (
90
- `Color ${ key } is not a valid color variable value. Please check the documentation for more information.` ,
99
+ `[SuperViz] Color ${ key } is not a valid color variable value. Please check the documentation for more information.` ,
91
100
) ;
92
101
}
93
102
} ) ;
@@ -140,44 +149,56 @@ const init = async (apiKey: string, options: SuperVizSdkOptions): Promise<Launch
140
149
throw new Error ( 'Failed to validate API key' ) ;
141
150
}
142
151
143
- const [ environment , waterMark , limits ] = await Promise . all ( [
144
- ApiService . fetchConfig ( apiUrl , apiKey ) ,
152
+ const [ waterMark , limits ] = await Promise . all ( [
145
153
ApiService . fetchWaterMark ( apiUrl , apiKey ) ,
146
154
ApiService . fetchLimits ( apiUrl , apiKey ) ,
147
155
] ) . catch ( ( ) => {
148
- throw new Error ( 'Failed to load configuration from server' ) ;
156
+ throw new Error ( '[SuperViz] Failed to load configuration from server' ) ;
149
157
} ) ;
150
158
151
- if ( ! environment || ! environment . ablyKey ) {
152
- throw new Error ( 'Failed to load configuration from server' ) ;
153
- }
154
-
155
- const { ablyKey } = environment ;
156
- const { participant, roomId, customColors : colors } = options ;
159
+ const { participant, roomId, customColors } = options ;
157
160
158
161
config . setConfig ( {
159
162
apiUrl,
160
- ablyKey,
161
163
apiKey,
162
164
conferenceLayerUrl,
163
165
environment : ( options . environment as EnvironmentTypes ) ?? EnvironmentTypes . PROD ,
164
166
roomId,
165
167
debug : options . debug ,
166
168
limits,
167
169
waterMark,
168
- colors : options . customColors ,
170
+ colors : customColors ,
169
171
features,
170
172
} ) ;
171
173
172
- setColorVariables ( options . customColors ) ;
174
+ setColorVariables ( customColors ) ;
173
175
174
- ApiService . createOrUpdateParticipant ( {
175
- name : participant . name ,
176
- participantId : participant . id ,
177
- avatar : participant . avatar ?. imageUrl ,
178
- } ) ;
176
+ const apiParticipant = await ApiService . fetchParticipant ( participant . id ) . catch ( ( ) => null ) ;
179
177
180
- return LauncherFacade ( options ) ;
178
+ if ( ! apiParticipant && ! participant . name ) {
179
+ throw new Error (
180
+ '[SuperViz] - Participant does not exist, create the user in the API or add the name in the initialization to initialize the SuperViz room.' ,
181
+ ) ;
182
+ }
183
+
184
+ if ( ! apiParticipant ) {
185
+ await ApiService . createParticipant ( {
186
+ participantId : participant . id ,
187
+ name : participant ?. name ,
188
+ avatar : participant . avatar ?. imageUrl ,
189
+ email : participant ?. email ,
190
+ } ) ;
191
+ }
192
+
193
+ return LauncherFacade ( {
194
+ ...options ,
195
+ participant : {
196
+ id : participant . id ,
197
+ name : participant . name ?? apiParticipant ?. name ,
198
+ avatar : participant . avatar ?? apiParticipant ?. avatar ,
199
+ email : participant . email ?? apiParticipant ?. email ,
200
+ } ,
201
+ } ) ;
181
202
} ;
182
203
183
204
export default init ;
0 commit comments