@altcraft/react-native-sdk 0.0.1 → 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 (34) 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 +493 -896
  5. package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +326 -0
  6. package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
  7. package/ios/Converter.swift +640 -0
  8. package/ios/Sdk.h +4 -3
  9. package/ios/Sdk.mm +124 -370
  10. package/ios/SdkModule.swift +436 -653
  11. package/lib/module/NativeSdk.js +35 -0
  12. package/lib/module/NativeSdk.js.map +1 -1
  13. package/lib/module/Utilities.js +94 -0
  14. package/lib/module/Utilities.js.map +1 -0
  15. package/lib/module/index.js +180 -419
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/typescript/src/NativeSdk.d.ts +117 -32
  18. package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
  19. package/lib/typescript/src/Utilities.d.ts +15 -0
  20. package/lib/typescript/src/Utilities.d.ts.map +1 -0
  21. package/lib/typescript/src/index.d.ts +113 -308
  22. package/lib/typescript/src/index.d.ts.map +1 -1
  23. package/package.json +2 -2
  24. package/src/NativeSdk.ts +156 -46
  25. package/src/Utilities.ts +116 -0
  26. package/src/index.tsx +255 -483
  27. package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -27
  28. package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
  29. package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
  30. package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
  31. package/ios/protocols/RNAPNSProvider.swift +0 -15
  32. package/ios/protocols/RNFCMProvider.swift +0 -24
  33. package/ios/protocols/RNHMSProvider.swift +0 -24
  34. package/ios/protocols/RNJWTProvider.swift +0 -15
package/src/NativeSdk.ts CHANGED
@@ -2,49 +2,113 @@ 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;
8
9
  steady?: boolean | null;
9
10
  active?: boolean | null;
10
- }
11
-
12
- export type SubscriptionData = {
13
- subscriptionId?: string | null;
14
- hashId?: string | null;
15
- provider?: string | null;
11
+ };
12
+
13
+ /** UTM parameters for event attribution. */
14
+ export type UTM = {
15
+ campaign?: string | null;
16
+ content?: string | null;
17
+ keyword?: string | null;
18
+ medium?: string | null;
19
+ source?: string | null;
20
+ temp?: string | null;
21
+ };
22
+
23
+ // ---------------- Subscription Types ----------------
24
+
25
+ /** Email subscription data. */
26
+ export type EmailSubscription = {
27
+ type: 'email';
28
+ resource_id: number;
29
+ email: string;
16
30
  status?: string | null;
17
-
18
- fields?: { [key: string]: string } | null;
19
-
20
- cats?: CategoryData[] | null;
21
- }
22
-
23
- export interface ProfileData {
31
+ priority?: number | null;
32
+ custom_fields?: { [key: string]: string } | null;
33
+ cats?: string[] | null;
34
+ };
35
+
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
+ };
46
+
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;
57
+ };
58
+
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 = {
24
82
  id?: string | null;
25
83
  status?: string | null;
26
84
  isTest?: boolean | null;
27
- subscription?: SubscriptionData | null;
28
- }
85
+ subscription?: Subscription | null;
86
+ };
29
87
 
30
- export interface Response {
88
+ /** API response payload. */
89
+ export type Response = {
31
90
  error?: number | null;
32
91
  errorText?: string | null;
33
92
  profile?: ProfileData | null;
34
- }
93
+ };
35
94
 
36
- export interface ResponseWithHttpCode {
95
+ /** API response with HTTP status code. */
96
+ export type ResponseWithHttpCode = {
37
97
  httpCode: number | null;
38
98
  response: Response | null;
39
- }
99
+ };
100
+
101
+ // ---------------- Config / SDK models ----------------
40
102
 
41
- export interface AppInfo {
103
+ /** Application metadata. */
104
+ export type AppInfo = {
42
105
  appID: string;
43
106
  appIID: string;
44
107
  appVer: string;
45
- }
108
+ };
46
109
 
47
- export interface AltcraftConfig {
110
+ /** SDK configuration object. */
111
+ export type AltcraftConfig = {
48
112
  apiUrl: string;
49
113
  rToken?: string | null;
50
114
  appInfo?: AppInfo | null;
@@ -58,34 +122,34 @@ export interface AltcraftConfig {
58
122
  android_pushReceiverModules?: string[] | null;
59
123
  android_pushChannelName?: string | null;
60
124
  android_pushChannelDescription?: string | null;
61
- }
125
+ };
62
126
 
63
- export interface TokenData {
127
+ /** Push token data. */
128
+ export type TokenData = {
64
129
  provider: string | null;
65
130
  token: string | null;
66
- }
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
+ };
67
141
 
68
142
  // ---------------- TurboModule Spec ----------------
69
143
 
70
144
  export interface Spec extends TurboModule {
71
145
  // init
72
- initialize(config: AltcraftConfig): Promise<void>;
73
-
74
- // auth
75
- setJwt(token: string | null): void;
76
146
 
77
- // platform-specific token setters
78
- setAndroidFcmToken(token: string | null): void;
79
- setAndroidHmsToken(token: string | null): void;
80
-
81
- setIosFcmToken(token: string | null): void;
82
- setIosHmsToken(token: string | null): void;
147
+ /** Initializes SDK with provided configuration. */
148
+ initialize(config: AltcraftConfig): Promise<void>;
83
149
 
84
- // other tokens
85
- setApnsToken(token: string | null): void; // iOS-only (no-op on Android)
86
- setRustoreToken(token: string | null): void; // Android-only (no-op on iOS)
150
+ // subscription
87
151
 
88
- // subscription (✅ string maps in spec)
152
+ /** Creates or updates push subscription. */
89
153
  pushSubscribe(
90
154
  sync: boolean | null,
91
155
  profileFields: { [key: string]: string } | null,
@@ -95,6 +159,7 @@ export interface Spec extends TurboModule {
95
159
  skipTriggers: boolean | null
96
160
  ): void;
97
161
 
162
+ /** Suspends active push subscription. */
98
163
  pushSuspend(
99
164
  sync: boolean | null,
100
165
  profileFields: { [key: string]: string } | null,
@@ -104,6 +169,7 @@ export interface Spec extends TurboModule {
104
169
  skipTriggers: boolean | null
105
170
  ): void;
106
171
 
172
+ /** Unsubscribes from push notifications. */
107
173
  pushUnSubscribe(
108
174
  sync: boolean | null,
109
175
  profileFields: { [key: string]: string } | null,
@@ -113,37 +179,63 @@ export interface Spec extends TurboModule {
113
179
  skipTriggers: boolean | null
114
180
  ): void;
115
181
 
182
+ /** Unsuspends current push subscription. */
116
183
  unSuspendPushSubscription(): Promise<ResponseWithHttpCode | null>;
184
+
185
+ /** Returns status of the latest subscription. */
117
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. */
118
192
  getStatusOfLatestSubscriptionForProvider(
119
193
  provider: string | null
120
194
  ): Promise<ResponseWithHttpCode | null>;
121
- getStatusForCurrentSubscription(): Promise<ResponseWithHttpCode | null>;
122
195
 
123
196
  // push payload (string-only)
197
+
198
+ /** Handles incoming push payload (string-only). */
124
199
  takePush(message: { [key: string]: string } | null): void;
125
200
 
126
201
  // events
202
+
203
+ /** Subscribes to SDK native events. */
127
204
  subscribeToEvents(): void;
205
+
206
+ /** Unsubscribes from SDK native events. */
128
207
  unsubscribeFromEvent(): void;
129
208
 
130
209
  // required for NativeEventEmitter
210
+
211
+ /** Required by NativeEventEmitter. */
131
212
  addListener(eventName: string): void;
213
+
214
+ /** Required by NativeEventEmitter. */
132
215
  removeListeners(count: number): void;
133
216
 
134
217
  // token API
218
+
219
+ /** Returns current push token. */
135
220
  getPushToken(): Promise<TokenData | null>;
136
- deleteDeviceToken(provider: string | null): Promise<void>;
137
- forcedTokenUpdate(): Promise<void>;
138
- changePushProviderPriorityList(priorityList: string[] | null): Promise<void>;
221
+
222
+ /** Sets push token for provider. */
139
223
  setPushToken(provider: string, token: string | null): Promise<void>;
140
224
 
141
225
  // sdk
226
+
227
+ /** Clears all SDK state and stored data. */
142
228
  clear(): Promise<void>;
143
- reinitializeRetryControlInThisSession(): void;
229
+
230
+ /** Requests notification permission (iOS/Android). */
144
231
  requestNotificationPermission(): void;
145
232
 
146
- // mobile events (✅ string maps in spec)
233
+ // mobile events
234
+
235
+ /**
236
+ * Sends a mobile event to the server.
237
+ *
238
+ */
147
239
  mobileEvent(
148
240
  sid: string,
149
241
  eventName: string,
@@ -151,21 +243,39 @@ export interface Spec extends TurboModule {
151
243
  payload: { [key: string]: string } | null,
152
244
  matching: { [key: string]: string } | null,
153
245
  matchingType: string | null,
154
- profileFields: { [key: string]: string } | null
246
+ profileFields: { [key: string]: string } | null,
247
+ subscription: { [key: string]: string } | null,
248
+ utm: UTM | null
155
249
  ): void;
156
250
 
251
+ // push event delivery
252
+
253
+ /** Sends push delivery event. */
157
254
  deliveryEvent(
158
255
  message: { [key: string]: string } | null,
159
256
  messageUID: string | null
160
257
  ): void;
161
258
 
259
+ // push event open
260
+
261
+ /** Sends push open event. */
162
262
  openEvent(
163
263
  message: { [key: string]: string } | null,
164
264
  messageUID: string | null
165
265
  ): void;
266
+
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;
166
275
  }
167
276
 
168
277
  // ---------------- Export ----------------
169
278
 
279
+ /** Native TurboModule instance. */
170
280
  const NativeSDK = TurboModuleRegistry.getEnforcing<Spec>('Sdk');
171
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
+ }