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.
- package/README.md +387 -8
- package/cpp/bindings/HybridAmplitudeWorker.cpp +5 -1
- package/lib/commonjs/analytics/config.js +31 -10
- package/lib/commonjs/analytics/config.js.map +1 -1
- package/lib/commonjs/analytics/index.js +8 -2
- package/lib/commonjs/analytics/index.js.map +1 -1
- package/lib/commonjs/analytics/network-guarded-fetch-transport.js +16 -0
- package/lib/commonjs/analytics/network-guarded-fetch-transport.js.map +1 -0
- package/lib/commonjs/analytics/nitro-transport.js +2 -0
- package/lib/commonjs/analytics/nitro-transport.js.map +1 -1
- package/lib/commonjs/analytics/plugins/context.js +7 -1
- package/lib/commonjs/analytics/plugins/context.js.map +1 -1
- package/lib/commonjs/analytics/react-native-client.js +142 -1
- package/lib/commonjs/analytics/react-native-client.js.map +1 -1
- package/lib/commonjs/diagnostics.js +92 -0
- package/lib/commonjs/diagnostics.js.map +1 -0
- package/lib/commonjs/errors.js +48 -0
- package/lib/commonjs/errors.js.map +1 -0
- package/lib/commonjs/experiment/experimentClient.js +84 -2
- package/lib/commonjs/experiment/experimentClient.js.map +1 -1
- package/lib/commonjs/experiment/index.js +12 -0
- package/lib/commonjs/experiment/index.js.map +1 -1
- package/lib/commonjs/experiment/stubClient.js +25 -0
- package/lib/commonjs/experiment/stubClient.js.map +1 -1
- package/lib/commonjs/experiment/typed-variants.js +52 -0
- package/lib/commonjs/experiment/typed-variants.js.map +1 -0
- package/lib/commonjs/experiment/types/config.js +32 -4
- package/lib/commonjs/experiment/types/config.js.map +1 -1
- package/lib/commonjs/index.js +105 -3
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +110 -12
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/native/http.js +23 -8
- package/lib/commonjs/native/http.js.map +1 -1
- package/lib/commonjs/native/http.web.js +2 -0
- package/lib/commonjs/native/http.web.js.map +1 -1
- package/lib/commonjs/native/hybrid.web.js +23 -0
- package/lib/commonjs/native/hybrid.web.js.map +1 -0
- package/lib/commonjs/native/storage.js +27 -15
- package/lib/commonjs/native/storage.js.map +1 -1
- package/lib/commonjs/network.js +154 -0
- package/lib/commonjs/network.js.map +1 -0
- package/lib/commonjs/presets.js +118 -0
- package/lib/commonjs/presets.js.map +1 -0
- package/lib/commonjs/testing.js +166 -0
- package/lib/commonjs/testing.js.map +1 -0
- package/lib/module/analytics/config.js +32 -11
- package/lib/module/analytics/config.js.map +1 -1
- package/lib/module/analytics/index.js +4 -1
- package/lib/module/analytics/index.js.map +1 -1
- package/lib/module/analytics/network-guarded-fetch-transport.js +11 -0
- package/lib/module/analytics/network-guarded-fetch-transport.js.map +1 -0
- package/lib/module/analytics/nitro-transport.js +2 -0
- package/lib/module/analytics/nitro-transport.js.map +1 -1
- package/lib/module/analytics/plugins/context.js +7 -1
- package/lib/module/analytics/plugins/context.js.map +1 -1
- package/lib/module/analytics/react-native-client.js +141 -1
- package/lib/module/analytics/react-native-client.js.map +1 -1
- package/lib/module/diagnostics.js +85 -0
- package/lib/module/diagnostics.js.map +1 -0
- package/lib/module/errors.js +41 -0
- package/lib/module/errors.js.map +1 -0
- package/lib/module/experiment/experimentClient.js +84 -2
- package/lib/module/experiment/experimentClient.js.map +1 -1
- package/lib/module/experiment/index.js +1 -0
- package/lib/module/experiment/index.js.map +1 -1
- package/lib/module/experiment/stubClient.js +25 -0
- package/lib/module/experiment/stubClient.js.map +1 -1
- package/lib/module/experiment/typed-variants.js +43 -0
- package/lib/module/experiment/typed-variants.js.map +1 -0
- package/lib/module/experiment/types/config.js +31 -4
- package/lib/module/experiment/types/config.js.map +1 -1
- package/lib/module/index.js +15 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +19 -6
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/native/http.js +23 -8
- package/lib/module/native/http.js.map +1 -1
- package/lib/module/native/http.web.js +2 -0
- package/lib/module/native/http.web.js.map +1 -1
- package/lib/module/native/hybrid.web.js +16 -0
- package/lib/module/native/hybrid.web.js.map +1 -0
- package/lib/module/native/storage.js +27 -15
- package/lib/module/native/storage.js.map +1 -1
- package/lib/module/network.js +138 -0
- package/lib/module/network.js.map +1 -0
- package/lib/module/presets.js +110 -0
- package/lib/module/presets.js.map +1 -0
- package/lib/module/testing.js +160 -0
- package/lib/module/testing.js.map +1 -0
- package/lib/typescript/analytics/config.d.ts +19 -6
- package/lib/typescript/analytics/config.d.ts.map +1 -1
- package/lib/typescript/analytics/index.d.ts +1 -1
- package/lib/typescript/analytics/index.d.ts.map +1 -1
- package/lib/typescript/analytics/network-guarded-fetch-transport.d.ts +6 -0
- package/lib/typescript/analytics/network-guarded-fetch-transport.d.ts.map +1 -0
- package/lib/typescript/analytics/nitro-transport.d.ts.map +1 -1
- package/lib/typescript/analytics/plugins/context.d.ts +2 -0
- package/lib/typescript/analytics/plugins/context.d.ts.map +1 -1
- package/lib/typescript/analytics/react-native-client.d.ts +43 -0
- package/lib/typescript/analytics/react-native-client.d.ts.map +1 -1
- package/lib/typescript/diagnostics.d.ts +21 -0
- package/lib/typescript/diagnostics.d.ts.map +1 -0
- package/lib/typescript/errors.d.ts +9 -0
- package/lib/typescript/errors.d.ts.map +1 -0
- package/lib/typescript/experiment/experimentClient.d.ts +9 -1
- package/lib/typescript/experiment/experimentClient.d.ts.map +1 -1
- package/lib/typescript/experiment/index.d.ts +1 -0
- package/lib/typescript/experiment/index.d.ts.map +1 -1
- package/lib/typescript/experiment/stubClient.d.ts +6 -1
- package/lib/typescript/experiment/stubClient.d.ts.map +1 -1
- package/lib/typescript/experiment/typed-variants.d.ts +9 -0
- package/lib/typescript/experiment/typed-variants.d.ts.map +1 -0
- package/lib/typescript/experiment/types/client.d.ts +21 -0
- package/lib/typescript/experiment/types/client.d.ts.map +1 -1
- package/lib/typescript/experiment/types/config.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +12 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/index.web.d.ts +16 -6
- package/lib/typescript/index.web.d.ts.map +1 -1
- package/lib/typescript/native/http.d.ts.map +1 -1
- package/lib/typescript/native/http.web.d.ts.map +1 -1
- package/lib/typescript/native/hybrid.web.d.ts +8 -0
- package/lib/typescript/native/hybrid.web.d.ts.map +1 -0
- package/lib/typescript/native/storage.d.ts.map +1 -1
- package/lib/typescript/network.d.ts +50 -0
- package/lib/typescript/network.d.ts.map +1 -0
- package/lib/typescript/presets.d.ts +50 -0
- package/lib/typescript/presets.d.ts.map +1 -0
- package/lib/typescript/testing.d.ts +11 -0
- package/lib/typescript/testing.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/analytics/config.ts +33 -8
- package/src/analytics/index.ts +3 -0
- package/src/analytics/network-guarded-fetch-transport.ts +10 -0
- package/src/analytics/nitro-transport.ts +2 -0
- package/src/analytics/plugins/context.ts +10 -1
- package/src/analytics/react-native-client.ts +217 -0
- package/src/diagnostics.ts +119 -0
- package/src/errors.ts +60 -0
- package/src/experiment/experimentClient.ts +116 -3
- package/src/experiment/index.ts +1 -0
- package/src/experiment/stubClient.ts +42 -1
- package/src/experiment/typed-variants.ts +68 -0
- package/src/experiment/types/client.ts +29 -0
- package/src/experiment/types/config.ts +29 -5
- package/src/index.ts +28 -2
- package/src/index.web.ts +33 -5
- package/src/native/http.ts +38 -8
- package/src/native/http.web.ts +2 -0
- package/src/native/hybrid.web.ts +21 -0
- package/src/native/storage.ts +27 -25
- package/src/network.ts +258 -0
- package/src/presets.ts +208 -0
- 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
|
+
}
|