@altcraft/react-native-sdk 0.0.2 → 0.0.3

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.
Files changed (33) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +5 -2
  3. package/android/build.gradle +2 -3
  4. package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +551 -478
  5. package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +164 -0
  6. package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
  7. package/ios/Converter.swift +483 -46
  8. package/ios/Sdk.mm +83 -271
  9. package/ios/SdkModule.swift +335 -493
  10. package/lib/module/NativeSdk.js +35 -0
  11. package/lib/module/NativeSdk.js.map +1 -1
  12. package/lib/module/Utilities.js +94 -0
  13. package/lib/module/Utilities.js.map +1 -0
  14. package/lib/module/index.js +224 -246
  15. package/lib/module/index.js.map +1 -1
  16. package/lib/typescript/src/NativeSdk.d.ts +107 -31
  17. package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
  18. package/lib/typescript/src/Utilities.d.ts +15 -0
  19. package/lib/typescript/src/Utilities.d.ts.map +1 -0
  20. package/lib/typescript/src/index.d.ts +162 -94
  21. package/lib/typescript/src/index.d.ts.map +1 -1
  22. package/package.json +2 -2
  23. package/src/NativeSdk.ts +135 -44
  24. package/src/Utilities.ts +116 -0
  25. package/src/index.tsx +281 -311
  26. package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -26
  27. package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
  28. package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
  29. package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
  30. package/ios/protocols/RNAPNSProvider.swift +0 -15
  31. package/ios/protocols/RNFCMProvider.swift +0 -24
  32. package/ios/protocols/RNHMSProvider.swift +0 -24
  33. package/ios/protocols/RNJWTProvider.swift +0 -15
package/src/NativeSdk.ts CHANGED
@@ -2,6 +2,7 @@ import { TurboModuleRegistry, type TurboModule } from 'react-native';
2
2
 
3
3
  // ---------------- Data models ----------------
4
4
 
5
+ /** Subscription category metadata. */
5
6
  export type CategoryData = {
6
7
  name?: string | null;
7
8
  title?: string | null;
@@ -9,6 +10,7 @@ export type CategoryData = {
9
10
  active?: boolean | null;
10
11
  };
11
12
 
13
+ /** UTM parameters for event attribution. */
12
14
  export type UTM = {
13
15
  campaign?: string | null;
14
16
  content?: string | null;
@@ -18,42 +20,95 @@ export type UTM = {
18
20
  temp?: string | null;
19
21
  };
20
22
 
21
- export type SubscriptionData = {
22
- subscriptionId?: string | null;
23
- hashId?: string | null;
24
- provider?: string | null;
23
+ // ---------------- Subscription Types ----------------
24
+
25
+ /** Email subscription data. */
26
+ export type EmailSubscription = {
27
+ type: 'email';
28
+ resource_id: number;
29
+ email: string;
25
30
  status?: string | null;
31
+ priority?: number | null;
32
+ custom_fields?: { [key: string]: string } | null;
33
+ cats?: string[] | null;
34
+ };
26
35
 
27
- fields?: { [key: string]: string } | null;
36
+ /** SMS subscription data. */
37
+ export type SmsSubscription = {
38
+ type: 'sms';
39
+ resource_id: number;
40
+ phone: string;
41
+ status?: string | null;
42
+ priority?: number | null;
43
+ custom_fields?: { [key: string]: string } | null;
44
+ cats?: string[] | null;
45
+ };
28
46
 
29
- cats?: CategoryData[] | null;
47
+ /** Push subscription data. */
48
+ export type PushSubscription = {
49
+ type: 'push';
50
+ resource_id: number;
51
+ provider: string;
52
+ subscription_id: string;
53
+ status?: string | null;
54
+ priority?: number | null;
55
+ custom_fields?: { [key: string]: string } | null;
56
+ cats?: string[] | null;
30
57
  };
31
58
 
32
- export interface ProfileData {
59
+ /** CC-data subscription (Telegram, WhatsApp, Viber, Notify). */
60
+ export type CcDataSubscription = {
61
+ type: 'cc_data';
62
+ resource_id: number;
63
+ channel: string; // 'telegram_bot', 'whatsapp', 'viber', 'notify'
64
+ cc_data: { [key: string]: string };
65
+ status?: string | null;
66
+ priority?: number | null;
67
+ custom_fields?: { [key: string]: string } | null;
68
+ cats?: string[] | null;
69
+ };
70
+
71
+ /** Union of all supported subscription types. */
72
+ export type Subscription =
73
+ | EmailSubscription
74
+ | SmsSubscription
75
+ | PushSubscription
76
+ | CcDataSubscription;
77
+
78
+ // ---------------- API models ----------------
79
+
80
+ /** Profile data returned from the API. */
81
+ export type ProfileData = {
33
82
  id?: string | null;
34
83
  status?: string | null;
35
84
  isTest?: boolean | null;
36
- subscription?: SubscriptionData | null;
37
- }
85
+ subscription?: Subscription | null;
86
+ };
38
87
 
39
- export interface Response {
88
+ /** API response payload. */
89
+ export type Response = {
40
90
  error?: number | null;
41
91
  errorText?: string | null;
42
92
  profile?: ProfileData | null;
43
- }
93
+ };
44
94
 
45
- export interface ResponseWithHttpCode {
95
+ /** API response with HTTP status code. */
96
+ export type ResponseWithHttpCode = {
46
97
  httpCode: number | null;
47
98
  response: Response | null;
48
- }
99
+ };
49
100
 
50
- export interface AppInfo {
101
+ // ---------------- Config / SDK models ----------------
102
+
103
+ /** Application metadata. */
104
+ export type AppInfo = {
51
105
  appID: string;
52
106
  appIID: string;
53
107
  appVer: string;
54
- }
108
+ };
55
109
 
56
- export interface AltcraftConfig {
110
+ /** SDK configuration object. */
111
+ export type AltcraftConfig = {
57
112
  apiUrl: string;
58
113
  rToken?: string | null;
59
114
  appInfo?: AppInfo | null;
@@ -67,34 +122,34 @@ export interface AltcraftConfig {
67
122
  android_pushReceiverModules?: string[] | null;
68
123
  android_pushChannelName?: string | null;
69
124
  android_pushChannelDescription?: string | null;
70
- }
125
+ };
71
126
 
72
- export interface TokenData {
127
+ /** Push token data. */
128
+ export type TokenData = {
73
129
  provider: string | null;
74
130
  token: string | null;
75
- }
131
+ };
132
+
133
+ /** SDK event emitted from native via NativeEventEmitter ("AltcraftSdkEvent"). */
134
+ export type SdkEvent = {
135
+ function: string;
136
+ code: number | null;
137
+ message: string;
138
+ type: 'event' | 'error' | 'retryError';
139
+ value?: Record<string, unknown> | null;
140
+ };
76
141
 
77
142
  // ---------------- TurboModule Spec ----------------
78
143
 
79
144
  export interface Spec extends TurboModule {
80
145
  // init
81
- initialize(config: AltcraftConfig): Promise<void>;
82
-
83
- // auth
84
- setJwt(token: string | null): void;
85
-
86
- // platform-specific token setters
87
- setAndroidFcmToken(token: string | null): void;
88
- setAndroidHmsToken(token: string | null): void;
89
-
90
- setIosFcmToken(token: string | null): void;
91
- setIosHmsToken(token: string | null): void;
92
146
 
93
- // other tokens
94
- setApnsToken(token: string | null): void; // iOS-only (no-op on Android)
95
- setRustoreToken(token: string | null): void; // Android-only (no-op on iOS)
147
+ /** Initializes SDK with provided configuration. */
148
+ initialize(config: AltcraftConfig): Promise<void>;
96
149
 
97
150
  // subscription
151
+
152
+ /** Creates or updates push subscription. */
98
153
  pushSubscribe(
99
154
  sync: boolean | null,
100
155
  profileFields: { [key: string]: string } | null,
@@ -104,6 +159,7 @@ export interface Spec extends TurboModule {
104
159
  skipTriggers: boolean | null
105
160
  ): void;
106
161
 
162
+ /** Suspends active push subscription. */
107
163
  pushSuspend(
108
164
  sync: boolean | null,
109
165
  profileFields: { [key: string]: string } | null,
@@ -113,6 +169,7 @@ export interface Spec extends TurboModule {
113
169
  skipTriggers: boolean | null
114
170
  ): void;
115
171
 
172
+ /** Unsubscribes from push notifications. */
116
173
  pushUnSubscribe(
117
174
  sync: boolean | null,
118
175
  profileFields: { [key: string]: string } | null,
@@ -122,37 +179,63 @@ export interface Spec extends TurboModule {
122
179
  skipTriggers: boolean | null
123
180
  ): void;
124
181
 
182
+ /** Unsuspends current push subscription. */
125
183
  unSuspendPushSubscription(): Promise<ResponseWithHttpCode | null>;
184
+
185
+ /** Returns status of the latest subscription. */
126
186
  getStatusOfLatestSubscription(): Promise<ResponseWithHttpCode | null>;
187
+
188
+ /** Returns status of current subscription. */
189
+ getStatusForCurrentSubscription(): Promise<ResponseWithHttpCode | null>;
190
+
191
+ /** Returns subscription status for specific provider. */
127
192
  getStatusOfLatestSubscriptionForProvider(
128
193
  provider: string | null
129
194
  ): Promise<ResponseWithHttpCode | null>;
130
- getStatusForCurrentSubscription(): Promise<ResponseWithHttpCode | null>;
131
195
 
132
196
  // push payload (string-only)
197
+
198
+ /** Handles incoming push payload (string-only). */
133
199
  takePush(message: { [key: string]: string } | null): void;
134
200
 
135
201
  // events
202
+
203
+ /** Subscribes to SDK native events. */
136
204
  subscribeToEvents(): void;
205
+
206
+ /** Unsubscribes from SDK native events. */
137
207
  unsubscribeFromEvent(): void;
138
208
 
139
209
  // required for NativeEventEmitter
210
+
211
+ /** Required by NativeEventEmitter. */
140
212
  addListener(eventName: string): void;
213
+
214
+ /** Required by NativeEventEmitter. */
141
215
  removeListeners(count: number): void;
142
216
 
143
217
  // token API
218
+
219
+ /** Returns current push token. */
144
220
  getPushToken(): Promise<TokenData | null>;
145
- deleteDeviceToken(provider: string | null): Promise<void>;
146
- forcedTokenUpdate(): Promise<void>;
147
- changePushProviderPriorityList(priorityList: string[] | null): Promise<void>;
221
+
222
+ /** Sets push token for provider. */
148
223
  setPushToken(provider: string, token: string | null): Promise<void>;
149
224
 
150
225
  // sdk
226
+
227
+ /** Clears all SDK state and stored data. */
151
228
  clear(): Promise<void>;
152
- unlockInitOperationsInThisSession(): void;
229
+
230
+ /** Requests notification permission (iOS/Android). */
153
231
  requestNotificationPermission(): void;
154
232
 
155
233
  // mobile events
234
+
235
+ /**
236
+ * Sends a mobile event to the server.
237
+ *
238
+ */
156
239
  mobileEvent(
157
240
  sid: string,
158
241
  eventName: string,
@@ -161,30 +244,38 @@ export interface Spec extends TurboModule {
161
244
  matching: { [key: string]: string } | null,
162
245
  matchingType: string | null,
163
246
  profileFields: { [key: string]: string } | null,
247
+ subscription: { [key: string]: string } | null,
164
248
  utm: UTM | null
165
249
  ): void;
166
250
 
167
251
  // push event delivery
252
+
253
+ /** Sends push delivery event. */
168
254
  deliveryEvent(
169
255
  message: { [key: string]: string } | null,
170
256
  messageUID: string | null
171
257
  ): void;
172
258
 
173
259
  // push event open
260
+
261
+ /** Sends push open event. */
174
262
  openEvent(
175
263
  message: { [key: string]: string } | null,
176
264
  messageUID: string | null
177
265
  ): void;
178
266
 
179
- // set UserDefault
180
- setUserDefaultsValue(
181
- suiteName: string | null,
182
- key: string,
183
- value: string | null
184
- ): void;
267
+ // set UserDefault
268
+
269
+ /** Sets value in UserDefaults / SharedPreferences. */
270
+ setUserDefaultsValue(
271
+ suiteName: string | null,
272
+ key: string,
273
+ value: string | null
274
+ ): void;
185
275
  }
186
276
 
187
277
  // ---------------- Export ----------------
188
278
 
279
+ /** Native TurboModule instance. */
189
280
  const NativeSDK = TurboModuleRegistry.getEnforcing<Spec>('Sdk');
190
281
  export default NativeSDK;
@@ -0,0 +1,116 @@
1
+ import type { Subscription } from './NativeSdk';
2
+
3
+ export class Utilities {
4
+
5
+ /** Converts a JS object to { [key: string]: string }
6
+ * for the RN bridge; drops null/undefined. */
7
+ static toNativeStringMap(
8
+ input: Record<string, unknown> | null
9
+ ): Record<string, string> | null {
10
+ if (input == null) return null;
11
+
12
+ const out: Record<string, string> = {};
13
+
14
+ for (const [key, value] of Object.entries(input)) {
15
+ if (value == null) continue;
16
+
17
+ switch (typeof value) {
18
+ case 'string':
19
+ out[key] = value;
20
+ break;
21
+
22
+ case 'number':
23
+ case 'boolean':
24
+ case 'bigint':
25
+ out[key] = String(value);
26
+ break;
27
+
28
+ default:
29
+ try {
30
+ out[key] = JSON.stringify(value);
31
+ } catch {
32
+ out[key] = String(value);
33
+ }
34
+ break;
35
+ }
36
+ }
37
+
38
+ return Object.keys(out).length > 0 ? out : null;
39
+ }
40
+
41
+ /**
42
+ * New Arch safe:
43
+ * TurboModule codegen does NOT support `any`.
44
+ * Therefore, we serialize any value for UserDefaults into `string`.
45
+ */
46
+ static toUserDefaultsString(value: unknown): string | null {
47
+ if (value == null) return null;
48
+
49
+ const t = typeof value;
50
+
51
+ if (t === 'string') return String(value);
52
+ if (t === 'number' || t === 'boolean') return String(value);
53
+
54
+ try {
55
+ return JSON.stringify(value);
56
+ } catch {
57
+ return String(value);
58
+ }
59
+ }
60
+
61
+ /** Converts Subscription object to native string map format. */
62
+ static subscriptionToNativeMap(
63
+ subscription: Subscription | null
64
+ ): Record<string, string> | null {
65
+ if (subscription == null) return null;
66
+
67
+ const result: Record<string, string> = {
68
+ type: subscription.type,
69
+ resource_id: String(subscription.resource_id),
70
+ };
71
+
72
+ if (subscription.status != null) result.status = subscription.status;
73
+ if (subscription.priority != null) result.priority = String(subscription.priority);
74
+
75
+ if (subscription.custom_fields != null) {
76
+ try {
77
+ result.custom_fields = JSON.stringify(subscription.custom_fields);
78
+ } catch {
79
+ result.custom_fields = '{}';
80
+ }
81
+ }
82
+
83
+ if (subscription.cats != null) {
84
+ try {
85
+ result.cats = JSON.stringify(subscription.cats);
86
+ } catch {
87
+ result.cats = '[]';
88
+ }
89
+ }
90
+
91
+ switch (subscription.type) {
92
+ case 'email':
93
+ result.email = subscription.email;
94
+ break;
95
+
96
+ case 'sms':
97
+ result.phone = subscription.phone;
98
+ break;
99
+
100
+ case 'push':
101
+ result.provider = subscription.provider;
102
+ result.subscription_id = subscription.subscription_id;
103
+ break;
104
+
105
+ case 'cc_data':
106
+ try {
107
+ result.cc_data = JSON.stringify(subscription.cc_data);
108
+ } catch {
109
+ result.cc_data = '{}';
110
+ }
111
+ break;
112
+ }
113
+
114
+ return result;
115
+ }
116
+ }