@altcraft/react-native-sdk 0.0.2 → 0.0.4

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 (36) hide show
  1. package/LICENSE +20 -20
  2. package/README.md +119 -27
  3. package/Sdk.podspec +2 -2
  4. package/android/build.gradle +3 -4
  5. package/android/src/main/java/com/altcraft/sdk/SdkEventEmitter.kt +50 -0
  6. package/android/src/main/java/com/altcraft/sdk/SdkModule.kt +663 -480
  7. package/android/src/main/java/com/altcraft/sdk/SdkPackage.kt +16 -8
  8. package/android/src/main/java/com/altcraft/sdk/utilities/Converter.kt +164 -0
  9. package/android/src/main/java/com/altcraft/sdk/utilities/Extension.kt +72 -0
  10. package/ios/Converter.swift +485 -48
  11. package/ios/Sdk.mm +195 -270
  12. package/ios/SdkModule.swift +456 -507
  13. package/lib/module/NativeSdk.js +35 -0
  14. package/lib/module/NativeSdk.js.map +1 -1
  15. package/lib/module/Utilities.js +178 -0
  16. package/lib/module/Utilities.js.map +1 -0
  17. package/lib/module/index.js +335 -245
  18. package/lib/module/index.js.map +1 -1
  19. package/lib/typescript/src/NativeSdk.d.ts +125 -33
  20. package/lib/typescript/src/NativeSdk.d.ts.map +1 -1
  21. package/lib/typescript/src/Utilities.d.ts +47 -0
  22. package/lib/typescript/src/Utilities.d.ts.map +1 -0
  23. package/lib/typescript/src/index.d.ts +218 -94
  24. package/lib/typescript/src/index.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/src/NativeSdk.ts +169 -46
  27. package/src/Utilities.ts +180 -0
  28. package/src/index.tsx +410 -312
  29. package/android/src/main/java/com/altcraft/sdk/interfaces/RnFCMProvider.kt +0 -26
  30. package/android/src/main/java/com/altcraft/sdk/interfaces/RnHMSProvider.kt +0 -32
  31. package/android/src/main/java/com/altcraft/sdk/interfaces/RnJWTProvider.kt +0 -21
  32. package/android/src/main/java/com/altcraft/sdk/interfaces/RnRustoreProvider.kt +0 -53
  33. package/ios/protocols/RNAPNSProvider.swift +0 -15
  34. package/ios/protocols/RNFCMProvider.swift +0 -24
  35. package/ios/protocols/RNHMSProvider.swift +0 -24
  36. 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
+ };
100
+
101
+ // ---------------- Config / SDK models ----------------
49
102
 
50
- export interface AppInfo {
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;
@@ -62,39 +117,37 @@ export interface AltcraftConfig {
62
117
 
63
118
  // -------- ANDROID-ONLY --------
64
119
  android_icon?: number | null;
65
- android_usingService?: boolean | null;
66
- android_serviceMessage?: string | null;
67
120
  android_pushReceiverModules?: string[] | null;
68
121
  android_pushChannelName?: string | null;
69
122
  android_pushChannelDescription?: string | null;
70
- }
123
+ };
71
124
 
72
- export interface TokenData {
125
+ /** Push token data. */
126
+ export type TokenData = {
73
127
  provider: string | null;
74
128
  token: string | null;
75
- }
129
+ };
130
+
131
+ /** SDK event emitted from native via NativeEventEmitter ("AltcraftSdkEvent"). */
132
+ export type SdkEvent = {
133
+ function: string;
134
+ code: number | null;
135
+ message: string;
136
+ type: 'event' | 'error' | 'retryError';
137
+ value?: Record<string, unknown> | null;
138
+ };
76
139
 
77
140
  // ---------------- TurboModule Spec ----------------
78
141
 
79
142
  export interface Spec extends TurboModule {
80
143
  // init
81
- initialize(config: AltcraftConfig): Promise<void>;
82
144
 
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
-
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)
145
+ /** Initializes SDK with provided configuration. */
146
+ initialize(config: AltcraftConfig): Promise<void>;
96
147
 
97
148
  // subscription
149
+
150
+ /** Creates or updates push subscription. */
98
151
  pushSubscribe(
99
152
  sync: boolean | null,
100
153
  profileFields: { [key: string]: string } | null,
@@ -104,6 +157,7 @@ export interface Spec extends TurboModule {
104
157
  skipTriggers: boolean | null
105
158
  ): void;
106
159
 
160
+ /** Suspends active push subscription. */
107
161
  pushSuspend(
108
162
  sync: boolean | null,
109
163
  profileFields: { [key: string]: string } | null,
@@ -113,6 +167,7 @@ export interface Spec extends TurboModule {
113
167
  skipTriggers: boolean | null
114
168
  ): void;
115
169
 
170
+ /** Unsubscribes from push notifications. */
116
171
  pushUnSubscribe(
117
172
  sync: boolean | null,
118
173
  profileFields: { [key: string]: string } | null,
@@ -122,37 +177,63 @@ export interface Spec extends TurboModule {
122
177
  skipTriggers: boolean | null
123
178
  ): void;
124
179
 
180
+ /** Unsuspends current push subscription. */
125
181
  unSuspendPushSubscription(): Promise<ResponseWithHttpCode | null>;
182
+
183
+ /** Returns status of the latest subscription. */
126
184
  getStatusOfLatestSubscription(): Promise<ResponseWithHttpCode | null>;
185
+
186
+ /** Returns status of current subscription. */
187
+ getStatusForCurrentSubscription(): Promise<ResponseWithHttpCode | null>;
188
+
189
+ /** Returns subscription status for specific provider. */
127
190
  getStatusOfLatestSubscriptionForProvider(
128
191
  provider: string | null
129
192
  ): Promise<ResponseWithHttpCode | null>;
130
- getStatusForCurrentSubscription(): Promise<ResponseWithHttpCode | null>;
131
193
 
132
194
  // push payload (string-only)
195
+
196
+ /** Handles incoming push payload (string-only). */
133
197
  takePush(message: { [key: string]: string } | null): void;
134
198
 
135
199
  // events
200
+
201
+ /** Subscribes to SDK native events. */
136
202
  subscribeToEvents(): void;
203
+
204
+ /** Unsubscribes from SDK native events. */
137
205
  unsubscribeFromEvent(): void;
138
206
 
139
207
  // required for NativeEventEmitter
208
+
209
+ /** Required by NativeEventEmitter. */
140
210
  addListener(eventName: string): void;
211
+
212
+ /** Required by NativeEventEmitter. */
141
213
  removeListeners(count: number): void;
142
214
 
143
215
  // token API
216
+
217
+ /** Returns current push token. */
144
218
  getPushToken(): Promise<TokenData | null>;
145
- deleteDeviceToken(provider: string | null): Promise<void>;
146
- forcedTokenUpdate(): Promise<void>;
147
- changePushProviderPriorityList(priorityList: string[] | null): Promise<void>;
219
+
220
+ /** Sets push token for provider. */
148
221
  setPushToken(provider: string, token: string | null): Promise<void>;
149
222
 
150
223
  // sdk
224
+
225
+ /** Clears all SDK state and stored data. */
151
226
  clear(): Promise<void>;
152
- unlockInitOperationsInThisSession(): void;
227
+
228
+ /** Requests notification permission (iOS/Android). */
153
229
  requestNotificationPermission(): void;
154
230
 
155
231
  // mobile events
232
+
233
+ /**
234
+ * Sends a mobile event to the server.
235
+ *
236
+ */
156
237
  mobileEvent(
157
238
  sid: string,
158
239
  eventName: string,
@@ -161,30 +242,72 @@ export interface Spec extends TurboModule {
161
242
  matching: { [key: string]: string } | null,
162
243
  matchingType: string | null,
163
244
  profileFields: { [key: string]: string } | null,
245
+ subscription: { [key: string]: string } | null,
164
246
  utm: UTM | null
165
247
  ): void;
166
248
 
167
249
  // push event delivery
250
+
251
+ /** Sends push delivery event. */
168
252
  deliveryEvent(
169
253
  message: { [key: string]: string } | null,
170
254
  messageUID: string | null
171
255
  ): void;
172
256
 
173
257
  // push event open
258
+
259
+ /** Sends push open event. */
174
260
  openEvent(
175
261
  message: { [key: string]: string } | null,
176
262
  messageUID: string | null
177
263
  ): void;
178
264
 
179
- // set UserDefault
180
- setUserDefaultsValue(
181
- suiteName: string | null,
182
- key: string,
183
- value: string | null
184
- ): void;
265
+ // set UserDefault
266
+
267
+ /** Sets value in UserDefaults / SharedPreferences. */
268
+ setUserDefaultsValue(
269
+ suiteName: string | null,
270
+ key: string,
271
+ value: string | null
272
+ ): void;
273
+
274
+ // profile API
275
+
276
+ /** Updates profile fields on the server. */
277
+ updateProfileFields(
278
+ profileFields: { [key: string]: string } | null,
279
+ skipTriggers: boolean | null
280
+ ): void;
281
+
282
+ // token management
283
+
284
+ /** Forces a push token refresh (delete + re-register). */
285
+ forcedTokenUpdate(): Promise<void>;
286
+
287
+ /** Deletes the device token for a given provider. */
288
+ deleteDeviceToken(provider: string): Promise<void>;
289
+
290
+ /** Changes the push provider priority list at runtime. */
291
+ changePushProviderPriorityList(priorityList: string[]): Promise<void>;
292
+
293
+ // push receiver helpers
294
+
295
+ /** Checks whether a push payload belongs to Altcraft. */
296
+ isAltcraftPush(message: { [key: string]: string } | null): boolean;
297
+
298
+ // SDK lifecycle
299
+
300
+ /** Allows re-running initial operations in this session (Android only). */
301
+ unlockInitialOperationsInThisSession(): void;
302
+
303
+ // iOS-only
304
+
305
+ /** Sets the App Group name for shared storage (iOS only). */
306
+ setAppGroup(groupName: string | null): void;
185
307
  }
186
308
 
187
309
  // ---------------- Export ----------------
188
310
 
311
+ /** Native TurboModule instance. */
189
312
  const NativeSDK = TurboModuleRegistry.getEnforcing<Spec>('Sdk');
190
313
  export default NativeSDK;
@@ -0,0 +1,180 @@
1
+ import type { Subscription } from './NativeSdk';
2
+
3
+ /** Supported action types for profile field manipulation. */
4
+ export type ActionFieldType =
5
+ | 'set'
6
+ | 'unset'
7
+ | 'incr'
8
+ | 'add'
9
+ | 'delete'
10
+ | 'upsert';
11
+
12
+ /**
13
+ * Builder for structured profile field actions.
14
+ *
15
+ * Produces a map entry compatible with the native SDK's ActionFieldBuilder.
16
+ * The resulting map is intended to be merged into `profileFields` before
17
+ * passing to `pushSubscribe`, `updateProfileFields`, or similar APIs.
18
+ */
19
+ export class ActionFieldBuilder {
20
+ private key: string;
21
+
22
+ constructor(key: string) {
23
+ this.key = key;
24
+ }
25
+
26
+ /** Set field to a specific value. */
27
+ set(value: unknown): Record<string, unknown> {
28
+ return { [this.key]: { action: 'set', value } };
29
+ }
30
+
31
+ /** Unset (remove) a field value. */
32
+ unset(value: unknown): Record<string, unknown> {
33
+ return { [this.key]: { action: 'unset', value } };
34
+ }
35
+
36
+ /** Increment a numeric field by the given value. */
37
+ incr(value: unknown): Record<string, unknown> {
38
+ return { [this.key]: { action: 'incr', value } };
39
+ }
40
+
41
+ /** Add a value to a set/array field. */
42
+ add(value: unknown): Record<string, unknown> {
43
+ return { [this.key]: { action: 'add', value } };
44
+ }
45
+
46
+ /** Delete a value from a set/array field. */
47
+ delete(value: unknown): Record<string, unknown> {
48
+ return { [this.key]: { action: 'delete', value } };
49
+ }
50
+
51
+ /** Upsert: set if missing, update if exists. */
52
+ upsert(value: unknown): Record<string, unknown> {
53
+ return { [this.key]: { action: 'upsert', value } };
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Creates an ActionFieldBuilder for the given field key.
59
+ *
60
+ * @param key Profile field key to operate on.
61
+ * @returns Builder instance with action methods.
62
+ */
63
+ export function actionField(key: string): ActionFieldBuilder {
64
+ return new ActionFieldBuilder(key);
65
+ }
66
+
67
+ export class Utilities {
68
+ /** Converts a JS object to { [key: string]: string }
69
+ * for the RN bridge; drops null/undefined. */
70
+ static toNativeStringMap(
71
+ input: Record<string, unknown> | null
72
+ ): Record<string, string> | null {
73
+ if (input == null) return null;
74
+
75
+ const out: Record<string, string> = {};
76
+
77
+ for (const [key, value] of Object.entries(input)) {
78
+ if (value == null) continue;
79
+
80
+ switch (typeof value) {
81
+ case 'string':
82
+ out[key] = value;
83
+ break;
84
+
85
+ case 'number':
86
+ case 'boolean':
87
+ case 'bigint':
88
+ out[key] = String(value);
89
+ break;
90
+
91
+ default:
92
+ try {
93
+ out[key] = JSON.stringify(value);
94
+ } catch {
95
+ out[key] = String(value);
96
+ }
97
+ break;
98
+ }
99
+ }
100
+
101
+ return Object.keys(out).length > 0 ? out : null;
102
+ }
103
+
104
+ /**
105
+ * New Arch safe:
106
+ * TurboModule codegen does NOT support `any`.
107
+ * Therefore, we serialize any value for UserDefaults into `string`.
108
+ */
109
+ static toUserDefaultsString(value: unknown): string | null {
110
+ if (value == null) return null;
111
+
112
+ const t = typeof value;
113
+
114
+ if (t === 'string') return String(value);
115
+ if (t === 'number' || t === 'boolean') return String(value);
116
+
117
+ try {
118
+ return JSON.stringify(value);
119
+ } catch {
120
+ return String(value);
121
+ }
122
+ }
123
+
124
+ /** Converts Subscription object to native string map format. */
125
+ static subscriptionToNativeMap(
126
+ subscription: Subscription | null
127
+ ): Record<string, string> | null {
128
+ if (subscription == null) return null;
129
+
130
+ const result: Record<string, string> = {
131
+ type: subscription.type,
132
+ resource_id: String(subscription.resource_id),
133
+ };
134
+
135
+ if (subscription.status != null) result.status = subscription.status;
136
+ if (subscription.priority != null)
137
+ result.priority = String(subscription.priority);
138
+
139
+ if (subscription.custom_fields != null) {
140
+ try {
141
+ result.custom_fields = JSON.stringify(subscription.custom_fields);
142
+ } catch {
143
+ result.custom_fields = '{}';
144
+ }
145
+ }
146
+
147
+ if (subscription.cats != null) {
148
+ try {
149
+ result.cats = JSON.stringify(subscription.cats);
150
+ } catch {
151
+ result.cats = '[]';
152
+ }
153
+ }
154
+
155
+ switch (subscription.type) {
156
+ case 'email':
157
+ result.email = subscription.email;
158
+ break;
159
+
160
+ case 'sms':
161
+ result.phone = subscription.phone;
162
+ break;
163
+
164
+ case 'push':
165
+ result.provider = subscription.provider;
166
+ result.subscription_id = subscription.subscription_id;
167
+ break;
168
+
169
+ case 'cc_data':
170
+ try {
171
+ result.cc_data = JSON.stringify(subscription.cc_data);
172
+ } catch {
173
+ result.cc_data = '{}';
174
+ }
175
+ break;
176
+ }
177
+
178
+ return result;
179
+ }
180
+ }