@rocapine/react-native-onboarding 1.76.0 → 1.78.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.
Files changed (71) hide show
  1. package/README.md +49 -5
  2. package/dist/OnboardingStudio.d.ts +14 -1
  3. package/dist/OnboardingStudio.d.ts.map +1 -1
  4. package/dist/OnboardingStudio.js +5 -1
  5. package/dist/OnboardingStudio.js.map +1 -1
  6. package/dist/OnboardingStudioClient.d.ts +47 -0
  7. package/dist/OnboardingStudioClient.d.ts.map +1 -1
  8. package/dist/OnboardingStudioClient.js +117 -33
  9. package/dist/OnboardingStudioClient.js.map +1 -1
  10. package/dist/assignmentId.d.ts +107 -0
  11. package/dist/assignmentId.d.ts.map +1 -0
  12. package/dist/assignmentId.js +215 -0
  13. package/dist/assignmentId.js.map +1 -0
  14. package/dist/index.d.ts +1 -0
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/paywalls/customScreens.d.ts +7 -0
  18. package/dist/paywalls/customScreens.d.ts.map +1 -1
  19. package/dist/paywalls/observer.d.ts +2 -0
  20. package/dist/paywalls/observer.d.ts.map +1 -1
  21. package/dist/paywalls/observer.js +21 -9
  22. package/dist/paywalls/observer.js.map +1 -1
  23. package/dist/paywalls/types.d.ts +22 -0
  24. package/dist/paywalls/types.d.ts.map +1 -1
  25. package/dist/products/adapters/expoIap.d.ts +34 -13
  26. package/dist/products/adapters/expoIap.d.ts.map +1 -1
  27. package/dist/products/adapters/expoIap.js +136 -21
  28. package/dist/products/adapters/expoIap.js.map +1 -1
  29. package/dist/requestIdentity.d.ts +41 -0
  30. package/dist/requestIdentity.d.ts.map +1 -0
  31. package/dist/requestIdentity.js +70 -0
  32. package/dist/requestIdentity.js.map +1 -0
  33. package/dist/types.d.ts +64 -0
  34. package/dist/types.d.ts.map +1 -1
  35. package/dist/types.js +6 -0
  36. package/dist/types.js.map +1 -1
  37. package/dist/userProperties/reserved.d.ts +17 -15
  38. package/dist/userProperties/reserved.d.ts.map +1 -1
  39. package/dist/userProperties/reserved.js +20 -14
  40. package/dist/userProperties/reserved.js.map +1 -1
  41. package/dist/version.d.ts +10 -0
  42. package/dist/version.d.ts.map +1 -0
  43. package/dist/version.js +13 -0
  44. package/dist/version.js.map +1 -0
  45. package/package.json +5 -1
  46. package/src/OnboardingStudio.ts +19 -1
  47. package/src/OnboardingStudioClient.ts +123 -47
  48. package/src/__tests__/assignmentId.clear.test.ts +413 -0
  49. package/src/__tests__/assignmentId.client.test.ts +288 -0
  50. package/src/__tests__/assignmentId.test.ts +195 -0
  51. package/src/__tests__/getOnboarding.query.test.ts +30 -0
  52. package/src/__tests__/onboardingMetadata.client.test.ts +76 -0
  53. package/src/__tests__/requestIdentity.client.test.ts +115 -0
  54. package/src/__tests__/requestIdentity.test.ts +112 -0
  55. package/src/assignmentId.ts +278 -0
  56. package/src/index.ts +1 -0
  57. package/src/paywalls/__tests__/PaywallProvider.observer.test.tsx +51 -0
  58. package/src/paywalls/__tests__/getPaywalls.test.ts +107 -2
  59. package/src/paywalls/__tests__/observer.test.ts +37 -0
  60. package/src/paywalls/__tests__/present.test.ts +14 -0
  61. package/src/paywalls/customScreens.ts +7 -0
  62. package/src/paywalls/observer.ts +22 -2
  63. package/src/paywalls/types.ts +22 -0
  64. package/src/products/adapters/__tests__/expoIap.contract.test.ts +305 -0
  65. package/src/products/adapters/expoIap.ts +171 -32
  66. package/src/requestIdentity.ts +101 -0
  67. package/src/types.ts +65 -0
  68. package/src/userProperties/__tests__/effectiveParams.test.ts +2 -2
  69. package/src/userProperties/__tests__/primitives.test.ts +1 -1
  70. package/src/userProperties/reserved.ts +20 -14
  71. package/src/version.ts +9 -0
@@ -9,30 +9,36 @@ exports.isReservedUserPropertyKey = exports.RESERVED_USER_PROPERTY_KEYS = void 0
9
9
  * `RESERVED_AUDIENCE_VARS`, which strips them because they are server-owned (a
10
10
  * client supplying `now` is self-selecting into a time-gated audience).
11
11
  *
12
- * The other six close a collision that is live today and merely unreachable,
13
- * because nobody hand-writes these names into `customAudienceParams`. The
14
- * client appends user params FIRST, then its own:
12
+ * The others are the params the client writes on every request: `projectId`,
13
+ * `platform`, the request identity (`appVersion`, `build`, `sdkVersion`,
14
+ * `environment` — see `requestIdentity.ts`), `assignmentId` (see
15
+ * `assignmentId.ts`), `draft`, `locale`, `omitNulls`.
16
+ * The client adds user params FIRST, then writes its own with
17
+ * `URLSearchParams.set`, which replaces any same-named user param (and removes
18
+ * a user `appVersion`/`build` when it has none of its own to send):
15
19
  *
16
20
  * Object.entries(userDefinedParams).forEach(([k, v]) => urlParams.append(k, v));
17
- * urlParams.append("projectId", this.projectId);
21
+ * urlParams.set("projectId", this.projectId);
18
22
  *
19
- * `URLSearchParams.append` permits duplicates, and the two server-side readers
20
- * disagree about which one wins: `url.searchParams.get("projectId")` returns the
21
- * FIRST occurrence (the user's value — so the request resolves the wrong
22
- * project, or 400s), while `Object.fromEntries(...)` is last-wins (so the
23
- * json-logic data still holds the real one). The result is a failed or
24
- * cross-project request with no diagnostic pointing at the property that caused
25
- * it.
23
+ * It used to `append` them, and the server's two readers disagree about which
24
+ * duplicate wins: `url.searchParams.get("projectId")` returns the FIRST
25
+ * occurrence (the user's value — so the request resolved the wrong project, or
26
+ * 400ed), while `Object.fromEntries(...)` is last-wins. `set` closes that on the
27
+ * wire, but a user property that silently loses to the SDK's value is still a
28
+ * bug in the host, so this is where it gets refused.
26
29
  *
27
30
  * A store whose whole purpose is to let a host name keys freely makes that a
28
- * plausible accident, so this is where it gets refused. Refusing — rather than
29
- * prefixing or escaping — keeps the wire format unchanged and puts the warning
30
- * at the call site responsible.
31
+ * plausible accident. Refusing — rather than prefixing or escaping — keeps the
32
+ * wire format unchanged and puts the warning at the call site responsible.
31
33
  */
32
34
  exports.RESERVED_USER_PROPERTY_KEYS = [
33
35
  "projectId",
34
36
  "platform",
35
37
  "appVersion",
38
+ "build",
39
+ "sdkVersion",
40
+ "environment",
41
+ "assignmentId",
36
42
  "draft",
37
43
  "locale",
38
44
  "omitNulls",
@@ -1 +1 @@
1
- {"version":3,"file":"reserved.js","sourceRoot":"","sources":["../../src/userProperties/reserved.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACU,QAAA,2BAA2B,GAAG;IACzC,WAAW;IACX,UAAU;IACV,YAAY;IACZ,OAAO;IACP,QAAQ;IACR,WAAW;IACX,QAAQ;IACR,KAAK;CACG,CAAC;AAIX,sFAAsF;AAC/E,MAAM,yBAAyB,GAAG,CAAC,GAAW,EAAW,EAAE,CAC/D,mCAAiD,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AADtD,QAAA,yBAAyB,6BAC6B"}
1
+ {"version":3,"file":"reserved.js","sourceRoot":"","sources":["../../src/userProperties/reserved.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACU,QAAA,2BAA2B,GAAG;IACzC,WAAW;IACX,UAAU;IACV,YAAY;IACZ,OAAO;IACP,YAAY;IACZ,aAAa;IACb,cAAc;IACd,OAAO;IACP,QAAQ;IACR,WAAW;IACX,QAAQ;IACR,KAAK;CACG,CAAC;AAIX,sFAAsF;AAC/E,MAAM,yBAAyB,GAAG,CAAC,GAAW,EAAW,EAAE,CAC/D,mCAAiD,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;AADtD,QAAA,yBAAyB,6BAC6B"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * This package's version, sent as `sdkVersion` on every request.
3
+ *
4
+ * A committed constant rather than a read of `package.json`: `rootDir` is
5
+ * `./src`, so the build cannot import it, and reading it at runtime would ship
6
+ * the file. `npm run check:versions` fails when this disagrees with
7
+ * `package.json`, and the `bump-version` skill updates it.
8
+ */
9
+ export declare const SDK_VERSION = "1.78.0";
10
+ //# sourceMappingURL=version.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,eAAO,MAAM,WAAW,WAAW,CAAC"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SDK_VERSION = void 0;
4
+ /**
5
+ * This package's version, sent as `sdkVersion` on every request.
6
+ *
7
+ * A committed constant rather than a read of `package.json`: `rootDir` is
8
+ * `./src`, so the build cannot import it, and reading it at runtime would ship
9
+ * the file. `npm run check:versions` fails when this disagrees with
10
+ * `package.json`, and the `bump-version` skill updates it.
11
+ */
12
+ exports.SDK_VERSION = "1.78.0";
13
+ //# sourceMappingURL=version.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAA;;;;;;;GAOG;AACU,QAAA,WAAW,GAAG,QAAQ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rocapine/react-native-onboarding",
3
- "version": "1.76.0",
3
+ "version": "1.78.0",
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",
@@ -39,6 +39,7 @@
39
39
  "vitest": "^4.1.5"
40
40
  },
41
41
  "peerDependencies": {
42
+ "expo-application": "*",
42
43
  "expo-font": "*",
43
44
  "expo-image": "*",
44
45
  "expo-router": ">=3.0.0",
@@ -46,6 +47,9 @@
46
47
  "react-native": "*"
47
48
  },
48
49
  "peerDependenciesMeta": {
50
+ "expo-application": {
51
+ "optional": true
52
+ },
49
53
  "expo-font": {
50
54
  "optional": true
51
55
  },
@@ -1,6 +1,7 @@
1
1
  import { OnboardingStudioClient } from "./OnboardingStudioClient";
2
2
  import type { OnboardingStudioClientOptions } from "./types";
3
3
  import { userPropertyStore } from "./userProperties/store";
4
+ import { getInstallationIdStore, type InstallationIdStore } from "./assignmentId";
4
5
  import type { UserPropertyStore } from "./userProperties/store";
5
6
  import type {
6
7
  UserProperties,
@@ -53,6 +54,18 @@ export type OnboardingStudioFacade = {
53
54
  * not changed — `getClient()?.clearCache()` is right there if both are wanted.
54
55
  */
55
56
  reset: () => void;
57
+ /**
58
+ * Deletes the stored installation id sent as `assignmentId`
59
+ * (`rocapine-assignment-id`) and forgets it in memory; a request already
60
+ * resolving it sends none. Works before `init`. The next request without a
61
+ * host id mints a NEW id, so on consent withdrawal also make the
62
+ * `assignmentId` option return `false`. Rejects if the removal fails or is
63
+ * still waiting after 2s.
64
+ *
65
+ * Separate from `reset()` on purpose: logging out must not move an install
66
+ * to another variant.
67
+ */
68
+ clearAssignmentId: () => Promise<void>;
56
69
  };
57
70
 
58
71
  /** Stable signature of a config, for the idempotency check in `init`. */
@@ -72,7 +85,11 @@ const configSignature = (projectId: string, options: Record<string, unknown>): s
72
85
  * `usePaywall()`, where that state is reachable and where a call cannot be made
73
86
  * before a provider exists.
74
87
  */
75
- export const createOnboardingStudio = (store: UserPropertyStore): OnboardingStudioFacade => {
88
+ export const createOnboardingStudio = (
89
+ store: UserPropertyStore,
90
+ // A getter, not the store: tests swap the process-wide store between cases.
91
+ installation: () => InstallationIdStore = getInstallationIdStore,
92
+ ): OnboardingStudioFacade => {
76
93
  let client: OnboardingStudioClient | null = null;
77
94
  let signature: string | null = null;
78
95
 
@@ -118,6 +135,7 @@ export const createOnboardingStudio = (store: UserPropertyStore): OnboardingStud
118
135
  removeUserProperty: (key) => store.remove(key),
119
136
  getUserProperties: () => store.get(),
120
137
  reset: () => store.reset(),
138
+ clearAssignmentId: () => installation().clear(),
121
139
  };
122
140
  };
123
141
 
@@ -19,11 +19,101 @@ import {
19
19
  } from "./paywalls/types";
20
20
 
21
21
  import { Platform } from "react-native";
22
+ import { readNativeAppInfo, resolveRequestIdentity } from "./requestIdentity";
23
+ import { getInstallationIdStore, resolveAssignmentId } from "./assignmentId";
24
+
25
+ /** Request header carrying the `assignmentId`; the server reads it case-insensitively. */
26
+ const ASSIGNMENT_ID_HEADER = "X-Assignment-Id";
22
27
 
23
28
  export class OnboardingStudioClient {
24
29
  private baseUrl: string;
25
30
  public projectId: string;
26
31
  public options: OnboardingStudioClientOptions;
32
+ private identity?: Record<string, string>;
33
+
34
+ /**
35
+ * `appVersion`, `build`, `sdkVersion`, `environment`: the params this SDK
36
+ * adds to every request (see `requestIdentity.ts`). Memoized: none of them
37
+ * can change while the process runs. Deliberately NOT part of any cache key:
38
+ * a key that changed with every app update would leave a user who is offline
39
+ * right after updating with no cached payload, and would stop a custom
40
+ * `cacheKey` from pinning content across updates.
41
+ */
42
+ identityParams(): Record<string, string> {
43
+ if (!this.identity) {
44
+ this.identity = resolveRequestIdentity(this.options, readNativeAppInfo());
45
+ }
46
+ return this.identity;
47
+ }
48
+
49
+ /**
50
+ * Writes the identity onto a querystring that already holds the user params.
51
+ * `appVersion` and `build` are removed first, because the identity omits them
52
+ * when neither the host nor `expo-application` has a value, and `set` alone
53
+ * would then leave a same-named user param on the wire (a property persisted
54
+ * before these names were reserved is re-read without a reserved-key check).
55
+ *
56
+ * Returns the request headers: the `assignmentId` travels as the
57
+ * `X-Assignment-Id` header, never on the querystring, because edge invocation
58
+ * logs record the full URL and the id may be a host's user id.
59
+ */
60
+ private async writeIdentity(urlParams: URLSearchParams): Promise<Record<string, string>> {
61
+ urlParams.delete("appVersion");
62
+ urlParams.delete("build");
63
+ for (const [key, value] of Object.entries(this.identityParams())) {
64
+ urlParams.set(key, value);
65
+ }
66
+ // A user param of that name never reaches the URL, whatever the id resolves to.
67
+ urlParams.delete("assignmentId");
68
+ const assignmentId = await this.getAssignmentId();
69
+ return assignmentId ? { [ASSIGNMENT_ID_HEADER]: assignmentId } : {};
70
+ }
71
+
72
+ /**
73
+ * The `assignmentId` the next request will carry: the host's `assignmentId`
74
+ * option, else the persisted installation id, else `undefined` (sent as
75
+ * absent). Resolved per request, never memoized here, because a host getter
76
+ * may change value on login. Never throws. See `assignmentId.ts`.
77
+ */
78
+ getAssignmentId(): Promise<string | undefined> {
79
+ return resolveAssignmentId(this.options.assignmentId, getInstallationIdStore());
80
+ }
81
+
82
+ /**
83
+ * Deletes the stored installation id (`rocapine-assignment-id`) and forgets
84
+ * the in-memory one; a request already resolving it sends none. The next
85
+ * request without a host id mints a NEW id — to stop sending one at all (e.g.
86
+ * consent withdrawn), also make the `assignmentId` option return `false`.
87
+ * Process-wide, like the id: same effect as `OnboardingStudio.clearAssignmentId()`.
88
+ * Rejects if the removal fails or is still waiting after 2s.
89
+ */
90
+ clearAssignmentId(): Promise<void> {
91
+ return getInstallationIdStore().clear();
92
+ }
93
+
94
+ /**
95
+ * Races `work` against `options.timeout`, rejecting with `Error("timeout")`.
96
+ * `work` covers identity resolution AND the fetch, so `timeout` is one
97
+ * end-to-end budget: a slow `assignmentId` getter cannot push
98
+ * `fallbackOnboarding` (or the rejection) past it. `work` gets an `expired()`
99
+ * check so a request whose budget ran out while the id resolved is never
100
+ * sent afterwards (the server would count a serve nobody receives).
101
+ */
102
+ private withTimeout<T>(work: (expired: () => boolean) => Promise<T>): Promise<T> {
103
+ const ms = this.options.timeout;
104
+ if (!ms) return work(() => false);
105
+ let timer: ReturnType<typeof setTimeout> | undefined;
106
+ let timedOut = false;
107
+ return Promise.race([
108
+ work(() => timedOut),
109
+ new Promise<never>((_, reject) => {
110
+ timer = setTimeout(() => {
111
+ timedOut = true;
112
+ reject(new Error("timeout"));
113
+ }, ms);
114
+ }),
115
+ ]).finally(() => clearTimeout(timer));
116
+ }
27
117
 
28
118
  constructor(projectId: string, options: OnboardingStudioClientOptions) {
29
119
  console.info("OnboardingStudioClient init: projectId ", projectId);
@@ -80,38 +170,32 @@ export class OnboardingStudioClient {
80
170
  });
81
171
  }
82
172
 
83
- urlParams.append("projectId", this.projectId);
84
- urlParams.append("platform", Platform.OS);
85
-
86
- const appVersion = this.options.appVersion; // TODO get the version from the expo app
87
- if (appVersion) {
88
- urlParams.append("appVersion", appVersion);
89
- }
173
+ // SDK-owned keys use `set`, never `append`: the server reads each with
174
+ // `searchParams.get`, which returns the FIRST occurrence, so an appended
175
+ // value would lose to a same-named user param added above.
176
+ urlParams.set("projectId", this.projectId);
177
+ urlParams.set("platform", Platform.OS);
90
178
 
91
179
  if (isSandbox) {
92
- urlParams.append("draft", "true");
180
+ urlParams.set("draft", "true");
93
181
  }
94
182
 
95
183
  // Add onboardingOptions to URL
96
184
  if (onboardingOptions?.locale) {
97
- urlParams.append("locale", onboardingOptions.locale);
185
+ urlParams.set("locale", onboardingOptions.locale);
98
186
  }
99
187
 
100
- const url = `${this.baseUrl}/get-onboarding-steps?${urlParams.toString()}`;
101
- console.info("OnboardingStudioClient getSteps url", url);
102
188
  try {
103
- const response = await Promise.race(
104
- this.options.timeout
105
- ? [
106
- fetch(url),
107
- new Promise<Response>((_, reject) =>
108
- setTimeout(
109
- () => reject(new Error("timeout")),
110
- this.options.timeout
111
- )
112
- ),
113
- ]
114
- : [fetch(url)]
189
+ // Identity (incl. the host's assignmentId getter) resolves inside the
190
+ // same timeout budget as the fetch — see `withTimeout`.
191
+ const response = await this.withTimeout(
192
+ async (expired) => {
193
+ const headers = await this.writeIdentity(urlParams);
194
+ if (expired()) throw new Error("timeout");
195
+ const url = `${this.baseUrl}/get-onboarding-steps?${urlParams.toString()}`;
196
+ console.info("OnboardingStudioClient getSteps url", url);
197
+ return fetch(url, { headers });
198
+ }
115
199
  );
116
200
  if (!response.ok) {
117
201
  throw new Error(
@@ -171,27 +255,23 @@ export class OnboardingStudioClient {
171
255
  });
172
256
  }
173
257
 
174
- urlParams.append("projectId", this.projectId);
175
- urlParams.append("platform", Platform.OS);
176
-
177
- const appVersion = this.options.appVersion; // TODO get the version from the expo app
178
- if (appVersion) {
179
- urlParams.append("appVersion", appVersion);
180
- }
258
+ // SDK-owned keys use `set`, never `append` (see getSteps).
259
+ urlParams.set("projectId", this.projectId);
260
+ urlParams.set("platform", Platform.OS);
181
261
 
182
262
  if (isSandbox) {
183
- urlParams.append("draft", "true");
263
+ urlParams.set("draft", "true");
184
264
  }
185
265
 
186
266
  // Add paywallOptions to URL
187
267
  if (paywallOptions?.locale) {
188
- urlParams.append("locale", paywallOptions.locale);
268
+ urlParams.set("locale", paywallOptions.locale);
189
269
  }
190
270
 
191
271
  // Narrows the response to a single paywall. Omitted unless explicitly
192
272
  // requested — see the method doc above.
193
273
  if (paywallOptions?.moment) {
194
- urlParams.append("moment", paywallOptions.moment);
274
+ urlParams.set("moment", paywallOptions.moment);
195
275
  }
196
276
 
197
277
  // Deliberately NOT sending spec §6.1's `omitNulls=true`: it's a
@@ -200,21 +280,17 @@ export class OnboardingStudioClient {
200
280
  // and `Paywall`'s declared `| null` fields. This client parses into those
201
281
  // typed objects, so the fields must always be present (possibly `null`),
202
282
  // not sometimes missing.
203
- const url = `${this.baseUrl}/get-paywalls?${urlParams.toString()}`;
204
- console.info("OnboardingStudioClient getPaywalls url", url);
205
283
  try {
206
- const response = await Promise.race(
207
- this.options.timeout
208
- ? [
209
- fetch(url),
210
- new Promise<Response>((_, reject) =>
211
- setTimeout(
212
- () => reject(new Error("timeout")),
213
- this.options.timeout
214
- )
215
- ),
216
- ]
217
- : [fetch(url)]
284
+ // Identity (incl. the host's assignmentId getter) resolves inside the
285
+ // same timeout budget as the fetch — see `withTimeout`.
286
+ const response = await this.withTimeout(
287
+ async (expired) => {
288
+ const headers = await this.writeIdentity(urlParams);
289
+ if (expired()) throw new Error("timeout");
290
+ const url = `${this.baseUrl}/get-paywalls?${urlParams.toString()}`;
291
+ console.info("OnboardingStudioClient getPaywalls url", url);
292
+ return fetch(url, { headers });
293
+ }
218
294
  );
219
295
  if (!response.ok) {
220
296
  throw new Error(