refactor: convert events module to TS#1258
Conversation
PR SummaryMedium Risk Overview Public API typing is tightened in Impression logging now branches on shape: objects with Geolocation Tests move to Reviewed by Cursor Bugbot for commit 1f4ed64. Bugbot is set up for automated code reviews on this repo. Configure here. |
| logEvent(event: BaseEvent, eventOptions?: SDKEventOptions): void; | ||
| logImpressionEvent( | ||
| impression: SDKProductImpression, | ||
| impression: SDKImpression | SDKImpression[] | SDKProductImpression | SDKProductImpression[], |
There was a problem hiding this comment.
I brought this up with @rmi22186 earlier. I would like for us to not support both arrays and non-arrays for these endpoints, but that might have to be a follow up project.
There was a problem hiding this comment.
Created a follow-up ticket to track this: https://go/j/SDKE-1199
| } | ||
| } else { | ||
| var position = { | ||
| this.startTracking = function(callback: ((position?: GeolocationPosition | { coords: { latitude: number | string; longitude: number | string } }) => void) | null): void { |
There was a problem hiding this comment.
We should define a TrackingCallback type here.
| }; | ||
| triggerCallback(callback, position); | ||
| } else { | ||
| if ('geolocation' in navigator) { |
There was a problem hiding this comment.
If this is a TypeScript migration, why are we adding a conditional here?
There was a problem hiding this comment.
This conditional was already present in the original events.js, it wasn't added as part of the migration. The only change here was flipping the if/else branch order to address a SonarCloud "unexpected negated condition" warning (the original had if (!mpInstance._Store.isTracking) first).
|
|
||
| function triggerCallback(callback, position) { | ||
| function triggerCallback( | ||
| callback: ((position?: GeolocationPosition | { coords: { latitude: number | string; longitude: number | string } }) => void) | null, |
There was a problem hiding this comment.
This should be a new type definition.
|
|
||
| if (event) { | ||
| event.EventCategory = mpInstance._Ecommerce.convertProductActionToEventType( | ||
| // TODO(SDKE-1106): Remove `as Function` casts when ecommerce.js is migrated to TS |
There was a problem hiding this comment.
| // TODO(SDKE-1106): Remove `as Function` casts when ecommerce.js is migrated to TS | |
| // https://go.mparticle.com/work/SDKE-1106 |
There was a problem hiding this comment.
Intentional, this is a temporary cast that will be resolved when the ecommerce module is migrated to TS. Didn't want to create an extra ticket for this so just kept comment and existing migration ticket as refrence
| element, | ||
| i; | ||
| element: Element, | ||
| i: number; |
There was a problem hiding this comment.
| i: number; | |
| elementIndex: number; |
There was a problem hiding this comment.
renamed i to elementIndex
|
|
||
| export interface SDKPromotionAction { | ||
| PromotionActionType: string; | ||
| PromotionActionType: string | valueof<typeof PromotionActionType>; |
There was a problem hiding this comment.
can we just use valueof<typeof PromotionActionType> so we can control the values? string would allow any string, thus negating the valueof
There was a problem hiding this comment.
Keeping string | valueof for now, tightening to just valueof causes a downstream cast failure in sdkToEventsApiConverter.ts where it converts to PromotionActionActionEnum. Fixing that properly would require changes beyond the scope of this migration PR. We can revisit this in a follow-up.
| workspaceToken?: string; | ||
| requiredWebviewBridgeName?: string; | ||
| isLoggingEnabled?: boolean; | ||
| timeout?: number; |
There was a problem hiding this comment.
is this a new value introduced in this pr?
There was a problem hiding this comment.
SDKConfig.timeout was already used in the original events.js (for the DOM event handler's setTimeout). It just wasn't declared in the SDKConfig interface, so TypeScript would error on access. Added the type to match the existing runtime shape.
| } from './config/constants'; | ||
| import { IMParticleInstanceManager } from '../../src/sdkRuntimeModels'; | ||
|
|
||
| declare global { |
There was a problem hiding this comment.
Can't this be imported from elsewhere?
There was a problem hiding this comment.
Added it in constants and importing from there
| } | ||
| } | ||
|
|
||
| const mParticle = window.mParticle as IMParticleInstanceManager; |
There was a problem hiding this comment.
IIRC, this should be imported from config?
There was a problem hiding this comment.
updated constants.ts to export mParticle with the IMParticleInstanceManager type, so this test file now imports it directly from config. Removed the local cast and the declare global Window declaration.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 3443450. Configure here.
…ression input safely
d6ac4c2 to
89505d4
Compare
|



Background
As part of the ongoing effort to migrate the mParticle Web SDK from JavaScript to TypeScript, this PR converts the events module and its corresponding test file to TypeScript for improved type safety and developer experience.
What Has Changed
src/events.jstosrc/events.tswith full type annotations on all method parameters, return types, and internal variablestest/src/tests-event-logging.jstotest/src/tests-event-logging.tswith proper global type declarations and type casts for intentionally invalid test inputssrc/events.interfaces.ts:TrackingCallbacktype for geolocation trackinglogImpressionEventsignature to acceptSDKImpression | SDKImpression[] | SDKProductImpression | SDKProductImpression[]promotionparam to acceptSDKPromotion | SDKPromotion[]src/sdkRuntimeModels.ts— widenedPromotionActionTypeto accept typed unionsrc/serverModel.ts— addedas stringcast forPromotionActionTypesrc/store.ts— addedtimeouttoSDKConfiginterfacetest/src/config/constants.ts— type cast forwindow.mParticleScreenshots/Video
Checklist
Additional Notes
Reference Issue (For employees only. Ignore if you are an outside contributor)