vwo-fme-react-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +80 -0
- package/LICENSE +202 -0
- package/NOTICE +18 -0
- package/README.md +352 -0
- package/dist/VWOContext.d.ts +24 -0
- package/dist/VWOProvider.d.ts +35 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +8 -0
- package/dist/logger/LogMessageBuilder.d.ts +63 -0
- package/dist/logger/Logger.d.ts +46 -0
- package/dist/logger/core/LogManager.d.ts +100 -0
- package/dist/logger/core/TransportManager.d.ts +80 -0
- package/dist/logger/enums/LogLevelEnum.d.ts +22 -0
- package/dist/logger/index.d.ts +17 -0
- package/dist/logger/transports/ConsoleTransport.d.ts +60 -0
- package/dist/services/loggerService.d.ts +35 -0
- package/dist/types/Common.d.ts +36 -0
- package/dist/useGetFlag.d.ts +21 -0
- package/dist/useGetFlagVariable.d.ts +29 -0
- package/dist/useSetAttribute.d.ts +20 -0
- package/dist/useTrackEvent.d.ts +21 -0
- package/dist/useVWOClient.d.ts +20 -0
- package/dist/utils/DataTypeUtil.d.ts +38 -0
- package/dist/vwo-fme-react-sdk.cjs.development.js +904 -0
- package/dist/vwo-fme-react-sdk.cjs.development.js.map +1 -0
- package/dist/vwo-fme-react-sdk.cjs.production.min.js +904 -0
- package/dist/vwo-fme-react-sdk.cjs.production.min.js.map +1 -0
- package/dist/vwo-fme-react-sdk.esm.js +886 -0
- package/dist/vwo-fme-react-sdk.esm.js.map +1 -0
- package/lib/VWOContext.ts +47 -0
- package/lib/VWOProvider.tsx +103 -0
- package/lib/index.ts +29 -0
- package/lib/logger/LogMessageBuilder.ts +99 -0
- package/lib/logger/Logger.ts +51 -0
- package/lib/logger/core/LogManager.ts +164 -0
- package/lib/logger/core/TransportManager.ts +142 -0
- package/lib/logger/enums/LogLevelEnum.ts +23 -0
- package/lib/logger/index.ts +18 -0
- package/lib/logger/transports/ConsoleTransport.ts +85 -0
- package/lib/services/LoggerService.ts +49 -0
- package/lib/types/Common.ts +42 -0
- package/lib/useGetFlag.ts +81 -0
- package/lib/useGetFlagVariable.ts +65 -0
- package/lib/useSetAttribute.ts +48 -0
- package/lib/useTrackEvent.ts +52 -0
- package/lib/useVWOClient.ts +41 -0
- package/lib/utils/DataTypeUtil.ts +48 -0
- package/package.json +84 -0
|
@@ -0,0 +1,904 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
|
6
|
+
|
|
7
|
+
var React = require('react');
|
|
8
|
+
var React__default = _interopDefault(React);
|
|
9
|
+
var vwoFmeNodeSdk = require('vwo-fme-node-sdk');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
13
|
+
*
|
|
14
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
15
|
+
* you may not use this file except in compliance with the License.
|
|
16
|
+
* You may obtain a copy of the License at
|
|
17
|
+
*
|
|
18
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
19
|
+
*
|
|
20
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
21
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
22
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
23
|
+
* See the License for the specific language governing permissions and
|
|
24
|
+
* limitations under the License.
|
|
25
|
+
*/
|
|
26
|
+
/**
|
|
27
|
+
* Abstract class representing a logger.
|
|
28
|
+
* This class provides the structure for logging mechanisms and should be extended by specific logger implementations.
|
|
29
|
+
*/
|
|
30
|
+
class Logger {}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
34
|
+
*
|
|
35
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
36
|
+
* you may not use this file except in compliance with the License.
|
|
37
|
+
* You may obtain a copy of the License at
|
|
38
|
+
*
|
|
39
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
40
|
+
*
|
|
41
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
42
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
43
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
44
|
+
* See the License for the specific language governing permissions and
|
|
45
|
+
* limitations under the License.
|
|
46
|
+
*/
|
|
47
|
+
var LogLevelEnum;
|
|
48
|
+
(function (LogLevelEnum) {
|
|
49
|
+
LogLevelEnum["TRACE"] = "trace";
|
|
50
|
+
LogLevelEnum["DEBUG"] = "debug";
|
|
51
|
+
LogLevelEnum["INFO"] = "info";
|
|
52
|
+
LogLevelEnum["WARN"] = "warn";
|
|
53
|
+
LogLevelEnum["ERROR"] = "error";
|
|
54
|
+
})(LogLevelEnum || (LogLevelEnum = {}));
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
58
|
+
*
|
|
59
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
60
|
+
* you may not use this file except in compliance with the License.
|
|
61
|
+
* You may obtain a copy of the License at
|
|
62
|
+
*
|
|
63
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
64
|
+
*
|
|
65
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
66
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
67
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
68
|
+
* See the License for the specific language governing permissions and
|
|
69
|
+
* limitations under the License.
|
|
70
|
+
*/
|
|
71
|
+
/**
|
|
72
|
+
* ConsoleTransport class implements the Logger interface to provide logging functionality.
|
|
73
|
+
* It outputs logs to the console based on the log level set in the configuration.
|
|
74
|
+
*/
|
|
75
|
+
class ConsoleTransport {
|
|
76
|
+
/**
|
|
77
|
+
* Constructor initializes the ConsoleTransport with a configuration object.
|
|
78
|
+
* @param {Record<string, any>} config - Configuration settings for the logger, including 'level'.
|
|
79
|
+
*/
|
|
80
|
+
constructor(config = {}) {
|
|
81
|
+
this.config = config; // Store the configuration
|
|
82
|
+
this.level = this.config.level; // Set the logging level from the configuration
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Logs a trace message.
|
|
86
|
+
* @param {string} message - The message to log.
|
|
87
|
+
*/
|
|
88
|
+
trace(message) {
|
|
89
|
+
this.consoleLog(LogLevelEnum.TRACE, message);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Logs a debug message.
|
|
93
|
+
* @param {string} message - The message to log.
|
|
94
|
+
*/
|
|
95
|
+
debug(message) {
|
|
96
|
+
this.consoleLog(LogLevelEnum.DEBUG, message);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Logs an informational message.
|
|
100
|
+
* @param {string} message - The message to log.
|
|
101
|
+
*/
|
|
102
|
+
info(message) {
|
|
103
|
+
this.consoleLog(LogLevelEnum.INFO, message);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Logs a warning message.
|
|
107
|
+
* @param {string} message - The message to log.
|
|
108
|
+
*/
|
|
109
|
+
warn(message) {
|
|
110
|
+
this.consoleLog(LogLevelEnum.WARN, message);
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Logs an error message.
|
|
114
|
+
* @param {string} message - The message to log.
|
|
115
|
+
*/
|
|
116
|
+
error(message) {
|
|
117
|
+
this.consoleLog(LogLevelEnum.ERROR, message);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Generic log function that logs messages to the console based on the log level.
|
|
121
|
+
* @param {string} level - The log level under which the message should be logged.
|
|
122
|
+
* @param {string} message - The message to log.
|
|
123
|
+
*/
|
|
124
|
+
consoleLog(level, message) {
|
|
125
|
+
console[level](message); // Use console's logging function dynamically based on the level
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
131
|
+
*
|
|
132
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
133
|
+
* you may not use this file except in compliance with the License.
|
|
134
|
+
* You may obtain a copy of the License at
|
|
135
|
+
*
|
|
136
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
137
|
+
*
|
|
138
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
139
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
140
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
141
|
+
* See the License for the specific language governing permissions and
|
|
142
|
+
* limitations under the License.
|
|
143
|
+
*/
|
|
144
|
+
/**
|
|
145
|
+
* Implements the ILogMessageBuilder interface to provide a concrete log message builder.
|
|
146
|
+
*/
|
|
147
|
+
class LogMessageBuilder {
|
|
148
|
+
/**
|
|
149
|
+
* Constructs a new LogMessageBuilder instance.
|
|
150
|
+
* @param {Record<string, any>} loggerConfig - Configuration for the logger.
|
|
151
|
+
* @param {Record<string, any>} transportConfig - Configuration for the transport mechanism.
|
|
152
|
+
*/
|
|
153
|
+
constructor(loggerConfig, transportConfig) {
|
|
154
|
+
this.loggerConfig = loggerConfig;
|
|
155
|
+
this.transportConfig = transportConfig;
|
|
156
|
+
// Set the prefix, defaulting to an empty string if not provided.
|
|
157
|
+
this.prefix = this.transportConfig.prefix || this.loggerConfig.prefix || '';
|
|
158
|
+
// Set the date and time format, defaulting to the logger's format if the transport's format is not provided.
|
|
159
|
+
this.dateTimeFormat = this.transportConfig.dateTimeFormat || this.loggerConfig.dateTimeFormat;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Formats a log message combining level, prefix, date/time, and the actual message.
|
|
163
|
+
* @param {string} level - The log level.
|
|
164
|
+
* @param {string} message - The message to log.
|
|
165
|
+
* @returns {string} The formatted log message.
|
|
166
|
+
*/
|
|
167
|
+
formatMessage(level, message) {
|
|
168
|
+
return `[${this.getFormattedLevel(level)}]: ${this.getFormattedPrefix(this.prefix)} ${this.getFormattedDateTime()} ${message}`;
|
|
169
|
+
}
|
|
170
|
+
getFormattedPrefix(prefix) {
|
|
171
|
+
return `${prefix}`;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Returns the formatted log level with appropriate coloring based on the log level.
|
|
175
|
+
* @param {string} level - The log level.
|
|
176
|
+
* @returns {string} The formatted log level.
|
|
177
|
+
*/
|
|
178
|
+
getFormattedLevel(level) {
|
|
179
|
+
const upperCaseLevel = level.toUpperCase();
|
|
180
|
+
let LogLevelColorInfoEnum;
|
|
181
|
+
LogLevelColorInfoEnum = {
|
|
182
|
+
[LogLevelEnum.TRACE]: upperCaseLevel,
|
|
183
|
+
[LogLevelEnum.DEBUG]: upperCaseLevel,
|
|
184
|
+
[LogLevelEnum.INFO]: upperCaseLevel,
|
|
185
|
+
[LogLevelEnum.WARN]: upperCaseLevel,
|
|
186
|
+
[LogLevelEnum.ERROR]: upperCaseLevel
|
|
187
|
+
};
|
|
188
|
+
return LogLevelColorInfoEnum[level];
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Retrieves the current date and time formatted according to the specified format.
|
|
192
|
+
* @returns {string} The formatted date and time.
|
|
193
|
+
*/
|
|
194
|
+
getFormattedDateTime() {
|
|
195
|
+
return this.dateTimeFormat();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
201
|
+
*
|
|
202
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
203
|
+
* you may not use this file except in compliance with the License.
|
|
204
|
+
* You may obtain a copy of the License at
|
|
205
|
+
*
|
|
206
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
207
|
+
*
|
|
208
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
209
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
210
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
211
|
+
* See the License for the specific language governing permissions and
|
|
212
|
+
* limitations under the License.
|
|
213
|
+
*/
|
|
214
|
+
/**
|
|
215
|
+
* Checks if a value is a function.
|
|
216
|
+
* @param val The value to check.
|
|
217
|
+
* @returns True if the value is a function, false otherwise.
|
|
218
|
+
*/
|
|
219
|
+
function isFunction(val) {
|
|
220
|
+
return Object.prototype.toString.call(val) === '[object Function]';
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Checks if a value is an object (excluding arrays, functions, regex, promises, and dates).
|
|
224
|
+
* @param val The value to check.
|
|
225
|
+
* @returns True if the value is a valid object, false otherwise.
|
|
226
|
+
*/
|
|
227
|
+
function isObject(val) {
|
|
228
|
+
return Object.prototype.toString.call(val) === '[object Object]';
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Checks if a value is a string.
|
|
232
|
+
* @param val The value to check.
|
|
233
|
+
* @returns True if the value is a string, false otherwise.
|
|
234
|
+
*/
|
|
235
|
+
function isString(val) {
|
|
236
|
+
return typeof val === 'string' || Object.prototype.toString.call(val) === '[object String]';
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
241
|
+
*
|
|
242
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
243
|
+
* you may not use this file except in compliance with the License.
|
|
244
|
+
* You may obtain a copy of the License at
|
|
245
|
+
*
|
|
246
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
247
|
+
*
|
|
248
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
249
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
250
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
251
|
+
* See the License for the specific language governing permissions and
|
|
252
|
+
* limitations under the License.
|
|
253
|
+
*/
|
|
254
|
+
var LogLevelNumberEnum;
|
|
255
|
+
(function (LogLevelNumberEnum) {
|
|
256
|
+
LogLevelNumberEnum[LogLevelNumberEnum["TRACE"] = 0] = "TRACE";
|
|
257
|
+
LogLevelNumberEnum[LogLevelNumberEnum["DEBUG"] = 1] = "DEBUG";
|
|
258
|
+
LogLevelNumberEnum[LogLevelNumberEnum["INFO"] = 2] = "INFO";
|
|
259
|
+
LogLevelNumberEnum[LogLevelNumberEnum["WARN"] = 3] = "WARN";
|
|
260
|
+
LogLevelNumberEnum[LogLevelNumberEnum["ERROR"] = 4] = "ERROR";
|
|
261
|
+
})(LogLevelNumberEnum || (LogLevelNumberEnum = {}));
|
|
262
|
+
/**
|
|
263
|
+
* Manages logging transports and delegates logging messages to them based on configuration.
|
|
264
|
+
* Implements the IlogTransport interface.
|
|
265
|
+
*/
|
|
266
|
+
class LogTransportManager {
|
|
267
|
+
/**
|
|
268
|
+
* Initializes the manager with a configuration object.
|
|
269
|
+
* @param {Record<string, any>} config - Configuration settings for the log manager.
|
|
270
|
+
*/
|
|
271
|
+
constructor(config) {
|
|
272
|
+
this.transports = [];
|
|
273
|
+
this.config = config;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Adds a new transport to the manager.
|
|
277
|
+
* @param {Record<string, any>} transport - The transport object to be added.
|
|
278
|
+
*/
|
|
279
|
+
addTransport(transport) {
|
|
280
|
+
this.transports.push(transport);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Determines if the log should be processed based on the transport and configuration levels.
|
|
284
|
+
* @param {string} transportLevel - The log level set for the transport.
|
|
285
|
+
* @param {string} configLevel - The log level set in the configuration.
|
|
286
|
+
* @returns {boolean} - Returns true if the log level is appropriate for logging, false otherwise.
|
|
287
|
+
*/
|
|
288
|
+
shouldLog(transportLevel, configLevel) {
|
|
289
|
+
// Default to the most specific level available
|
|
290
|
+
// transportLevel = transportLevel || configLevel || this.config.level;
|
|
291
|
+
const targetLevel = LogLevelNumberEnum[transportLevel.toUpperCase()];
|
|
292
|
+
const desiredLevel = LogLevelNumberEnum[(configLevel || this.config.level).toUpperCase()];
|
|
293
|
+
return targetLevel >= desiredLevel;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Logs a message at TRACE level.
|
|
297
|
+
* @param {string} message - The message to log.
|
|
298
|
+
*/
|
|
299
|
+
trace(message) {
|
|
300
|
+
this.log(LogLevelEnum.TRACE, message);
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Logs a message at DEBUG level.
|
|
304
|
+
* @param {string} message - The message to log.
|
|
305
|
+
*/
|
|
306
|
+
debug(message) {
|
|
307
|
+
this.log(LogLevelEnum.DEBUG, message);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Logs a message at INFO level.
|
|
311
|
+
* @param {string} message - The message to log.
|
|
312
|
+
*/
|
|
313
|
+
info(message) {
|
|
314
|
+
this.log(LogLevelEnum.INFO, message);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Logs a message at WARN level.
|
|
318
|
+
* @param {string} message - The message to log.
|
|
319
|
+
*/
|
|
320
|
+
warn(message) {
|
|
321
|
+
this.log(LogLevelEnum.WARN, message);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Logs a message at ERROR level.
|
|
325
|
+
* @param {string} message - The message to log.
|
|
326
|
+
*/
|
|
327
|
+
error(message) {
|
|
328
|
+
this.log(LogLevelEnum.ERROR, message);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Delegates the logging of messages to the appropriate transports.
|
|
332
|
+
* @param {string} level - The level at which to log the message.
|
|
333
|
+
* @param {string} message - The message to log.
|
|
334
|
+
*/
|
|
335
|
+
log(level, message) {
|
|
336
|
+
for (let i = 0; i < this.transports.length; i++) {
|
|
337
|
+
const logMessageBuilder = new LogMessageBuilder(this.config, this.transports[i]);
|
|
338
|
+
const formattedMessage = logMessageBuilder.formatMessage(level, message);
|
|
339
|
+
if (this.shouldLog(level, this.transports[i].level)) {
|
|
340
|
+
if (this.transports[i].log && isFunction(this.transports[i].log)) {
|
|
341
|
+
// Use custom log handler if available
|
|
342
|
+
this.transports[i].log(level, message);
|
|
343
|
+
} else {
|
|
344
|
+
// Otherwise, use the default log method
|
|
345
|
+
this.transports[i][level](formattedMessage);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
354
|
+
*
|
|
355
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
356
|
+
* you may not use this file except in compliance with the License.
|
|
357
|
+
* You may obtain a copy of the License at
|
|
358
|
+
*
|
|
359
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
360
|
+
*
|
|
361
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
362
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
363
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
364
|
+
* See the License for the specific language governing permissions and
|
|
365
|
+
* limitations under the License.
|
|
366
|
+
*/
|
|
367
|
+
/**
|
|
368
|
+
* LogManager class provides logging functionality with support for multiple transports.
|
|
369
|
+
* It is designed as a singleton to ensure a single instance throughout the application.
|
|
370
|
+
*/
|
|
371
|
+
class LogManager extends Logger {
|
|
372
|
+
/**
|
|
373
|
+
* Constructor for LogManager.
|
|
374
|
+
* @param {Record<string, any>} config - Configuration object for LogManager.
|
|
375
|
+
*/
|
|
376
|
+
constructor(config) {
|
|
377
|
+
super();
|
|
378
|
+
this.name = 'VWO Logger'; // Default logger name
|
|
379
|
+
this.requestId = new Date().getTime().toString(); // current timestamp in milliseconds
|
|
380
|
+
this.level = LogLevelEnum.ERROR; // Default logging level
|
|
381
|
+
this.prefix = 'VWO-SDK'; // Default prefix for log messages
|
|
382
|
+
this.config = config;
|
|
383
|
+
if (config.isAlwaysNewInstance || !LogManager.instance) {
|
|
384
|
+
LogManager.instance = this;
|
|
385
|
+
// Initialize configuration with defaults or provided values
|
|
386
|
+
this.config.name = config.name || this.name;
|
|
387
|
+
this.config.requestId = config.requestId || this.requestId;
|
|
388
|
+
this.config.level = config.level || this.level;
|
|
389
|
+
this.config.prefix = config.prefix || this.prefix;
|
|
390
|
+
this.config.dateTimeFormat = config.dateTimeFormat || this.dateTimeFormat;
|
|
391
|
+
this.transportManager = new LogTransportManager(this.config);
|
|
392
|
+
this.handleTransports();
|
|
393
|
+
}
|
|
394
|
+
return LogManager.instance;
|
|
395
|
+
}
|
|
396
|
+
dateTimeFormat() {
|
|
397
|
+
return new Date().toISOString(); // Default date-time format for log messages
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Provides access to the singleton instance of LogManager.
|
|
401
|
+
* @returns {LogManager} The singleton instance.
|
|
402
|
+
*/
|
|
403
|
+
static get Instance() {
|
|
404
|
+
return LogManager.instance;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Handles the initialization and setup of transports based on configuration.
|
|
408
|
+
*/
|
|
409
|
+
handleTransports() {
|
|
410
|
+
this.addTransport(new ConsoleTransport({
|
|
411
|
+
level: this.config.level
|
|
412
|
+
}));
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Adds a single transport to the LogManager.
|
|
416
|
+
* @param {Record<any, any>} transport - The transport object to add.
|
|
417
|
+
*/
|
|
418
|
+
addTransport(transport) {
|
|
419
|
+
this.transportManager.addTransport(transport);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Adds multiple transports to the LogManager.
|
|
423
|
+
* @param {Array<Record<any, any>>} transports - The list of transport objects to add.
|
|
424
|
+
*/
|
|
425
|
+
addTransports(transports) {
|
|
426
|
+
for (let i = 0; i < transports.length; i++) {
|
|
427
|
+
this.addTransport(transports[i]);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Logs a trace message.
|
|
432
|
+
* @param {string} message - The message to log at trace level.
|
|
433
|
+
*/
|
|
434
|
+
trace(message) {
|
|
435
|
+
this.transportManager.log(LogLevelEnum.TRACE, message);
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Logs a debug message.
|
|
439
|
+
* @param {string} message - The message to log at debug level.
|
|
440
|
+
*/
|
|
441
|
+
debug(message) {
|
|
442
|
+
this.transportManager.log(LogLevelEnum.DEBUG, message);
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Logs an informational message.
|
|
446
|
+
* @param {string} message - The message to log at info level.
|
|
447
|
+
*/
|
|
448
|
+
info(message) {
|
|
449
|
+
this.transportManager.log(LogLevelEnum.INFO, message);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Logs a warning message.
|
|
453
|
+
* @param {string} message - The message to log at warn level.
|
|
454
|
+
*/
|
|
455
|
+
warn(message) {
|
|
456
|
+
this.transportManager.log(LogLevelEnum.WARN, message);
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Logs an error message.
|
|
460
|
+
* @param {string} message - The message to log at error level.
|
|
461
|
+
*/
|
|
462
|
+
error(message) {
|
|
463
|
+
this.transportManager.log(LogLevelEnum.ERROR, message);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
469
|
+
*
|
|
470
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
471
|
+
* you may not use this file except in compliance with the License.
|
|
472
|
+
* You may obtain a copy of the License at
|
|
473
|
+
*
|
|
474
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
475
|
+
*
|
|
476
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
477
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
478
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
479
|
+
* See the License for the specific language governing permissions and
|
|
480
|
+
* limitations under the License.
|
|
481
|
+
*/
|
|
482
|
+
let logger = null;
|
|
483
|
+
/**
|
|
484
|
+
* Initializes the global logger instance.
|
|
485
|
+
* This function should be called inside `VWOProvider` before logging is used anywhere in the application.
|
|
486
|
+
*
|
|
487
|
+
* @param {object} config - The logger configuration object.
|
|
488
|
+
* @param {object} config.logger - Optional logging configuration (e.g., log level, transports).
|
|
489
|
+
* @returns {void} No return value; initializes the logger instance.
|
|
490
|
+
*/
|
|
491
|
+
function initLogger(config) {
|
|
492
|
+
if (!logger) {
|
|
493
|
+
logger = new LogManager(config.logger || {});
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
/**
|
|
497
|
+
* Retrieves the global logger instance.
|
|
498
|
+
* Ensures that `initLogger` has been called before attempting to use logging.
|
|
499
|
+
*
|
|
500
|
+
* @returns {LogManager} The global logger instance.
|
|
501
|
+
*/
|
|
502
|
+
function getLogger() {
|
|
503
|
+
if (!logger) {
|
|
504
|
+
logger = new LogManager({
|
|
505
|
+
level: 'error'
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
return logger;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
513
|
+
*
|
|
514
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
515
|
+
* you may not use this file except in compliance with the License.
|
|
516
|
+
* You may obtain a copy of the License at
|
|
517
|
+
*
|
|
518
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
519
|
+
*
|
|
520
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
521
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
522
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
523
|
+
* See the License for the specific language governing permissions and
|
|
524
|
+
* limitations under the License.
|
|
525
|
+
*/
|
|
526
|
+
/**
|
|
527
|
+
* Context for VWO SDK
|
|
528
|
+
*/
|
|
529
|
+
const VWOContext = /*#__PURE__*/React.createContext({
|
|
530
|
+
vwoClient: null,
|
|
531
|
+
userContext: {}
|
|
532
|
+
});
|
|
533
|
+
/**
|
|
534
|
+
* Hook to use the VWO context
|
|
535
|
+
* @returns VWO context
|
|
536
|
+
*/
|
|
537
|
+
const useVWOContext = () => {
|
|
538
|
+
const logger = getLogger();
|
|
539
|
+
try {
|
|
540
|
+
// Fetch the context
|
|
541
|
+
const context = React.useContext(VWOContext);
|
|
542
|
+
// If the context is not found, throw an error
|
|
543
|
+
if (!context) {
|
|
544
|
+
logger.error('useVWOContext must be used within a VWOProvider');
|
|
545
|
+
return null;
|
|
546
|
+
}
|
|
547
|
+
return context;
|
|
548
|
+
} catch (error) {
|
|
549
|
+
logger.error(`Error in useVWOContext hook: ${error}`);
|
|
550
|
+
return null;
|
|
551
|
+
}
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
556
|
+
*
|
|
557
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
558
|
+
* you may not use this file except in compliance with the License.
|
|
559
|
+
* You may obtain a copy of the License at
|
|
560
|
+
*
|
|
561
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
562
|
+
*
|
|
563
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
564
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
565
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
566
|
+
* See the License for the specific language governing permissions and
|
|
567
|
+
* limitations under the License.
|
|
568
|
+
*/
|
|
569
|
+
/**
|
|
570
|
+
* VWOProvider component to provide the VWO SDK instance and context to the app
|
|
571
|
+
* @param client - VWO SDK instance
|
|
572
|
+
* @param config - VWO SDK config (initialization config)
|
|
573
|
+
* @param context - VWO SDK context (userContext)
|
|
574
|
+
* @param children - React children (ReactNode)
|
|
575
|
+
* @returns VWOProvider component
|
|
576
|
+
*/
|
|
577
|
+
const VWOProvider = ({
|
|
578
|
+
client,
|
|
579
|
+
config,
|
|
580
|
+
context,
|
|
581
|
+
children
|
|
582
|
+
}) => {
|
|
583
|
+
const [vwoClient, setVwoClient] = React.useState(client || null);
|
|
584
|
+
const vwoClientRef = React.useRef(vwoClient);
|
|
585
|
+
const isMounted = React.useRef(true);
|
|
586
|
+
// Initialize logger globally before using it
|
|
587
|
+
React.useEffect(() => {
|
|
588
|
+
if (config?.logger) {
|
|
589
|
+
initLogger(config);
|
|
590
|
+
}
|
|
591
|
+
}, [config]);
|
|
592
|
+
const logger = getLogger();
|
|
593
|
+
// Initialize the VWO SDK instance when the component mounts
|
|
594
|
+
React.useEffect(() => {
|
|
595
|
+
if (!vwoClient && !config) {
|
|
596
|
+
logger.error("VWOProvider Error: Either `client` or `config` must be provided.");
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
if (!context || !isObject(context)) {
|
|
600
|
+
logger.error("VWOProvider Error: `context` is required and must be a valid object.");
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
isMounted.current = true;
|
|
604
|
+
async function initializeVWO() {
|
|
605
|
+
if (!vwoClient && config) {
|
|
606
|
+
try {
|
|
607
|
+
// Initialize the VWO SDK instance
|
|
608
|
+
const instance = await vwoFmeNodeSdk.init(config);
|
|
609
|
+
if (isMounted.current) {
|
|
610
|
+
// Update the VWO SDK instance
|
|
611
|
+
setVwoClient(instance);
|
|
612
|
+
// Update the ref with the new instance
|
|
613
|
+
vwoClientRef.current = instance;
|
|
614
|
+
}
|
|
615
|
+
} catch (error) {
|
|
616
|
+
logger.error("VWO-SDK Initialization failed:");
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
// Initialize the VWO SDK instance
|
|
621
|
+
initializeVWO();
|
|
622
|
+
// Cleanup the VWO SDK instance when the component unmounts
|
|
623
|
+
return () => {
|
|
624
|
+
isMounted.current = false;
|
|
625
|
+
if (vwoClientRef.current?.destroy) vwoClientRef.current.destroy();
|
|
626
|
+
};
|
|
627
|
+
}, [vwoClient, config]);
|
|
628
|
+
// Provide the VWO SDK instance and context to the app
|
|
629
|
+
return React__default.createElement(VWOContext.Provider, {
|
|
630
|
+
value: {
|
|
631
|
+
vwoClient,
|
|
632
|
+
userContext: context
|
|
633
|
+
}
|
|
634
|
+
}, children);
|
|
635
|
+
};
|
|
636
|
+
|
|
637
|
+
/**
|
|
638
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
639
|
+
*
|
|
640
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
641
|
+
* you may not use this file except in compliance with the License.
|
|
642
|
+
* You may obtain a copy of the License at
|
|
643
|
+
*
|
|
644
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
645
|
+
*
|
|
646
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
647
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
648
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
649
|
+
* See the License for the specific language governing permissions and
|
|
650
|
+
* limitations under the License.
|
|
651
|
+
*/
|
|
652
|
+
/**
|
|
653
|
+
* Returns the VWO SDK client instance
|
|
654
|
+
* @returns VWO SDK client instance
|
|
655
|
+
*/
|
|
656
|
+
const useVWOClient = () => {
|
|
657
|
+
const logger = getLogger();
|
|
658
|
+
try {
|
|
659
|
+
const context = useVWOContext();
|
|
660
|
+
// If the VWO SDK client is not found, throw an error
|
|
661
|
+
if (!context || !context.vwoClient) {
|
|
662
|
+
logger.error('useVWOClient must be used within a VWOProvider !!');
|
|
663
|
+
return null;
|
|
664
|
+
}
|
|
665
|
+
// Return the VWO SDK client
|
|
666
|
+
return context.vwoClient;
|
|
667
|
+
} catch (error) {
|
|
668
|
+
logger.error(`Error in useVWOClient hook: ${error}`);
|
|
669
|
+
return null;
|
|
670
|
+
}
|
|
671
|
+
};
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
675
|
+
*
|
|
676
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
677
|
+
* you may not use this file except in compliance with the License.
|
|
678
|
+
* You may obtain a copy of the License at
|
|
679
|
+
*
|
|
680
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
681
|
+
*
|
|
682
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
683
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
684
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
685
|
+
* See the License for the specific language governing permissions and
|
|
686
|
+
* limitations under the License.
|
|
687
|
+
*/
|
|
688
|
+
/**
|
|
689
|
+
* Hook to get a feature flag value
|
|
690
|
+
* @param featureKey - The key of the feature flag to get
|
|
691
|
+
* @returns The feature flag value
|
|
692
|
+
*/
|
|
693
|
+
const useGetFlag = featureKey => {
|
|
694
|
+
// This is the return schema for the flag if the feature key is not found or the flag is not enabled
|
|
695
|
+
const errorReturnSchema = {
|
|
696
|
+
isEnabled: () => false,
|
|
697
|
+
getVariables: () => [],
|
|
698
|
+
getVariable: (_key, defaultValue) => defaultValue
|
|
699
|
+
};
|
|
700
|
+
const logger = getLogger();
|
|
701
|
+
try {
|
|
702
|
+
if (!featureKey) {
|
|
703
|
+
logger.error('Feature key is required for useGetFlag hook');
|
|
704
|
+
return errorReturnSchema;
|
|
705
|
+
}
|
|
706
|
+
const [flag, setFlag] = React.useState(null);
|
|
707
|
+
const {
|
|
708
|
+
vwoClient,
|
|
709
|
+
userContext
|
|
710
|
+
} = useVWOContext();
|
|
711
|
+
if (!vwoClient) {
|
|
712
|
+
logger.error('VWO Client is missing in useGetFlag hook. Ensure VWOProvider is correctly initialized.');
|
|
713
|
+
return errorReturnSchema;
|
|
714
|
+
}
|
|
715
|
+
if (!userContext || !isObject(userContext)) {
|
|
716
|
+
logger.error('Invalid user context in useGetFlag hook. Ensure a valid userContext is provided.');
|
|
717
|
+
return errorReturnSchema;
|
|
718
|
+
}
|
|
719
|
+
// Memoize the userContext to avoid unnecessary re-fetching
|
|
720
|
+
const stableUserContext = React.useMemo(() => userContext, [userContext]);
|
|
721
|
+
// Memoize the getFlag function to avoid re-creation
|
|
722
|
+
const getFlag = React.useMemo(() => {
|
|
723
|
+
return async () => {
|
|
724
|
+
try {
|
|
725
|
+
const result = await vwoClient.getFlag(featureKey, stableUserContext);
|
|
726
|
+
setFlag(result);
|
|
727
|
+
} catch (error) {
|
|
728
|
+
logger.error(`Error fetching feature flag "${featureKey}": ${error}`);
|
|
729
|
+
setFlag({});
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
}, [featureKey, vwoClient, stableUserContext]);
|
|
733
|
+
// Runs only when `getFlag` reference changes
|
|
734
|
+
React.useEffect(() => {
|
|
735
|
+
getFlag();
|
|
736
|
+
}, [getFlag]);
|
|
737
|
+
return flag;
|
|
738
|
+
} catch (error) {
|
|
739
|
+
logger.error(`Error getting feature flag: ${error}`);
|
|
740
|
+
return errorReturnSchema;
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
746
|
+
*
|
|
747
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
748
|
+
* you may not use this file except in compliance with the License.
|
|
749
|
+
* You may obtain a copy of the License at
|
|
750
|
+
*
|
|
751
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
752
|
+
*
|
|
753
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
754
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
755
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
756
|
+
* See the License for the specific language governing permissions and
|
|
757
|
+
* limitations under the License.
|
|
758
|
+
*/
|
|
759
|
+
/**
|
|
760
|
+
* Hook to get all variables from a flag
|
|
761
|
+
* @param flag - The flag to get the variables from
|
|
762
|
+
* @returns The variables from the flag
|
|
763
|
+
*/
|
|
764
|
+
const useGetFlagVariables = flag => {
|
|
765
|
+
const logger = getLogger();
|
|
766
|
+
try {
|
|
767
|
+
if (!flag || !isObject(flag)) {
|
|
768
|
+
logger.error('Flag is required for useGetFlagVariables hook and should be an object');
|
|
769
|
+
return [];
|
|
770
|
+
}
|
|
771
|
+
return flag.getVariables();
|
|
772
|
+
} catch (error) {
|
|
773
|
+
logger.error(`Error getting flag variables: ${error}`);
|
|
774
|
+
return [];
|
|
775
|
+
}
|
|
776
|
+
};
|
|
777
|
+
/**
|
|
778
|
+
* Hook to get a flag variable
|
|
779
|
+
* @param flag - The flag to get the variable from
|
|
780
|
+
* @param variableKey - The key of the variable to get
|
|
781
|
+
* @param defaultValue - The default value to return if the variable is not found
|
|
782
|
+
* @returns The value of the variable
|
|
783
|
+
*/
|
|
784
|
+
const useGetFlagVariable = (flag, variableKey, defaultValue) => {
|
|
785
|
+
const logger = getLogger();
|
|
786
|
+
try {
|
|
787
|
+
if (!flag || !isObject(flag)) {
|
|
788
|
+
logger.error('Flag is required for useGetFlagVariable hook and should be an object');
|
|
789
|
+
return [];
|
|
790
|
+
}
|
|
791
|
+
if (!flag || !variableKey) {
|
|
792
|
+
logger.error('Flag and variable key are required for useGetFlagVariable hook');
|
|
793
|
+
return defaultValue;
|
|
794
|
+
}
|
|
795
|
+
return flag.getVariable(variableKey, defaultValue);
|
|
796
|
+
} catch (error) {
|
|
797
|
+
logger.error(`Error getting flag variable: ${error}`);
|
|
798
|
+
return defaultValue;
|
|
799
|
+
}
|
|
800
|
+
};
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
804
|
+
*
|
|
805
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
806
|
+
* you may not use this file except in compliance with the License.
|
|
807
|
+
* You may obtain a copy of the License at
|
|
808
|
+
*
|
|
809
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
810
|
+
*
|
|
811
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
812
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
813
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
814
|
+
* See the License for the specific language governing permissions and
|
|
815
|
+
* limitations under the License.
|
|
816
|
+
*/
|
|
817
|
+
/**
|
|
818
|
+
* Hook to track an event
|
|
819
|
+
* @param eventName - The name of the event to track
|
|
820
|
+
* @param eventProperties - The properties of the event to track (optional)
|
|
821
|
+
*/
|
|
822
|
+
const useTrackEvent = (eventName, eventProperties = {}) => {
|
|
823
|
+
const logger = getLogger();
|
|
824
|
+
try {
|
|
825
|
+
if (!eventName && !isString(eventName)) {
|
|
826
|
+
logger.error('Event name is required for useTrackEvent hook and it should be a string');
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
// Fetch the vwoClient and userContext from the context
|
|
830
|
+
const {
|
|
831
|
+
vwoClient,
|
|
832
|
+
userContext
|
|
833
|
+
} = useVWOContext();
|
|
834
|
+
if (!vwoClient) {
|
|
835
|
+
logger.error('VWO Client is missing in useTrackEvent hook. Ensure VWOProvider is correctly initialized.');
|
|
836
|
+
return {};
|
|
837
|
+
}
|
|
838
|
+
if (!userContext || !isObject(userContext)) {
|
|
839
|
+
logger.error('Invalid user context in useTrackEvent hook. Ensure a valid userContext is provided.');
|
|
840
|
+
return {};
|
|
841
|
+
}
|
|
842
|
+
// Track the event
|
|
843
|
+
vwoClient.trackEvent(eventName, userContext, eventProperties);
|
|
844
|
+
} catch (error) {
|
|
845
|
+
logger.error(`Error tracking event: ${error}`);
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
|
|
849
|
+
/**
|
|
850
|
+
* Copyright 2025 Wingify Software Pvt. Ltd.
|
|
851
|
+
*
|
|
852
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
853
|
+
* you may not use this file except in compliance with the License.
|
|
854
|
+
* You may obtain a copy of the License at
|
|
855
|
+
*
|
|
856
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
857
|
+
*
|
|
858
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
859
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
860
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
861
|
+
* See the License for the specific language governing permissions and
|
|
862
|
+
* limitations under the License.
|
|
863
|
+
*/
|
|
864
|
+
/**
|
|
865
|
+
* Hook to set attributes for the user
|
|
866
|
+
* @param attributeMap - The map of attributes to set
|
|
867
|
+
*/
|
|
868
|
+
const useSetAttribute = attributeMap => {
|
|
869
|
+
const logger = getLogger();
|
|
870
|
+
try {
|
|
871
|
+
if (!attributeMap || !isObject(attributeMap) || Object.keys(attributeMap).length === 0) {
|
|
872
|
+
logger.error('attributeMap(object having key-value pairs of user attributes) is required for useSetAttribute hook');
|
|
873
|
+
return;
|
|
874
|
+
}
|
|
875
|
+
// Fetch the vwoClient and userContext from the context
|
|
876
|
+
const {
|
|
877
|
+
vwoClient,
|
|
878
|
+
userContext
|
|
879
|
+
} = useVWOContext();
|
|
880
|
+
if (!userContext || !isObject(userContext)) {
|
|
881
|
+
logger.error('Invalid user context in useSetAttribute hook. Ensure a valid userContext is provided.');
|
|
882
|
+
return {};
|
|
883
|
+
}
|
|
884
|
+
// Set the attributes
|
|
885
|
+
vwoClient.setAttribute(attributeMap, userContext);
|
|
886
|
+
} catch (error) {
|
|
887
|
+
logger.error(`Error setting attributes: ${error}`);
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
|
|
891
|
+
Object.defineProperty(exports, 'init', {
|
|
892
|
+
enumerable: true,
|
|
893
|
+
get: function () {
|
|
894
|
+
return vwoFmeNodeSdk.init;
|
|
895
|
+
}
|
|
896
|
+
});
|
|
897
|
+
exports.VWOProvider = VWOProvider;
|
|
898
|
+
exports.useGetFlag = useGetFlag;
|
|
899
|
+
exports.useGetFlagVariable = useGetFlagVariable;
|
|
900
|
+
exports.useGetFlagVariables = useGetFlagVariables;
|
|
901
|
+
exports.useSetAttribute = useSetAttribute;
|
|
902
|
+
exports.useTrackEvent = useTrackEvent;
|
|
903
|
+
exports.useVWOClient = useVWOClient;
|
|
904
|
+
//# sourceMappingURL=vwo-fme-react-sdk.cjs.production.min.js.map
|