refactor: update forwarder module to TS#1260
Conversation
PR SummaryMedium Risk Overview
Implementation detail: several internal calls use Reviewed by Cursor Bugbot for commit e820005. Bugbot is set up for automated code reviews on this repo. Configure here. |
| import { Batch } from '@mparticle/event-models'; | ||
| import { IMParticleUser, ISDKUserIdentity } from './identity-user-interfaces'; | ||
| import KitBlocker from './kitBlocking'; | ||
| import { Dictionary } from './utils'; |
There was a problem hiding this comment.
Clean up these imports
| } | ||
| if ( | ||
| !self.isEnabledForUserAttributes( | ||
| !(this.isEnabledForUserAttributes as Function)( |
There was a problem hiding this comment.
Why do we need as Function?
There was a problem hiding this comment.
The method is defined later in the constructor body but called here, TS can't verify it exists at this point. Cast has no runtime effect, it's a TS workaround for the constructor-function pattern.
|
|
||
| // Check user attribute filtering rules | ||
| clonedEvent.UserAttributes = KitFilterHelper.filterUserAttributes( | ||
| clonedEvent.UserAttributes = (KitFilterHelper.filterUserAttributes as Function)( |
There was a problem hiding this comment.
Same here. This feels like we're trying to bypass type safety.
There was a problem hiding this comment.
Type mismatch: UserAttributes is string | string[] | null values, but filterUserAttributes expects string only. Cast avoids the TS error, I thought fixing the signature in KitFilterHelper is out of scope for this migration.
| } | ||
|
|
||
| for (const forwarder of mpInstance._Store.activeForwarders) { | ||
| for ( |
There was a problem hiding this comment.
Is there a reason we needed to change this to a standard for loop?
There was a problem hiding this comment.
Reverted to the original for...of loop.
|
|
||
| if (result) { | ||
| mpInstance.Logger.verbose(result); | ||
| mpInstance.Logger.verbose(result as string); |
There was a problem hiding this comment.
Isn't this already a string?
There was a problem hiding this comment.
Good catch, forwarder.process() and forwarder.processBatch() already return string per the interface, so as string is unnecessary there. Removed it from processBatch.
| // sideloaded kits, this will need to be refactored. | ||
| this.processSideloadedKits = function(mpConfig) { | ||
| this.processSideloadedKits = function( | ||
| mpConfig: IConfigResponse & { sideloadedKits?: { kitInstance: { register: Function; name: string }; filterDictionary: IKitFilterSettings }[] } |
There was a problem hiding this comment.
If we don't have a type for this, let's make one.
There was a problem hiding this comment.
Created a SideloadedKit interface in forwarders.interfaces.ts that reuses the existing UnregisteredKit type. Replaced the inline type.
| endSession(): void; | ||
| init(apiKey: string, config: SDKInitConfig, instanceName?: string): void; | ||
| _getActiveForwarders(): ConfiguredKit[]; | ||
| _getActiveForwarders(): MPForwarder[]; |
There was a problem hiding this comment.
Let's be careful with this because MPForwarder and ConfiguredKit might have specific "contracts".
There was a problem hiding this comment.
Good catch, reverted to ConfiguredKit[]. Active forwarders have gone through configuration, so ConfiguredKit is the correct contract. The MPForwarder (Dictionary) type was too loose.
| name: string | ||
| ): Dictionary<string> | null; | ||
| filterUserIdentities?( | ||
| userIdentitiesObject: Record<string, string>, |
There was a problem hiding this comment.
I think we may have this type defined for this.
There was a problem hiding this comment.
Updated to use Dictionary, which is already used in kitFilterHelper.ts for the same purpose and is already imported in this file.
94f8634 to
d614b96
Compare
| filterUserIdentities?( | ||
| userIdentitiesObject: Dictionary<string>, | ||
| filterList: number[] | ||
| ): ISDKUserIdentity[]; |
There was a problem hiding this comment.
Wrong parameter type for filterUserIdentities declaration
Medium Severity
The newly added filterUserIdentities type declaration specifies userIdentitiesObject: Dictionary<string> as its first parameter, but all callers in forwarders.ts pass ISDKUserIdentity[] (an array of user identity objects, not a string dictionary). This incorrect type definition misrepresents the actual API contract and will mislead any future TypeScript consumer relying on this interface for type checking.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit d614b96. Configure here.
There was a problem hiding this comment.
The Dictionary type matches the actual implementation in helpers.js - it iterates with for...in and Object.keys(), expecting an object like { email: "foo@bar.com" }. The callers in forwarders.ts pass ISDKUserIdentity[] which is a pre-existing type mismatch in the JS codebase - the as Function casts already bypass the type check. Fixing this properly would require refactoring helpers.filterUserIdentities to accept both shapes, which is out of scope for this migration.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
There are 3 total unresolved issues (including 2 from previous reviews).
❌ 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 1ba077d. Configure here.
|





Background
What Has Changed
src/forwarders.jstosrc/forwarders.tswith full type annotations on all methods, parameters, and return typessrc/forwarders.interfaces.tswith corrected and expanded interface definitions to support the conversionsrc/sdkRuntimeModels.tswith type adjustments needed for forwarder-related modelssrc/store.tswith type refinements required by the forwarder moduleScreenshots/Video
Checklist
Additional Notes
Reference Issue (For employees only. Ignore if you are an outside contributor)