@usesophi/sophi-event-react-native 0.0.0-stage → 0.0.1-alpha01

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 (74) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +116 -2
  3. package/SophiEventSDK-ReactNative.podspec +23 -0
  4. package/android/build.gradle +39 -0
  5. package/android/consumer-rules.pro +1 -0
  6. package/android/src/main/AndroidManifest.xml +3 -0
  7. package/android/src/main/java/com/sophi/eventreactnative/SophiEventsModule.kt +199 -0
  8. package/android/src/main/java/com/sophi/eventreactnative/SophiEventsPackage.kt +11 -0
  9. package/index.ts +4 -0
  10. package/ios/Frameworks/SophiEventSDK.xcframework/Info.plist +44 -0
  11. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Headers/SophiEventSDK-Swift.h +382 -0
  12. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Info.plist +0 -0
  13. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.abi.json +21707 -0
  14. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +413 -0
  15. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  16. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios.swiftinterface +413 -0
  17. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/Modules/module.modulemap +3 -0
  18. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/PrivacyInfo.xcprivacy +153 -0
  19. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64/SophiEventSDK.framework/SophiEventSDK +0 -0
  20. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Headers/SophiEventSDK-Swift.h +760 -0
  21. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Info.plist +0 -0
  22. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +21707 -0
  23. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +413 -0
  24. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  25. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +413 -0
  26. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +21707 -0
  27. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +413 -0
  28. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  29. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/SophiEventSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +413 -0
  30. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/Modules/module.modulemap +3 -0
  31. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/PrivacyInfo.xcprivacy +153 -0
  32. package/ios/Frameworks/SophiEventSDK.xcframework/ios-arm64_x86_64-simulator/SophiEventSDK.framework/SophiEventSDK +0 -0
  33. package/ios/Frameworks/SophiIdentity.xcframework/Info.plist +44 -0
  34. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Headers/SophiIdentity-Swift.h +382 -0
  35. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Info.plist +0 -0
  36. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.abi.json +6314 -0
  37. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.private.swiftinterface +170 -0
  38. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  39. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios.swiftinterface +170 -0
  40. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/Modules/module.modulemap +3 -0
  41. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/PrivacyInfo.xcprivacy +50 -0
  42. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64/SophiIdentity.framework/SophiIdentity +0 -0
  43. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Headers/SophiIdentity-Swift.h +760 -0
  44. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Info.plist +0 -0
  45. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.abi.json +6314 -0
  46. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +170 -0
  47. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  48. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/arm64-apple-ios-simulator.swiftinterface +170 -0
  49. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.abi.json +6314 -0
  50. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +170 -0
  51. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  52. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/SophiIdentity.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +170 -0
  53. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/Modules/module.modulemap +3 -0
  54. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/PrivacyInfo.xcprivacy +50 -0
  55. package/ios/Frameworks/SophiIdentity.xcframework/ios-arm64_x86_64-simulator/SophiIdentity.framework/SophiIdentity +0 -0
  56. package/ios/SophiEventsModule.m +54 -0
  57. package/ios/SophiEventsModuleImpl.swift +178 -0
  58. package/lib/index.d.ts +4 -0
  59. package/lib/index.js +10 -0
  60. package/lib/src/SophiEvents.d.ts +3 -0
  61. package/lib/src/SophiEvents.js +11 -0
  62. package/lib/src/client.d.ts +60 -0
  63. package/lib/src/client.js +151 -0
  64. package/lib/src/types.d.ts +204 -0
  65. package/lib/src/types.js +3 -0
  66. package/lib/src/wire.d.ts +10 -0
  67. package/lib/src/wire.js +191 -0
  68. package/native-versions.json +16 -0
  69. package/package.json +62 -4
  70. package/react-native.config.js +10 -0
  71. package/src/SophiEvents.ts +13 -0
  72. package/src/client.ts +191 -0
  73. package/src/types.ts +216 -0
  74. package/src/wire.ts +189 -0
package/src/wire.ts ADDED
@@ -0,0 +1,189 @@
1
+ // Converts the public camelCase event into the wire form both native SDKs decode
2
+ // (`event_type`, snake_case fields). Only documented fields are accepted, so iOS and Android
3
+ // see exactly the same input. Structural problems are reported here with wire paths, the same
4
+ // paths the native SDKs use for their own validation (`basket.line_items[0].quantity`).
5
+ import type {SophiEvent} from './types';
6
+
7
+ type Kind = 's' | 'n' | 'i' | 'b' | 'strings' | 'segments' | 'json' | {object: Schema} | {array: Schema};
8
+ type Field = [name: string, kind: Kind, required: boolean];
9
+ type Schema = Field[];
10
+
11
+ const page: Schema = [
12
+ ['type', 's', true], ['url', 's', true], ['title', 's', false], ['language', 's', false],
13
+ ['categoryId', 's', false], ['searchQuery', 's', false], ['custom', 'json', false],
14
+ ];
15
+ const consent: Schema = [['gdprOptin', 'b', true], ['version', 's', true], ['timestamp', 'i', true]];
16
+ const identity: Schema = [['uuid', 's', false], ['email', 's', false], ['phoneNumber', 's', false]];
17
+ const profile: Schema = [
18
+ ['name', 's', false], ['surname', 's', false], ['gender', 's', false], ['birthday', 's', false],
19
+ ['language', 's', false],
20
+ ];
21
+ const behavior: Schema = [
22
+ ['returning', 'b', false], ['hasTransacted', 'b', false], ['transactionCount', 'i', false],
23
+ ['lastTransactionAt', 'i', false],
24
+ ];
25
+ const segmentation: Schema = [['staticSegmentIds', 'segments', false]];
26
+ const user: Schema = [
27
+ ['consent', {object: consent}, true], ['identity', {object: identity}, false],
28
+ ['profile', {object: profile}, false], ['behavior', {object: behavior}, false],
29
+ ['segmentation', {object: segmentation}, false], ['custom', 'json', false],
30
+ ];
31
+ const product: Schema = [
32
+ ['id', 's', true], ['categoryIds', 'strings', true], ['name', 's', true], ['url', 's', true],
33
+ ['productImageUrl', 's', true], ['taxonomy', 'strings', true], ['currency', 's', true],
34
+ ['unitPrice', 'n', true], ['unitSalePrice', 'n', true], ['inStock', 'i', true],
35
+ ['variantId', 's', false], ['sku', 's', false], ['groupCode', 's', false],
36
+ ['omnibusPrice', 'n', false], ['stock', 'i', false], ['brand', 's', false], ['color', 's', false],
37
+ ['size', 's', false], ['gender', 's', false], ['locale', 's', false], ['description', 's', false],
38
+ ['tags', 'strings', false], ['custom', 'json', false],
39
+ ];
40
+ const lineItem: Schema = [
41
+ ['productId', 's', true], ['quantity', 'i', true], ['currency', 's', true], ['unitPrice', 'n', true],
42
+ ['unitSalePrice', 'n', true], ['lineTotal', 'n', true], ['variantId', 's', false], ['sku', 's', false],
43
+ ];
44
+ const basket: Schema = [
45
+ ['currency', 's', true], ['total', 'n', true], ['lineItems', {array: lineItem}, true],
46
+ ['basketId', 's', false], ['subtotal', 'n', false], ['tax', 'n', false], ['shippingCost', 'n', false],
47
+ ['discountTotal', 'n', false], ['couponCode', 's', false],
48
+ ];
49
+ const listingItem: Schema = [
50
+ ['productId', 's', true], ['position', 'i', true], ['name', 's', true], ['url', 's', true],
51
+ ['productImageUrl', 's', true], ['currency', 's', true], ['unitPrice', 'n', true],
52
+ ['unitSalePrice', 'n', true], ['variantId', 's', false], ['inStock', 'i', false], ['brand', 's', false],
53
+ ];
54
+ const listing: Schema = [
55
+ ['categoryId', 's', false], ['slug', 's', false], ['searchQuery', 's', false], ['name', 's', false],
56
+ ['taxonomy', 'strings', false], ['totalResults', 'i', false], ['pageNumber', 'i', false],
57
+ ['pageSize', 'i', false], ['sort', 's', false], ['appliedFilters', 'json', false],
58
+ ['items', {array: listingItem}, false],
59
+ ];
60
+ const transaction: Schema = [
61
+ ['orderId', 's', true], ['currency', 's', true], ['total', 'n', true], ['lineItems', {array: lineItem}, true],
62
+ ['subtotal', 'n', false], ['tax', 'n', false], ['shippingCost', 'n', false],
63
+ ['discountTotal', 'n', false], ['couponCode', 's', false], ['paymentMethod', 's', false],
64
+ ];
65
+ const event: Schema = [
66
+ ['page', {object: page}, true], ['product', {object: product}, false], ['basket', {object: basket}, false],
67
+ ['listing', {object: listing}, false], ['transaction', {object: transaction}, false],
68
+ ['user', {object: user}, false], ['custom', 'json', false],
69
+ ];
70
+
71
+ export const EVENT_TYPES = [
72
+ 'page_view', 'product_view', 'listing_view', 'basket_view', 'add_to_cart', 'remove_from_cart',
73
+ 'checkout_started', 'purchase', 'identify', 'custom',
74
+ ] as const;
75
+
76
+ const MAX_JSON_DEPTH = 16;
77
+
78
+ /** camelCase public name to wire name: `productImageUrl` -> `product_image_url`. */
79
+ export function wireName(name: string): string {
80
+ return name.replace(/[A-Z]/g, letter => '_' + letter.toLowerCase());
81
+ }
82
+
83
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
84
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) return false;
85
+ const prototype = Object.getPrototypeOf(value);
86
+ return prototype === Object.prototype || prototype === null;
87
+ }
88
+
89
+ /** Copies a JSON value, rejecting what JSON cannot carry. Returns false when invalid. */
90
+ function copyJson(value: unknown, depth: number, seen: Set<unknown>, out: {value?: unknown}): boolean {
91
+ if (depth > MAX_JSON_DEPTH) return false;
92
+ if (value === null || typeof value === 'string' || typeof value === 'boolean') {
93
+ out.value = value;
94
+ return true;
95
+ }
96
+ if (typeof value === 'number') {
97
+ if (!Number.isFinite(value)) return false;
98
+ out.value = value;
99
+ return true;
100
+ }
101
+ if (Array.isArray(value) || isPlainObject(value)) {
102
+ if (seen.has(value)) return false;
103
+ seen.add(value);
104
+ const entries = Array.isArray(value) ? value.map((v, i) => [i, v] as const) : Object.entries(value);
105
+ const copy: any = Array.isArray(value) ? [] : {};
106
+ for (const [key, child] of entries) {
107
+ const inner: {value?: unknown} = {};
108
+ if (!copyJson(child, depth + 1, seen, inner)) return false;
109
+ copy[key] = inner.value;
110
+ }
111
+ seen.delete(value);
112
+ out.value = copy;
113
+ return true;
114
+ }
115
+ return false; // functions, undefined, dates, maps, class instances, symbols, bigint
116
+ }
117
+
118
+ function convert(value: unknown, kind: Kind, path: string, errors: string[]): unknown {
119
+ switch (kind) {
120
+ case 's':
121
+ if (typeof value !== 'string') errors.push(path);
122
+ return value;
123
+ case 'n':
124
+ if (typeof value !== 'number' || !Number.isFinite(value)) errors.push(path);
125
+ return value;
126
+ case 'i':
127
+ if (typeof value !== 'number' || !Number.isSafeInteger(value)) errors.push(path);
128
+ return value;
129
+ case 'b':
130
+ if (typeof value !== 'boolean') errors.push(path);
131
+ return value;
132
+ case 'strings':
133
+ if (!Array.isArray(value) || !value.every(item => typeof item === 'string')) errors.push(path);
134
+ return Array.isArray(value) ? [...value] : value;
135
+ case 'segments':
136
+ if (!Array.isArray(value) || !value.every(item =>
137
+ typeof item === 'string' || (typeof item === 'number' && Number.isSafeInteger(item)))) errors.push(path);
138
+ return Array.isArray(value) ? [...value] : value;
139
+ case 'json': {
140
+ const out: {value?: unknown} = {};
141
+ if (!isPlainObject(value) || !copyJson(value, 1, new Set(), out)) {
142
+ errors.push(path);
143
+ return undefined;
144
+ }
145
+ return out.value;
146
+ }
147
+ }
148
+ if ('object' in kind) return convertObject(value, kind.object, path, errors);
149
+ if (!Array.isArray(value)) {
150
+ errors.push(path);
151
+ return undefined;
152
+ }
153
+ return value.map((item, index) => convertObject(item, kind.array, `${path}[${index}]`, errors));
154
+ }
155
+
156
+ function convertObject(value: unknown, schema: Schema, path: string, errors: string[]): unknown {
157
+ if (!isPlainObject(value)) {
158
+ errors.push(path);
159
+ return undefined;
160
+ }
161
+ const known = new Set(schema.map(([name]) => name));
162
+ for (const key of Object.keys(value)) {
163
+ if (!known.has(key) && value[key] !== undefined) errors.push(`${path}.${key}`);
164
+ }
165
+ const out: Record<string, unknown> = {};
166
+ for (const [name, kind, required] of schema) {
167
+ const fieldPath = `${path}.${wireName(name)}`;
168
+ const raw = value[name];
169
+ if (raw === undefined || raw === null) {
170
+ if (required) errors.push(fieldPath);
171
+ continue;
172
+ }
173
+ out[wireName(name)] = convert(raw, kind, fieldPath, errors);
174
+ }
175
+ return out;
176
+ }
177
+
178
+ /** Wire JSON for the native SDKs, or the invalid field paths. */
179
+ export function toWire(input: SophiEvent): {json: string} | {fields: string[]} {
180
+ const errors: string[] = [];
181
+ if (!isPlainObject(input)) return {fields: ['event']};
182
+ if (!(EVENT_TYPES as readonly unknown[]).includes(input.type)) errors.push('event_type');
183
+ const body = convertObject({...input, type: undefined}, event, 'event', errors) as Record<string, unknown>;
184
+ if (errors.length) {
185
+ // Paths are relative to the event, as in the native SDKs: `page.url`, not `event.page.url`.
186
+ return {fields: [...new Set(errors.map(e => (e === 'event' ? e : e.replace(/^event\./, ''))))]};
187
+ }
188
+ return {json: JSON.stringify({event_type: input.type, ...body})};
189
+ }