@nafzo/react-native-sdk 0.0.0-stage → 1.0.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.
@@ -0,0 +1,123 @@
1
+
2
+ /**
3
+ * Generate a UUID v4.
4
+ * Uses native crypto when available, with a fallback for React Native.
5
+ */
6
+ export function generateUUID() {
7
+ if (globalThis.crypto?.randomUUID) {
8
+ return globalThis.crypto.randomUUID();
9
+ }
10
+
11
+ return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
12
+ /[xy]/g,
13
+ (character) => {
14
+ const random = Math.floor(Math.random() * 16);
15
+
16
+ const value =
17
+ character === "x"
18
+ ? random
19
+ : (random & 0x3) | 0x8;
20
+
21
+ return value.toString(16);
22
+ }
23
+ );
24
+ }
25
+
26
+ /**
27
+ * Remove trailing slashes from an API URL.
28
+ */
29
+ export function normalizeBaseUrl(url) {
30
+ if (!url || typeof url !== "string") {
31
+ throw new Error("Nafzo: A valid baseUrl is required");
32
+ }
33
+
34
+ return url.trim().replace(/\/+$/, "");
35
+ }
36
+
37
+ /**
38
+ * Validate SDK initialization configuration.
39
+ */
40
+ export function validateConfig(config) {
41
+ if (!config || typeof config !== "object") {
42
+ throw new Error("Nafzo: Configuration is required");
43
+ }
44
+
45
+ if (!config.assistantId || typeof config.assistantId !== "string") {
46
+ throw new Error("Nafzo: assistantId is required");
47
+ }
48
+
49
+ if (!["android", "ios"].includes(config.platform)) {
50
+ throw new Error("Nafzo: platform must be android or ios");
51
+ }
52
+
53
+ if (
54
+ typeof config.appIdentifier !== "string" ||
55
+ !config.appIdentifier.trim()
56
+ ) {
57
+ throw new Error("Nafzo: appIdentifier is required");
58
+ }
59
+
60
+ const appIdentifier = config.appIdentifier.trim();
61
+
62
+ const packageNameRegex =
63
+ /^[a-zA-Z][a-zA-Z0-9_]*(\.[a-zA-Z][a-zA-Z0-9_]*)+$/;
64
+
65
+ if (!packageNameRegex.test(appIdentifier)) {
66
+ throw new Error(
67
+ "Nafzo: appIdentifier must be a valid Android package name or iOS Bundle ID"
68
+ );
69
+ }
70
+
71
+ if (
72
+ config.storage &&
73
+ (
74
+ typeof config.storage.getItem !== "function" ||
75
+ typeof config.storage.setItem !== "function"
76
+ )
77
+ ) {
78
+ throw new Error(
79
+ "Nafzo: storage must provide getItem() and setItem() methods"
80
+ );
81
+ }
82
+
83
+ return true;
84
+ }
85
+
86
+ /**
87
+ * Create a consistent SDK error.
88
+ */
89
+ export function createNafzoError(message, status = null, data = null) {
90
+ const error = new Error(message);
91
+
92
+ error.name = "NafzoError";
93
+ error.status = status;
94
+ error.data = data;
95
+
96
+ return error;
97
+ }
98
+
99
+ /**
100
+ * Normalize message text.
101
+ */
102
+ export function normalizeMessage(message) {
103
+ if (typeof message !== "string") {
104
+ return "";
105
+ }
106
+
107
+ return message.trim();
108
+ }
109
+
110
+ /**
111
+ * Platform Identifier.
112
+ */
113
+ export function getPlatformIdentifier(platform, androidId, iosId) {
114
+ if (platform === "android") {
115
+ return androidId;
116
+ }
117
+
118
+ if (platform === "ios") {
119
+ return iosId;
120
+ }
121
+
122
+ throw new Error("Nafzo: Unsupported platform");
123
+ }