posthog-react-native 1.1.5 → 2.0.0-alpha10
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 +9 -0
- package/lib/index.cjs.js +1773 -0
- package/lib/index.cjs.js.map +1 -0
- package/lib/index.d.ts +214 -0
- package/lib/index.esm.js +1737 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/node_modules/tslib/tslib.es6.d.ts +35 -0
- package/lib/posthog-core/src/eventemitter.d.ts +8 -0
- package/lib/posthog-core/src/index.d.ts +87 -0
- package/lib/posthog-core/src/lz-string.d.ts +8 -0
- package/lib/posthog-core/src/types.d.ts +68 -0
- package/lib/posthog-core/src/utils.d.ts +13 -0
- package/lib/posthog-react-native/index.d.ts +10 -0
- package/lib/posthog-react-native/src/PostHogProvider.d.ts +13 -0
- package/lib/posthog-react-native/src/PosthogContext.d.ts +5 -0
- package/lib/posthog-react-native/src/autocapture.d.ts +3 -0
- package/lib/posthog-react-native/src/hooks/useFeatureFlag.d.ts +1 -0
- package/lib/posthog-react-native/src/hooks/useFeatureFlags.d.ts +3 -0
- package/lib/posthog-react-native/src/hooks/useLifecycleTracker.d.ts +2 -0
- package/lib/posthog-react-native/src/hooks/useNavigationTracker.d.ts +5 -0
- package/lib/posthog-react-native/src/hooks/usePostHog.d.ts +2 -0
- package/lib/posthog-react-native/src/legacy.d.ts +4 -0
- package/lib/posthog-react-native/src/optional-imports.d.ts +6 -0
- package/lib/posthog-react-native/src/posthog-rn.d.ts +14 -0
- package/lib/posthog-react-native/src/storage.d.ts +8 -0
- package/lib/posthog-react-native/src/types.d.ts +12 -0
- package/package.json +35 -51
- package/RNPostHog.podspec +0 -21
- package/android/build.gradle +0 -46
- package/android/src/main/AndroidManifest.xml +0 -1
- package/android/src/main/java/com/posthog/reactnative/core/RNPostHogModule.kt +0 -250
- package/android/src/main/java/com/posthog/reactnative/core/RNPostHogPackage.kt +0 -44
- package/build/cjs/__mocks__/bridge.d.ts +0 -14
- package/build/cjs/__mocks__/bridge.js +0 -15
- package/build/cjs/__mocks__/bridge.js.map +0 -1
- package/build/cjs/__tests__/bridge.spec.d.ts +0 -1
- package/build/cjs/__tests__/bridge.spec.js +0 -19
- package/build/cjs/__tests__/bridge.spec.js.map +0 -1
- package/build/cjs/__tests__/configuration.spec.d.ts +0 -1
- package/build/cjs/__tests__/configuration.spec.js +0 -131
- package/build/cjs/__tests__/configuration.spec.js.map +0 -1
- package/build/cjs/__tests__/index.spec.d.ts +0 -1
- package/build/cjs/__tests__/index.spec.js +0 -9
- package/build/cjs/__tests__/index.spec.js.map +0 -1
- package/build/cjs/__tests__/posthog.spec.d.ts +0 -1
- package/build/cjs/__tests__/posthog.spec.js +0 -193
- package/build/cjs/__tests__/posthog.spec.js.map +0 -1
- package/build/cjs/bridge.d.ts +0 -50
- package/build/cjs/bridge.js +0 -9
- package/build/cjs/bridge.js.map +0 -1
- package/build/cjs/configuration.d.ts +0 -3
- package/build/cjs/configuration.js +0 -94
- package/build/cjs/configuration.js.map +0 -1
- package/build/cjs/index.d.ts +0 -5
- package/build/cjs/index.js +0 -6
- package/build/cjs/index.js.map +0 -1
- package/build/cjs/make-pkg.d.ts +0 -1
- package/build/cjs/make-pkg.js +0 -5
- package/build/cjs/make-pkg.js.map +0 -1
- package/build/cjs/middleware.d.ts +0 -41
- package/build/cjs/middleware.js +0 -114
- package/build/cjs/middleware.js.map +0 -1
- package/build/cjs/posthog.d.ts +0 -228
- package/build/cjs/posthog.js +0 -309
- package/build/cjs/posthog.js.map +0 -1
- package/build/cjs/utils.d.ts +0 -1
- package/build/cjs/utils.js +0 -7
- package/build/cjs/utils.js.map +0 -1
- package/build/cjs/wrapper.d.ts +0 -22
- package/build/cjs/wrapper.js +0 -114
- package/build/cjs/wrapper.js.map +0 -1
- package/build/coverage/clover.xml +0 -169
- package/build/coverage/coverage-final.json +0 -8
- package/build/coverage/lcov-report/base.css +0 -212
- package/build/coverage/lcov-report/bridge.ts.html +0 -228
- package/build/coverage/lcov-report/configuration.ts.html +0 -228
- package/build/coverage/lcov-report/index.html +0 -151
- package/build/coverage/lcov-report/index.ts.html +0 -60
- package/build/coverage/lcov-report/middleware.ts.html +0 -429
- package/build/coverage/lcov-report/posthog.ts.html +0 -960
- package/build/coverage/lcov-report/prettify.css +0 -1
- package/build/coverage/lcov-report/prettify.js +0 -1
- package/build/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/build/coverage/lcov-report/sorter.js +0 -158
- package/build/coverage/lcov-report/utils.ts.html +0 -54
- package/build/coverage/lcov-report/wrapper.ts.html +0 -228
- package/build/coverage/lcov.info +0 -365
- package/build/esm/__mocks__/bridge.d.ts +0 -14
- package/build/esm/__mocks__/bridge.js +0 -13
- package/build/esm/__mocks__/bridge.js.map +0 -1
- package/build/esm/__tests__/bridge.spec.d.ts +0 -1
- package/build/esm/__tests__/bridge.spec.js +0 -19
- package/build/esm/__tests__/bridge.spec.js.map +0 -1
- package/build/esm/__tests__/configuration.spec.d.ts +0 -1
- package/build/esm/__tests__/configuration.spec.js +0 -129
- package/build/esm/__tests__/configuration.spec.js.map +0 -1
- package/build/esm/__tests__/index.spec.d.ts +0 -1
- package/build/esm/__tests__/index.spec.js +0 -7
- package/build/esm/__tests__/index.spec.js.map +0 -1
- package/build/esm/__tests__/posthog.spec.d.ts +0 -1
- package/build/esm/__tests__/posthog.spec.js +0 -191
- package/build/esm/__tests__/posthog.spec.js.map +0 -1
- package/build/esm/bridge.d.ts +0 -50
- package/build/esm/bridge.js +0 -7
- package/build/esm/bridge.js.map +0 -1
- package/build/esm/configuration.d.ts +0 -3
- package/build/esm/configuration.js +0 -92
- package/build/esm/configuration.js.map +0 -1
- package/build/esm/index.d.ts +0 -5
- package/build/esm/index.js +0 -4
- package/build/esm/index.js.map +0 -1
- package/build/esm/make-pkg.d.ts +0 -1
- package/build/esm/make-pkg.js +0 -3
- package/build/esm/make-pkg.js.map +0 -1
- package/build/esm/middleware.d.ts +0 -41
- package/build/esm/middleware.js +0 -112
- package/build/esm/middleware.js.map +0 -1
- package/build/esm/posthog.d.ts +0 -228
- package/build/esm/posthog.js +0 -307
- package/build/esm/posthog.js.map +0 -1
- package/build/esm/utils.d.ts +0 -1
- package/build/esm/utils.js +0 -4
- package/build/esm/utils.js.map +0 -1
- package/build/esm/wrapper.d.ts +0 -22
- package/build/esm/wrapper.js +0 -112
- package/build/esm/wrapper.js.map +0 -1
- package/build/package.json +0 -3
- package/ios/RNPostHog/RNPostHog.h +0 -9
- package/ios/RNPostHog/RNPostHog.m +0 -128
- package/ios/RNPostHog.xcodeproj/project.pbxproj +0 -336
- package/ios/RNPostHog.xcodeproj/project.xcworkspace/contents.xcworkspacedata +0 -7
- package/src/__mocks__/bridge.ts +0 -12
- package/src/__tests__/bridge.spec.ts +0 -23
- package/src/__tests__/configuration.spec.ts +0 -79
- package/src/__tests__/index.spec.ts +0 -7
- package/src/__tests__/posthog.spec.ts +0 -108
- package/src/bridge.ts +0 -61
- package/src/configuration.ts +0 -61
- package/src/index.ts +0 -5
- package/src/make-pkg.ts +0 -3
- package/src/middleware.ts +0 -128
- package/src/modules.d.ts +0 -2
- package/src/posthog.ts +0 -305
- package/src/utils.ts +0 -3
- package/src/wrapper.ts +0 -61
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { configure } from '../configuration'
|
|
2
|
-
|
|
3
|
-
const apiKey = 'test-api-key'
|
|
4
|
-
|
|
5
|
-
function withIntegrity<T extends {}>(config: T): T & { json: string } {
|
|
6
|
-
const json = JSON.stringify(config)
|
|
7
|
-
|
|
8
|
-
return {
|
|
9
|
-
...(config as any),
|
|
10
|
-
json
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
it('uses the default configuration', async () => {
|
|
15
|
-
expect(await configure(apiKey, {})).toEqual(
|
|
16
|
-
withIntegrity({
|
|
17
|
-
apiKey,
|
|
18
|
-
captureApplicationLifecycleEvents: false,
|
|
19
|
-
captureDeepLinks: false,
|
|
20
|
-
debug: false,
|
|
21
|
-
flushAt: 20,
|
|
22
|
-
flushInterval: 30,
|
|
23
|
-
host: 'https://app.posthog.com',
|
|
24
|
-
recordScreenViews: false,
|
|
25
|
-
|
|
26
|
-
android: {
|
|
27
|
-
collectDeviceId: true
|
|
28
|
-
},
|
|
29
|
-
ios: {
|
|
30
|
-
captureInAppPurchases: false,
|
|
31
|
-
capturePushNotifications: false,
|
|
32
|
-
maxQueueSize: 1000,
|
|
33
|
-
shouldUseBluetooth: false,
|
|
34
|
-
shouldUseLocationServices: false
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('produces a valid configuration', async () => {
|
|
41
|
-
const config = await configure(apiKey, {
|
|
42
|
-
captureApplicationLifecycleEvents: true,
|
|
43
|
-
debug: true,
|
|
44
|
-
flushAt: 42,
|
|
45
|
-
flushInterval: 72,
|
|
46
|
-
recordScreenViews: true,
|
|
47
|
-
|
|
48
|
-
android: {
|
|
49
|
-
collectDeviceId: false
|
|
50
|
-
},
|
|
51
|
-
ios: {
|
|
52
|
-
capturePushNotifications: true
|
|
53
|
-
}
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
expect(config).toEqual(
|
|
57
|
-
withIntegrity({
|
|
58
|
-
apiKey,
|
|
59
|
-
captureApplicationLifecycleEvents: true,
|
|
60
|
-
captureDeepLinks: false,
|
|
61
|
-
debug: true,
|
|
62
|
-
flushAt: 42,
|
|
63
|
-
flushInterval: 72,
|
|
64
|
-
host: 'https://app.posthog.com',
|
|
65
|
-
recordScreenViews: true,
|
|
66
|
-
|
|
67
|
-
android: {
|
|
68
|
-
collectDeviceId: false
|
|
69
|
-
},
|
|
70
|
-
ios: {
|
|
71
|
-
captureInAppPurchases: false,
|
|
72
|
-
capturePushNotifications: true,
|
|
73
|
-
maxQueueSize: 1000,
|
|
74
|
-
shouldUseBluetooth: false,
|
|
75
|
-
shouldUseLocationServices: false
|
|
76
|
-
}
|
|
77
|
-
})
|
|
78
|
-
)
|
|
79
|
-
})
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import mockConsole, { RestoreConsole } from 'jest-mock-console'
|
|
2
|
-
|
|
3
|
-
import { PostHog } from '../posthog'
|
|
4
|
-
import Bridge from '../bridge'
|
|
5
|
-
|
|
6
|
-
jest.mock('../bridge')
|
|
7
|
-
|
|
8
|
-
const nextTick = () => new Promise(resolve => setImmediate(resolve))
|
|
9
|
-
const getBridgeStub = <K extends keyof typeof Bridge>(name: K): jest.Mock<(typeof Bridge)[K]> => (Bridge as any)[name]
|
|
10
|
-
let posthog: PostHog.Client = null!
|
|
11
|
-
let restoreConsole: RestoreConsole = null!
|
|
12
|
-
|
|
13
|
-
beforeEach(async () => {
|
|
14
|
-
restoreConsole = mockConsole()
|
|
15
|
-
posthog = new PostHog.Client()
|
|
16
|
-
Object.keys(Bridge).forEach(key => getBridgeStub(key as any).mockClear())
|
|
17
|
-
|
|
18
|
-
await posthog.setup('api key')
|
|
19
|
-
})
|
|
20
|
-
|
|
21
|
-
afterEach(() => {
|
|
22
|
-
restoreConsole()
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
it('is ready', () => expect(posthog.ready).toBe(true))
|
|
26
|
-
|
|
27
|
-
it('catches bridge errors', async () => {
|
|
28
|
-
const error = new Error('test-error')
|
|
29
|
-
const onError = jest.fn()
|
|
30
|
-
|
|
31
|
-
getBridgeStub('capture').mockImplementationOnce(() => Promise.reject(error) as any)
|
|
32
|
-
posthog.catch(onError)
|
|
33
|
-
posthog.capture('test')
|
|
34
|
-
|
|
35
|
-
expect(onError).not.toHaveBeenCalled()
|
|
36
|
-
await new Promise(resolve => setImmediate(resolve))
|
|
37
|
-
expect(onError).toHaveBeenCalledWith(error)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('waits for .setup()', async () => {
|
|
41
|
-
const client = new PostHog.Client()
|
|
42
|
-
|
|
43
|
-
client.capture('test 1')
|
|
44
|
-
client.capture('test 2')
|
|
45
|
-
|
|
46
|
-
expect(Bridge.capture).not.toHaveBeenCalled()
|
|
47
|
-
await client.setup('key')
|
|
48
|
-
|
|
49
|
-
expect(Bridge.capture).toHaveBeenNthCalledWith(1, 'test 1')
|
|
50
|
-
expect(Bridge.capture).toHaveBeenNthCalledWith(2, 'test 2')
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('does .capture()', () => testCall('capture')('Added to cart', { productId: 'azertyuiop' }))
|
|
54
|
-
|
|
55
|
-
it('does .screen()', () => testCall('screen')('Shopping cart', { from: 'Product page' }))
|
|
56
|
-
|
|
57
|
-
it('does .identify()', () => testCall('identify')('sloth', { eats: 'leaves' }))
|
|
58
|
-
|
|
59
|
-
it('does .identify() with null', () => testCall('identify')(null, { eats: 'leaves' }))
|
|
60
|
-
|
|
61
|
-
it('does .alias()', () => testCall('alias')('new alias'))
|
|
62
|
-
|
|
63
|
-
it('does .reset()', testCall('reset'))
|
|
64
|
-
it('does .flush()', testCall('flush'))
|
|
65
|
-
it('does .enable()', testCall('enable'))
|
|
66
|
-
it('does .disable()', testCall('disable'))
|
|
67
|
-
|
|
68
|
-
it('does .getAnonymousId()', testCall('getAnonymousId'))
|
|
69
|
-
|
|
70
|
-
it('logs uncaught bridge errors', async () => {
|
|
71
|
-
const error = {
|
|
72
|
-
message: 'test-error'
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
getBridgeStub('capture').mockImplementationOnce(() => Promise.reject(error) as any)
|
|
76
|
-
|
|
77
|
-
expect(posthog.capture('test')).rejects.toBe(error)
|
|
78
|
-
expect(console.error).not.toHaveBeenCalled()
|
|
79
|
-
await nextTick()
|
|
80
|
-
expect(console.error).toHaveBeenCalledWith('Uncaught PostHog error', error)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
function testCall<K extends keyof typeof Bridge>(name: K) {
|
|
84
|
-
return (async (...args: any[]) => {
|
|
85
|
-
posthog.constructor.prototype[name].call(posthog, ...args)
|
|
86
|
-
await nextTick()
|
|
87
|
-
expect(Bridge[name]).toHaveBeenNthCalledWith(1, ...args)
|
|
88
|
-
}) as (typeof Bridge)[K]
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
it('enables setting integrations from the middleware', async () => {
|
|
92
|
-
const integrations = {
|
|
93
|
-
'Google PostHog': false,
|
|
94
|
-
Mixpanel: { foo: 'bar' }
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
posthog.middleware(async ({ next, context, data }) =>
|
|
98
|
-
// @ts-ignore ts is expecting newId for some reasons
|
|
99
|
-
next(context, { ...data, integrations })
|
|
100
|
-
)
|
|
101
|
-
|
|
102
|
-
const captureSpy = jest.fn()
|
|
103
|
-
getBridgeStub('capture').mockImplementationOnce(captureSpy)
|
|
104
|
-
posthog.capture('test')
|
|
105
|
-
await nextTick()
|
|
106
|
-
|
|
107
|
-
expect(captureSpy).toBeCalledWith('test')
|
|
108
|
-
})
|
package/src/bridge.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { NativeModules } from 'react-native'
|
|
2
|
-
|
|
3
|
-
export interface Context extends JsonMap {
|
|
4
|
-
$lib: string
|
|
5
|
-
$lib_version: string
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface Configuration {
|
|
9
|
-
apiKey: string
|
|
10
|
-
host: string
|
|
11
|
-
recordScreenViews: boolean
|
|
12
|
-
captureApplicationLifecycleEvents: boolean
|
|
13
|
-
captureDeepLinks?: boolean
|
|
14
|
-
flushAt?: number
|
|
15
|
-
flushInterval?: number
|
|
16
|
-
debug: boolean
|
|
17
|
-
json: string
|
|
18
|
-
context: Context
|
|
19
|
-
android: {
|
|
20
|
-
collectDeviceId?: boolean
|
|
21
|
-
}
|
|
22
|
-
ios: {
|
|
23
|
-
capturePushNotifications?: boolean
|
|
24
|
-
captureInAppPurchases?: boolean
|
|
25
|
-
maxQueueSize?: number
|
|
26
|
-
shouldUseBluetooth?: boolean
|
|
27
|
-
shouldUseLocationServices?: boolean
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export type JsonValue = boolean | number | string | null | JsonList | JsonMap
|
|
32
|
-
export interface JsonMap {
|
|
33
|
-
[key: string]: JsonValue
|
|
34
|
-
[index: number]: JsonValue
|
|
35
|
-
}
|
|
36
|
-
export interface JsonList extends Array<JsonValue> {}
|
|
37
|
-
|
|
38
|
-
export interface Options {
|
|
39
|
-
context?: Context
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface Bridge {
|
|
43
|
-
setup(configuration: Configuration): Promise<void>
|
|
44
|
-
capture(event: string, properties: JsonMap): Promise<void>
|
|
45
|
-
identify(distinctId: string | null, properties: JsonMap): Promise<void>
|
|
46
|
-
screen(screen: string, properties: JsonMap): Promise<void>
|
|
47
|
-
alias(alias: string): Promise<void>
|
|
48
|
-
reset(): Promise<void>
|
|
49
|
-
flush(): Promise<void>
|
|
50
|
-
enable(): Promise<void>
|
|
51
|
-
disable(): Promise<void>
|
|
52
|
-
getAnonymousId(): Promise<string>
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const bridge: Bridge = NativeModules.RNPostHog
|
|
56
|
-
|
|
57
|
-
if (!bridge) {
|
|
58
|
-
throw new Error('Failed to load PostHog native module.')
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
export default bridge
|
package/src/configuration.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { Configuration } from './bridge'
|
|
2
|
-
import { PostHog } from './posthog'
|
|
3
|
-
|
|
4
|
-
const defaults = {
|
|
5
|
-
android: ({ collectDeviceId = true }: Partial<Configuration['android']>) => ({
|
|
6
|
-
collectDeviceId
|
|
7
|
-
}),
|
|
8
|
-
ios: ({
|
|
9
|
-
capturePushNotifications = false,
|
|
10
|
-
captureInAppPurchases = false,
|
|
11
|
-
shouldUseBluetooth = false,
|
|
12
|
-
shouldUseLocationServices = false,
|
|
13
|
-
maxQueueSize = 1000
|
|
14
|
-
}: Partial<Configuration['ios']>) => ({
|
|
15
|
-
captureInAppPurchases,
|
|
16
|
-
capturePushNotifications,
|
|
17
|
-
maxQueueSize,
|
|
18
|
-
shouldUseBluetooth,
|
|
19
|
-
shouldUseLocationServices
|
|
20
|
-
})
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export const configure = async (
|
|
24
|
-
apiKey: string,
|
|
25
|
-
{
|
|
26
|
-
flushAt = 20,
|
|
27
|
-
flushInterval = 30,
|
|
28
|
-
debug = false,
|
|
29
|
-
recordScreenViews = false,
|
|
30
|
-
captureApplicationLifecycleEvents = false,
|
|
31
|
-
captureDeepLinks = false,
|
|
32
|
-
host = 'https://app.posthog.com',
|
|
33
|
-
ios = {},
|
|
34
|
-
android = {}
|
|
35
|
-
}: PostHog.Configuration
|
|
36
|
-
): Promise<Configuration> => {
|
|
37
|
-
const config = {
|
|
38
|
-
apiKey,
|
|
39
|
-
captureApplicationLifecycleEvents,
|
|
40
|
-
captureDeepLinks,
|
|
41
|
-
debug,
|
|
42
|
-
flushAt,
|
|
43
|
-
flushInterval,
|
|
44
|
-
host,
|
|
45
|
-
recordScreenViews,
|
|
46
|
-
|
|
47
|
-
context: {
|
|
48
|
-
$lib: 'posthog-react-native',
|
|
49
|
-
$lib_version: require('../package.json').version
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
android: defaults.android(android),
|
|
53
|
-
ios: defaults.ios(ios)
|
|
54
|
-
}
|
|
55
|
-
const json = JSON.stringify(config)
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
...config,
|
|
59
|
-
json
|
|
60
|
-
}
|
|
61
|
-
}
|
package/src/index.ts
DELETED
package/src/make-pkg.ts
DELETED
package/src/middleware.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { Context, Options, JsonMap } from './bridge'
|
|
2
|
-
import { assertNever } from './utils'
|
|
3
|
-
import { NativeWrapper } from './wrapper'
|
|
4
|
-
|
|
5
|
-
export interface MiddlewarePayload<T extends string, D extends {}> {
|
|
6
|
-
type: T
|
|
7
|
-
data: D
|
|
8
|
-
context: Context
|
|
9
|
-
next(context?: Partial<Context>): void
|
|
10
|
-
// tslint:disable-next-line:unified-signatures
|
|
11
|
-
next(context?: Partial<Context>, data?: D): void
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface CapturePayload
|
|
15
|
-
extends MiddlewarePayload<
|
|
16
|
-
'capture',
|
|
17
|
-
{
|
|
18
|
-
event: string
|
|
19
|
-
properties: JsonMap
|
|
20
|
-
}
|
|
21
|
-
> {}
|
|
22
|
-
|
|
23
|
-
export interface ScreenPayload
|
|
24
|
-
extends MiddlewarePayload<
|
|
25
|
-
'screen',
|
|
26
|
-
{
|
|
27
|
-
screen: string
|
|
28
|
-
properties: JsonMap
|
|
29
|
-
}
|
|
30
|
-
> {}
|
|
31
|
-
|
|
32
|
-
export interface IdentifyPayload
|
|
33
|
-
extends MiddlewarePayload<
|
|
34
|
-
'identify',
|
|
35
|
-
{
|
|
36
|
-
distinctId: string | null
|
|
37
|
-
properties: JsonMap
|
|
38
|
-
}
|
|
39
|
-
> {}
|
|
40
|
-
|
|
41
|
-
export interface AliasPayload
|
|
42
|
-
extends MiddlewarePayload<
|
|
43
|
-
'alias',
|
|
44
|
-
{
|
|
45
|
-
alias: string
|
|
46
|
-
}
|
|
47
|
-
> {}
|
|
48
|
-
|
|
49
|
-
export type Payload = CapturePayload | IdentifyPayload | ScreenPayload | AliasPayload
|
|
50
|
-
|
|
51
|
-
export type Middleware = (payload: Payload) => void | Promise<void>
|
|
52
|
-
export type PayloadFromType<T> = Extract<Payload, { type: T }>
|
|
53
|
-
|
|
54
|
-
export class MiddlewareChain {
|
|
55
|
-
private readonly middlewares: Middleware[] = []
|
|
56
|
-
|
|
57
|
-
constructor(private readonly wrapper: NativeWrapper<any>) {}
|
|
58
|
-
|
|
59
|
-
public add(middleware: Middleware) {
|
|
60
|
-
this.middlewares.push(middleware)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public async run<T extends Payload['type'], P extends PayloadFromType<T>>(type: T, data: P['data']) {
|
|
64
|
-
const payload: Payload = await this.exec(type, data)
|
|
65
|
-
|
|
66
|
-
switch (payload.type) {
|
|
67
|
-
case 'alias':
|
|
68
|
-
return this.wrapper.run('alias', alias => alias(payload.data.alias))
|
|
69
|
-
case 'identify':
|
|
70
|
-
return this.wrapper.run('identify', identify => identify(payload.data.distinctId, payload.data.properties))
|
|
71
|
-
case 'screen':
|
|
72
|
-
return this.wrapper.run('screen', screen => screen(payload.data.screen, payload.data.properties))
|
|
73
|
-
case 'capture':
|
|
74
|
-
return this.wrapper.run('capture', capture => capture(payload.data.event, payload.data.properties))
|
|
75
|
-
default:
|
|
76
|
-
return assertNever(payload)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private async exec<T extends Payload['type'], P extends PayloadFromType<T>>(
|
|
81
|
-
type: T,
|
|
82
|
-
data: P['data'],
|
|
83
|
-
index = 0
|
|
84
|
-
): Promise<P> {
|
|
85
|
-
const { middlewares } = this
|
|
86
|
-
const middleware = middlewares[index]
|
|
87
|
-
const ctx = {}
|
|
88
|
-
|
|
89
|
-
if (index >= middlewares.length || !middleware) {
|
|
90
|
-
return makePayload(type, data)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
let called = false
|
|
94
|
-
|
|
95
|
-
return new Promise<P>((resolve, reject) =>
|
|
96
|
-
Promise.resolve(
|
|
97
|
-
middleware.call(
|
|
98
|
-
middleware,
|
|
99
|
-
makePayload(type, data, (nextProps = data) => {
|
|
100
|
-
if (called) {
|
|
101
|
-
throw new Error('middleware.payload.next() can only be called one time')
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
called = true
|
|
105
|
-
this.exec(type, nextProps, index + 1)
|
|
106
|
-
.then(resolve)
|
|
107
|
-
.catch(reject)
|
|
108
|
-
})
|
|
109
|
-
)
|
|
110
|
-
).catch(reject)
|
|
111
|
-
)
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const notImplemented = (name: string) => () => {
|
|
116
|
-
throw new Error(`.${name}() not implemented`)
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const makePayload = <T extends Payload['type'], P extends PayloadFromType<T>>(
|
|
120
|
-
type: T,
|
|
121
|
-
data: P['data'],
|
|
122
|
-
next: (data?: P['data']) => void = notImplemented('next')
|
|
123
|
-
) =>
|
|
124
|
-
({
|
|
125
|
-
data,
|
|
126
|
-
next,
|
|
127
|
-
type
|
|
128
|
-
} as P)
|
package/src/modules.d.ts
DELETED