|
| 1 | +import type { CollateralEvents } from './events/collateral'; |
| 2 | +import type { TradeEvents } from './events/trade'; |
| 3 | +import type { VaultEvents } from './events/vault'; |
| 4 | +import type { IfStakingEvents } from './events/ifStaking'; |
| 5 | +import type { EarnEvents } from './events/earn'; |
| 6 | +import type { AmplifyEvents } from './events/amplify'; |
| 7 | +import type { OnboardingEvents } from './events/onboarding'; |
| 8 | +import type { SurveyEvents } from './events/survey'; |
| 9 | +import type { PnlEvents } from './events/pnl'; |
| 10 | +import type { SystemEvents } from './events/system'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Marker type for events that require no properties. |
| 14 | + * We use {} rather than Record<string, never> because Record<string, never> |
| 15 | + * collapses optional fields to never under intersection with platform extensions. |
| 16 | + */ |
| 17 | +// eslint-disable-next-line @typescript-eslint/ban-types |
| 18 | +export type NoProperties = {}; |
| 19 | + |
| 20 | +// eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 21 | +export interface PostHogEventMap |
| 22 | + extends CollateralEvents, |
| 23 | + TradeEvents, |
| 24 | + VaultEvents, |
| 25 | + IfStakingEvents, |
| 26 | + EarnEvents, |
| 27 | + AmplifyEvents, |
| 28 | + OnboardingEvents, |
| 29 | + SurveyEvents, |
| 30 | + PnlEvents, |
| 31 | + SystemEvents {} |
| 32 | + |
| 33 | +export type PostHogEvent = keyof PostHogEventMap; |
| 34 | + |
| 35 | +/** |
| 36 | + * Extracts event names whose property type has no required keys (i.e., empty `{}`). |
| 37 | + * These events can be captured without passing a properties argument. |
| 38 | + */ |
| 39 | +type EmptyEventsOf<TMap extends PostHogEventMap> = { |
| 40 | + [K in keyof TMap]: keyof TMap[K] extends never ? K : never; |
| 41 | +}[keyof TMap]; |
| 42 | + |
| 43 | +/** |
| 44 | + * Typed capture function. Overloads are ordered so that: |
| 45 | + * 1. Events with no properties can be called with just the event name |
| 46 | + * 2. Events with properties require the properties argument |
| 47 | + * |
| 48 | + * TypeScript resolves overloads top-to-bottom — the zero-argument overload |
| 49 | + * must come first, otherwise it would never match. |
| 50 | + */ |
| 51 | +export type CaptureEvent<TMap extends PostHogEventMap = PostHogEventMap> = { |
| 52 | + <E extends EmptyEventsOf<TMap> & string>(event: E): void; |
| 53 | + <E extends keyof TMap & string>(event: E, properties: TMap[E]): void; |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Extend the shared event map with platform-specific overrides and new events. |
| 58 | + * |
| 59 | + * @typeParam TOverrides - Properties to intersect onto existing events (use optional fields) |
| 60 | + * @typeParam TNew - Entirely new platform-specific events |
| 61 | + * |
| 62 | + * @example |
| 63 | + * ```ts |
| 64 | + * type WebEventMap = ExtendEventMap< |
| 65 | + * { collateral_deposit_submitted: { source?: string } }, |
| 66 | + * { web_only_event: { detail: string } } |
| 67 | + * >; |
| 68 | + * ``` |
| 69 | + */ |
| 70 | +export type ExtendEventMap< |
| 71 | + TOverrides extends Partial<Record<PostHogEvent, object>> = NoProperties, |
| 72 | + TNew extends Record<string, object> = NoProperties |
| 73 | +> = { |
| 74 | + [K in PostHogEvent]: K extends keyof TOverrides |
| 75 | + ? PostHogEventMap[K] & TOverrides[K] |
| 76 | + : PostHogEventMap[K]; |
| 77 | +} & TNew; |
0 commit comments