react-native-asleep 1.2.0 → 2.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.
@@ -5,6 +5,7 @@ import type {
5
5
  AsleepAnalysisResult,
6
6
  AsleepAverageReport,
7
7
  AsleepConfig,
8
+ AsleepCredentials,
8
9
  AsleepErrorCategory,
9
10
  AsleepEventType,
10
11
  AsleepReport,
@@ -21,18 +22,22 @@ import { createStore } from "./store/createStore";
21
22
  const emitter = new EventEmitter(AsleepModule);
22
23
 
23
24
  // Native sessions are already gone for these errors and no close event follows.
24
- const TERMINAL_TRACKING_ERROR_CODES = new Set<string>(["UPLOAD_TRACKING_TERMINATED", "INTERRUPTION_RECOVERY_FAILED"]);
25
+ const TERMINAL_TRACKING_ERROR_CODES = new Set<string>(["UPLOAD_TRACKING_TERMINATED"]);
25
26
 
26
- // iOS 3.2.0 stops recording but leaves the native session open.
27
- const RECORDING_DEAD_ERROR_CODES = new Set<string>(["AUDIO_INITIALIZATION_FAILED"]);
27
+ // iOS stops recording but leaves the native session open; stopTracking() closes it.
28
+ const RECORDING_DEAD_ERROR_CODES = new Set<string>(["AUDIO_INITIALIZATION_FAILED", "INTERRUPTION_RECOVERY_FAILED"]);
28
29
 
29
30
  // iOS 3.2.0 needs an explicit foreground resume after this failure.
30
31
  const RECOVERY_REQUIRED_ERROR_CODES = new Set<string>(["CANNOT_ACTIVATE_IN_BACKGROUND"]);
31
32
 
32
- // Android SDK 3.2.x tears the session down for exactly these codes. Keep this
33
- // synchronized with AsleepModule.kt's TERMINAL_TRACKING_ERROR_CODES.
33
+ // The first 13 codes are the Android SDK 3.3.x session-teardown set; keep them
34
+ // synchronized with AsleepModule.kt's TERMINAL_TRACKING_ERROR_CODES. The rest are
35
+ // start failures after which no native session exists: iOS 3.3.2 create HTTP
36
+ // 400/403/429/404 (22400, 11403, 11404) and insufficient storage (11008, both
37
+ // platforms). Android reports create failures as 22000. 11010 (alreadyTracking)
38
+ // is deliberately absent because the existing session survives it.
34
39
  const TERMINAL_TRACKING_SDK_CODES = new Set<number>([
35
- 11003, 22000, 22401, 22409, 22422, 22500, 23499, 24000, 24400, 24401, 24403, 24404, 24500,
40
+ 11003, 22000, 22401, 22409, 22422, 22500, 23499, 24000, 24400, 24401, 24403, 24404, 24500, 11008, 11403, 11404, 22400,
36
41
  ]);
37
42
 
38
43
  // The native session survives these exhausted upload attempts.
@@ -225,6 +230,35 @@ const clearErrorIfUnchanged = (errorBefore: AsleepError | null) => {
225
230
  if (current !== null && current === errorBefore) useAsleepStore.setState({ error: null });
226
231
  };
227
232
 
233
+ type NativeCredentials = { apiKey?: string; appId?: string; appSecret?: string };
234
+
235
+ // Runtime guard for JS callers and casts that bypass the AsleepCredentials union.
236
+ const resolveCredentials = (config: AsleepCredentials): NativeCredentials => {
237
+ const { apiKey, appId, appSecret } = (config ?? {}) as { apiKey?: unknown; appId?: unknown; appSecret?: unknown };
238
+ const hasApiKey = apiKey !== undefined;
239
+ const hasToken = appId !== undefined || appSecret !== undefined;
240
+ if (hasApiKey && hasToken) {
241
+ return storeAndThrow(
242
+ new AsleepError("INVALID_ARGUMENT", "Pass either apiKey or appId/appSecret credentials, not both."),
243
+ );
244
+ }
245
+ if (!hasApiKey && !hasToken) {
246
+ return storeAndThrow(
247
+ new AsleepError("INVALID_ARGUMENT", "Credentials are required: pass apiKey or appId/appSecret."),
248
+ );
249
+ }
250
+ const isNonEmpty = (value: unknown): value is string => typeof value === "string" && value.length > 0;
251
+ if (hasApiKey) {
252
+ if (!isNonEmpty(apiKey))
253
+ return storeAndThrow(new AsleepError("INVALID_ARGUMENT", "apiKey must be a non-empty string."));
254
+ return { apiKey };
255
+ }
256
+ if (!isNonEmpty(appId) || !isNonEmpty(appSecret)) {
257
+ return storeAndThrow(new AsleepError("INVALID_ARGUMENT", "appId and appSecret must both be non-empty strings."));
258
+ }
259
+ return { appId, appSecret };
260
+ };
261
+
228
262
  const normalizeReport = (report: any, sessionId: string): AsleepReport => {
229
263
  const converted = convertKeysToCamelCase(report);
230
264
  if (converted && !converted.session && converted.sessionId) {
@@ -251,6 +285,7 @@ const normalizeReport = (report: any, sessionId: string): AsleepReport => {
251
285
 
252
286
  export const asleepActions: AsleepActions = {
253
287
  setup: async (config) => {
288
+ const credentials = resolveCredentials(config);
254
289
  const state = useAsleepStore.getState();
255
290
  if (state.setupStatus === "inProgress") {
256
291
  storeAndThrow(new AsleepError("OPERATION_IN_PROGRESS", "Setup is already in progress."));
@@ -263,7 +298,15 @@ export const asleepActions: AsleepActions = {
263
298
  addLog("[setup] Start");
264
299
  useAsleepStore.setState({ setupStatus: "inProgress" });
265
300
  try {
266
- await AsleepModule.setup(config.apiKey, config.baseUrl, config.callbackUrl, config.service, config.enableODA);
301
+ await AsleepModule.setup(
302
+ credentials.apiKey,
303
+ credentials.appId,
304
+ credentials.appSecret,
305
+ config.baseUrl,
306
+ config.callbackUrl,
307
+ config.service,
308
+ config.enableODA,
309
+ );
267
310
  useAsleepStore.setState({
268
311
  setupStatus: "complete",
269
312
  isODAEnabled: config.enableODA || false,
@@ -277,6 +320,7 @@ export const asleepActions: AsleepActions = {
277
320
  },
278
321
 
279
322
  initAsleepConfig: async (config) => {
323
+ const credentials = resolveCredentials(config);
280
324
  const state = useAsleepStore.getState();
281
325
  if (state.setupStatus === "inProgress") {
282
326
  return storeAndThrow(new AsleepError("OPERATION_IN_PROGRESS", "Setup is already in progress."));
@@ -288,7 +332,14 @@ export const asleepActions: AsleepActions = {
288
332
  addLog("[initAsleepConfig] Start");
289
333
  useAsleepStore.setState({ setupStatus: "inProgress" });
290
334
  try {
291
- await AsleepModule.initAsleepConfig(config.apiKey, config.userId, config.baseUrl, config.callbackUrl);
335
+ await AsleepModule.initAsleepConfig(
336
+ credentials.apiKey,
337
+ credentials.appId,
338
+ credentials.appSecret,
339
+ config.userId,
340
+ config.baseUrl,
341
+ config.callbackUrl,
342
+ );
292
343
  useAsleepStore.setState({ setupStatus: "complete", isInitialized: true });
293
344
  clearErrorIfUnchanged(errorBefore);
294
345
  addLog("[initAsleepConfig] Success");
@@ -698,8 +749,9 @@ const transitionForEvent = (
698
749
  if (terminal) {
699
750
  return {
700
751
  partial: {
701
- // iOS 3.2.1 closeSessionSilently does not emit didClose, so this
702
- // branch must clear the projected session state itself.
752
+ // iOS closeSessionSilently (upload 4xx) does not emit didClose, and
753
+ // start failures never create a session, so this branch must clear
754
+ // the projected session state itself.
703
755
  error,
704
756
  trackingStatus: "idle",
705
757
  isAnalyzing: false,
@@ -712,8 +764,8 @@ const transitionForEvent = (
712
764
  if (recordingDead) {
713
765
  return {
714
766
  partial: {
715
- // iOS 3.2.0 audioInitializationFailed stops its recorder but
716
- // intentionally keeps the native session open (didClose=false).
767
+ // iOS audioInitializationFailed and interruptionRecoveryFailed stop
768
+ // the recorder but keep the native session open (didClose=false).
717
769
  error,
718
770
  trackingStatus: "idle",
719
771
  isAnalyzing: false,
@@ -725,8 +777,8 @@ const transitionForEvent = (
725
777
  }
726
778
  if (recoveryRequired) {
727
779
  return {
728
- // iOS 3.2.0 cannotActivateInBackground keeps the session alive and
729
- // requires an explicit foreground resume.
780
+ // iOS cannotActivateInBackground keeps the session alive. 3.3.x also
781
+ // retries on foreground; only a later upload proves recovery.
730
782
  partial: { error, trackingStatus: "recoveryRequired" },
731
783
  log: `[onTrackingError] error: ${error.message}`,
732
784
  };
@@ -739,8 +791,9 @@ const transitionForEvent = (
739
791
  return {
740
792
  partial: {
741
793
  error: null,
742
- // iOS 3.2.1 can emit didResume twice. Always clear the error, but
743
- // mutate the paused state only for the first event.
794
+ // iOS 3.2.1-3.3.0 can emit didResume twice, and recovery paths can
795
+ // resume without a prior pause. Always clear the error, but mutate
796
+ // the paused state only for the first event.
744
797
  ...(state.trackingStatus === "paused" ? { trackingStatus: "tracking" as const } : {}),
745
798
  },
746
799
  log:
package/src/index.ts CHANGED
@@ -43,6 +43,7 @@ export type {
43
43
  AsleepAnalysisResult,
44
44
  AsleepAverageReport,
45
45
  AsleepConfig,
46
+ AsleepCredentials,
46
47
  AsleepErrorCategory,
47
48
  AsleepEventType,
48
49
  AsleepNeverSleptSession,