@rocapine/react-native-onboarding 1.44.2 → 1.44.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.
@@ -1 +1 @@
1
- {"version":3,"file":"getOnboarding.query.d.ts","sourceRoot":"","sources":["../../../src/infra/queries/getOnboarding.query.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAsB,MAAM,aAAa,CAAC;AAK3E,eAAO,MAAM,kBAAkB,GAAI,QAAQ,SAAS,YAAY,EAC9D,QAAQ,sBAAsB,EAC9B,QAAQ,MAAM,EACd,sBAAsB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzC,gBAAgB,CAAC,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,KAAK,IAAI;;mBAWrC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;;CAmCnD,CAAC"}
1
+ {"version":3,"file":"getOnboarding.query.d.ts","sourceRoot":"","sources":["../../../src/infra/queries/getOnboarding.query.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,8BAA8B,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,UAAU,EAAsB,MAAM,aAAa,CAAC;AAK3E,eAAO,MAAM,kBAAkB,GAAI,QAAQ,SAAS,YAAY,EAC9D,QAAQ,sBAAsB,EAC9B,QAAQ,MAAM,EACd,sBAAsB,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACzC,gBAAgB,CAAC,UAAU,EAAE,UAAU,CAAC,QAAQ,CAAC,KAAK,IAAI;;mBAWrC,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;;CA+DnD,CAAC"}
@@ -18,32 +18,57 @@ const getOnboardingQuery = (client, locale, customAudienceParams, setOnboarding)
18
18
  ],
19
19
  queryFn: async () => {
20
20
  var _a;
21
- // Try to get data from AsyncStorage first for production
22
- if (!(((_a = client === null || client === void 0 ? void 0 : client.options) === null || _a === void 0 ? void 0 : _a.isSandbox) || false)) {
23
- try {
24
- const cachedData = await async_storage_1.default.getItem(cacheKey);
25
- if (cachedData) {
26
- setOnboarding && setOnboarding(JSON.parse(cachedData));
27
- return JSON.parse(cachedData);
21
+ const isProduction = !(((_a = client === null || client === void 0 ? void 0 : client.options) === null || _a === void 0 ? void 0 : _a.isSandbox) || false);
22
+ // Fetches the live payload, pushes it to the provider, and caches it —
23
+ // but NEVER caches the offline fallback. Caching the fallback (with
24
+ // `staleTime: Infinity` + cache-first below) would pin every future
25
+ // launch to the fallback even after the network recovers, and the device
26
+ // would stop calling the edge function entirely. Returns the payload.
27
+ const fetchAndCache = async () => {
28
+ const { data, headers } = await client.getSteps({ locale }, customAudienceParams);
29
+ const isFallback = headers["ONBS-Onboarding-Id"] === "fallback";
30
+ console.info("onbs-onboarding-name", headers["ONBS-Onboarding-Name"]);
31
+ console.info("onbs-onboarding-id", headers["ONBS-Onboarding-Id"]);
32
+ setOnboarding && setOnboarding(data);
33
+ if (!isFallback) {
34
+ try {
35
+ await async_storage_1.default.setItem(cacheKey, JSON.stringify(data));
36
+ }
37
+ catch (error) {
38
+ console.warn("Failed to cache onboarding questions:", error);
28
39
  }
29
40
  }
30
- catch (error) {
31
- console.warn("Failed to load cached onboarding questions:", error);
32
- }
41
+ return data;
42
+ };
43
+ // Sandbox / draft: always fetch fresh, no caching.
44
+ if (!isProduction) {
45
+ return fetchAndCache();
33
46
  }
34
- // Fetch from API
35
- const { data, headers } = await client.getSteps({ locale }, customAudienceParams);
36
- console.info("onbs-onboarding-name", headers["ONBS-Onboarding-Name"]);
37
- console.info("onbs-onboarding-id", headers["ONBS-Onboarding-Id"]);
38
- setOnboarding && setOnboarding(data);
39
- // Cache the steps
47
+ // Production: stale-while-revalidate. Serve the cached payload instantly
48
+ // (fast first paint, offline-resilient) AND refresh from the network in
49
+ // the background, so studio re-deploys propagate and a stale cache heals.
50
+ let cached = null;
40
51
  try {
41
- await async_storage_1.default.setItem(cacheKey, JSON.stringify(data));
52
+ const cachedData = await async_storage_1.default.getItem(cacheKey);
53
+ if (cachedData)
54
+ cached = JSON.parse(cachedData);
42
55
  }
43
56
  catch (error) {
44
- console.warn("Failed to cache onboarding questions:", error);
57
+ console.warn("Failed to load cached onboarding questions:", error);
58
+ }
59
+ if (cached) {
60
+ setOnboarding && setOnboarding(cached);
61
+ // Background revalidation — updates the cache + provider state when a
62
+ // fresh real payload arrives. Errors are swallowed: the cache already
63
+ // painted, so an offline revalidation must not surface as a query error.
64
+ void fetchAndCache().catch((error) => {
65
+ console.warn("Background onboarding revalidation failed:", error);
66
+ });
67
+ return cached;
45
68
  }
46
- return data;
69
+ // No cache yet: await the network. A fallback result is returned but NOT
70
+ // cached, so the next launch retries instead of being pinned to it.
71
+ return fetchAndCache();
47
72
  },
48
73
  staleTime: Infinity,
49
74
  };
@@ -1 +1 @@
1
- {"version":3,"file":"getOnboarding.query.js","sourceRoot":"","sources":["../../../src/infra/queries/getOnboarding.query.ts"],"names":[],"mappings":";;;;;;AAAA,8FAAqE;AAKrE,MAAM,QAAQ,GAAG,4BAA4B,CAAC;AAEvC,MAAM,kBAAkB,GAAG,CAChC,MAA8B,EAC9B,MAAc,EACd,oBAAyC,EACzC,aAA0D,EAC1D,EAAE;IACF,OAAO;QACL,QAAQ,EAAE;YACR,qBAAqB;YACrB,MAAM,CAAC,SAAS;YAChB,MAAM,CAAC,OAAO,CAAC,SAAS;YACxB,MAAM,CAAC,OAAO,CAAC,OAAO;YACtB,MAAM;YACN,IAAI,CAAC,SAAS,CAAC,oBAAoB,CAAC;SACrC;QACD,OAAO,EAAE,KAAK,IAAmC,EAAE;;YACjD,yDAAyD;YACzD,IAAI,CAAC,CAAC,CAAA,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,OAAO,0CAAE,SAAS,KAAI,KAAK,CAAC,EAAE,CAAC;gBAC3C,IAAI,CAAC;oBACH,M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AAC,uCAAuC,EAAE,KAAK,CAAC,CAAC;oBAC/D,CAAC;gBACH,CAAC;gBAED,OAAO,IAAI,CAAC;YACd,CAAC,CAAC;YAEF,mDAAmD;YACnD,IAAI,CAAC,YAAY,EAAE,CAAC;gBAClB,OAAO,aAAa,EAAE,CAAC;YACzB,CAAC;YAED,yEAAyE;YACzE,wEAAwE;YACxE,0EAA0E;YAC1E,IAAI,MAAM,GAAgC,IAAI,CAAC;YAC/C,IAAI,CAAC;gBACH,MAAM,UAAU,GAAG,MAAM,uBAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;gBACxD,IAAI,UAAU;oBAAE,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAyB,CAAC;YAC1E,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,CAAC,IAAI,CAAC,6CAA6C,EAAE,KAAK,CAAC,CAAC;YACrE,CAAC;YAED,IAAI,MAAM,EAAE,CAAC;gBACX,aAAa,IAAI,aAAa,CAAC,MAAM,CAAC,CAAC;gBACvC,sEAAsE;gBACtE,sEAAsE;gBACtE,yEAAyE;gBACzE,KAAK,aAAa,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;oBACnC,OAAO,CAAC,IAAI,CAAC,4CAA4C,EAAE,KAAK,CAAC,CAAC;gBACpE,CAAC,CAAC,CAAC;gBACH,OAAO,MAAM,CAAC;YAChB,CAAC;YAED,yEAAyE;YACzE,oEAAoE;YACpE,OAAO,aAAa,EAAE,CAAC;QACzB,CAAC;QACD,SAAS,EAAE,QAAQ;KACpB,CAAC;AACJ,CAAC,CAAC;AA9EW,QAAA,kBAAkB,sBA8E7B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/react-native-onboarding",
3
- "version": "1.44.2",
3
+ "version": "1.44.4",
4
4
  "description": "Headless React Native SDK for Rocapine Onboarding Studio - Data fetching, state management, and hooks",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -0,0 +1,101 @@
1
+ import { describe, it, expect, vi, beforeEach } from "vitest";
2
+
3
+ // AsyncStorage is mocked — the query reads/writes the production cache through it.
4
+ const { getItem, setItem } = vi.hoisted(() => ({
5
+ getItem: vi.fn<(key: string) => Promise<string | null>>(),
6
+ setItem: vi.fn<(key: string, value: string) => Promise<void>>(),
7
+ }));
8
+ vi.mock("@react-native-async-storage/async-storage", () => ({
9
+ default: { getItem, setItem },
10
+ }));
11
+
12
+ import { getOnboardingQuery } from "../infra/queries/getOnboarding.query";
13
+
14
+ const CACHE_KEY = "rocapine-onboarding-studio";
15
+
16
+ const realOnboarding = { metadata: { id: "real" }, steps: [], fonts: {} } as any;
17
+ const fallbackOnboarding = { metadata: { id: "fallback" }, steps: [], fonts: {} } as any;
18
+
19
+ // Builds a fake client whose `getSteps` resolves to the given payload + the
20
+ // ONBS-Onboarding-Id header the SDK uses to flag the offline fallback.
21
+ const makeClient = (
22
+ { isSandbox, onboardingId, data }: { isSandbox: boolean; onboardingId: string; data: any }
23
+ ) => {
24
+ const getSteps = vi.fn().mockResolvedValue({
25
+ data,
26
+ headers: {
27
+ "ONBS-Onboarding-Id": onboardingId,
28
+ "ONBS-Onboarding-Name": onboardingId,
29
+ "ONBS-Audience-Id": onboardingId,
30
+ },
31
+ });
32
+ return {
33
+ projectId: "p1",
34
+ options: { isSandbox, baseUrl: undefined },
35
+ getSteps,
36
+ } as any;
37
+ };
38
+
39
+ const run = (client: any, setOnboarding = vi.fn()) =>
40
+ (getOnboardingQuery(client, "en", {}, setOnboarding) as any).queryFn();
41
+
42
+ const flush = () => new Promise((r) => setTimeout(r, 0));
43
+
44
+ describe("getOnboardingQuery", () => {
45
+ beforeEach(() => {
46
+ getItem.mockReset();
47
+ setItem.mockReset();
48
+ setItem.mockResolvedValue(undefined);
49
+ });
50
+
51
+ it("sandbox: always fetches, never touches the cache", async () => {
52
+ const client = makeClient({ isSandbox: true, onboardingId: "real", data: realOnboarding });
53
+ const data = await run(client);
54
+
55
+ expect(client.getSteps).toHaveBeenCalledTimes(1);
56
+ expect(getItem).not.toHaveBeenCalled();
57
+ expect(setItem).toHaveBeenCalledWith(CACHE_KEY, JSON.stringify(realOnboarding));
58
+ expect(data).toBe(realOnboarding);
59
+ });
60
+
61
+ it("production cache miss + real payload: fetches and caches", async () => {
62
+ getItem.mockResolvedValue(null);
63
+ const client = makeClient({ isSandbox: false, onboardingId: "real", data: realOnboarding });
64
+ const setOnboarding = vi.fn();
65
+ const data = await run(client, setOnboarding);
66
+
67
+ expect(client.getSteps).toHaveBeenCalledTimes(1);
68
+ expect(setItem).toHaveBeenCalledWith(CACHE_KEY, JSON.stringify(realOnboarding));
69
+ expect(setOnboarding).toHaveBeenCalledWith(realOnboarding);
70
+ expect(data).toBe(realOnboarding);
71
+ });
72
+
73
+ it("production cache miss + fallback: returns it but does NOT cache it", async () => {
74
+ getItem.mockResolvedValue(null);
75
+ const client = makeClient({ isSandbox: false, onboardingId: "fallback", data: fallbackOnboarding });
76
+ const data = await run(client);
77
+
78
+ expect(client.getSteps).toHaveBeenCalledTimes(1);
79
+ expect(setItem).not.toHaveBeenCalled();
80
+ expect(data).toBe(fallbackOnboarding);
81
+ });
82
+
83
+ it("production cache hit: returns cache immediately and revalidates in the background", async () => {
84
+ const cached = { metadata: { id: "cached" }, steps: [], fonts: {} } as any;
85
+ getItem.mockResolvedValue(JSON.stringify(cached));
86
+ const client = makeClient({ isSandbox: false, onboardingId: "real", data: realOnboarding });
87
+ const setOnboarding = vi.fn();
88
+
89
+ const data = await run(client, setOnboarding);
90
+
91
+ // Served from cache without blocking on the network.
92
+ expect(data).toEqual(cached);
93
+ expect(setOnboarding).toHaveBeenCalledWith(cached);
94
+
95
+ // Background revalidation runs after the cache is returned.
96
+ await flush();
97
+ expect(client.getSteps).toHaveBeenCalledTimes(1);
98
+ expect(setItem).toHaveBeenCalledWith(CACHE_KEY, JSON.stringify(realOnboarding));
99
+ expect(setOnboarding).toHaveBeenCalledWith(realOnboarding);
100
+ });
101
+ });
@@ -21,37 +21,65 @@ export const getOnboardingQuery = <StepType extends BaseStepType>(
21
21
  JSON.stringify(customAudienceParams),
22
22
  ],
23
23
  queryFn: async (): Promise<Onboarding<StepType>> => {
24
- // Try to get data from AsyncStorage first for production
25
- if (!(client?.options?.isSandbox || false)) {
26
- try {
27
- const cachedData = await AsyncStorage.getItem(cacheKey);
28
- if (cachedData) {
29
- setOnboarding && setOnboarding(JSON.parse(cachedData));
30
- return JSON.parse(cachedData);
24
+ const isProduction = !(client?.options?.isSandbox || false);
25
+
26
+ // Fetches the live payload, pushes it to the provider, and caches it —
27
+ // but NEVER caches the offline fallback. Caching the fallback (with
28
+ // `staleTime: Infinity` + cache-first below) would pin every future
29
+ // launch to the fallback even after the network recovers, and the device
30
+ // would stop calling the edge function entirely. Returns the payload.
31
+ const fetchAndCache = async (): Promise<Onboarding<StepType>> => {
32
+ const { data, headers } = await client!.getSteps<StepType>(
33
+ { locale },
34
+ customAudienceParams
35
+ );
36
+
37
+ const isFallback = headers["ONBS-Onboarding-Id"] === "fallback";
38
+ console.info("onbs-onboarding-name", headers["ONBS-Onboarding-Name"]);
39
+ console.info("onbs-onboarding-id", headers["ONBS-Onboarding-Id"]);
40
+ setOnboarding && setOnboarding(data);
41
+
42
+ if (!isFallback) {
43
+ try {
44
+ await AsyncStorage.setItem(cacheKey, JSON.stringify(data));
45
+ } catch (error) {
46
+ console.warn("Failed to cache onboarding questions:", error);
31
47
  }
32
- } catch (error) {
33
- console.warn("Failed to load cached onboarding questions:", error);
34
48
  }
35
- }
36
49
 
37
- // Fetch from API
38
- const { data, headers } = await client!.getSteps<StepType>(
39
- { locale },
40
- customAudienceParams
41
- );
50
+ return data;
51
+ };
42
52
 
43
- console.info("onbs-onboarding-name", headers["ONBS-Onboarding-Name"]);
44
- console.info("onbs-onboarding-id", headers["ONBS-Onboarding-Id"]);
45
- setOnboarding && setOnboarding(data);
53
+ // Sandbox / draft: always fetch fresh, no caching.
54
+ if (!isProduction) {
55
+ return fetchAndCache();
56
+ }
46
57
 
47
- // Cache the steps
58
+ // Production: stale-while-revalidate. Serve the cached payload instantly
59
+ // (fast first paint, offline-resilient) AND refresh from the network in
60
+ // the background, so studio re-deploys propagate and a stale cache heals.
61
+ let cached: Onboarding<StepType> | null = null;
48
62
  try {
49
- await AsyncStorage.setItem(cacheKey, JSON.stringify(data));
63
+ const cachedData = await AsyncStorage.getItem(cacheKey);
64
+ if (cachedData) cached = JSON.parse(cachedData) as Onboarding<StepType>;
50
65
  } catch (error) {
51
- console.warn("Failed to cache onboarding questions:", error);
66
+ console.warn("Failed to load cached onboarding questions:", error);
67
+ }
68
+
69
+ if (cached) {
70
+ setOnboarding && setOnboarding(cached);
71
+ // Background revalidation — updates the cache + provider state when a
72
+ // fresh real payload arrives. Errors are swallowed: the cache already
73
+ // painted, so an offline revalidation must not surface as a query error.
74
+ void fetchAndCache().catch((error) => {
75
+ console.warn("Background onboarding revalidation failed:", error);
76
+ });
77
+ return cached;
52
78
  }
53
79
 
54
- return data;
80
+ // No cache yet: await the network. A fallback result is returned but NOT
81
+ // cached, so the next launch retries instead of being pinned to it.
82
+ return fetchAndCache();
55
83
  },
56
84
  staleTime: Infinity,
57
85
  };