react-native-nitro-amplitude 0.2.0 → 0.5.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 (155) hide show
  1. package/README.md +387 -8
  2. package/cpp/bindings/HybridAmplitudeWorker.cpp +5 -1
  3. package/lib/commonjs/analytics/config.js +31 -10
  4. package/lib/commonjs/analytics/config.js.map +1 -1
  5. package/lib/commonjs/analytics/index.js +8 -2
  6. package/lib/commonjs/analytics/index.js.map +1 -1
  7. package/lib/commonjs/analytics/network-guarded-fetch-transport.js +16 -0
  8. package/lib/commonjs/analytics/network-guarded-fetch-transport.js.map +1 -0
  9. package/lib/commonjs/analytics/nitro-transport.js +2 -0
  10. package/lib/commonjs/analytics/nitro-transport.js.map +1 -1
  11. package/lib/commonjs/analytics/plugins/context.js +7 -1
  12. package/lib/commonjs/analytics/plugins/context.js.map +1 -1
  13. package/lib/commonjs/analytics/react-native-client.js +142 -1
  14. package/lib/commonjs/analytics/react-native-client.js.map +1 -1
  15. package/lib/commonjs/diagnostics.js +92 -0
  16. package/lib/commonjs/diagnostics.js.map +1 -0
  17. package/lib/commonjs/errors.js +48 -0
  18. package/lib/commonjs/errors.js.map +1 -0
  19. package/lib/commonjs/experiment/experimentClient.js +84 -2
  20. package/lib/commonjs/experiment/experimentClient.js.map +1 -1
  21. package/lib/commonjs/experiment/index.js +12 -0
  22. package/lib/commonjs/experiment/index.js.map +1 -1
  23. package/lib/commonjs/experiment/stubClient.js +25 -0
  24. package/lib/commonjs/experiment/stubClient.js.map +1 -1
  25. package/lib/commonjs/experiment/typed-variants.js +52 -0
  26. package/lib/commonjs/experiment/typed-variants.js.map +1 -0
  27. package/lib/commonjs/experiment/types/config.js +32 -4
  28. package/lib/commonjs/experiment/types/config.js.map +1 -1
  29. package/lib/commonjs/index.js +105 -3
  30. package/lib/commonjs/index.js.map +1 -1
  31. package/lib/commonjs/index.web.js +110 -12
  32. package/lib/commonjs/index.web.js.map +1 -1
  33. package/lib/commonjs/native/http.js +23 -8
  34. package/lib/commonjs/native/http.js.map +1 -1
  35. package/lib/commonjs/native/http.web.js +2 -0
  36. package/lib/commonjs/native/http.web.js.map +1 -1
  37. package/lib/commonjs/native/hybrid.web.js +23 -0
  38. package/lib/commonjs/native/hybrid.web.js.map +1 -0
  39. package/lib/commonjs/native/storage.js +27 -15
  40. package/lib/commonjs/native/storage.js.map +1 -1
  41. package/lib/commonjs/network.js +154 -0
  42. package/lib/commonjs/network.js.map +1 -0
  43. package/lib/commonjs/presets.js +118 -0
  44. package/lib/commonjs/presets.js.map +1 -0
  45. package/lib/commonjs/testing.js +166 -0
  46. package/lib/commonjs/testing.js.map +1 -0
  47. package/lib/module/analytics/config.js +32 -11
  48. package/lib/module/analytics/config.js.map +1 -1
  49. package/lib/module/analytics/index.js +4 -1
  50. package/lib/module/analytics/index.js.map +1 -1
  51. package/lib/module/analytics/network-guarded-fetch-transport.js +11 -0
  52. package/lib/module/analytics/network-guarded-fetch-transport.js.map +1 -0
  53. package/lib/module/analytics/nitro-transport.js +2 -0
  54. package/lib/module/analytics/nitro-transport.js.map +1 -1
  55. package/lib/module/analytics/plugins/context.js +7 -1
  56. package/lib/module/analytics/plugins/context.js.map +1 -1
  57. package/lib/module/analytics/react-native-client.js +141 -1
  58. package/lib/module/analytics/react-native-client.js.map +1 -1
  59. package/lib/module/diagnostics.js +85 -0
  60. package/lib/module/diagnostics.js.map +1 -0
  61. package/lib/module/errors.js +41 -0
  62. package/lib/module/errors.js.map +1 -0
  63. package/lib/module/experiment/experimentClient.js +84 -2
  64. package/lib/module/experiment/experimentClient.js.map +1 -1
  65. package/lib/module/experiment/index.js +1 -0
  66. package/lib/module/experiment/index.js.map +1 -1
  67. package/lib/module/experiment/stubClient.js +25 -0
  68. package/lib/module/experiment/stubClient.js.map +1 -1
  69. package/lib/module/experiment/typed-variants.js +43 -0
  70. package/lib/module/experiment/typed-variants.js.map +1 -0
  71. package/lib/module/experiment/types/config.js +31 -4
  72. package/lib/module/experiment/types/config.js.map +1 -1
  73. package/lib/module/index.js +15 -3
  74. package/lib/module/index.js.map +1 -1
  75. package/lib/module/index.web.js +19 -6
  76. package/lib/module/index.web.js.map +1 -1
  77. package/lib/module/native/http.js +23 -8
  78. package/lib/module/native/http.js.map +1 -1
  79. package/lib/module/native/http.web.js +2 -0
  80. package/lib/module/native/http.web.js.map +1 -1
  81. package/lib/module/native/hybrid.web.js +16 -0
  82. package/lib/module/native/hybrid.web.js.map +1 -0
  83. package/lib/module/native/storage.js +27 -15
  84. package/lib/module/native/storage.js.map +1 -1
  85. package/lib/module/network.js +138 -0
  86. package/lib/module/network.js.map +1 -0
  87. package/lib/module/presets.js +110 -0
  88. package/lib/module/presets.js.map +1 -0
  89. package/lib/module/testing.js +160 -0
  90. package/lib/module/testing.js.map +1 -0
  91. package/lib/typescript/analytics/config.d.ts +19 -6
  92. package/lib/typescript/analytics/config.d.ts.map +1 -1
  93. package/lib/typescript/analytics/index.d.ts +1 -1
  94. package/lib/typescript/analytics/index.d.ts.map +1 -1
  95. package/lib/typescript/analytics/network-guarded-fetch-transport.d.ts +6 -0
  96. package/lib/typescript/analytics/network-guarded-fetch-transport.d.ts.map +1 -0
  97. package/lib/typescript/analytics/nitro-transport.d.ts.map +1 -1
  98. package/lib/typescript/analytics/plugins/context.d.ts +2 -0
  99. package/lib/typescript/analytics/plugins/context.d.ts.map +1 -1
  100. package/lib/typescript/analytics/react-native-client.d.ts +43 -0
  101. package/lib/typescript/analytics/react-native-client.d.ts.map +1 -1
  102. package/lib/typescript/diagnostics.d.ts +21 -0
  103. package/lib/typescript/diagnostics.d.ts.map +1 -0
  104. package/lib/typescript/errors.d.ts +9 -0
  105. package/lib/typescript/errors.d.ts.map +1 -0
  106. package/lib/typescript/experiment/experimentClient.d.ts +9 -1
  107. package/lib/typescript/experiment/experimentClient.d.ts.map +1 -1
  108. package/lib/typescript/experiment/index.d.ts +1 -0
  109. package/lib/typescript/experiment/index.d.ts.map +1 -1
  110. package/lib/typescript/experiment/stubClient.d.ts +6 -1
  111. package/lib/typescript/experiment/stubClient.d.ts.map +1 -1
  112. package/lib/typescript/experiment/typed-variants.d.ts +9 -0
  113. package/lib/typescript/experiment/typed-variants.d.ts.map +1 -0
  114. package/lib/typescript/experiment/types/client.d.ts +21 -0
  115. package/lib/typescript/experiment/types/client.d.ts.map +1 -1
  116. package/lib/typescript/experiment/types/config.d.ts.map +1 -1
  117. package/lib/typescript/index.d.ts +12 -3
  118. package/lib/typescript/index.d.ts.map +1 -1
  119. package/lib/typescript/index.web.d.ts +16 -6
  120. package/lib/typescript/index.web.d.ts.map +1 -1
  121. package/lib/typescript/native/http.d.ts.map +1 -1
  122. package/lib/typescript/native/http.web.d.ts.map +1 -1
  123. package/lib/typescript/native/hybrid.web.d.ts +8 -0
  124. package/lib/typescript/native/hybrid.web.d.ts.map +1 -0
  125. package/lib/typescript/native/storage.d.ts.map +1 -1
  126. package/lib/typescript/network.d.ts +50 -0
  127. package/lib/typescript/network.d.ts.map +1 -0
  128. package/lib/typescript/presets.d.ts +50 -0
  129. package/lib/typescript/presets.d.ts.map +1 -0
  130. package/lib/typescript/testing.d.ts +11 -0
  131. package/lib/typescript/testing.d.ts.map +1 -0
  132. package/package.json +1 -1
  133. package/src/analytics/config.ts +33 -8
  134. package/src/analytics/index.ts +3 -0
  135. package/src/analytics/network-guarded-fetch-transport.ts +10 -0
  136. package/src/analytics/nitro-transport.ts +2 -0
  137. package/src/analytics/plugins/context.ts +10 -1
  138. package/src/analytics/react-native-client.ts +217 -0
  139. package/src/diagnostics.ts +119 -0
  140. package/src/errors.ts +60 -0
  141. package/src/experiment/experimentClient.ts +116 -3
  142. package/src/experiment/index.ts +1 -0
  143. package/src/experiment/stubClient.ts +42 -1
  144. package/src/experiment/typed-variants.ts +68 -0
  145. package/src/experiment/types/client.ts +29 -0
  146. package/src/experiment/types/config.ts +29 -5
  147. package/src/index.ts +28 -2
  148. package/src/index.web.ts +33 -5
  149. package/src/native/http.ts +38 -8
  150. package/src/native/http.web.ts +2 -0
  151. package/src/native/hybrid.web.ts +21 -0
  152. package/src/native/storage.ts +27 -25
  153. package/src/network.ts +258 -0
  154. package/src/presets.ts +208 -0
  155. package/src/testing.ts +177 -0
package/src/testing.ts ADDED
@@ -0,0 +1,177 @@
1
+ import type { AmplitudeReturn, Result } from "@amplitude/analytics-core";
2
+ import type { AmplitudeReactNativeClient } from "./analytics/react-native-client";
3
+ import type { Client } from "./experiment/types/client";
4
+ import type { Storage } from "./experiment/types/storage";
5
+ import type { ExperimentUser } from "./experiment/types/user";
6
+ import type { Variant, Variants } from "./experiment/types/variant";
7
+
8
+ type MockFunction<Args extends unknown[], Return> = ((
9
+ ...args: Args
10
+ ) => Return) & {
11
+ calls: Args[];
12
+ };
13
+
14
+ function createMockFunction<Args extends unknown[], Return>(
15
+ implementation: (...args: Args) => Return,
16
+ ): MockFunction<Args, Return> {
17
+ const fn = ((...args: Args) => {
18
+ fn.calls.push(args);
19
+ return implementation(...args);
20
+ }) as MockFunction<Args, Return>;
21
+ fn.calls = [];
22
+ return fn;
23
+ }
24
+
25
+ function createReturn(eventType: string): AmplitudeReturn<Result> {
26
+ const result: Result = {
27
+ event: { event_type: eventType },
28
+ code: 200,
29
+ message: "dry_run",
30
+ };
31
+ return {
32
+ promise: Promise.resolve(result),
33
+ };
34
+ }
35
+
36
+ export type FakeExperimentStorage = Storage & {
37
+ values: Map<string, string>;
38
+ };
39
+
40
+ export function createFakeExperimentStorage(
41
+ initialValues: Record<string, string> = {},
42
+ ): FakeExperimentStorage {
43
+ const values = new Map(Object.entries(initialValues));
44
+ return {
45
+ values,
46
+ get: async (key) => values.get(key) ?? null,
47
+ put: async (key, value) => {
48
+ values.set(key, value);
49
+ },
50
+ delete: async (key) => {
51
+ values.delete(key);
52
+ },
53
+ reset: async () => {
54
+ values.clear();
55
+ },
56
+ };
57
+ }
58
+
59
+ export function createMockAmplitudeClient(): AmplitudeReactNativeClient {
60
+ let userId: string | undefined;
61
+ let deviceId = "mock-device-id";
62
+ let sessionId: number | undefined;
63
+ const client = {
64
+ init: createMockFunction((_: string, nextUserId?: string) => {
65
+ userId = nextUserId;
66
+ return createReturn("$init");
67
+ }),
68
+ track: createMockFunction(() => createReturn("$track")),
69
+ logEvent: createMockFunction(() => createReturn("$track")),
70
+ identify: createMockFunction(() => createReturn("$identify")),
71
+ groupIdentify: createMockFunction(() => createReturn("$groupidentify")),
72
+ setGroup: createMockFunction(() => createReturn("$groupidentify")),
73
+ revenue: createMockFunction(() => createReturn("revenue_amount")),
74
+ flush: createMockFunction(() => createReturn("$flush")),
75
+ flushWithResult: createMockFunction(async () => ({
76
+ ok: true,
77
+ sent: 0,
78
+ failed: 0,
79
+ dropped: 0,
80
+ retried: 0,
81
+ finishedAt: Date.now(),
82
+ })),
83
+ add: createMockFunction(() => createReturn("$add")),
84
+ remove: createMockFunction(() => createReturn("$remove")),
85
+ setUserId: createMockFunction((nextUserId: string | undefined) => {
86
+ userId = nextUserId;
87
+ }),
88
+ getUserId: createMockFunction(() => userId),
89
+ setDeviceId: createMockFunction((nextDeviceId: string) => {
90
+ deviceId = nextDeviceId;
91
+ }),
92
+ getDeviceId: createMockFunction(() => deviceId),
93
+ setSessionId: createMockFunction((nextSessionId: number) => {
94
+ sessionId = nextSessionId;
95
+ }),
96
+ getSessionId: createMockFunction(() => sessionId),
97
+ extendSession: createMockFunction(() => undefined),
98
+ setOptOut: createMockFunction(() => undefined),
99
+ reset: createMockFunction(() => {
100
+ userId = undefined;
101
+ deviceId = "mock-device-id-reset";
102
+ }),
103
+ shutdown: createMockFunction(() => undefined),
104
+ getDiagnostics: createMockFunction(() => ({
105
+ initialized: true,
106
+ userId,
107
+ deviceId,
108
+ sessionId,
109
+ queueSize: 0,
110
+ activeInstanceNames: ["$default_instance"],
111
+ })),
112
+ healthCheck: createMockFunction(async () => ({
113
+ ok: true,
114
+ analyticsInitialized: true,
115
+ nativeAvailable: true,
116
+ storageWritable: true,
117
+ errors: [],
118
+ })),
119
+ };
120
+ return client as unknown as AmplitudeReactNativeClient;
121
+ }
122
+
123
+ export function createMockExperimentClient(
124
+ initialVariants: Variants = {},
125
+ ): Client {
126
+ let user: ExperimentUser = {};
127
+ const variants = new Map<string, Variant>(Object.entries(initialVariants));
128
+ let client: Client;
129
+ client = {
130
+ start: createMockFunction(async (nextUser?: ExperimentUser) => {
131
+ user = nextUser ?? user;
132
+ }),
133
+ stop: createMockFunction(() => undefined),
134
+ fetch: createMockFunction(async () => client),
135
+ fetchWithMetadata: createMockFunction(async () => ({
136
+ fetched: true,
137
+ flagKeys: Array.from(variants.keys()),
138
+ cacheHit: false,
139
+ durationMillis: 0,
140
+ source: "network" as const,
141
+ })),
142
+ fetchOrThrow: createMockFunction(async () => client),
143
+ variant: createMockFunction((key: string, fallback?: string | Variant) => {
144
+ return variants.get(key) ?? normalizeFallback(fallback);
145
+ }),
146
+ variantWithMetadata: createMockFunction(
147
+ (key: string, fallback?: string | Variant) => ({
148
+ variant: variants.get(key) ?? normalizeFallback(fallback),
149
+ fallback: !variants.has(key),
150
+ stale: false,
151
+ reason: variants.has(key) ? undefined : "fallback",
152
+ }),
153
+ ),
154
+ all: createMockFunction(() => Object.fromEntries(variants)),
155
+ clear: createMockFunction(() => variants.clear()),
156
+ clearVariants: createMockFunction(() => variants.clear()),
157
+ hasCachedVariant: createMockFunction((key: string) => variants.has(key)),
158
+ getLastFetchTime: createMockFunction(() => undefined),
159
+ exposure: createMockFunction(() => undefined),
160
+ getUser: createMockFunction(() => ({ ...user })),
161
+ setUser: createMockFunction((nextUser: ExperimentUser) => {
162
+ user = { ...nextUser };
163
+ }),
164
+ getUserProvider: createMockFunction(() => ({
165
+ getUser: async () => ({ ...user }),
166
+ })),
167
+ setUserProvider: createMockFunction(() => client),
168
+ };
169
+ return client;
170
+
171
+ function normalizeFallback(fallback?: string | Variant): Variant {
172
+ if (typeof fallback === "string") {
173
+ return { value: fallback };
174
+ }
175
+ return fallback ?? {};
176
+ }
177
+ }